aboutsummaryrefslogtreecommitdiff
path: root/nagios-data-search
diff options
context:
space:
mode:
Diffstat (limited to 'nagios-data-search')
-rwxr-xr-xnagios-data-search72
1 files changed, 72 insertions, 0 deletions
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