#!/bin/sh

# This plugin executes check_ping on a rotating set of targets maintained in:
TARGETSFILE=/var/tmp/check_internet
OWNER="nagios:nagios"
# Targets that were reachable the last time they were pinged are listed as follows:
#	<IP address> <current PTR hostname> <avg RTA>
# Targets that were NOT reachable will be listed as follows:
#	<IP address> <historic PTR hostname>

# Since the plugin occasionally "loses" hosts from $TARGETSFILE (about one per
# month for me), we will occasionally reinitialize it from a master file:
MASTERFILE="$TARGETSFILE.master"
MASTERDAYS="14"
REMASTERED=false
if [ -e $MASTERFILE ]; then
   if [ -r $MASTERFILE -a -f $MASTERFILE ]; then
      if [ "`find $MASTERFILE -maxdepth 0 -mtime +$MASTERDAYS -print`" != "" ]; then
         cp $MASTERFILE $TARGETSFILE
         touch $MASTERFILE
         REMASTERED=true
      fi
   else
      echo "UNKNOWN: Cannot read master list of targets host $MASTERFILE"
      exit 3
   fi
fi

MAXTRIES=3
PINGER="`dirname $0`/check_ping"
PARAMS="-w 4000,100% -c 4000,100% -p 3 -t 3"

if [ ! -w $TARGETSFILE ]; then
   echo "UNKNOWN: Need a writeable list of target hosts in $TARGETSFILE"
   exit 3
fi
if [ `grep -c '[0-9]' $TARGETSFILE` -lt $MAXTRIES ]; then
   echo "UNKNOWN: Need a list of at least $MAXTRIES target hosts in $TARGETSFILE"
   exit 3
fi
if [ ! -x $PINGER ]; then
   echo "UNKNOWN: I need a copy of check_ping to function"
   exit 3
fi

UPCOUNT=0
DOWNCOUNT=0

while [ $UPCOUNT -eq 0 -a $DOWNCOUNT -lt $MAXTRIES ]; do
   # Grab first entry from file
   READLIN=`head -1 $TARGETSFILE`
   tail -n +2 $TARGETSFILE > $TARGETSFILE.tmp.$$
   mv -f $TARGETSFILE.tmp.$$ $TARGETSFILE
   if [ `id -u` -eq 0 ]; then
      chown $OWNER $TARGETSFILE
   fi
   # Parse it
   ADDRESS=`echo $READLIN | awk '{ print $1; }'`
   LASTFQDN=`echo $READLIN | awk '{ print $2; }'`
   # Make backup file
   echo "$ADDRESS $LASTFQDN" > $TARGETSFILE.bak.$$

   # Test the target
   RESULTTXT=`$PINGER -H $ADDRESS $PARAMS`
   RESULTSTAT=$?
   if [ $RESULTSTAT -eq 0 ]; then
      UPCOUNT=`expr $UPCOUNT + 1`
      RESULTTXT=`echo unknown $RESULTTXT | sed -e 's/.* RTA *= *//' -e 's/ .*//'`
      CURRFQDN=`( echo no.ptr.invalid. ; host $ADDRESS | grep 'domain name pointer' ) | sed -e 's/.* //' | tail -1`
      if [ "$CURRFQDN" = "no.ptr.invalid." ]; then
         CURRFQDN="$LASTFQDN"
      fi
      sed -e 's/ .*/ '"$CURRFQDN $RESULTTXT/" $TARGETSFILE.bak.$$ >> $TARGETSFILE
      rm -f $TARGETSFILE.bak.$$
   else
      DOWNCOUNT=`expr $DOWNCOUNT + 1`
      cat $TARGETSFILE.bak.$$ >> $TARGETSFILE
      rm -f $TARGETSFILE.bak.$$
   fi
done

# Only provide stats when run as a *service* check
# (Stats from *host* checks "confuse" n2rrd)
STATS=""
if [ "$NAGIOS_SERVICEDESC" != "" -a "$MAGIOS_SERVICEDESC" != '$' ]; then
   SCANNED=`expr $UPCOUNT + $DOWNCOUNT`
   STATS="scanned=$SCANNED;;;0; up=$UPCOUNT;;;0;$SCANNED down=$DOWNCOUNT;;$MAXTRIES;0;$SCANNED"
   TOTAL=`awk 'BEGIN { c=0; } { if (NF>0) c++; } END { print c; }' $TARGETSFILE`
   LASTUP=`awk 'BEGIN { c=0; } { if (NF>2) c++; } END { print c; }' $TARGETSFILE`
   LASTDOWN=`awk 'BEGIN { c=0; } { if (NF<3) c++; } END { print c; }' $TARGETSFILE`
   STATS="$STATS total=$TOTAL;;;0; total_up=$LASTUP;;;0;$TOTAL total_down=$LASTDOWN;;;0;$TOTAL"
fi

if [ $UPCOUNT -gt 0 ]; then
   if $REMASTERED ; then
      echo "NOTIFICATION: Remastered the list of Internet hosts, at least some are reachable|$STATS"
      exit 1
   else
      echo "OK: The Internet seems to be reachable|$STATS"
      exit 0
   fi
else
   echo "CRITICAL: Found $MAXTRIES Internet hosts unreachable|$STATS"
   exit 2
fi
