forked from vide/nagios-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_graphite.sh
executable file
·150 lines (120 loc) · 3.81 KB
/
check_graphite.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
144
145
146
147
148
149
150
#!/bin/bash
function printArray {
echo ${VALUES[*]};
}
function sortValues {
VALUES=( $(for i in ${VALUES[@]}; do echo "$i"; done | sort -n) )
}
function avgValue {
echo "${VALUES[@]}" | awk '{ for (v=1; v<=NF; v++) total+=$v; } END { printf("%.1f\n", total/NF); }'
}
function maxValue {
sortValues
echo "${VALUES[${#VALUES[*]}-1]}"
}
function minValue {
sortValues
echo "${VALUES[0]}"
}
function lastValue {
echo "${VALUES[${#VALUES[*]}-1]}"
}
# we require JSON.sh
function parseGraphite {
i=0
while read LINE
do
VALUE=$(echo "${LINE}"|awk -F' ' '{print $2}')
[ "${VALUE}" != "null" ] && { VALUES[${i}]="${VALUE}"; (( i++ )); }
done < <(curl "${G_URL}" 2>/dev/null|${JSON_SH}|egrep '\[[[:digit:]],"datapoints",[[:digit:]]{1,},0\]')
}
function printHelp() {
cat >&2 <<EOF
$@
check_graphite.sh
USAGE:
--warning|-w Warning threshold
--critical|-c Critical threshold
--graphite|-g Graphite server base URL (eg. http://graphite.local/)
--metric|-m Set the Graphite metric name to check
--tag|-t [tag name] Set an optional tag value for the metric. Used in the nagios output (default: "Value")
--start-time|-s Set the series start time (default: now minus 15 min). Accepts "GNU date" format
--end-time|-e Set the series end time (default: now). Accepts "GNU date" format
--calc-mode|-a [last|avg|max|min] Specify how to calculate the number that will be checked against thresholds (default: last)
EOF
exit 2
}
# main starts here
declare -a VALUES
JSON_SH="$(dirname $0)/JSON.sh"
STIME="now - 15 min"
ETIME="now"
CMODE="last"
TAG="Value"
while [ $# -gt 0 ]
do
case "$1" in
--warning|-w) WARNING="$2"; shift 1; shift 1;;
--critical|-c) CRITICAL="$2"; shift 1; shift 1;;
--graphite|-g) BASE_GRAPHITE="$2"; shift 1; shift 1;;
--metric|-m) METRIC_NAME="$2"; shift 1; shift 1;;
--tag|-t) TAG="$2"; shift 1; shift 1;;
--start-time|-s) STIME="$2"; shift 1; shift 1;;
--end-time|-e) ETIME="$2"; shift 1; shift 1;;
--calc-mode|-a) CMODE="$2"; shift 1; shift 1;;
*) printHelp "Missing parameter" ;;
esac
done
FROM=$(date -d "${STIME}" "+%H%%3A%M_%Y%m%d") || printHelp "Invalid start time"
UNTIL=$(date -d "${ETIME}" "+%H%%3A%M_%Y%m%d") || printHelp "Invalid end time"
ADDITIONAL_PARAMS="format=json&from=${FROM}&until=${UNTIL}"
if [ -z "${WARNING}" ];
then
printHelp "Please specify a warning threshold"
fi
if [ -z "${CRITICAL}" ];
then
printHelp "Please specify a critical threshold"
fi
if [ -z "${BASE_GRAPHITE}" ];
then
printHelp "Please specify a Graphite server base URL (eg. http://graphite.local/)"
fi
if [ -z "${METRIC_NAME}" ];
then
printHelp "Please specify a Graphite metric name to check"
fi
G_URL="${BASE_GRAPHITE}/render/?target=${METRIC_NAME}&${ADDITIONAL_PARAMS}"
parseGraphite
case "${CMODE}" in
last)
MY_VALUE=$(lastValue)
;;
avg)
MY_VALUE=$(avgValue)
;;
max)
MY_VALUE=$(maxValue)
;;
min)
MY_VALUE=$(minValue)
;;
*)
printHelp "Unknown calc mode ${CMODE}"
;;
esac
[ -z ${MY_VALUE} ] && { echo "ERROR retrieving values"; exit 2; }
if (( $(bc <<< "${MY_VALUE} >= ${CRITICAL}") > 0 ))
then
echo "CRITICAL ${TAG}=${MY_VALUE}|${TAG}=${MY_VALUE}"
exit 2
else
if (( $(bc <<< "${MY_VALUE} >= ${WARNING}") > 0 ))
then
echo "WARNING ${TAG}=${MY_VALUE}|${TAG}=${MY_VALUE}"
exit 1
fi
echo "OK ${TAG}=${MY_VALUE}|${TAG}=${MY_VALUE}"
exit 0
fi
exit 2