-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_snapshot.sh
executable file
·134 lines (118 loc) · 2.91 KB
/
check_snapshot.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
#!/bin/bash
usage()
{
cat <<EOF
Check number of snapshot for each vm in xoa.
Options:
-t auth token
-s fqdn xoa server
-a snapshot's age (a.k.a. grace period)
-n number of snapshot
-x comma separated host excluded from check
-h this help
EOF
exit 1
}
if [ $# -eq 0 ]; then
usage
exit 0
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-h)
usage
exit 0
;;
-s)
XOASERVER=$2
shift
;;
-t)
TOKEN=$2
shift
;;
-n)
NUMBER=$2
shift
;;
-x)
IFS=',' read -r -a EXCLUDED <<< "$2"
shift
;;
-a)
AGE=$2
shift
;;
-*)
usage
exit 0
;;
*)
usage
exit 0
;;
esac
shift
done
check_snapshot()
{
#get response code from curl
STATUS=$(curl -k -s -o /dev/null -w "%{http_code}" -b authenticationToken=${TOKEN} "https://${XOASERVER}/rest/v0" )
#check if response code is 200
if [ "$STATUS" -ne 200 ]; then
echo "CRITICAL - Can't connect to $XOASERVER - Response code: $STATUS"
exit 2
fi
VMLIST=$(curl -k -s -b authenticationToken=${TOKEN} "https://${XOASERVER}/rest/v0/vms?filter=snapshots:length:>=${NUMBER}&fields=name_label,power_state" | jq .[].name_label)
#convert to array
readarray -t VMARRAY <<<"$VMLIST"
#get rid of ""
for i in "${!VMARRAY[@]}"; do
cleanarray+=("$( echo "${VMARRAY[i]}" | sed 's/^.//;s/.$//')")
done
VMARRAY=("${cleanarray[@]}")
unset cleanarray
#get rid of EXCLUDED vms
for excluded in "${EXCLUDED[@]}"; do
for vm in "${!VMARRAY[@]}"; do
if [ "${VMARRAY[vm]}" = "$excluded" ];then
unset 'VMARRAY[vm]'
fi
done
done
#rebuild array without gaps
for i in "${!VMARRAY[@]}"; do
VMRESULT+=( "${VMARRAY[i]}" )
done
TIME=$(date +%s -d "${AGE} day ago")
SNAPLIST=$(curl -k -s -b authenticationToken=${TOKEN} "https://${XOASERVER}/rest/v0/vm-snapshots?filter=snapshot_time:<${TIME}&fields=name_label,uuid,%24snapshot_of"| jq '.[]."$snapshot_of"')
#converto to array
readarray -t SNAPARRAY <<<"$SNAPLIST"
for i in "${!SNAPARRAY[@]}"; do
cleanarray+=($( echo "${SNAPARRAY[i]}" | sed 's/^.//;s/.$//'))
done
SNAPARRAY=("${cleanarray[@]}")
unset cleanarray
for vm in "${!VMRESULT[@]}"; do
UUID=$(curl -k -s -b authenticationToken=${TOKEN} "https://${XOASERVER}/rest/v0/vms?filter=name_label:${VMRESULT[vm]}&fields=uuid" | jq .[].uuid | sed 's/^.//;s/.$//')
for snap in "${!SNAPARRAY[@]}"; do
if [ "$UUID" = "${SNAPARRAY[snap]}" ];then
RESULT+=( "${VMRESULT[vm]}" )
break
fi
done
done
#answer
if [ -z "$RESULT" ]
then
echo "OK - No VM has more than $NUMBER snapshots and $AGE days old"
exit 0
else
echo "CRITICAL - VMs have more than $NUMBER snapshots which are older than $AGE days"
for i in "${!RESULT[@]}"; do
echo ${RESULT[i]}
done
exit 2
fi
}
check_snapshot