API now does state histories with --start and --end

dev
Eric Loyd 1 year ago
parent fec6530898
commit bc02a4d412

@ -30,7 +30,7 @@ APIinfo["config/host"]=".[]"
APIinfo["config/service"]=""
APIinfo["config/hostgroup"]=".[]"
APIinfo["config/servicegroup"]=".[]"
APIinfo["config/command"]=""
APIinfo["config/command"]=".[]"
APIinfo["config/contact"]=".contact[]"
APIinfo["config/contactgroup"]=".[]"
APIinfo["config/timeperiod"]=""
@ -38,7 +38,7 @@ APIinfo["config/timeperiod"]=""
APIinfo["objects/hoststatus"]=".hoststatus[]"
APIinfo["objects/servicestatus"]=".servicestatus[]"
APIinfo["objects/logentries"]=""
APIinfo["objects/statehistory"]=""
APIinfo["objects/statehistory"]=".stateentry[]"
APIinfo["objects/comment"]=""
APIinfo["objects/downtime"]=""
APIinfo["objects/contact"]=".contact[]"
@ -54,11 +54,15 @@ APIinfo["objects/servicegroupmembers"]=".servicegroup[]"
APIinfo["objects/contactgroupmembers"]=""
APIinfo["objects/rrdexport"]=""
APIinfo["objects/cpexport"]=""
APIinfo["objects/hostavailability"]=""
APIinfo["objects/serviceavailability"]=""
APIinfo["objects/hostavailability"]=".hostavailability[]"
APIinfo["objects/serviceavailability"]=".serviceavailability[]"
APIinfo["objects/sla"]=""
APIinfo["objects/bpi"]=""
do_debug() {
[ "$verbose" -ge "$1" ] && echo "$2" >&2
}
get_myAPI() {
case "$1" in
o*) myOptions[API]="objects";;
@ -71,9 +75,11 @@ get_myAPI() {
get_myAPIep() {
case "$1" in
co|comment) myOptions[APIep]="comment";;
com|command) myOptions[APIep]="command";;
cg|contactgroup) myOptions[APIep]="contactgroup";;
c|contact) myOptions[APIep]="contact";;
dt|downtime) myOptions[APIep]="downtime";;
ha|hostavailability) myOptions[API]="objects"; myOptions[APIep]="hostavailability";;
hgm|hostgroupmembers) myOptions[API]="objects"; myOptions[APIep]="hostgroupmembers";;
hg|hostgroup) myOptions[APIep]="hostgroup";;
h|host) myOptions[APIep]="host";;
@ -81,9 +87,10 @@ get_myAPIep() {
le|logentries) myOptions[APIep]="logentries";;
sgm|servicegroupmembers) myOptions[API]="objects"; myOptions[APIep]="servicegroupmembers";;
sg|servicegroup) myOptions[APIep]="servicegroup";;
sa|serviceavailability) myOptions[API]="objects"; myOptions[APIep]="serviceavailability";;
s|service) myOptions[APIep]="service";;
ss|servicestatus) myOptions[APIep]="servicestatus";;
sh|statehistory) myOptions[APIep]="statehistory";;
sh|statehistory) myOptions[API]="objects"; myOptions[APIep]="statehistory";;
tp|timeperiod) myOptions[APIep]="timeperiod";;
*) myOptions[APIep]="";;
esac
@ -122,6 +129,9 @@ print_help() {
-c|--command check_command=<value>
-cca current_check_attempt=<value>
-cnn current_notification_number=<n>
--cname command_name]=<...>
--cline command_line]=<...>
--ctrace Take the --cname given, get its --cline, and replace arguments to show a valid command (whew!)
-cg|--contactgroup contact_groups=<value>
-cn|--configname config_name=<value>
... comment | downtime | contact | host | service | hostgroup | ...
@ -148,6 +158,8 @@ print_help() {
-sg|--servicegroup servicegroup=<value>
-sgm servicegroup_members (mainly for creating servicegroups)
-s|--service service_description=<value>
--start For things that have date selectors, this is a "show me after" selector: YYYYMMDDHHMMSS
--end For things that have date selectors, this is a "show me before" selector: YYYYMMDDHHMMSS
--state 0, 1, or 2 (or other, I suppose)
--stype 0, 1 (SOFT or HARD)
--test Don't call the API, just show what would happen
@ -175,6 +187,22 @@ do_stats() {
exit
}
# Convert YYYYMMDDHHSS to Unix Timestamp - for instance, date -d "20240701 08:05" +"%s"
convert_time() {
do_debug 2 "CONVERT_TIME input is $1"
theYear="${1:0:4}"; [ -z "$theYear" ] && echo "" && return # We need a valid year, at least
theMonth="${1:4:2}"; [ -z "$theMonth" ] && theMonth="01"
theDay="${1:6:2}"; [ -z "$theDay" ] && theDay="01"
theHour="${1:8:2}"; [ -z "$theHour" ] && theHour="00"
theMinute="${1:10:2}"; [ -z "$theMinute" ] && theMinute="00"
theSecond="${1:12:2}"; [ -z "$theSecond" ] && theSecond="00"
theTime="$theYear-$theMonth-$theDay $theHour:$theMinute:$theSecond"
do_debug 2 "CONVERT_TIME output is $theTime"
do_debug 2 `date -d "$theTime" +"%s"`
date -d "$theTime" +"%s"
}
# get_opts
while [ -n "$1" ]; do
case "$1" in
--ack) myDir[problem_has_been_acknowledged]="$2"; shift 2;;
@ -182,6 +210,9 @@ while [ -n "$1" ]; do
--api) get_myAPI "$2"; shift 2;;
-c|--command) myDir[check_command]="$2"; shift 2;;
-cca) myDir[current_check_attempt]="$2"; shift 2;;
--cname) myDir[command_name]="$2"; shift 2;;
--cline) myDir[command_line]="$2"; shift 2;;
--ctrace) myOptions[CommandTrace]="true"; shift 1;;
-cnn) myDir[current_notification_number]="$2"; shift 2;;
-cg|--contactgroup) myDir[contact_groups]="$2"; shift 2;;
-cn|--configname) myDir[config_name]="$2"; shift 2;;
@ -196,28 +227,33 @@ while [ -n "$1" ]; do
-hgm) myDir[hostgroup_members]="$2"; shift 2;;
-h|--host) myDir[host_name]="$2"; shift 2;;
-j|--jq) myOptions[MoreJQ]="$2"; shift 2;;
--key) APIKEY="$2"; shift 2;;
--keyfile) APIkeyFile="$2"; shift 2;;
--key) apikey="$2"; shift 2;;
--keyfile) apikeyfile="$2"; shift 2;;
-o|--opt) myOptions[Options]+="$2,"; shift 2;;
--output) myDir[output]="$2"; shift 2;;
-q) myOptions[Quick]="true"; myOptions[Options]+="c,"; shift 1;;
-qq) myOptions[Quick]="true"; myOptions[Options]+="c,C,"; shift 1;;
-qq) myOptions[Quick]="true"; myOptions[Options]+="c,c,"; shift 1;;
-Q|--quick) myOptions[Quick]="true"; shift 1;;
--save) myOptions[Save]="$2"; shift 2;;
-sg|--servicegroup) myDir[servicegroup_name]="$2"; shift 2;;
-sgm) myDir[servicegroup_members]="$2"; shift 2;;
-s|--service) myDir[service_description]="$2"; shift 2;;
--end) myOptions[End]="$2"; shift 2;;
--start) myOptions[Start]="$2"; shift 2;;
--stats) do_stats; shift 2;;
--state) myDir[current_state]="$2"; shift 2;;
--stype) myDir[state_type]="$2"; shift 2;;
--test) myOptions[TestMode]="true"; shift 1;;
-t|--object) get_myAPIep "$2"; shift 2;;
--url) XI_URL="$2"; shift 2;;
--url) xi_url="$2"; shift 2;;
-v|--verbose) verbose=$(($verbose + 1)); shift 1;;
*) shift 1;;
esac
done
[ -n "${myOptions[Start]}" ] && myOptions[Start]=`convert_time "${myOptions[Start]}"`
[ -n "${myOptions[End]}" ] && myOptions[End]=`convert_time "${myOptions[End]}"`
if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
while read url key; do
if [ -z "$XI_URL" ]; then
@ -231,14 +267,13 @@ if [ -n "$APIkeyFile" -a -r "$APIkeyFile" ]; then
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"
url="${XI_URL}/api/v1/${api_start}/${api_command}?pretty=0"
[ -n "${myOptions[Start]}" ] && url+="&starttime=${myOptions[Start]}"
[ -n "${myOptions[End]}" ] && url+="&endtime=${myOptions[End]}"
url+="&apikey=${APIKEY}"
do_debug 2 "start=$1 command=$2"
do_debug 1 "Executing: $url"
$curl -XGET -k "$url"
@ -338,11 +373,15 @@ jq_get_fields() {
# Otherwise, let's parse the JSON data here
# Parse our string
do_debug 1 "APIinfo=${APIinfo[${myOptiosn[API]}/${myOptions[APIep]}]}"
do_debug 1 "APIinfo=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}"
jqString=${APIinfo[${myOptions[API]}/${myOptions[APIep]}]}
do_debug 2 " Before: jqString=$jqString"
for thing in "${!myDir[@]}"; do
[ -n "${myDir[$thing]}" ] && jqString+="| select(.$thing $(jq_check_case ${myDir[$thing]}))"
do_debug 2 " After: jqString=$jqString"
done
# endpoint specific things
case "${myOptions[APIep]}" in
hostgroupmembers)
[[ ${myOptions[Options]} =~ "h," ]] && jqString+="| .members[] | .[] | .host_name"
@ -357,7 +396,10 @@ case "${myOptions[APIep]}" in
hoststatus) tmpQuick=".host_name,.address,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";;
servicestatus) tmpQuick=".service_description,.host_name,.current_state,.state_type,.last_check,.current_check_attempt,.normal_check_interval,.retry_check_interval,.max_check_attempts,.check_command";;
contact) tmpQuick=".contact_name,.email_address,.host_notifications_enabled,.service_notifications_enabled,.is_active";;
command) ;;
statehistory) tmpQuick=".host_name,.service_description,.state_time,.state_change,.state,.state_type,.current_check_attempt,.max_check_attempts,.last_state,.last_hard_state,.output";;
esac
do_debug 1 "myOptions[Options]=${myOptions[Options]}"
if [ -n "${myOptions[Quick]}" -a -n "${myOptions[Fields]}" ]; then
myOptions[Fields]="$tmpQuick,${myOptions[Fields]}"

Loading…
Cancel
Save