Jump to content

PHP Ping Monitor I made, but it won't check cisco switches


davil

Recommended Posts

Hi all,

 

with the help of some other person's code (not sure where I got it as it was a while back, but thanks to that person all the same),

 

I've created a simple ping monitor in PHP that will ping all machines in a given INI file and then report back if any of them aren't working. it's very basic but it seems to work for most of our servers here but it can't monitor Cisco switches for some reason, probably to do with the protocol involved but I'm no networking expert so I wonder if anybody can help with this?

 

<html>
<head>
<style type='text/css'>

*{
font-family:verdana,tahoma,arial;
font-size:17px;
}

.light{width:30px;}

h1{
	font-size:25px;
}
</style>



<meta http-equiv="refresh" content="30">
</head>
<body>
<?php

$time1=date('H:i:s');
echo "Last Refresh Time = $time1<br/><hr/>";

error_reporting(0);

/*-----------------------------------------------------------------------------------------*/    
    // Checksum calculation function
    function icmpChecksum($data)
    {
    if (strlen($data)%2)
    $data .= "\x00";
    
    $bit = unpack('n*', $data);
    $sum = array_sum($bit);
    
    while ($sum >> 16)
    $sum = ($sum >> 16) + ($sum & 0xffff);
    
    return pack('n*', ~$sum);
    }
    
    
/*-----------------------------------------------------------------------------------------*/
    function PingTry1($pingaddress){
    // Making the package
    $type= "\x08";
    $code= "\x00";
    $checksum= "\x00\x00";
    $identifier = "\x00\x00";
    $seqNumber = "\x00\x00";
    $data= "testing123";
    $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
    $checksum = icmpChecksum($package); // Calculate the checksum
    $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
    // And off to the sockets
    $socket = socket_create(AF_INET, SOCK_RAW, 1);
    
    socket_set_option ( $socket, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0) );
    socket_connect($socket, $pingaddress, null);

    $startTime = microtime(true);
    socket_send($socket, $package, strLen($package), 0);
    if (socket_read($socket, 255)) {
    return true;
    }
    else{
    return false;
    }
    
    socket_close($socket);
    
    }
/*-----------------------------------------------------------------------------------------*/
   function DoTheCheck($name,$ip){
   	   global $errors;
   	   global $j;
   	   if (PingTry1($ip)==1){
   	//do nothing
   //ECHO " $name ....... --> UP<br/>";
      
                      }else{
                      $j++;
                      $errors[$j] = "$name --> $ip";
                     
                      }
    
    }
/*-----------------------------------------------------------------------------------------*/

//READ IN THE INI FILE INTO $filedata Array

$myFile1="hosts.ini";
$filehandle1 = fopen($myFile1, 'r') or die("Couldn't open file [$myFile1]");
$number1=count(file($myFile1));;
$filedata = fread($filehandle1, filesize($myFile1));
fclose($filehandle1);

// Create an array with each line of the file
$array1 = explode("\r\n", $filedata);

unset($filedata); //free up a bit of memory

foreach ($array1 as &$line) { // step through the array, line by line
if (!empty($line)){
list ($name,$ip)=split(",",$line);
    DoTheCheck($name,$ip);
    			 }
}


    if ($errors){
    	   
    	    echo 'The Following Hosts are down - <br/><br/><table>';
    	    
foreach ($errors as &$value) {
$k++;
    echo '<tr><td><img class="light" src="red.png" /></td><td>'.$errors[$k].'</td></tr>';
}
echo '</tr></table>';
    }
else{echo '<img class="light" src="green.png" /><h1>ALL IPS ARE UP!</h1>';}
    ?>
    </body>
    </html>

 

here are the green.png and red.png files if anybody wants to try this out:

redzx.png

greenoo.png

 

just rename them to green.png and red.png and store in same folder as the PHP above.

 

also you'll need to create a hosts.ini text file that just looks like this:

 

Myserver1,10.100.0.1

My Other Server [the NAS],10.100.1.12

Myserver-3,10.124.0.1

 

you get the idea.

 

Any help is much appreciated.

Link to comment
Share on other sites

Alright, here's the thing.  Right now I'm a Cisco Discovery (CCNA track) student.  I want to bring this to your attention.  Switches are layer 2 devices (which mean they operate at the MAC address level), which can be assigned an IP address (on VLAN 1--the administrative VLAN) for in band management.  The proper method of determining network health of a switch would be using SNMP not ICMP echoes (Packet InterNet Groper). 

 

1. Check if a host (preferably the server) on the same network can ping the switch using the command line, not the script.

 

It is possible that the reply from the switch may not be ICMP 0 (echo-reply) and you can modify your script to accept maybe a 3:2.

Or you may have to configure your cisco switch to have an ip address on a vlan, and make sure that the sever is on the same VLAN (and same subnetwork).

 

Look into SNMP as an alternative. 

Link to comment
Share on other sites

Hi Andrew, thanks for the post, I know a little about the OSI model etc and I know about SNMP but the problem is I'm not the overall network administrator of the wider WAN we work on and the SNMP community settings aren't the same on every switch I deal with, therefore ping is my only option currently. I do know however that SNMP will give better information about the switch's health but I'm fighting a losing battle as I've no CCNA behind me and I've only been given limited access to the switches (show ver etc.) through my tacacs authentication. So as usual, I'm left doing things the awkward way. We had an old copy of solarwinds ipmonitor and it was very handy but the server died on it's ass. so I'm just trying to come up with a basic system that covers our needs, but I saw a tweet last night that linked me to this:

 

http://www.softinventive.com/products/total-network-monitor/

 

might just try that as it's free. Getting money is difficult at the moment with the state my country is in.

Link to comment
Share on other sites

I can ping them using "ping", I am unfamiliar with CLI. Hold on, A quick google tells me CLI means command-line interface. strange I never heard that acronym in 16 years of usings PCs lol but I presume you mean command prompt  / terminal or whatever. anyway, to get back to the point, yep they'll ping no problem. I'm not sure if it's vlan1 (about 75% sure) or whatever but it's a fairly basic setup on most of the switches yes.

Link to comment
Share on other sites

Google the friend who set this up for you and have him try a terminal connection with the IP address and have it preg_match a successful login.

 

*beyond the scope of this poster.* 

 

Too many TLA s I know.  (so you don't have to Google: three letter acronyms)

 

OR:

 

try this

 

http://www.php.net/manual/en/function.fsockopen.php#52966

 

Link to comment
Share on other sites

I don't think you followed that link because the example on sockets page shows you how to query mac addresses from cisco switches.  Using the function I linked you could query the MAC table on the switch--a feat you couldn't do without it being online.  This could mitigate your problem.

Link to comment
Share on other sites

Yes you are right I haven't yet checked the link, but I did say I'd check out sockets (fsockopen) - I'm posting on my iPod last couple of days as I'm at an Arts festival, give a guy a chance lol. Thanks for ur help though, didn't mean to be rude bout the acronym thing it was just that I posted my train of thought rather than changing the post after I figured out the CLI thing

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.