]>
git.immae.eu Git - perso/Immae/Config/Nix.git/blob - flakes/private/monitoring/plugins/check_zfs_snapshot
7 # Copyright (c) 2016 Josef Friedrich <josef@friedrich.rocks>
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 ########################################################################
30 ########################################################################
32 # This date function must be placed on the top of this file because
33 # they are used in some global variables.
38 # Get the four digit year integer from now.
41 # The current 4 digit year.
48 # Convert a date in the format YYYY-MM-DD to a four digit year integer.
51 # a date in the format YYYY-MM-DD
54 # four digit year integer
58 if [ "$OS" = 'Linux' ]; then
62 OPTIONS
="-j -f %Y-%m-%d $1"
70 # Convert a UNIX timestamp to a datetime string.
78 _timestamp_to_datetime
() {
80 if [ "$OS" = 'Linux' ]; then
86 date $OPTIONS +%Y
-%m
-%d.
%H
:%M
:%S
92 # Get the current UNIX timestamp.
95 # %current UNIX timestamp
101 PROJECT_PAGES
='https://github.com/Josef-Friedrich/check_zfs_snapshot
102 https://exchange.icinga.com/joseffriedrich/check_zfs_snapshot
103 https://exchange.nagios.org/directory/Plugins/System-Metrics/File-System/check_zfs_snapshot/details'
106 FIRST_RELEASE
=2016-09-08
107 SHORT_DESCRIPTION
="Monitoring plugin to check how long ago the last \
108 snapshot of a ZFS dataset was created."
109 USAGE
="check_zfs_snapshot v$VERSION
110 Copyright (c) $(_date_to_year $FIRST_RELEASE)-$(_now_to_year) \
111 Josef Friedrich <josef@friedrich.rocks>
116 Usage: check_zfs_snapshot <options>
119 -c, --critical=OPT_CRITICAL
120 Interval in seconds for critical state.
121 -d, --dataset=OPT_DATASET
122 The ZFS dataset to check.
125 -s, --short-description
126 Show a short description of the command.
128 Show the version number.
129 -w, --warning=OPT_WARNING
130 Interval in seconds for warning state. Must be lower than -c
134 Time interval in seconds for last snapshot.
140 How many snapshot exists in the given dataset and all child
150 _get_last_snapshot
() {
151 zfs get creation
-Hpr -t snapshot
"$1" | \
152 awk 'BEGIN {max = 0} {if ($3>max) max=$3} END {print max}'
156 while getopts ':c:d:hsvw:-:' OPT
; do
164 OPT_DATASET
="$OPTARG"
173 echo "$SHORT_DESCRIPTION"
187 echo "Invalid option “-$OPTARG”!" >&2
192 echo "Option “-$OPTARG” requires an argument!" >&2
197 LONG_OPTARG
="${OPTARG#*=}"
202 OPT_CRITICAL
=$LONG_OPTARG
206 OPT_DATASET
="$LONG_OPTARG"
215 echo "$SHORT_DESCRIPTION"
225 OPT_WARNING
=$LONG_OPTARG
228 critical
*|dataset
*|warning
*)
229 echo "Option “--$OPTARG” requires an argument!" >&2
233 help*|short
-description*|version
*)
234 echo "No argument allowed for the option “--$OPTARG”!" >&2
238 '') # "--" terminates argument processing
243 echo "Invalid option “--$OPTARG”!" >&2
255 # FreeBSD wc adds some whitespaces before the number!
256 # cat $HOME/debug | wc -l
259 COUNT
="$(zfs list -t snapshot | grep "$1" | wc -l)"
263 _performance_data
() {
265 last_ago=${DIFF}s;$OPT_WARNING;$OPT_CRITICAL;0 \
266 count=$(_snapshot_count "$OPT_DATASET");;;0\
270 ## This SEPARATOR is required for test purposes. Please don’t remove! ##
274 if [ -z "$OPT_WARNING" ]; then
279 if [ -z "$OPT_CRITICAL" ]; then
284 if [ -z "$OPT_DATASET" ]; then
285 echo "Dataset has to be set! Use option -d <dataset>" >&2
290 if ! zfs list
"$OPT_DATASET" > /dev
/null
2>&1; then
291 echo "'$OPT_DATASET' is no ZFS dataset!" >&2
296 NOW
=$(_now_to_timestamp)
298 CREATION_DATE
=$(_get_last_snapshot "$OPT_DATASET")
300 DIFF
=$((NOW - CREATION_DATE))
302 if [ "$OPT_WARNING" -gt "$OPT_CRITICAL" ]; then
303 echo '-w OPT_WARNING must be smaller than -c OPT_CRITICAL'
310 if [ "$DIFF" -gt "$OPT_CRITICAL" ]; then
311 RETURN
=$STATE_CRITICAL
313 elif [ "$DIFF" -gt "$OPT_WARNING" ]; then
314 RETURN
=$STATE_WARNING
321 DATE
="$(_timestamp_to_datetime "$CREATION_DATE")"
323 echo "$MESSAGE Last snapshot for dataset '$OPT_DATASET' was created on $DATE $(_performance_data)"