PHP Pinger

Discuss Programming

PHP Pinger

Postby X11 » Mon Jun 16, 2003 11:22 pm

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";
?>
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Tue Jun 17, 2003 7:15 am

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).
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Tue Jun 17, 2003 8:39 am

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.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Tue Jun 17, 2003 1:25 pm

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
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Tue Jun 17, 2003 7:39 pm

Cool, thanks for that.

I learn somthing new everyday.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby X11 » Tue Jun 17, 2003 7:48 pm

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
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia

Postby Void Main » Tue Jun 17, 2003 8:21 pm

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.
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby X11 » Tue Jun 17, 2003 11:16 pm

Cool thanks, This will run every hour in cron.hourly, it should be alright for what I want.
X11
guru
guru
 
Posts: 674
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron