aboutsummaryrefslogtreecommitdiff
path: root/nagios-data-search
diff options
context:
space:
mode:
Diffstat (limited to 'nagios-data-search')
-rwxr-xr-xnagios-data-search43
1 files changed, 37 insertions, 6 deletions
diff --git a/nagios-data-search b/nagios-data-search
index 5be21ca..ffa0bb4 100755
--- a/nagios-data-search
+++ b/nagios-data-search
@@ -8,6 +8,9 @@
# $ nds abc- def- ghi-
# $ nds -s WORDPRESS
#
+# Add -d to include a unique list of the hosts downstream of any/all of the
+# matched hosts. This is incompatible with the -s option.
+#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2016
#
@@ -17,35 +20,46 @@ self=nagios-data-search
# Usage printing function
usage() {
- printf 'USAGE: %s [-hs] STRING\n' "$self"
+ printf 'USAGE: %s [-h | -d | -s] STRING\n' "$self"
}
-# By default we search hosts, not services
+# By default we search hosts, not services, and don't include downstreams
+downstream=0
services=0
# Handle options, just -h help at the moment
OPTIND=1
-while getopts 'hs' opt ; do
+while getopts 'dhs' opt ; do
case "$opt" in
h)
usage
exit 0
;;
+ d)
+ downstream=1
+ ;;
s)
services=1
;;
'?')
usage >&2
- exit 1
+ exit 2
;;
esac
done
shift "$((OPTIND-1))"
+# There is no such thing as a downstream service (well, not really), so bail if
+# the user asked for them
+if ((downstream && services)) ; then
+ printf '%s: Cannot specify both -d and -s\n' "$self" >&2
+ exit 2
+fi
+
# We need exactly one argument after that
if ! (($#)) ; then
usage >&2
- exit 1
+ exit 2
fi
# Define the path to the Livestatus socket
@@ -69,4 +83,21 @@ Columns: host_name
Filter: host_name ~ $search
EOF
fi
-done | sort -V
+done |
+
+# Pass output through a while-read loop that appends the output of ndl(1) for
+# this host if downstream is set; otherwise, stdin just gets quietly duplicated
+# by this block
+while IFS= read -r host ; do
+ printf '%s\n' "$host"
+ if ((downstream)) ; then
+ ndl -- "$host"
+ fi
+done |
+
+# Sort the output version-wise (so that foo-bar-ap-1 comes before
+# foo-bar-ap-10); note that -V is not a POSIX flag (supported by GNU sort)
+sort -V |
+
+# Ensure the output is unique
+uniq