-
Notifications
You must be signed in to change notification settings - Fork 0
/
icinga-show
executable file
·79 lines (71 loc) · 1.78 KB
/
icinga-show
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
#!/bin/sh
usage(){
echo "usage: $(basename "$0") <host> <service>"
exit 1
}
urlencode() {
# cannot use --data-urlencode from curl because it transforms " " into "+"
printf '%s' "$1" | jq -sRr @uri
}
show() {
tmpfile=$(mktemp --suffix .json)
host=$(urlencode "$1")
service=$(urlencode "$2")
http_code=$(curl -o "$tmpfile" -w "%{http_code}" -s "$ICINGA_URL/monitoring/service/show?host=$host&service=$service&format=json"\
--cookie "$COOKIEFILE"\
--cookie-jar "$COOKIEFILE")
cat "$tmpfile"
rm "$tmpfile"
if [ "$http_code" -eq 302 ];then
return 3
elif [ "$http_code" -eq 404 ];then
return 4
else
return 0
fi
}
case $# in
2)
host=$1
service=$2
;;
*)
echo "Wrong number of arguments"
usage
;;
esac
icinga_status=$(show "$host" "$service")
case $? in
3)
echo "Please login first"
exit
;;
4)
echo "Service '$service' not found for host '$host'"
exit
;;
esac
status=$(echo "$icinga_status" | jq -r '.data.service_state')
host_name=$(echo "$icinga_status" | jq -r '.data.host_name')
service_output=$(echo "$icinga_status" | jq -r '.data.service_output')
service_long_output=$(echo "$icinga_status" | jq -r '.data.service_long_output')
service_last_check=$(echo "$icinga_status" | jq -r '(.data.service_last_check|tonumber|strftime("%F %T"))')
if [ "$status" -eq 0 ]; then
status_symbol="✔"
color_code="\033[0;32m" # Grün
elif [ "$status" -eq 1 ]; then
status_symbol="!"
color_code="\033[0;33m" # Gelb
else
status_symbol="✘"
color_code="\033[0;31m" # Rot
fi
# Ergebnis formatieren und ausgeben
printf "Status: %b%s\033[0m, " "$color_code" "$status_symbol"
printf "Host: \"%s\"" "$host_name"
printf ", Last Check: %s\n" "$service_last_check"
if [ -z "$service_long_output" ];then
printf "%s\n" "$service_output"
else
printf "%s\n" "$service_long_output"
fi