aboutsummaryrefslogtreecommitdiff
path: root/sysutils/ganglia-monitor-core/files/gmetasnap.sh
blob: 3fbbe73ec2b1d88307153728cbfd3744333982e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/sh
#
# gmetasnapshot - Ganglia gmetad snapshot manager
#
# $FreeBSD: head/sysutils/ganglia-monitor-core/files/gmetasnap.sh 300897 2012-07-14 14:29:18Z beat $

command=`basename $0`

def_rrddir=/var/db/ganglia/rrds
def_snapdir=/var/db/ganglia/snaps
def_snapname=snap
def_comp=gzip

rrddir=$def_rrddir
snapdir=$def_snapdir
snapname=$def_snapname
comp=$def_comp
delete_old=0
quiet=0

usage()
{
	exitcode=$1
	shift
	if [ -n "$*" ]; then
		warn $*
	fi
	cat <<EOU
usage:
	${command} [<options>] save [<snapname>]
	${command} [<options>] restore [<snapname>]

options:
	-D		Delete .old file after creating snapshot.
	-h		Display this message
	-q		Avoid output unless there is an error.
	-r <rrddir>	Set the rrddir [default: ${def_rrddir}]
	-s <snapdir>	Set the snapdir [default: ${def_snapdir}]
	-z <comptype>	Set the compression type.  Valid values are
			gzip, bzip2, and none. [default: ${def_comp}]

notes:
	- The default snapname is: ${def_snapname}
	- ${command} will not create rrddir or snapdir.
EOU
	exit $1
}

err()
{
	exitcode=$1
	shift
	echo ${command} $* >&2
	exit $exitcode
}

warn()
{
	echo ${command} $* >&2
}

status()
{
	if [ $quiet -eq 0 ]; then
		echo $*
	fi
}

compsuffix()
{
	case "$1" in
	gzip)
		echo ".gz"
		;;
	bzip2)
		echo ".bz2"
		;;
	none)
		echo ""
		;;
	*)
		echo "Unsupposed compression type ignored: $1" >&2
		echo ""
		;;
	esac
}

compflag()
{
	case "$1" in
	gzip)
		echo "z"
		;;
	bzip2)
		echo "y"
		;;
	none)
		echo ""
		;;
	*)
		echo "Unsupposed compression type ignored: $1" >&2
		echo ""
		;;
	esac
}

while [ -n "$1" ]; do
	case "$1" in
	-D)
		shift
		delete_old=1
		;;
	-h)
		shift
		usage 0
		;;
	-q)
		shift
		quiet=1
		;;
	-r)
		shift
		if [ -z "$1" ]; then
			usage 1 "-r requires an argument"
		fi
		rrddir=$1
		shift
		;;
	-s)
		shift
		if [ -z "$1" ]; then
			usage 1 "-s requires an argument"
		fi
		snapdir=$1
		shift
		;;
	-z)
		shift
		if [ -z "$1" ]; then
			usage 1 "-z requires an argument"
		fi
		rrddir=$1
		shift
		;;
	-*)
		usage 1 "unknown argument $1"
		;;
	*)
		break
	esac
done

if [ -n "$2" ]; then
	snapname=$2
fi

basefile=${snapdir}/${snapname}.tar`compsuffix ${comp}`
tarcmd="tar `compflag ${comp}`"

case "$1" in
save)
	if [ ! -d ${rrddir} ]; then
		err 2 "rrddir ${rrddir} does not exist"
	fi
	if [ ! -d ${snapdir} ]; then
		err 2 "snapdir ${snapdir} does not exist"
	fi
	status "saving ${rrddir} to ${basefile}"
	cd ${rrddir}
	if ! ${tarcmd}cf ${basefile}.new .; then
		err 2 "Failed to create ${basefile}.new"
	fi
	if [ -e ${basefile} ]; then
		mv ${basefile} ${basefile}.old
		sync
	fi
	mv ${basefile}.new ${basefile}
	sync
	if [ $delete_old -ne 0 ]; then
		rm ${basefile}.old
	fi
	sync
	;;
restore)
	for file in ${basefile} ${basefile}.new ${basefile}.old; do
		if [ -e ${file} ]; then
			if ${tarcmd}tf ${file} >/dev/null 2>&1 ; then
				sourcefile=${file}
				break
			else
				warn "${file} exists but is not a valid tarball.  Ignoring."
			fi
		fi
	done
	if [ -z "$sourcefile" ]; then
		err 1 "no snapshot found in ${snapdir}."
	fi
	status "restoring ${rrddir} from ${sourcefile}"
	if [ ! -d "${rrddir}" ]; then
		err 1 "${rrddir} does not exist"
	fi
	cd ${rrddir}
	${tarcmd}xpf ${sourcefile}
	;;
*)
	usage 1 "unknown command: $1"
	;;
esac