{"id":492,"date":"2023-04-14T14:10:33","date_gmt":"2023-04-14T14:10:33","guid":{"rendered":"https:\/\/www.aixperts.co.uk\/?p=492"},"modified":"2023-08-18T10:46:52","modified_gmt":"2023-08-18T10:46:52","slug":"zabbix-api-shell-script-to-show-maintenance-association","status":"publish","type":"post","link":"https:\/\/www.aixperts.co.uk\/?p=492","title":{"rendered":"Zabbix API shell script to show maintenance association"},"content":{"rendered":"\n<p>Just a quick note to show how you can get a server to show which Zabbix maintenance group it is in upon login.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n#\n# Script to show if host is in maintenance.\n\nhost=\"$(hostname)\"\n\n# First a little function to generate output for the curl call. This is to allow the use of a variable in the JSON string.\n\ngenerate_post_data()\n{\n  cat &lt;&lt;EOF\n{\n\"jsonrpc\": \"2.0\",\n\"method\": \"host.get\",\n\"params\": {\n    \"filter\": { \"host\": &#091;\"${host}\"] },\n    \"output\": \"extend\" },\n\"auth\": \"5159d718ca9a12a932a8b34506575a9ce0f58ff115da7f1d12a6feed957b90fd\",\n\"id\": 1\n}\nEOF\n}\n\n# Now send the API call and store the maintenance ID\n\nmaintID=$(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\" | .&#091;] | .\"maintenanceid\"' | tr -d '\"')\n\ntypeset -A maint\n\n# Create an associative array of maintenance groups for later lookup\n\nfor period in $(curl -s -X GET -H \"Content-Type: application\/json-rpc\" -d '{\n    \"jsonrpc\": \"2.0\",\n    \"method\": \"maintenance.get\",\n    \"params\": {\n        \"output\": \"extend\"\n    },\n    \"auth\": \"5159d718ca9a12a932a8b34506575a9ce0f58ff115da7f1d12a6feed957b90fd\",\n    \"id\": 1\n}'  http:\/\/zabbix-dev.eur-d.howdev.corp\/zabbix\/api_jsonrpc.php | jq '.\"result\" | .&#091;] | .maintenanceid + \"-\" + .name' | tr ' ' '_')\ndo\n    maint&#091;$(echo ${period} | cut -d- -f 1 | tr -d '\"')]=\"$(echo ${period} | cut -d- -f 2 | tr -d '\"')\"\ndone\n\n# And we're ready to output which group this system is in\n\n&#091;&#091; \"${maintID}\" != \"\" ]] &amp;&amp; echo \"This server is in maintenance group ${maint&#091;${maintID}]}\"\n\nexit 0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Just a quick note to show how you can get a server to show which Zabbix maintenance group it is in upon login.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,148],"tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-automation","category-scripting"],"_links":{"self":[{"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=492"}],"version-history":[{"count":3,"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":495,"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/492\/revisions\/495"}],"wp:attachment":[{"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aixperts.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}