Skip to content

Instantly share code, notes, and snippets.

@shokoe
Last active September 5, 2017 16:50
Show Gist options
  • Save shokoe/57d54e375c8a8cb88f7555636d7845ca to your computer and use it in GitHub Desktop.
Save shokoe/57d54e375c8a8cb88f7555636d7845ca to your computer and use it in GitHub Desktop.
Simple Nagios status.dat parser with static fields. Receives host name regex.
#!/bin/bash
get_regex(){
awk -v R="$1" 'BEGIN {RS="\n\n"; FS="\n";}
{
if ($1=="hoststatus {" && match($2, "^\thost_name="R))
print $2
};' /var/cache/nagios3/status.dat | sed "s#[\t ][\t ]*[^=]*=# #g; s# ##"
}
# output - <type=service>:<host_name>:<service_description>:<sec since last_check>:<sec since last_state_change>:<current_state>
# e.g - host:nodePub-alg-08b78d20264fd34a8:79:107:1
get_services(){
awk 'BEGIN {RS="\n\n"; FS="\n";}
{if ($1=="servicestatus {" && $2=="\thost_name=""'"$1"'")
print $2"\t"$3"\t"$16"\t"$34"\t"$25};' /var/cache/nagios3/status.dat |\
sed "s# #::#g; s#[\t ][\t ]*[^=]*=# #g; s# ##" | while read h n s t c; do
echo "service:$h:${n//::/ }:$((`date +%s`-$t)):$((`date +%s`-$c)):$s"
done
}
# output - <type=host>:<host_name>:<sec since last_check>:<sec since last_state_change>:<current_state>
# e.g - host:nodePub-alg-08b78d20264fd34a8:79:107:1
get_host(){
awk 'BEGIN {RS="\n\n"; FS="\n";}
{if ($1=="hoststatus {" && $2=="\thost_name=""'"$1"'")
print $2" "$15" "$24" "$30};' /var/cache/nagios3/status.dat |\
sed "s#[\t ][\t ]*[^=]*=# #g; s# ##" | while read h s t c; do
echo "host:$h:$((`date +%s`-$t)):$((`date +%s`-$c)):$s"
done
}
for host in `get_regex $1`; do
get_host $host
get_services $host
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment