#!/usr/bin/env bash APIkeyFile=".nagiosapikey" [ ! -r "$APIkeyFile" ] && APIkeyFile="${HOME}/.nagiosapikey" [ ! -r "$APIkeyFile" ] && APIkeyFile="" APIKEY="" XI_URL="" curl="curl -k -s" # curl -XGET "https://192.168.1.19/nagiosxi/api/v1/objects/service?apikey=" verbose="0" doCreate="" myConfigName="" myHost="" myService="" myHG="" mySG="" myFields="" myCommand="" mySave="" myFile="" tmpJSON="" moreJQ="" myAPI="objects" myAPIep="servicestatus" get_myAPI() { case "$1" in objects|config|system) myAPI="$1";; *) myAPI="";; esac } get_myAPIep() { case "$1" in hoststatus|servicestatus|logentries|statehistory|comment|downtime|contact|host|service|hostgroup|servicegroup|contactgroup|timeperiod) myAPIep="$1";; hostgroupmembers|servicegroupmembers) myAPIep="$1";; *) myAPIep="";; esac } while [ -n "$1" ]; do case "$1" in --keyfile) APIkeyFile="$2"; shift 2;; -j|--jq) moreJQ="$2"; shift 2;; -v|--verbose) verbose=$(($verbose + 1)); shift 1;; --create) doCreate="true"; shift 1;; --key) APIKEY="$2"; shift 2;; --url) XI_URL="$2"; shift 2;; --save) mySave="$2"; shift 2;; --file) myFile="$2"; shift 2;; -f|--fields) myFields="$2"; shift 2;; -h|--host) myHost="$2"; shift 2;; -s|--service) myService="$2"; shift 2;; -c|--command) myCommand="$2"; shift 2;; -cn|--configname) myConfigName="$2"; shift 2;; -hg|--hostgroup) myHG="$2"; shift 2;; -sg|--servicegroup) mySG="$2"; shift 2;; --api) get_myAPI "$2"; shift 2;; -t|--object) get_myAPIep "$2"; shift 2;; *) shift 1;; esac done if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then while read url key; do if [ -z "$XI_URL" ]; then XI_URL="$url" APIKEY="$key" continue fi if [ "$url" = "$XI_URL" ]; then APIKEY="$key" fi done < "$APIkeyFile" fi do_debug() { [ "$verbose" -ge "$1" ] && echo "$2" >&2 } do_api() { api_start="$1" api_command="$2" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0" do_debug 2 "start=$1 command=$2" do_debug 1 "Executing: $url" $curl -XGET -k "$url" } # curl -XPOST "http://192.168.1.128/nagiosxi/api/v1/config/hostgroup?apikey=fsZZ4pXaKaVjSG7IYcjMRYhK8NqcqN2NGPck8gPhFoZMJGKj4YUjZCF8qSqsK7Ln&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&applyconfig=1" do_api_post() { api_start="$1" api_command="$2" url="${XI_URL}/api/v1/${api_start}/${api_command}?apikey=${APIKEY}&pretty=0" do_debug 2 "start=$1 command=$2" do_debug 1 "Executing: $url" $curl -XPOST -k "$url" -d "hostgroup_name=$myHG&alias=$myHG&applyconfig=0" } # At this time, all we can create ia a hostgroup create_hostgroup() { do_debug 1 "about to do an API post call" do_api_post config hostgroup } do_create() { do_debug 1 "about to do a create_command" [ -n "$myHG" ] && create_hostgroup exit } # If we said "--create" then we want to make something if [ -n "$doCreate" ]; then do_create exit fi # Grab a copy of the JSON data so we don't have to keep making calls over and over # If we used an existing file, then just use that if [ -z "$myFile" ]; then tmpJSON=`mktemp` do_debug 1 "tmp file is $tmpJSON" do_debug 2 "myAPI is $myAPI and myAPIep is $myAPIep" do_api "$myAPI" "$myAPIep" > $tmpJSON else do_debug 1 "myFile=$myFile" tmpJSON="$myFile" fi # if mySave is not empty, then we're just saving it into the file called $mySave if [ -n "$mySave" ]; then mv $tmpJSON $mySave do_debug 1 "JSON data saved to $mySave" exit fi # Otherwise, let's parse the JSON data here # Parse our string jqString="" if [ "$myAPIep" = "servicestatus" ]; then jqString=".servicestatus[]" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" [ -n "$mySG" ] && jqString="$jqString | select(.servicegroup_name==\"$mySG\")" elif [ "$myAPIep" = "hostgroup" ]; then jqString=".[]" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" elif [ "$myAPIep" = "host" ]; then jqString=".[]" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" elif [ "$myAPIep" = "service" ]; then jqString=".[]" [ -n "$myHost" ] && jqString="$jqString | select(.host_name | test(\"$myHost\"))" [ -n "$myService" ] && jqString="$jqString | select(.service_description | test(\"$myService\"))" [ -n "$myConfigName" ] && jqString="$jqString | select(.check_command | test(\"$myConfigName\"))" [ -n "$myCommand" ] && jqString="$jqString | select(.check_command | test(\"$myCommand\"))" elif [ "$myAPIep" = "hostgroupmembers" ]; then jqString=".hostgroup[]" [ -n "$myHG" ] && jqString="$jqString | select(.hostgroup_name==\"$myHG\")" fi [ -n "$myFields" ] && jqString="$jqString | $myFields" jqString="$jqString $moreJQ" do_debug 1 "jqString=$jqString" cat $tmpJSON | jq -r "$jqString" [ -z "$myFile" ] && rm $tmpJSON