| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  | #!/usr/bin/php
 | 
					
						
							|  |  |  | <? | 
					
						
							|  |  |  | #
 | 
					
						
							|  |  |  | # Test Reachability
 | 
					
						
							|  |  |  | #
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | include("config.php"); | 
					
						
							|  |  |  | include("includes/functions.php"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-17 20:26:29 +00:00
										 |  |  | $device_query = mysql_query("SELECT * FROM `devices` WHERE `device_id` LIKE '%" . $argv[1] . "' AND disabled = '0' ORDER BY `device_id` DESC"); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  | while ($device = mysql_fetch_array($device_query)) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |    $id = $device['device_id']; | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |    $hostname = $device['hostname']; | 
					
						
							|  |  |  |    $old_status = $device['status']; | 
					
						
							|  |  |  |    $community = $device['community']; | 
					
						
							|  |  |  |    $snmpver = $device['snmpver']; | 
					
						
							| 
									
										
										
										
											2009-02-02 16:00:11 +00:00
										 |  |  |    $port = $device['port']; | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |    echo("$hostname\n"); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:24:06 +00:00
										 |  |  |    $status = shell_exec($config['fping'] . " $hostname | cut -d ' ' -f 3"); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |    $status = trim($status); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-09 13:14:03 +00:00
										 |  |  |    if(strstr($status, "alive")) { | 
					
						
							| 
									
										
										
										
											2009-02-02 16:00:11 +00:00
										 |  |  |      $pos = shell_exec($config['snmpget'] . " -$snmpver -c $community -t 1 $hostname:$port sysDescr.0"); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |      if($pos == '') {  | 
					
						
							|  |  |  |        $status='0'; | 
					
						
							| 
									
										
										
										
											2009-02-02 16:00:11 +00:00
										 |  |  |        $posb = shell_exec($config['snmpget'] . " -$snmpver -c $community -t 1 $hostname:$port 1.3.6.1.2.1.7526.2.4"); | 
					
						
							| 
									
										
										
										
											2007-04-09 13:14:03 +00:00
										 |  |  |        if($posb == '') { } else {  | 
					
						
							|  |  |  |          $status='1';  | 
					
						
							|  |  |  |        } | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |      } else {  | 
					
						
							|  |  |  |        $status='1';  | 
					
						
							|  |  |  |      } | 
					
						
							|  |  |  |    } else { | 
					
						
							|  |  |  |      $status='0'; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-02 16:00:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |    if($status != $device['status']) { | 
					
						
							| 
									
										
										
										
											2007-11-23 11:37:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |      if($device['sysContact']) { $email = $device['sysContact']; } else { $email = $config['email_default']; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |      mysql_query("UPDATE `devices` SET `status`= '$status' WHERE `device_id` = '" . $device['device_id'] . "'"); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |      if ($status == '1') {  | 
					
						
							|  |  |  |        $stat = "Up";  | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |        mysql_query("INSERT INTO alerts (importance, device_id, message) VALUES ('0', '" . $device['device_id'] . "', 'Device is up\n')"); | 
					
						
							| 
									
										
										
										
											2007-11-23 11:37:28 +00:00
										 |  |  |        mail($email, "DeviceUp: " . $device['hostname'], "Device Up: " . $device['hostname'] . " at " . date('l dS F Y h:i:s A'), $config['email_headers']); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |      } else { | 
					
						
							|  |  |  |        $stat = "Down"; | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |        mysql_query("INSERT INTO alerts (importance, device_id, message) VALUES ('9', '" . $device['device_id'] . "', 'Device is down\n')"); | 
					
						
							| 
									
										
										
										
											2007-11-23 11:37:28 +00:00
										 |  |  |        mail($email, "Device Down: " . $device['hostname'], "Device Down: " . $device['hostname'] . " at " . date('l dS F Y h:i:s A'), $config['email_headers']); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |      } | 
					
						
							| 
									
										
										
										
											2007-04-08 21:27:05 +00:00
										 |  |  |      mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'Device status changed to $stat')"); | 
					
						
							| 
									
										
										
										
											2007-04-03 14:10:23 +00:00
										 |  |  |      echo("Status Changed!\n"); | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ?>
 |