PHP Pinger

Discuss Programming
Post Reply
X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

PHP Pinger

Post by X11 »

I wrote this program in php to ping all the computers on my network and to email me if one is down. But it dont dang work!

Code: Select all

#!/usr/bin/php -q

<?php

$ipblock = "192.168.0";
$srange="1";
$erange="5";
$administrator = "exeleven@tpg.com.au";
$hostname = "billy-nra";

$darray = getdate();
$date = $darray[hours].":".$darray[minutes].":".$darray[seconds]." ". $darray[mday]."/".$darray[mon]."/".$darray[year]." (".$darray[0].")";

print "pinger started on $date\n";

while( $erange > $srange )
{
        $address = $ipblock.".".$srange;
        print "trying $address\n";
        exec( "ping -w 10 -c 1 $address | grep \"$address\"",$output,$return);
        foreach( $output as $file )
                $output2 += $file."\n";
        if ( $return) { die("major error - ping did NOT WORK CORRECTLY\n$output2"); }
        if ( strstr($output,"64 bytes from ".$address) )
        {
                print "host $address is responding\n";
        }
        else
        {
                print "host $address is not responding\n";
                print $output2;
                $subject = $address." failed";
                $messege = "This is a messege from ".$hostname."\n";
                $messege += "--------------------------------\n\n";
                $messege += "we suspect that the host ".$address." has failed\n";
                $messege += "output from ping here:\n";
                $messege += $output2;
                mail( $administrator , $subject , $messege , "$from\r\nX-Priority: 1 (Highest)");
        }
        $srange++;
}
print "done\n";
?>

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

Why PHP, just to see if you can? I do this sort of stuff all the time in sh/bash/ksh scripts. There are also good free system monitors that do ping checks, among many more advanced checks like Big Brother or Nagios (used to be called NetSaint).

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

Good point, im much better at PHP.

I could do this in a shell script, except I couldnt work out how to have an "if" work on the output of the ping and email me if it failed.

Could you please show me how to do this.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

Code: Select all

if ping -c 1 -w 1 $IPADDR > /dev/null 2>&1; then
  echo "Host $IPADDR is up"
else
  echo "Host $IPADDR is down, mailing my master"
  echo "Dude, you have a problem with $IPADDR" | mail -s "ping $IPADDR failed" x11@somewhere.com
fi

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

Cool, thanks for that.

I learn somthing new everyday.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

This is it at the moment, I cant remember how to add damn numbers together.

Code: Select all

#!/bin/sh
IPRANGE="192.168.0."
IPFROM=1
IPTILL=5

while [ "$IPFROM" -lt "$IPTILL" ]
do
        IPADDR=$IPRANGE+$IPFROM
        if ping -c 1 -w 1 $IPADDR > /dev/null 2>&1; then
          echo "Host $IPADDR is up"
        else
          echo "Host $IPADDR is down, mailing my master"
          echo "Dude, you have a problem with $IPADDR" | mail -s "ping $IPADDR failed" exeleven@tpg.com.au
        fi
        $IPFROM=$($IPFROM+$(1))

done

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

I prefer bash or ksh for this:

Code: Select all

#!/bin/bash
IPRANGE="192.168.0"
IPFROM=1
IPTILL=5
                                                                                
while ((IPFROM <= IPTILL))
do
        IPADDR=$IPRANGE.$IPFROM
        if ping -c 1 -w 1 $IPADDR > /dev/null 2>&1; then
          echo "Host $IPADDR is up"
        else
          echo "Host $IPADDR is down, mailing my master"
          echo "Dude, you have a problem with $IPADDR" | mail -s "ping $IPADDR failed" exeleven@tpg.com.au
        fi
        ((IPFROM=IPFROM+1))
                                                                                
done
Now, I don't know if you are just doing this as an exercise or if you actually plan on using it. If you actually plan on using it and you intend to run it like every 5 minutes from a cron you will be very annoyed because you will get many email messages when something goes down for an extended period of time, or the box running the pinger loses network connectivity.

Big Brother started out as a very similar script to what you are writing above for pinging hosts but it has logic built in so that it will send one message when a system cannot be contacted and remember that it is down. It still checks it every 5 minutes but will only send one message that it is down. Then when the system comes back up it sends another message. I have it set to send me a message to both my email and my pager depending on the system. Actually, I also have it set to send a message every hour that a system is down but it will notify me the minute a system comes back up. There are also many other checks that it will notify you of like disk space, bandwidth, CPU utilization, makes sure processes are running, etc, etc, etc. I even set it to monitor the temperature and the humidity in the computer room and notify when they are out of limits.

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 »

Cool thanks, This will run every hour in cron.hourly, it should be alright for what I want.

Post Reply