Files
librenms-librenms/poll-device.php
T

277 lines
11 KiB
PHP
Raw Normal View History

2007-04-03 14:10:23 +00:00
#!/usr/bin/php
2009-09-07 11:07:59 +00:00
<?php
2007-04-03 14:10:23 +00:00
include("config.php");
include("includes/functions.php");
2008-03-22 13:30:33 +00:00
echo("Observer Poller v".$config['version']."\n\n");
if($argv[1] == "--device" && $argv[2]) {
$where = "AND `device_id` = '".$argv[2]."'";
} elseif ($argv[1] == "--odd") {
$where = "AND MOD(device_id,2) = 1";
} elseif ($argv[1] == "--even") {
$where = "AND MOD(device_id,2) = 0";
2009-04-23 21:13:56 +00:00
} elseif ($argv[1] == "--odd3") {
$where = "AND MOD(device_id,2) = 1 AND device_id NOT LIKE '%1'";
} elseif ($argv[1] == "--even3") {
$where = "AND MOD(device_id,2) = 0 AND device_id NOT LIKE '%2'";
} elseif ($argv[1] == "--other3") {
$where = "AND (device_id LIKE '%1' OR device_id LIKE '%2')";
} elseif ($argv[1] == "--all") {
2009-04-23 21:13:56 +00:00
} else {
echo("--device <device id> Poll single device\n");
echo("--all Poll all devices\n\n");
echo("No polling type specified!\n");
exit;
}
echo("Starting polling run:\n\n");
$device_query = mysql_query("SELECT * FROM `devices` WHERE `ignore` = '0' $where ORDER BY `device_id` ASC");
2007-04-03 14:10:23 +00:00
while ($device = mysql_fetch_array($device_query)) {
2008-03-19 18:55:31 +00:00
echo("Polling " . $device['hostname'] . " ( device_id ".$device['device_id']." )\n\n");
unset($update); unset($update_query); unset($seperator); unset($version); unset($uptime); unset($features);
2009-03-30 11:20:18 +00:00
unset($sysLocation); unset($hardware); unset($sysDescr); unset($sysContact); unset($sysName);
2007-04-09 13:14:03 +00:00
$pingable = isPingable($device['hostname']);
$host_rrd = $config['rrd_dir'] . "/" . $device['hostname'];
if(!is_dir($host_rrd)) { mkdir($host_rrd); echo("Created directory : $host_rrd\n"); }
if($pingable) { echo("Pings : yes :)\n"); } else { echo("Pings : no :(\n"); }
2007-04-03 14:10:23 +00:00
$snmpable = FALSE;
2007-04-03 14:10:23 +00:00
if($pingable) {
$snmpable = isSNMPable($device['hostname'], $device['community'], $device['snmpver'], $device['port']);
2008-03-19 18:55:31 +00:00
if($snmpable) { echo("SNMP : yes :)\n"); } else { echo("SNMP : no :(\n"); }
2007-04-03 14:10:23 +00:00
}
2007-04-08 12:23:45 +00:00
2008-03-31 13:08:17 +00:00
unset($snmpdata);
2008-03-19 18:55:31 +00:00
if ($snmpable) {
$status = '1';
2008-03-19 18:55:31 +00:00
if($device['os'] == "FreeBSD" || $device['os'] == "OpenBSD" || $device['os'] == "Linux" || $device['os'] == "Windows" || $device['os'] == "Voswall") {
$uptimeoid = ".1.3.6.1.2.1.25.1.1.0";
} else {
$uptimeoid = "1.3.6.1.2.1.1.3.0";
}
2009-04-23 21:13:56 +00:00
$snmp_cmd = $config['snmpget'] . " -m SNMPv2-MIB:HOST-RESOURCES-MIB -O qv -" . $device['snmpver'] . " -c " . $device['community'] . " " . $device['hostname'].":".$device['port'];
2009-03-02 18:12:09 +00:00
$snmp_cmd .= " $uptimeoid sysLocation.0 sysContact.0 sysName.0";
2008-11-04 15:00:51 +00:00
#$snmp_cmd .= " | grep -v 'Cisco Internetwork Operating System Software'";
2008-12-09 13:55:41 +00:00
if($device['os'] == "IOS" || $device['os'] == "IOS XE") {
2009-04-23 21:13:56 +00:00
$snmp_cmdb = $config['snmpget'] . " -m ENTITY-MIB -O qv -" . $device['snmpver'] . " -c " . $device['community'] . " " . $device['hostname'].":".$device['port'];
$snmp_cmdb .= " .1.3.6.1.2.1.47.1.1.1.1.13.1 .1.3.6.1.2.1.47.1.1.1.1.4.1 .1.3.6.1.2.1.47.1.1.1.1.13.1001 .1.3.6.1.2.1.47.1.1.1.1.4.1001";
list($a,$b,$c,$d) = explode("\n", shell_exec($snmp_cmdb));
if($b == "0") { $ciscomodel = $a; }
if($d == "0") { $ciscomodel = $c; }
} else { unset($ciscomodel); }
2008-03-19 18:55:31 +00:00
$snmpdata = shell_exec($snmp_cmd);
2008-11-04 15:00:51 +00:00
# $snmpdata = preg_replace("/^.*IOS/","", $snmpdata);
2007-04-03 14:10:23 +00:00
$snmpdata = trim($snmpdata);
2007-04-11 20:57:56 +00:00
$snmpdata = str_replace("\"", "", $snmpdata);
2009-03-02 18:12:09 +00:00
list($sysUptime, $sysLocation, $sysContact, $sysName) = explode("\n", $snmpdata);
2009-04-23 21:13:56 +00:00
$sysDescr = trim(shell_exec($config['snmpget'] . " -m SNMPv2-MIB -O qv -" . $device['snmpver'] . " -c " . $device['community'] . " " . $device['hostname'].":".$device['port'] . " sysDescr.0"));
2007-04-03 14:10:23 +00:00
$sysUptime = str_replace("(", "", $sysUptime);
$sysUptime = str_replace(")", "", $sysUptime);
list($days, $hours, $mins, $secs) = explode(":", $sysUptime);
list($secs, $microsecs) = explode(".", $secs);
$timeticks = mktime(0, $secs, $mins, $hours, $days, 0);
$hours = $hours + ($days * 24);
$mins = $mins + ($hours * 60);
$secs = $secs + ($mins * 60);
2007-04-08 21:27:05 +00:00
$uptime = $secs;
2007-04-03 14:10:23 +00:00
switch ($device['os']) {
2007-04-03 14:10:23 +00:00
case "FreeBSD":
case "DragonFly":
case "OpenBSD":
case "Linux":
case "m0n0wall":
case "Voswall":
case "NetBSD":
case "pfSense":
include("includes/polling/device-unix.inc.php");
2007-04-03 14:10:23 +00:00
break;
2008-11-26 17:58:47 +00:00
2007-04-03 14:10:23 +00:00
case "Windows":
2008-11-26 17:58:47 +00:00
include("includes/polling/device-windows.inc.php");
2007-04-03 14:10:23 +00:00
break;
2007-05-20 19:21:35 +00:00
2008-03-19 19:16:16 +00:00
case "ScreenOS":
include("includes/polling/device-screenos.inc.php");
2008-03-19 18:55:31 +00:00
break;
2008-04-03 21:52:59 +00:00
case "Fortigate":
include("includes/polling/device-fortigate.inc.php");
break;
case "JunOS":
include("includes/polling/device-junos.inc.php");
break;
2007-04-03 14:10:23 +00:00
case "IOS":
2008-12-09 13:55:41 +00:00
case "IOS XE":
2007-04-08 21:27:05 +00:00
include("includes/polling/device-ios.inc.php");
2007-04-03 14:10:23 +00:00
break;
2007-05-20 19:21:35 +00:00
2007-04-03 14:10:23 +00:00
case "ProCurve":
$sysDescr = str_replace(", ", ",", $sysDescr);
2007-04-08 21:27:05 +00:00
list($hardware, $features, $version) = explode(",", $sysDescr);
list($version) = explode("(", $version);
2007-04-03 14:10:23 +00:00
if(!strstr($ciscomodel, " ")) {
2007-04-08 21:27:05 +00:00
$hardware = str_replace("\"", "", $ciscomodel);
2007-04-03 14:10:23 +00:00
}
2007-04-08 18:09:21 +00:00
include("includes/polling/device-procurve.inc.php");
2007-04-03 14:10:23 +00:00
break;
2008-09-03 20:04:34 +00:00
case "BCM96348":
include("includes/polling/adslline.inc.php");
break;
2007-04-03 14:10:23 +00:00
case "Snom":
2007-04-08 21:27:05 +00:00
include("includes/polling/device-snom.inc.php");
2007-04-03 14:10:23 +00:00
break;
default:
pollDevice();
}
2008-03-31 13:08:17 +00:00
$sysLocation = str_replace("\"","", $sysLocation);
2007-04-08 12:23:45 +00:00
echo("Polling temperatures\n");
2007-04-08 12:23:45 +00:00
include("includes/polling/temperatures.inc.php");
include("includes/polling/device-netstats.inc.php");
echo("Polling interfaces\n");
2008-03-20 11:53:19 +00:00
2008-11-19 12:12:54 +00:00
$where = "WHERE device_id = '" . $device['device_id'] . "' AND deleted = '0'";
include("includes/polling/interfaces.inc.php");
2007-04-08 12:23:45 +00:00
2008-11-26 17:58:47 +00:00
$update_uptime_attrib = mysql_query("UPDATE devices_attribs SET attrib_value = NOW() WHERE `device_id` = '" . $device['device_id'] . "' AND `attrib_type` = 'polled'");
if(mysql_affected_rows() == '0') {
$insert_uptime_attrib = mysql_query("INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES ('" . $device['device_id'] . "', 'polled', NOW())");
}
2007-04-03 14:10:23 +00:00
} else {
2007-04-08 21:27:05 +00:00
$status = '0';
2007-04-03 14:10:23 +00:00
}
2007-04-11 20:57:56 +00:00
unset( $update ) ;
unset( $seperator) ;
if ( $sysContact && $sysContact != $device['sysContact'] ) {
$update .= $seperator . "`sysContact` = '$sysContact'";
$seperator = ", ";
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'Contact -> $sysContact')");
}
echo("$update\n");
2007-04-11 20:57:56 +00:00
2009-03-02 18:12:09 +00:00
$sysName = strtolower($sysName);
if ( $sysName && $sysName != $device['sysName'] ) {
$update .= $seperator . "`sysName` = '$sysName'";
$seperator = ", ";
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'sysName -> $sysName')");
}
if ( $sysDescr && $sysDescr != $device['sysDescr'] ) {
2007-05-20 19:21:35 +00:00
$update .= $seperator . "`sysDescr` = '$sysDescr'";
2007-04-03 14:10:23 +00:00
$seperator = ", ";
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'sysDescr -> $sysDescr')");
2007-04-03 14:10:23 +00:00
}
2007-04-11 20:57:56 +00:00
2008-03-31 13:08:17 +00:00
if ( $sysLocation && $device['location'] != $sysLocation ) {
$update .= $seperator . "`location` = '$sysLocation'";
2007-04-03 14:10:23 +00:00
$seperator = ", ";
2008-03-31 13:08:17 +00:00
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'Location -> $sysLocation')");
2007-04-03 14:10:23 +00:00
}
2007-04-11 20:57:56 +00:00
2007-04-08 21:27:05 +00:00
if ( $version && $device['version'] != $version ) {
$update .= $seperator . "`version` = '$version'";
2007-04-03 14:10:23 +00:00
$seperator = ", ";
2007-04-08 21:27:05 +00:00
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'OS Version -> $version')");
2007-04-03 14:10:23 +00:00
}
2007-04-11 20:57:56 +00:00
2007-04-08 21:27:05 +00:00
if ( $features && $features != $device['features'] ) {
$update .= $seperator . "`features` = '$features'";
2007-04-03 14:10:23 +00:00
$seperator = ", ";
2007-04-08 21:27:05 +00:00
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'OS Features -> $features')");
2007-04-03 14:10:23 +00:00
}
2007-04-11 20:57:56 +00:00
2007-04-08 21:27:05 +00:00
if ( $hardware && $hardware != $device['hardware'] ) {
$update .= $seperator . "`hardware` = '$hardware'";
2007-04-03 14:10:23 +00:00
$seperator = ", ";
2007-04-08 21:27:05 +00:00
mysql_query("INSERT INTO eventlog (host, interface, datetime, message) VALUES ('" . $device['device_id'] . "', NULL, NOW(), 'Hardware -> $hardware')");
2007-04-03 14:10:23 +00:00
}
2007-04-08 21:27:05 +00:00
if( $device['status'] != $status ) {
$update .= $seperator . "`status` = '$status'";
2007-04-03 14:10:23 +00:00
$seperator = ", ";
2007-04-08 21:27:05 +00:00
if ($status == '1') { $stat = "Up";
mysql_query("INSERT INTO alerts (importance, device_id, message) VALUES ('0', '" . $device['device_id'] . "', 'Device is up\n')");
2007-04-03 14:10:23 +00:00
} else {
$stat = "Down";
mysql_query("INSERT INTO alerts (importance, device_id, message) VALUES ('9', '" . $device['device_id'] . "', 'Device is down\n')");
2007-04-03 14:10:23 +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
}
2007-04-08 21:27:05 +00:00
if ($uptime) {
2007-06-24 14:56:47 +00:00
$old_uptime = @mysql_result(mysql_query("SELECT `attrib_value` FROM `devices_attribs` WHERE `device_id` = '" . $device['device_id'] . "' AND `attrib_type` = 'uptime'"), 0);
2007-06-24 14:56:47 +00:00
if( $uptime < $old_uptime ) {
if($device['sysContact']) { $email = $device['sysContact']; } else { $email = $config['email_default']; }
mail($notify_email, "Device Rebooted: " . $device['hostname'], "Device Rebooted :" . $device['hostname'] . " at " . date('l dS F Y h:i:s A'), $config['email_headers']);
2007-06-24 14:56:47 +00:00
}
$uptimerrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/uptime.rrd";
2008-03-12 16:35:00 +00:00
$old_uptimerrd = "rrd/" . $device['hostname'] . "-uptime.rrd";
if(is_file($old_uptimerrd) && !is_file($uptimerrd)) { rename($old_uptimerrd, $uptimerrd); echo("Moving $old_uptimerrd to $uptimerrd"); }
if(!is_file($uptimerrd)) {
2008-04-14 09:32:54 +00:00
$woo = shell_exec($config['rrdtool'] . " create $uptimerrd \
DS:uptime:GAUGE:600:0:U \
RRA:AVERAGE:0.5:1:600 \
RRA:AVERAGE:0.5:6:700 \
RRA:AVERAGE:0.5:24:775 \
2008-04-14 09:32:54 +00:00
RRA:AVERAGE:0.5:288:797");
2007-04-07 19:25:55 +00:00
}
2008-03-09 21:13:27 +00:00
rrdtool_update($uptimerrd, "N:$uptime");
2007-04-07 19:25:55 +00:00
2007-04-08 21:27:05 +00:00
$update_uptime_attrib = mysql_query("UPDATE devices_attribs SET attrib_value = '$uptime' WHERE `device_id` = '" . $device['device_id'] . "' AND `attrib_type` = 'uptime'");
if(mysql_affected_rows() == '0') {
2007-04-08 21:27:05 +00:00
$insert_uptime_attrib = mysql_query("INSERT INTO devices_attribs (`device_id`, `attrib_type`, `attrib_value`) VALUES ('" . $device['device_id'] . "', 'uptime', '$uptime')");
}
2007-06-24 14:56:47 +00:00
mysql_query("UPDATE `devices` SET `last_polled` = NOW() WHERE `device_id` = '". $device['device_id'] ."'");
2008-03-19 18:55:31 +00:00
} ## End if snmpable
2007-04-03 14:10:23 +00:00
if ($update) {
$update_query = "UPDATE `devices` SET ";
$update_query .= $update;
2007-04-08 21:27:05 +00:00
$update_query .= " WHERE `device_id` = '" . $device['device_id'] . "'";
echo("Updating " . $device['hostname'] . "\n");
2007-04-03 14:10:23 +00:00
$update_result = mysql_query($update_query);
} else {
echo("No Changes to " . $device['hostname'] . "\n");
2007-04-03 14:10:23 +00:00
}
echo("\n");
2007-04-03 14:10:23 +00:00
}
?>