-
Notifications
You must be signed in to change notification settings - Fork 0
/
icinga2-api.sh
63 lines (49 loc) · 2.57 KB
/
icinga2-api.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
#!/bin/bash
# vim: set tabstop=2 smarttab shiftwidth=2 softtabstop=2 expandtab foldmethod=syntax :
#
# Source can be found here:
# https://github.com/ChrLau/scripts/blob/master/icinga2-api.sh
ICINGA2_API_USER=""
ICINGA2_API_PASSWORD=""
ICINGA2_API_HOST=ip.ip.ip.ip
ICINGA2_API_PORT=
VERSION="1.1"
SCRIPT="$(basename "$0")"
JSON_PP="$(command -v json_pp)"
CURL="$(command -v curl)"
# Test if curl is present and executeable
if [ ! -x "$CURL" ]; then
echo "This script requires curl for sending HTTP(S)-Requests to the API"
exit 3;
fi
# Test if json_pp is present and executeable
if [ ! -x "$JSON_PP" ]; then
echo "This script requires json_pp (pretty-print JSON) to display the retrieved results in a nice JSON-formatted syntax."
exit 4;
fi
function HELP {
echo "$SCRIPT v$VERSION): Query the Icinga2-API for latest check results"
echo "Usage: $SCRIPT FQDN Servicename"
echo ""
echo "Both FQDN & Servicename can specified as RegEx."
echo "Provide only FQDN to list all Services of this host."
}
# For later version where it's possible to specify what results to retrieve from the API
#ONLY_ERRORS="&& !match(\"0\",service.state)"
#HOSTFILTER="\"filter\": \"match(\"'$HOST'\",host.name)"
#SERVICEFILTER="\"filter\": \"regex(pattern, service.name)\", \"filter_vars\": { \"pattern\": \"'$SERVICENAME'\" }"
# Print help if no arguments are given
if [ "$#" -eq 0 ]; then
HELP
elif [ "$#" -eq 1 ]; then
HOST="$1"
results=$($CURL -s -u "$ICINGA2_API_USER":"$ICINGA2_API_PASSWORD" -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST -k "https://$ICINGA2_API_HOST:$ICINGA2_API_PORT/v1/objects/services/" -d '{"filter": "match(\"'"$HOST"'\",host.name)", "attrs": ["__name", "state", "action_url", "last_check_result"] }')
# Get services based on service name
#results=$($CURL -s -u "$ICINGA2_API_USER":"$ICINGA2_API_PASSWORD" -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST -k "https://$ICINGA2_API_HOST:$ICINGA2_API_PORT/v1/objects/services/" -d '{"filter": "match(\"'"$HOST"'\",service.name)", "attrs": ["__name", "state", "action_url", "last_check_result"] }')
echo "$results" | "$JSON_PP"
elif [ "$#" -eq "2" ]; then
HOST="$1"
SERVICENAME="$2"
results=$($CURL -s -u "$ICINGA2_API_USER":"$ICINGA2_API_PASSWORD" -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST -k "https://$ICINGA2_API_HOST:$ICINGA2_API_PORT/v1/objects/services/" -d '{ "filter": "regex(\"'"$HOST"'\",host.name) && regex(\"'"$SERVICENAME"'\",service.name)", "attrs": ["__name", "state", "action_url", "last_check_result"] }')
echo "$results" | "$JSON_PP"
fi