forked from chooko/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_unifi-video.sh
81 lines (63 loc) · 2.41 KB
/
check_unifi-video.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
#!/bin/bash
# script to list all UniFi Video cameras from the given controller and get some infos
# https://github.com/binarybear-de/cmk_check_unifi-video
SCRIPTBUILD="BUILD 2021-01-24"
###############################################################
# you should not need to edit anything here - use the config file!
###############################################################
CONFIG_FILE=/etc/check_mk/unifi-nvr.cfg
CONFIG_ACCESS=$(stat -c %a $CONFIG_FILE)
CONFIG_OWNER=$(stat -c %U $CONFIG_FILE)
if [ ! $CONFIG_ACCESS = 700 ] || [ ! $CONFIG_OWNER = root ] ; then
echo "Config permission mismatch, must be 700 with owner root (current: $CONFIG_ACCESS, owner $CONFIG_OWNER)!"
exit 1
fi
# source the settings from file
. $CONFIG_FILE
#Translate thresholds in seconds
WARN=$(date -d "-$WARN_HOURS hours" '+%s')
CRIT=$(date -d "-$CRIT_HOURS hours" '+%s')
#set UNKNOWN STATE if script is interrupted for some reason
STATE=3
#do a for-loop for all MAC addresses
getDeviceInfo() {
echo $CURL_RESPONSE | jq ".data[].$1"
}
###############################################################
for mac in "${MAC_LIST[@]}"; do
# request JSON from API
CURL_RESPONSE=$(curl -k -s "https://${ADDRESS}/api/2.0/camera?apiKey=${API_KEY}&mac=${mac}")
# get some infos
CAM_NAME=$(getDeviceInfo name)
CAM_LAST_REC=$(expr $(getDeviceInfo lastRecordingStartTime) / 1000)
CAM_LAST_REC_HUMAN=$(date -d @"$CAM_LAST_REC")
CAM_STATE=$(getDeviceInfo state)
# Longer ago than CRIT
if [ "$CAM_LAST_REC" -lt "$CRIT" ]; then
DESCRIPTION="Last Recording: $CAM_LAST_REC_HUMAN on $CAM_NAME is more than $CRIT_HOURS hours ago!"
STATE=2
# Longer ago then WARN but less ago then CRIT
elif [ "$CAM_LAST_REC" -lt "$WARN" -a "$CAM_LAST_REC" -gt "$CRIT" ]; then
DESCRIPTION="Last Recording: $CAM_LAST_REC_HUMAN on $CAM_NAME is more then $WARN_HOURS hours ago!"
STATE=1
# Less ago than WARN
elif [ "$CAM_LAST_REC" -gt "$WARN" ]; then
DESCRIPTION="Last Recording: $CAM_LAST_REC_HUMAN"
STATE=0
# Exit as Unknown if something else happens...
else
DESCRIPTION="Something else happened, script failed."
STATE=3
fi
# Check if CAM is connected
if [ "$CAM_STATE" == '"CONNECTED"' ]; then
DESCRIPTION="Connected, $DESCRIPTION"
elif [ "$CAM_STATE" == '"DISCONNECTED"' ]; then
DESCRIPTION="Disconnected!, $DESCRIPTION"
STATE=2
else
DESCRIPTION="Unknown State: $CAM_STATE, $DESCRIPTION"
STATE=2
fi
echo "$STATE Camera-${CAM_NAME//\"} - $DESCRIPTION"
done