Zabbix API shell script to show maintenance association

Just a quick note to show how you can get a server to show which Zabbix maintenance group it is in upon login.

#!/bin/bash

#
# Script to show if host is in maintenance.

host="$(hostname)"

# First a little function to generate output for the curl call. This is to allow the use of a variable in the JSON string.

generate_post_data()
{
  cat <<EOF
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
    "filter": { "host": ["${host}"] },
    "output": "extend" },
"auth": "5159d718ca9a12a932a8b34506575a9ce0f58ff115da7f1d12a6feed957b90fd",
"id": 1
}
EOF
}

# Now send the API call and store the maintenance ID

maintID=$(curl -s -X GET -H "Content-Type: application/json-rpc" -d "$(generate_post_data)" http://zabbix-dev.eur-d.howdev.corp/zabbix/api_jsonrpc.php | jq '."result" | .[] | ."maintenanceid"' | tr -d '"')

typeset -A maint

# Create an associative array of maintenance groups for later lookup

for period in $(curl -s -X GET -H "Content-Type: application/json-rpc" -d '{
    "jsonrpc": "2.0",
    "method": "maintenance.get",
    "params": {
        "output": "extend"
    },
    "auth": "5159d718ca9a12a932a8b34506575a9ce0f58ff115da7f1d12a6feed957b90fd",
    "id": 1
}'  http://zabbix-dev.eur-d.howdev.corp/zabbix/api_jsonrpc.php | jq '."result" | .[] | .maintenanceid + "-" + .name' | tr ' ' '_')
do
    maint[$(echo ${period} | cut -d- -f 1 | tr -d '"')]="$(echo ${period} | cut -d- -f 2 | tr -d '"')"
done

# And we're ready to output which group this system is in

[[ "${maintID}" != "" ]] && echo "This server is in maintenance group ${maint[${maintID}]}"

exit 0
This entry was posted in Automation, Scripting. Bookmark the permalink.

Leave a Reply