Jump to content

Quick Uptime Test For About 100 Cisco Switches


davil
 Share

Recommended Posts

Hi All,

just wondering if anybody has any advice for a quick uptime test tool that I'm writing, that I want to integrate into our company Intranet here - We have Windows systems primarily, but I will setup Ubuntu in a VM if I really have to. The most important thing here is speed, as there's at least 100 switches and about 20 servers and I need the monitor to continue, and run once a minute or so. I've used some examples from the PHP.net pages and tried to cobble something together as I'm definitely no PHP pro

Anyway, I have tried the following PHP code:

<html>
<head>
<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
                      }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>

and that code above works great for monitoring Windows servers - it's very fast at checking through 20 anyway - the page 'refreshes' in about 2 seconds, even if hosts are down etc.

but the way it works doesn't suit Cisco switches, so I had to try to figure out something else for those.

I re-wrote the code using the PEAR NET_PING class to look like this:

<?php

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

require_once "Net/Ping.php";
$ping = Net_Ping::factory();

$ping->setArgs(array('count' => 2, 'ttl' => 50, 'timeout' => 1));
/*-----------------------------------------------------------------------------------------*/
function DoPing($ip)
{
	global $ping;

	$results = $ping->ping($ip);
	if ($results->_loss==0) {return true;}else{return false;}
}
/*-----------------------------------------------------------------------------------------*/
   function DoTheCheck($name,$ip){
   	   global $errors;
   	   global $j;

   	   if (DoPing($ip)==1){
   	//do nothing
                      }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)) && (!strstr($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>';}

?>

but unfortunately when I add in ten Cisco switches into the list to be checked, it works, but can take a minute or two to run the script. This just isn't fast enough for a monitoring system, especially if I want to store the data in a MySQL table and graph it later on.

Is there any way to streamline this so that it can quickly do an uptime check on these servers & switches - I'm trying to do the same in Ruby with 'net/ping' but getting nowhere as well.

the switches themselves do respond to normal pings.

And I know most people will just think "use SNMP you fool!!!" but the SNMP public and private settings here are all over the place and I'm not allowed to change them on any switches. also I don't fully understand SNMP but if there's a way to do an SNMP scan on a list of hosts, no matter what their settings are, I'll gladly go that route.

So basically I'm looking for overall advice on this sort of thing -

Should I use:

Sockets ?

ICMP ?

Some sort of Layer 2 scanning for the mac addresses ?

echo (port 7) ?

Something else (but not SNMP unless it's the ONLY option)?

Should I use PHP or Ruby or is there a command line EXE out there already that I can use ?

can nmap be used to scan a text file full of hosts and return just the hosts that are down (or some similar info that I can parse in PHP)

I'm not looking for a full software to do this sort of monitoring - it needs to be something I can utilise in our own site , so preferably PHP / JS / Ruby (preferably something I can read, but I'll take advice on any language at this stage), or an EXE that just works, that I can parse the data from.

There must be some way to do this fast, otherwise how would IPmonitor exist?

Anyhow thanks for taking the time to read through my long rant. If anybody has any advice, I'd really appreciate it.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...