aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-06-20 13:08:15 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-06-20 13:08:15 +1200
commitcdc34fa78b326bd241aae7399190c739e25bf025 (patch)
tree2d12fdf161b74dcde13baf33bb46f0137f443952
parent8c58b160f4f51718d49aadf1def398b3383ab022 (diff)
downloadnagscripts-cdc34fa78b326bd241aae7399190c739e25bf025.tar.gz
Update to latest version with MKLivestatusmk-livestatus
No longer using NDOUtils at work, these are much nicer and more likely to work with any given Nagios installation now anyway. Added nagios-data-search, nagios-downtstream-list, nagios-exists, nagios-problem-list, and nagios-unhandled-list.
-rw-r--r--LICENSE3
-rw-r--r--README.markdown1
-rwxr-xr-xnagios-acknowledge86
-rw-r--r--nagios-acknowledge.125
-rwxr-xr-xnagios-data-search72
-rw-r--r--nagios-data-search.119
-rwxr-xr-xnagios-downstream-list62
-rw-r--r--nagios-downstream-list.117
-rwxr-xr-xnagios-downtime100
-rw-r--r--nagios-downtime.129
-rwxr-xr-xnagios-exists79
-rw-r--r--nagios-exists.129
-rwxr-xr-xnagios-force-check89
-rw-r--r--nagios-force-check.128
-rwxr-xr-xnagios-problem-list61
-rw-r--r--nagios-problem-list.130
-rwxr-xr-xnagios-unhandled-list65
-rw-r--r--nagios-unhandled-list.130
18 files changed, 693 insertions, 132 deletions
diff --git a/LICENSE b/LICENSE
index d77e562..77ba6a1 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)
-Copyright (c) 2014 Tom Ryder
+Copyright (c) 2016 Tom Ryder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-
diff --git a/README.markdown b/README.markdown
index b922a2c..e38e3fe 100644
--- a/README.markdown
+++ b/README.markdown
@@ -17,4 +17,3 @@ Copyright (c) [Tom Ryder][2]. Distributed under [MIT License][3].
[1]: https://www.inspire.net.nz/
[2]: https://sanctum.geek.nz/
[3]: http://opensource.org/licenses/MIT
-
diff --git a/nagios-acknowledge b/nagios-acknowledge
index 51fdce8..f9b1081 100755
--- a/nagios-acknowledge
+++ b/nagios-acknowledge
@@ -5,18 +5,21 @@
# it's annoying to do with the web interface for large sets of hosts or
# services.
#
-# $ nac <host>[/<service>] [optional comment]
+# $ nac (<host[/service][,host[/service],...>|-) [optional comment]
#
-# Good for for/while loops:
+# You can specify multiple objects by separating them with commas:
#
-# for hostname in hosta hostb hostc; do nac "$hostname" ... ; done
-# while read -r hostname; do nac "$hostname" ... ; done < downtime-hostnames
+# $ nac abc-example-ap-1,abc-example-ap-2/VOLTAGE 'Power problems at abc-example'
+#
+# Even easier is to pipe them into stdin by specifying - as the object:
+#
+# $ nul | grep abc-example | nac - 'Power problems at abc-example'
#
# By default, does not send ACKNOWLEDGEMENT notifications. Use -n if you do
# want that.
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
-# Copyright: 2014 Sanctum
+# Copyright: 2016
#
# Name self
@@ -24,16 +27,16 @@ self=nagios-acknowledge
# Usage printing function
usage() {
- printf 'USAGE: %s [-n] <host[/service]> [comment]\n' "$self"
+ printf 'USAGE: %s [-n] (<host[/service][,host[/service],...>|-) [comment]\n' "$self"
}
# Default to not notifying
notify=0
-# Process options
+# Figure out whether we should notify
OPTIND=1
while getopts 'hn' opt ; do
- case "$opt" in
+ case $opt in
n)
notify=1
;;
@@ -60,37 +63,66 @@ fi
now=$(date +%s)
spec=$1
sticky=1
+notify=$notify
persistent=1
author=${SUDO_USER:-$USER}
comment=${2:-'no comment given'}
cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd}
-# If a service name is specified after a slash, figure that out
-if [[ $spec == */* ]] ; then
- host=${spec%/*}
- service=${spec##*/}
-else
- host=$spec
- service=
-fi
+# How to get the objects depends on the spec (the first argument)
+declare -a objects
+case $spec in
+
+ # If the spec is just "-", we just read unique objects from stdin
+ -)
+ while read -r object ; do
+ [[ $object ]] || continue
+ objects[${#objects[@]}]=$object
+ done < <(sort -u)
+ ;;
+
+ # If the spec is anything else, we break it up with commas and read the
+ # objects that way
+ *)
+ IFS=, read -a objects -r < <(printf '%s\n' "$spec")
+ ;;
+esac
+
+# All the hosts or services must exist, just to be strict; they don't
+# necessarily have to be in a PROBLEM state though
+for object in "${objects[@]}" ; do
+ nagios-exists "$object" && continue
+ printf '%s: Host/service %s does not seem to exist\n' \
+ "$self" "$object" >&2
+ exit 1
+done
# Quietly replace semicolons in comment with commas
comment=${comment//;/,}
-# Write command and print message if it fails; succeed silently
-if [[ $service ]] ; then
- cmd=$(printf '[%lu] ACKNOWLEDGE_SVC_PROBLEM;%s;%s;%u;%u;%u;%s;%s' \
- "$now" "$host" "$service" \
- "$sticky" "$notify" "$persistent" "$author" "$comment")
-else
- cmd=$(printf '[%lu] ACKNOWLEDGE_HOST_PROBLEM;%s;%u;%u;%u;%s;%s' \
- "$now" "$host" \
- "$sticky" "$notify" "$persistent" "$author" "$comment")
-fi
+# Write commands to schedule downtime for each of the objects, bail if a single
+# one of them fails
+for object in "${objects[@]}" ; do
+ case $object in
+ */*)
+ host=${object%/*}
+ service=${object##*/}
+ cmd=$(printf '[%lu] ACKNOWLEDGE_SVC_PROBLEM;%s;%s;%u;%u;%u;%s;%s' \
+ "$now" "$host" "$service" \
+ "$sticky" "$notify" "$persistent" "$author" "$comment")
+ ;;
+ *)
+ host=$object
+ cmd=$(printf '[%lu] ACKNOWLEDGE_HOST_PROBLEM;%s;%u;%u;%u;%s;%s' \
+ "$now" "$host" \
+ "$sticky" "$notify" "$persistent" "$author" "$comment")
+ ;;
+ esac
+ printf '%s\n' "$cmd" > "$cmdfile" || exit
+done
# Attempt to write command to file
if ! printf '%s\n' "$cmd" > "$cmdfile" ; then
printf '%s: Failed to write command to file\n' "$self" >&2
exit 1
fi
-
diff --git a/nagios-acknowledge.1 b/nagios-acknowledge.1
index 48f31f7..2641acf 100644
--- a/nagios-acknowledge.1
+++ b/nagios-acknowledge.1
@@ -8,28 +8,25 @@
.br
.B nagios-acknowledge
[-n]
-.I HOSTNAME[/SERVICE]
+.I (HOSTNAME[/SERVICE][,HOSTNAME[,SERVICE],...]|-)
.I [COMMENT]
.SH SYNOPSIS
-.B nac
-webhost 'DDoS I think, fixing'
-.PP
-.B nac
--n webhost/HTTP 'Apache HTTPD crashed, fixing'
-.PP
-for hostname in ns{1..3} ; do
+nac webhost 'DDoS I think, fixing'
.br
-.B
- nac
-"$hostname" 'DNS is completely rooted, addressing now'
+nac -n webhost/HTTP 'Apache HTTPD crashed, fixing'
.br
-done
+nac authns1,authns2,recdns1,recdns2 'DNS is all buggered'
+.br
+npl | grep abc-example | nac - 'Power problems at abc-example'
.SH DESCRIPTION
.B nagios-acknowledge
will format and attempt to write a Nagios command to the Nagios commands file
which it expects to find at /usr/local/nagios/var/rw/nagios.cmd to acknowledge
a problem for a particular host or service.
.PP
+Multiple hosts or services can be separated by commas, and the special value -
+allows hosts or services to be read from stdin.
+.PP
The user's current login name will be used as the author field for the
acknowledgement, taking into account any use of
.I sudo(8)
@@ -40,8 +37,8 @@ parameter is optional.
By default, the script won't send an ACKNOWLEDGEMENT notification. Add a -n
option if you actually want to do this.
.SH SEE ALSO
-nagios-downtime(1), nagios-force-check(1)
+nagios-data-search(1), nagios-descendent-list(1), nagios-downtime(1),
+nagios-force-check(1), nagios-problem-list(1), nagios-unhandled-list(1)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
.PP
-
diff --git a/nagios-data-search b/nagios-data-search
new file mode 100755
index 0000000..5be21ca
--- /dev/null
+++ b/nagios-data-search
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+
+#
+# nagios-data-search(1) -- List all the hosts (or services) matching any of a
+# set of strings.
+#
+# $ nds abc-example-
+# $ nds abc- def- ghi-
+# $ nds -s WORDPRESS
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+#
+
+# Name self
+self=nagios-data-search
+
+# Usage printing function
+usage() {
+ printf 'USAGE: %s [-hs] STRING\n' "$self"
+}
+
+# By default we search hosts, not services
+services=0
+
+# Handle options, just -h help at the moment
+OPTIND=1
+while getopts 'hs' opt ; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ s)
+ services=1
+ ;;
+ '?')
+ usage >&2
+ exit 1
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# We need exactly one argument after that
+if ! (($#)) ; then
+ usage >&2
+ exit 1
+fi
+
+# Define the path to the Livestatus socket
+socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live}
+
+# Iterate through the search terms
+for search ; do
+ if ((services)) ; then
+ unixcat "$socket" <<EOF
+GET services
+Separators: 10 47 44 124
+Columns: host_name description
+Filter: host_name ~ $search
+Filter: description ~ $search
+Or: 2
+EOF
+ else
+ unixcat "$socket" <<EOF
+GET hosts
+Columns: host_name
+Filter: host_name ~ $search
+EOF
+ fi
+done | sort -V
diff --git a/nagios-data-search.1 b/nagios-data-search.1
new file mode 100644
index 0000000..3c1adf7
--- /dev/null
+++ b/nagios-data-search.1
@@ -0,0 +1,19 @@
+.TH NAGIOS-DATA-SEARCH 1 "Nagios Data Search" "Nagscripts"
+.SH NAME
+.B nds, nagios-data-search
+\- List hosts (or services) with names matching at least one of a set of
+strings
+.SH USAGE
+.B nagios-data-search [-hs] STRING1 [STRING2 ...]
+.SH DESCRIPTION
+.B nagios-data-search
+will write to standard output the names of hosts matching any of the given
+arguments as a substring.
+.P
+If -s is given, services with a host or service description matching any of the
+strings will be printed instead.
+.SH SEE ALSO
+nagios-acknowledge(1), nagios-downtime(1), nagios-force-check(1),
+nagios-notification-switch(1), nagios-problem-list(1), nagios-unhandled-list(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
diff --git a/nagios-downstream-list b/nagios-downstream-list
new file mode 100755
index 0000000..6485e89
--- /dev/null
+++ b/nagios-downstream-list
@@ -0,0 +1,62 @@
+#!/usr/bin/env bash
+
+#
+# nagios-downstream-list(1) -- List all the descendents of at least one given
+# host, unique and sorted.
+#
+# $ ndl abc-example-mc-1
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+#
+
+# Name self
+self=nagios-downstream-list
+
+# Usage printing function
+usage() {
+ printf 'USAGE: %s HOST1 [HOST2 ... ]\n' "$self"
+}
+
+# Handle options, just -h help at the moment
+OPTIND=1
+while getopts 'h' opt ; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ '?')
+ usage >&2
+ exit 1
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# We need at least one argument remaining
+if ! (($#)) ; then
+ usage >&2
+ exit 1
+fi
+
+# Define the path to the Livestatus socket
+socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live}
+
+# Recursive function to print all the descendents of the arguments
+descendents() {
+ for ancestor ; do
+ while read -r child ; do
+ printf '%s\n' "$child"
+ descendents "$child"
+ done < <(unixcat "$socket" <<EOF
+GET hosts
+Columns: host_name
+Filter: parents >= $ancestor
+EOF
+ )
+ done | sort -uV
+}
+
+# Run the recursive function on all the arguments given to this script
+descendents "$@"
diff --git a/nagios-downstream-list.1 b/nagios-downstream-list.1
new file mode 100644
index 0000000..9633d97
--- /dev/null
+++ b/nagios-downstream-list.1
@@ -0,0 +1,17 @@
+.TH NAGIOS-DOWNSTREAM-LIST 1 "Nagios Downstream List" "Nagscripts"
+.SH NAME
+.B ndl, nagios-downstream-list
+\- List hosts dependent on a given host
+.SH USAGE
+.B nagios-downstream-list
+.I HOSTNAME1 [HOSTNAME2 ...]
+.SH DESCRIPTION
+.B nagios-downstream-list
+will write to standard output the descendent hosts of the host(s) given as the
+sole argument as read from a MK LiveStatus socket.
+.SH SEE ALSO
+nagios-acknowledge(1), nagios-data-search(1), nagios-downtime(1),
+nagios-force-check(1), nagios-notification-switch(1), nagios-problem-list(1),
+nagios-unhandled-list(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
diff --git a/nagios-downtime b/nagios-downtime
index 9a886e0..fdf9988 100755
--- a/nagios-downtime
+++ b/nagios-downtime
@@ -5,17 +5,20 @@
# because it's annoying to do with the web interface for large sets of hosts
# or services.
#
-# $ ndt <host>[/<service>] <start> <end> [optional comment]
+# $ ndt (<host[/service][,host[/service],...>|-) <start> <end> [comment]\n' "$self"
#
-# Good for for/while loops:
+# You can specify multiple objects by separating them with commas:
#
-# for hostname in hosta hostb hostc; do ndt "$hostname" ... ; done
-# while read -r hostname; do ndt "$hostname" ... ; done < downtime-hosts
+# $ ndt abc-example-ap-1,abc-example-ap-2/VOLTAGE 9am 10am 'Power problems at abc-example'
+#
+# Even easier is to pipe them into stdin by specifying - as the object:
+#
+# $ nds abc-example | ndt - 9am 10am 'Power problems at abc-example'
#
# Assumes date(1) with +%s format and --date option (probably only GNU date).
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
-# Copyright: 2014 Sanctum
+# Copyright: 2016
#
# Name self
@@ -23,7 +26,7 @@ self=nagios-downtime
# Usage printing function
usage() {
- printf 'USAGE: %s <host[/service]> <start> <end> [comment]\n' "$self"
+ printf 'USAGE: %s [<host[/service][,host[/service],...>|-] <start> <end> [comment]\n' "$self"
}
# Process options (just help at the moment)
@@ -59,42 +62,65 @@ author=${SUDO_USER:-$USER}
comment=${4:-'no comment given'}
cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd}
-# If a service name is specified after a slash, figure that out
-if [[ $spec == */* ]] ; then
- host=${spec%/*}
- service=${spec##*/}
-else
- host=$spec
- service=
-fi
+# How to get the objects depends on the spec (the first argument)
+declare -a objects
+case $spec in
-# Quietly replace semicolons in comment with commas
-comment=${comment//;/,}
+ # If the spec is just "-", we just read unique objects from stdin
+ -)
+ while read -r object ; do
+ [[ $object ]] || continue
+ objects[${#objects[@]}]=$object
+ done < <(sort -u)
+ ;;
-# Attempt to parse start and end dates; fail usefully if the call doesn't work
-if ! dtstart=$(date +%s --date "$2") ; then
- printf '%s: Could not parse start date %s' "$self" "$2" >&2
- exit 1
-fi
-if ! dtend=$(date +%s --date "$3") ; then
- printf '%s: Could not parse end date %s' "$self" "$3" >&2
+ # If the spec is anything else, we break it up with commas and read the
+ # objects that way
+ *)
+ IFS=, read -a objects -r < <(printf '%s\n' "$spec")
+ ;;
+esac
+
+# All the hosts or services must exist, just to be strict
+for object in "${objects[@]}" ; do
+ nagios-exists "$object" && continue
+ printf '%s: Host/service %s does not seem to exist\n' \
+ "$self" "$object" >&2
exit 1
-fi
+done
-# Write command and print message if it fails; succeed silently
-if [[ $service ]] ; then
- cmd=$(printf '[%lu] SCHEDULE_SVC_DOWNTIME;%s;%s;%s;%s;%u;%u;%u;%s;%s' \
- "$now" "$host" "$service" "$dtstart" "$dtend" \
- "$fixed" "$trigger" "$duration" "$author" "$comment")
-else
- cmd=$(printf '[%lu] SCHEDULE_HOST_DOWNTIME;%s;%s;%s;%u;%u;%u;%s;%s' \
- "$now" "$host" "$dtstart" "$dtend" \
- "$fixed" "$trigger" "$duration" "$author" "$comment")
-fi
+# Attempt to parse start and end dates; fail if the call doesn't work
+dta=$(date +%s --date "$2") || exit
+dtb=$(date +%s --date "$3") || exit
-# Attempt to write command to file
-if ! printf '%s\n' "$cmd" > "$cmdfile" ; then
- printf '%s: Failed to write command to file\n' "$self" >&2
+# If the end time is less than right now, this is probably a mistake
+dtn=$(date +%s)
+if ((dtn > dtb)) ; then
+ printf '%s: Refusing to schedule downtime ending in the past (%s)\n' \
+ "$self" "$(date -d @"$dtb" +%c)"
exit 1
fi
+# Quietly replace semicolons in comment with commas
+comment=${comment//;/,}
+
+# Write commands to schedule downtime for each of the objects, bail if a single
+# one of them fails
+for object in "${objects[@]}" ; do
+ case $object in
+ */*)
+ host=${object%/*}
+ service=${object##*/}
+ cmd=$(printf '[%lu] SCHEDULE_SVC_DOWNTIME;%s;%s;%s;%s;%u;%u;%u;%s;%s' \
+ "$now" "$host" "$service" "$dta" "$dtb" \
+ "$fixed" "$trigger" "$duration" "$author" "$comment")
+ ;;
+ *)
+ host=$object
+ cmd=$(printf '[%lu] SCHEDULE_HOST_DOWNTIME;%s;%s;%s;%u;%u;%u;%s;%s' \
+ "$now" "$host" "$dta" "$dtb" \
+ "$fixed" "$trigger" "$duration" "$author" "$comment")
+ ;;
+ esac
+ printf '%s\n' "$cmd" > "$cmdfile" || exit
+done
diff --git a/nagios-downtime.1 b/nagios-downtime.1
index c4ef614..3581651 100644
--- a/nagios-downtime.1
+++ b/nagios-downtime.1
@@ -7,29 +7,28 @@
-h
.br
.B nagios-downtime
-.I HOSTNAME[/SERVICE]
+.I (HOSTNAME[/SERVICE][,HOSTNAME[,SERVICE],...]|-)
.I START
.I END
.I [COMMENT]
.SH SYNOPSIS
-.B ndt
-webhost now 10pm 'Upgrading to latest OS release'
+ndt webhost now 10pm 'Upgrading to latest Ubuntu release'
.PP
-.B ndt
-webhost/HTTP now 10pm 'Upgrading Apache HTTPD'
-.PP
-for hostname in ns{1..3} ; do
+ndt webhost/HTTP now 10pm 'Upgrading Apache HTTPD'
.br
-.B
- ndt
-"$hostname" 'sunday 3:00am' 'sunday 4:00am' 'Outage for entire DNS system for upgrades'
+ndt authns1,authns2,recdns1,recdns2 'sunday 3:00am' 'sunday 4:00am' 'Outage for entire DNS system for upgrades'
.br
-done
+nds abc-example | ndt - 9am 10am 'Power problems at abc-example'
.SH DESCRIPTION
.B nagios-downtime
will format and attempt to write a Nagios command to the Nagios commands file
which it expects to find at /usr/local/nagios/var/rw/nagios.cmd to schedule
-downtime for a particular host or service. The two date fields
+downtime for given hosts or services.
+.PP
+Multiple hosts or services can be separated by commas, and the special value -
+allows hosts or services to be read from stdin.
+.PP
+The two date fields
.I START
and
.I END
@@ -46,8 +45,8 @@ changes. Note that the
.I COMMENT
parameter is optional.
.SH SEE ALSO
-nagios-acknowledge(1), nagios-force-check(1)
+nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1),
+nagios-force-check(1), nagios-notification-switch(1), nagios-problem-list(1),
+nagios-unhandled-list(1)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
-.PP
-
diff --git a/nagios-exists b/nagios-exists
new file mode 100755
index 0000000..c9e6114
--- /dev/null
+++ b/nagios-exists
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+
+#
+# nagios-exists(1) -- Return an exit status corresponding to whether HOST or
+# HOST/SERVICE exists in Nagios. Mostly for use in scripts.
+#
+# $ nex abc-example-mc-1
+# $ nex webhost/HTTP
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+#
+
+# Name self
+self=nagios-exists
+
+# Usage printing function
+usage() {
+ printf 'USAGE: %s HOST[/SERVICE]\n' "$self"
+}
+
+# Handle options, just -h help at the moment
+OPTIND=1
+while getopts 'h' opt ; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ '?')
+ usage >&2
+ exit 1
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# We need exactly one argument remaining
+if (($# != 1)) ; then
+ usage >&2
+ exit 1
+fi
+
+# Define the path to the Livestatus socket
+socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live}
+
+# Which query to run depends on what the argument looks like.
+case $1 in
+
+ # If there's a slash in the argument, it's a host/service tuplet.
+ */*)
+ host_name=${1%/*}
+ service_description=${1#*/}
+ while read -r _ ; do
+ exit
+ done < <(unixcat "$socket" <<EOF
+GET services
+Columns: host_name service_description
+Filter: host_name = $host_name
+Filter: service_description = $service_description
+EOF
+ )
+ exit 1
+ ;;
+
+ # Otherwise, assume it's a host.
+ *)
+ host_name=$1
+ while read -r _ ; do
+ exit
+ done < <(unixcat "$socket" <<EOF
+GET hosts
+Columns: host_name
+Filter: host_name = $host_name
+EOF
+ )
+ exit 1
+ ;;
+esac
diff --git a/nagios-exists.1 b/nagios-exists.1
new file mode 100644
index 0000000..9411cb6
--- /dev/null
+++ b/nagios-exists.1
@@ -0,0 +1,29 @@
+.TH NAGIOS-EXISTS 1 "Nagios Exists" "Nagscripts"
+.SH NAME
+.B nex, nagios-exists
+\- Return an exit status based on whether a host or host/service pair exists in
+Nagios, checked via a MK Livestatus socket.
+.SH USAGE
+.B nagios-exists
+-h
+.br
+.B nagios-exists
+.I HOSTNAME[/SERVICE]
+.SH SYNOPSIS
+.B nex
+webhost
+.br
+.B nex
+webhost/HTTP
+.SH DESCRIPTION
+.B nagios-exists
+will query a Nagios installation via a MK Livestatus socket to determine
+whether a host or a host/service combination exists. If it exists, it exits
+with 0; otherwise it exits with 1. Under normal circumstances, it produces no
+output; it's intended to be used in scripts.
+.P
+The path to the socket defaults to /usr/local/nagios/var/rw/live. It can be
+overriden by setting MK_LIVESTATUS_SOCKET in the script's environment.
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
+.PP
diff --git a/nagios-force-check b/nagios-force-check
index 8c19620..ef12e35 100755
--- a/nagios-force-check
+++ b/nagios-force-check
@@ -4,10 +4,11 @@
# nagios-force-check(1) -- Force an immediate check of a nominated host or
# service.
#
-# $ nfc <host>[/<service>]
+# $ nac <host>[/<service>]
+#
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
-# Copyright: 2014 Sanctum
+# Copyright: 2016
#
# Name self
@@ -15,13 +16,17 @@ self=nagios-force-check
# Usage printing function
usage() {
- printf 'USAGE: %s [-n] <host[/service]>\n' "$self"
+ printf 'USAGE: %s (-a | <host[/service]> [<host[/service]..])\n' "$self"
}
# Handle options, just -h help at the moment
OPTIND=1
-while getopts 'h' opt ; do
+check_all_problems=0
+while getopts 'ah' opt ; do
case "$opt" in
+ a)
+ check_all_problems=1
+ ;;
h)
usage
exit 0
@@ -34,8 +39,13 @@ while getopts 'h' opt ; do
done
shift "$((OPTIND-1))"
-# Bail if no arguments left; we need at least the host/service name
-if ! (($#)) ; then
+# Bail if -a was selected but there are more arguments
+if ((check_all_problems)) && (($#)) ; then
+ usage >&2
+ exit 1
+
+# Bail if -a wasn't selected and there are no arguments
+elif ! ((check_all_problems)) && ! (($#)) ; then
usage >&2
exit 1
fi
@@ -43,35 +53,54 @@ fi
# Define relatively fixed/guaranteed fields for Nagios command; note that the
# comment has a default of 'no comment given'
now=$(date +%s)
-spec=$1
cmdfile=${NAGCMD_FILE:-/usr/local/nagios/var/rw/nagios.cmd}
-# If a service name is specified after a slash, figure that out
-if [[ $spec == */* ]] ; then
- host=${spec%/*}
- service=${spec##*/}
+# Specs are either arguments or all unhandled problems
+declare -a specs
+if ((check_all_problems)) ; then
+ while read -r object ; do
+ specs=("${specs[@]}" "$object")
+ done < <(nagios-problem-list)
else
- host=$spec
- service=
+ specs=("$@")
fi
-# Write command and print message if it fails; succeed silently
-declare -a cmds
-if [[ $service ]] ; then
- cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu' \
- "$now" "$host" "$service" "$now")")
-else
- cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_FORCED_HOST_CHECK;%s;%lu' \
- "$now" "$host" "$now")")
- cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_SVC_CHECKS;%s;%lu' \
- "$now" "$host" "$now")")
-fi
+# Iterate through the specs given and run the force recheck command
+for spec in "${specs[@]}" ; do
-# Attempt to write command to file
-for cmd in "${cmds[@]}" ; do
- if ! printf '%s\n' "$cmd" >> "$cmdfile" ; then
- printf '%s: Failed to write command to file\n' "$self" >&2
- exit 1
+ # Check the host or service exists
+ if ! nagios-exists "$spec" ; then
+ printf '%s: Host/service %s does not seem to exist\n' \
+ "$self" "$spec" >&2
+ fi
+
+ # If a service name is specified after a slash, figure that out
+ if [[ $spec == */* ]] ; then
+ host=${spec%/*}
+ service=${spec##*/}
+ else
+ host=$spec
+ service=
fi
-done
+ # Write command and print message if it fails; succeed silently
+ declare -a cmds
+ cmds=()
+ if [[ $service ]] ; then
+ cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_SVC_CHECK;%s;%s;%lu' \
+ "$now" "$host" "$service" "$now")")
+ else
+ cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_CHECK;%s;%lu' \
+ "$now" "$host" "$now")")
+ cmds=("${cmds[@]}" "$(printf '[%lu] SCHEDULE_HOST_SVC_CHECKS;%s;%lu' \
+ "$now" "$host" "$now")")
+ fi
+
+ # Attempt to write commands to file
+ for cmd in "${cmds[@]}" ; do
+ if ! printf '%s\n' "$cmd" >> "$cmdfile" ; then
+ printf '%s: Failed to write command to file\n' "$self" >&2
+ exit 1
+ fi
+ done
+done
diff --git a/nagios-force-check.1 b/nagios-force-check.1
index 27d7114..9836247 100644
--- a/nagios-force-check.1
+++ b/nagios-force-check.1
@@ -7,7 +7,10 @@
-h
.br
.B nagios-force-check
-.I HOSTNAME[/SERVICE]
+-a
+.br
+.B nagios-force-check
+.I HOSTNAME[/SERVICE] [HOSTNAME[/SERVICE]...]
.SH SYNOPSIS
.B nfc
webhost
@@ -26,11 +29,24 @@ done
.B nagios-force-check
will format and attempt to write a Nagios command to the Nagios commands file
which it expects to find at /usr/local/nagios/var/rw/nagios.cmd to force a
-check of a particular host (and all its services) or just one particular
-service.
+check of at least one particular host (and all its services) or at least one
+particular service.
+.P
+You can use the
+.B -a
+switch on its own (with no targets specified) to force checks on all problem
+services returned by
+.B nagios-problem-list(1)
+-- this is equivalent to running something like:
+.P
+npl | while read -r object ; do
+.br
+ nfc "$object"
+.br
+done
.SH SEE ALSO
-nagios-acknowledge(1), nagios-downtime(1)
+nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1),
+nagios-downtime(1), nagios-notification-switch(1), nagios-problem-list(1),
+nagios-unhandled-list(1)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>
-.PP
-
diff --git a/nagios-problem-list b/nagios-problem-list
new file mode 100755
index 0000000..3927feb
--- /dev/null
+++ b/nagios-problem-list
@@ -0,0 +1,61 @@
+#!/usr/bin/env bash
+
+#
+# nagios-problem-list(1) -- List all the hosts and services in a problem state,
+# handled or not
+#
+# $ npl
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+#
+
+# Name self
+self=nagios-problem-list
+
+# Usage printing function
+usage() {
+ printf 'USAGE: %s [-h]\n' "$self"
+}
+
+# By default we search hosts, not services
+services=0
+
+# Handle options, just -h help at the moment
+OPTIND=1
+while getopts 'h' opt ; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ '?')
+ usage >&2
+ exit 1
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# Any arguments after that are abuse
+if (($#)) ; then
+ usage >&2
+ exit 1
+fi
+
+# Define the path to the Livestatus socket
+socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live}
+
+unixcat "$socket" <<EOF | sort -V
+GET hosts
+Columns: host_name
+Filter: state != 0
+Filter: state != 3
+EOF
+unixcat "$socket" <<EOF | sort -V
+GET services
+Separators: 10 47 44 124
+Columns: host_name description
+Filter: state != 0
+Filter: state != 4
+EOF
diff --git a/nagios-problem-list.1 b/nagios-problem-list.1
new file mode 100644
index 0000000..8ce7bcc
--- /dev/null
+++ b/nagios-problem-list.1
@@ -0,0 +1,30 @@
+.TH NAGIOS-PROBLEM-LIST 1 "Nagios Problem List" "Nagscripts"
+.SH NAME
+.B npl, nagios-problem-list
+\- List hosts and services presently in problem state for ndt(1) input
+.SH USAGE
+.B nagios-problem-list
+.PP
+.B npl
+| while read -r object ; do
+.br
+ ndt "$object" 'sunday 3:00am' 'sunday 4:00am' 'Outage for all problems'
+.br
+done
+.SH DESCRIPTION
+.B nagios-problem-list
+will write to standard output the hosts and services that are presently in
+problem state. It's intended as a way to select hosts for
+.B nac(1)
+or
+.B ndt(1)
+input. It differs from
+.B nagios-unhandled-list
+in that it shows all objects in problem state, including any that are
+acknowledged or in downtime.
+.SH SEE ALSO
+nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1),
+nagios-downtime(1), nagios-force-check(1), nagios-notification-switch(1),
+nagios-unhandled-list(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
diff --git a/nagios-unhandled-list b/nagios-unhandled-list
new file mode 100755
index 0000000..d5d97fd
--- /dev/null
+++ b/nagios-unhandled-list
@@ -0,0 +1,65 @@
+#!/usr/bin/env bash
+
+#
+# nagios-unhandled-list(1) -- List all unhandled hosts and services in a
+# problem state.
+#
+# $ npl
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+#
+
+# Name self
+self=nagios-unhandled-list
+
+# Usage printing function
+usage() {
+ printf 'USAGE: %s [-h]\n' "$self"
+}
+
+# By default we search hosts, not services
+services=0
+
+# Handle options, just -h help at the moment
+OPTIND=1
+while getopts 'h' opt ; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ '?')
+ usage >&2
+ exit 1
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# Any arguments after that are abuse
+if (($#)) ; then
+ usage >&2
+ exit 1
+fi
+
+# Define the path to the Livestatus socket
+socket=${MK_LIVESTATUS_SOCKET:-/usr/local/nagios/var/rw/live}
+
+unixcat "$socket" <<EOF | sort -V
+GET hosts
+Columns: host_name
+Filter: state != 0
+Filter: state != 3
+Filter: acknowledged = 0
+Filter: scheduled_downtime_depth = 0
+EOF
+unixcat "$socket" <<EOF | sort -V
+GET services
+Separators: 10 47 44 124
+Columns: host_name description
+Filter: state != 0
+Filter: state != 4
+Filter: acknowledged = 0
+Filter: scheduled_downtime_depth = 0
+EOF
diff --git a/nagios-unhandled-list.1 b/nagios-unhandled-list.1
new file mode 100644
index 0000000..d67eaec
--- /dev/null
+++ b/nagios-unhandled-list.1
@@ -0,0 +1,30 @@
+.TH NAGIOS-UNHANDLED-LIST 1 "Nagios Unhandled List" "Nagscripts"
+.SH NAME
+.B nul, nagios-unhandled-list
+\- List hosts and services presently in unhandled problem state for ndt(1) input
+.SH USAGE
+.B nagios-unhandled-list
+.PP
+.B nul
+| while read -r object ; do
+.br
+ ndt "$object" 'sunday 3:00am' 'sunday 4:00am' 'Outage for all unhandled problems'
+.br
+done
+.SH DESCRIPTION
+.B nagios-unhandled-list
+will write to standard output the hosts and services that are presently in
+unhandled problem state. It's intended as a way to select hosts for
+.B nac(1)
+or
+.B ndt(1)
+input. It differs from
+.B nagios-problem-list
+in that it shows only objects in unhandled problem state, thereby excluding any
+that are acknowledged or in downtime.
+.SH SEE ALSO
+nagios-acknowledge(1), nagios-data-search(1), nagios-descendent-list(1),
+nagios-downtime(1), nagios-force-check(1), nagios-notification-switch(1),
+nagios-problem-list(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>