From 4eb51fad87f333b36bcb32633e2e0653bdff120d Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Tue, 19 Dec 2006 22:41:04 +0000
Subject: [PATCH] Check that Nagios monitors all hosts found by Enodia.

---
 Makefile               |  3 ++-
 check_enodia_monitored | 39 +++++++++++++++++++++++++++++++++++++++
 check_ping             | 18 ++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100755 check_enodia_monitored
 create mode 100755 check_ping

diff --git a/Makefile b/Makefile
index 5b2059b..5475d93 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
 DESTDIR = /usr/local/nagios/libexec/
 SCRIPTS = check_cups check_glsa check_saned check_lpd check_hddtemp \
-	check_link_status check_true check_lysrdiff check_syslog
+	check_link_status check_true check_lysrdiff check_syslog \
+	check_ping check_enodia_monitored
 
 all:;
 
diff --git a/check_enodia_monitored b/check_enodia_monitored
new file mode 100755
index 0000000..b55b7be
--- /dev/null
+++ b/check_enodia_monitored
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Check that all hosts in /opt/enodia/export/active_ips.txt are
+# monitored by Nagios.  More accurately, for each line in
+# active_ips.txt, check that /usr/local/nagios/var/checked_ips/$ip
+# exists and is at most 24 hours old.  That file is created by the
+# check_ping script.
+
+CHECKED=/usr/local/nagios/var/checked_ips
+UNCHECKED=`tempfile`
+unmonitored=""
+
+find "$CHECKED" -mtime +1 -exec rm {} \;
+while read ip host
+do
+    if [ ! -f $CHECKED/$ip ]
+    then
+	echo "$ip ($host)" >> "$UNCHECKED"
+    fi
+done < /opt/enodia/export/active_ips.txt
+
+unchecked=`wc -l < "$UNCHECKED"`
+if [ $unchecked = 0 ]
+then
+    echo "OK - all checked | unchecked=0"
+    mv -f "$UNCHECKED" /usr/local/nagios/var/unchecked
+    exit 0
+else
+    echo -n "CRITICAL - $unchecked unchecked hosts: "
+    sed 's/\.lysator\.liu\.se)$/)/' "$UNCHECKED" \
+    | awk 'BEGIN { sz = 0 }
+             NR > 1 && sz < 198 { printf ", "; sz += 2 } 
+             sz < 200 { printf "%s", $0; sz += length($0) }
+	     END { if (sz >= 200) printf ", others" } ' \
+    | sed 's/, \([^,]*\)$/ and \1/'
+    echo ". | unchecked=$unchecked"
+    mv -f "$UNCHECKED" /usr/local/nagios/var/unchecked
+    exit 2
+fi
diff --git a/check_ping b/check_ping
new file mode 100755
index 0000000..4ae64d6
--- /dev/null
+++ b/check_ping
@@ -0,0 +1,18 @@
+#!/bin/sh
+[ "$1" = "-H" ] || fatal
+HOSTADDRESS="$2"
+[ "$3" = "-w" ] || fatal
+WARN="$4"
+[ "$5" = "-c" ] || fatal
+CRIT="$6"
+[ "$7" = "-p" ] || fatal
+P="$8"
+
+IP_TMP=`tempfile`
+if getent hosts  $HOSTADDRESS  > "$IP_TMP"
+then
+    read ip host < "$IP_TMP"
+    touch /usr/local/nagios/var/checked_ips/$ip
+fi
+rm -f "$IP_TMP"
+exec /usr/nagios/libexec/check_ping -H "$HOSTADDRESS" -w "$WARN" -c "$CRIT" -p "$P"
-- 
GitLab