aboutsummaryrefslogtreecommitdiff
path: root/modules/private/monitoring/plugins/check_zfs_snapshot
blob: 56f8c4fff89a25a89920b49cba8148e98a50b3c3 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /bin/sh

OS=$(uname)

# MIT License
#
# Copyright (c) 2016 Josef Friedrich <josef@friedrich.rocks>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

########################################################################
# Date functions
########################################################################

# This date function must be placed on the top of this file because
# they are used in some global variables.

# to_year ###

##
# Get the four digit year integer from now.
#
# Return:
#   The current 4 digit year.
##
_now_to_year() {
	date +%Y
}

##
# Convert a date in the format YYYY-MM-DD to a four digit year integer.
#
# Parameters:
#   a date in the format YYYY-MM-DD
#
# Return:
#   four digit year integer
##
_date_to_year() {
	local OPTIONS
	if [ "$OS" = 'Linux' ]; then
		OPTIONS="--date $1"
	# FreeBSD, Darwin
	else
		OPTIONS="-j -f %Y-%m-%d $1"
	fi
	date $OPTIONS +%Y
}

# to_datetime ###

##
# Convert a UNIX timestamp to a datetime string.
#
# Parameters:
#   UNIX timestamp
#
# Return:
#   %Y-%m-%d.%H:%M:%S
##
_timestamp_to_datetime() {
	local OPTIONS
	if [ "$OS" = 'Linux' ]; then
		OPTIONS="--date @$1"
	# FreeBSD, Darwin
	else
		OPTIONS="-j -f %s $1"
	fi
	date $OPTIONS +%Y-%m-%d.%H:%M:%S
}

# to_timestamp ###

##
# Get the current UNIX timestamp.
#
# Return:
#   %current UNIX timestamp
##
_now_to_timestamp() {
	date +%s
}

PROJECT_PAGES='https://github.com/Josef-Friedrich/check_zfs_snapshot
https://exchange.icinga.com/joseffriedrich/check_zfs_snapshot
https://exchange.nagios.org/directory/Plugins/System-Metrics/File-System/check_zfs_snapshot/details'

VERSION=1.2
FIRST_RELEASE=2016-09-08
SHORT_DESCRIPTION="Monitoring plugin to check how long ago the last \
snapshot of a ZFS dataset was created."
USAGE="check_zfs_snapshot v$VERSION
Copyright (c) $(_date_to_year $FIRST_RELEASE)-$(_now_to_year) \
Josef Friedrich <josef@friedrich.rocks>

$SHORT_DESCRIPTION


Usage: check_zfs_snapshot <options>

Options:
 -c, --critical=OPT_CRITICAL
    Interval in seconds for critical state.
 -d, --dataset=OPT_DATASET
    The ZFS dataset to check.
 -h, --help
    Show this help.
 -s, --short-description
    Show a short description of the command.
 -v, --version
    Show the version number.
 -w, --warning=OPT_WARNING
    Interval in seconds for warning state. Must be lower than -c

Performance data:
 - last_ago
    Time interval in seconds for last snapshot.
 - warning
    Interval in seconds.
 - critical
    Interval in seconds.
 - snapshot_count
    How many snapshot exists in the given dataset and all child
    datasets exists.
"

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

_get_last_snapshot() {
	zfs get creation -Hpr -t snapshot "$1" | \
		awk 'BEGIN {max = 0} {if ($3>max) max=$3} END {print max}'
}

_getopts() {
	while getopts ':c:d:hsvw:-:' OPT ; do
		case $OPT in

			c)
				OPT_CRITICAL=$OPTARG
				;;

			d)
				OPT_DATASET="$OPTARG"
				;;

			h)
				echo "$USAGE"
				exit 0
				;;

			s)
				echo "$SHORT_DESCRIPTION"
				exit 0
				;;

			v)
				echo "$VERSION"
				exit 0
				;;

			w)
				OPT_WARNING=$OPTARG
				;;

			\?)
				echo "Invalid option “-$OPTARG”!" >&2
				exit 2
				;;

			:)
				echo "Option “-$OPTARG” requires an argument!" >&2
				exit 3
				;;

			-)
				LONG_OPTARG="${OPTARG#*=}"

				case $OPTARG in

					critical=?*)
						OPT_CRITICAL=$LONG_OPTARG
						;;

					dataset=?*)
						OPT_DATASET="$LONG_OPTARG"
						;;

					help)
						echo "$USAGE"
						exit 0
						;;

					short-description)
						echo "$SHORT_DESCRIPTION"
						exit 0
						;;

					version)
						echo "$VERSION"
						exit 0
						;;

					warning=?*)
						OPT_WARNING=$LONG_OPTARG
						;;

					critical*|dataset*|warning*)
						echo "Option “--$OPTARG” requires an argument!" >&2
						exit 3
						;;

					help*|short-description*|version*)
						echo "No argument allowed for the option “--$OPTARG”!" >&2
						exit 4
						;;

					'')	# "--" terminates argument processing
						break
						;;

					*)
						echo "Invalid option “--$OPTARG”!" >&2
						exit 2
						;;

				esac
				;;

		esac
	done
}

_snapshot_count() {
	# FreeBSD wc adds some whitespaces before the number!
	# cat $HOME/debug | wc -l
	#        7
	local COUNT
	COUNT="$(zfs list -t snapshot | grep "$1" | wc -l)"
	echo $COUNT
}

_performance_data() {
	echo "| \
last_ago=${DIFF}s;$OPT_WARNING;$OPT_CRITICAL;0 \
count=$(_snapshot_count "$OPT_DATASET");;;0\
"
}

## This SEPARATOR is required for test purposes. Please don’t remove! ##

_getopts $@

if [ -z "$OPT_WARNING" ]; then
	# 1 day
	OPT_WARNING=86400
fi

if [ -z "$OPT_CRITICAL" ]; then
	# 3 day
	OPT_CRITICAL=259200
fi

if [ -z "$OPT_DATASET" ]; then
	echo "Dataset has to be set! Use option -d <dataset>" >&2
	echo "$USAGE" >&2
	exit $STATE_UNKNOWN
fi

if ! zfs list "$OPT_DATASET" > /dev/null 2>&1; then
	echo "'$OPT_DATASET' is no ZFS dataset!" >&2
	echo "$USAGE" >&2
	exit $STATE_UNKNOWN
fi

NOW=$(_now_to_timestamp)

CREATION_DATE=$(_get_last_snapshot "$OPT_DATASET")

DIFF=$((NOW - CREATION_DATE))

if [ "$OPT_WARNING" -gt "$OPT_CRITICAL" ]; then
	echo '-w OPT_WARNING must be smaller than -c OPT_CRITICAL'
	_usage >&2
	exit $STATE_UNKNOWN
fi

RETURN=STATE_UNKNOWN

if [ "$DIFF" -gt "$OPT_CRITICAL" ]; then
	RETURN=$STATE_CRITICAL
	MESSAGE="CRITICAL:"
elif [ "$DIFF" -gt "$OPT_WARNING" ]; then
	RETURN=$STATE_WARNING
	MESSAGE="WARNING:"
else
	RETURN=$STATE_OK
	MESSAGE="OK:"
fi

DATE="$(_timestamp_to_datetime "$CREATION_DATE")"

echo "$MESSAGE Last snapshot for dataset '$OPT_DATASET' was created on $DATE $(_performance_data)"

exit $RETURN