-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_oc_backup.sh
executable file
·143 lines (124 loc) · 4.05 KB
/
check_oc_backup.sh
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
#!/bin/sh
#
# (c)2016 Christian Kujau <[email protected]>
#
# Check if our daily Owncloud Exports are somewhat complete and we're not too
# many records short, for various meanings of "too many".
#
# For this Nagios check to work, the export files are expected to look like this:
# - calendar-YYYY-mm-dd_NAME.ics
# - contacts-YYYY-mm-dd.vcf
#
# The Nagios user also needs permission to enter the backup directory:
# - setfacl -m u:naemon:x ~user/{,misc/{,oc_backup}}
# - setfacl -m u:naemon:r ~user/misc/oc_backup/*.{ics,vcf} # Not needed for umask=0022
#
# FIXME: don't hard code our calendar names :-\
CALENDARS="default consol"
# Kill it with fire
_die() {
echo "$1"
exit "$2"
}
# Parsing options
while getopts "d:s:w:c:vuh" opt; do
case $opt in
d) DIR="$OPTARG" ;;
s)
STATEDIR="$OPTARG"
[ -d "$STATEDIR" ] || mkdir -m0700 "$STATEDIR" || _die "$STATEDIR could not be created!" 3
;;
w) WARN="$OPTARG" ;;
c) CRIT="$OPTARG" ;;
v) VERBOSE=1 ;;
u) UPDATE=1 ;;
h|*)
echo "$(basename "$0") -d [dir] -s [statedir] -w [deviate%] -c [deviate%] [-v] [-u]"
;;
esac
done
# Sanity checks
[ ! -d "$DIR" ] || [ -z "$WARN" ] || [ -z "$CRIT" ] && _die "Missing/wrong arguments, bailing out." 3
# Initialze error values
ERR_CRIT=0
ERR_WARN=0
# Calendars
for CAL in $CALENDARS; do
# Check if the .ics file exist, bail out if it doesn't.
FILE="$DIR"/calendar-$(date +%Y-%m-%d)_"$CAL".ics
if [ -s "$FILE" ]; then
NUM=$(grep -Ec '^BEGIN:VEVENT' "$FILE")
else
_die "Calendar ""$FILE"" not found!" 3
fi
# Check calender state file
if [ -f "$STATEDIR"/calendar_"$CAL".state ]; then
OLDNUM=$(cat "$STATEDIR"/calendar_"$CAL".state)
DIFF=$(echo "scale=5; ($NUM - $OLDNUM) / $OLDNUM * 100" | bc -l | sed 's/^-//;s/^\./0./;s/\.[0-9]*//')
[ "$VERBOSE" = "1" ] && echo "CAL: $CAL OLDNUM: $OLDNUM NUM: $NUM DIFF: $DIFF%"
# Check if the difference is within our thresholds.
if [ "$DIFF" -ge "$CRIT" ]; then
ERR_CRIT=$((ERR_CRIT+1))
OUTPUT="${OUTPUT}cal: ${CAL} / old: ${OLDNUM} curr: ${NUM} diff: ${DIFF} "
elif [ "$DIFF" -ge "$WARN" ]; then
ERR_WARN=$((ERR_WARN+1))
OUTPUT="${OUTPUT}cal: ${CAL} / old: ${OLDNUM} curr: ${NUM} diff: ${DIFF} "
else
:
fi
# Update state file with current value, if requested with -u
if [ "$UPDATE" = "1" ]; then
echo "Updating state file ""$STATEDIR""/calendar_""$CAL"".state as requested."
echo "$NUM" > "$STATEDIR"/calendar_"$CAL".state
fi
else
# Create a state file on the initial run
echo "$NUM" > "$STATEDIR"/calendar_"$CAL".state || ERR_WARN=-100
fi
done
# Contacts
# Note: we're only supporting ONE addressbook now.
for p in "$DIR"/contacts-$(date +%Y-%m-%d).vcf; do
# Check if the .vcf file exist, bail out if it doesn't.
if [ -s "$p" ]; then
NUM=$(grep -Ec '^FN' "$p")
else
_die "Contacts ""$p"" not found!" 3
fi
if [ -f "$STATEDIR"/contacts.state ]; then
OLDNUM=$(cat "$STATEDIR"/contacts.state)
DIFF=$(echo "scale=5; ($NUM - $OLDNUM) / $OLDNUM * 100" | bc -l | sed 's/^-//;s/^\./0./;s/\.[0-9]*//')
[ "$VERBOSE" = "1" ] && echo "CONTACTS: OLDNUM: $OLDNUM NUM: $NUM DIFF: $DIFF%"
# Check if the difference is within our thresholds.
if [ "$DIFF" -ge "$CRIT" ]; then
ERR_CRIT=$((ERR_CRIT+1))
OUTPUT="${OUTPUT}contacts / old: ${OLDNUM} curr: ${NUM} diff: ${DIFF} "
elif [ "$DIFF" -ge "$WARN" ]; then
ERR_WARN=$((ERR_WARN+1))
OUTPUT="${OUTPUT}contacts / old: ${OLDNUM} curr: ${NUM} diff: ${DIFF} "
else
:
fi
# Update state file with current value, if requested with -u
if [ "$UPDATE" = "1" ]; then
echo "$NUM" > "$STATEDIR"/contacts.state
echo "Updating state file ""$STATEDIR""/contacts.state as requested."
fi
else
echo "$NUM" > "$STATEDIR"/contacts.state || ERR_WARN=-100
fi
done
# Results
if [ $ERR_CRIT -gt 0 ]; then
echo "CRITICAL: $CRIT% threshold reached! - $OUTPUT"
exit 2
elif [ $ERR_WARN -gt 0 ]; then
echo "WARNING: $WARN% threshold reached! - $OUTPUT"
exit 1
elif [ $ERR_CRIT -eq 0 ] && [ $ERR_WARN -eq 0 ]; then
echo "OK: Thresholds are within expected levels."
exit 0
else
echo "UNKNOWN: Something wicked happened, bailing out!"
exit 3
fi