function get_sensor_rrd($device, $sensor) { return rrd_name($device['hostname'], get_sensor_rrd_name($device, $sensor)); }
function poll_sensor($device, $class, $unit) { global $config, $memcache, $agent_sensors; $sensors = array(); $misc_sensors = array(); $all_sensors = array(); foreach (dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ?', array($class, $device['device_id'])) as $sensor) { if ($sensor['poller_type'] == 'agent') { $misc_sensors[] = $sensor; } elseif ($sensor['poller_type'] == 'ipmi') { $misc_sensors[] = $sensor; } else { $sensors[] = $sensor; } } $snmp_data = bulk_sensor_snmpget($device, $sensors); foreach ($sensors as $sensor) { echo 'Checking (' . $sensor['poller_type'] . ") {$class} " . $sensor['sensor_descr'] . '... ' . PHP_EOL; if ($sensor['poller_type'] == 'snmp') { $mibdir = null; $sensor_value = trim(str_replace('"', '', $snmp_data[$sensor['sensor_oid']])); if (file_exists('includes/polling/sensors/' . $class . '/' . $device['os'] . '.inc.php')) { require_once 'includes/polling/sensors/' . $class . '/' . $device['os'] . '.inc.php'; } if ($class == 'temperature') { preg_match('/[\\d\\.\\-]+/', $sensor_value, $temp_response); if (!empty($temp_response[0])) { $sensor_value = $temp_response[0]; } } elseif ($class == 'state') { if (!is_numeric($sensor_value)) { $state_value = dbFetchCell('SELECT `state_value` FROM `state_translations` LEFT JOIN `sensors_to_state_indexes` ON `state_translations`.`state_index_id` = `sensors_to_state_indexes`.`state_index_id` WHERE `sensors_to_state_indexes`.`sensor_id` = ? AND `state_translations`.`state_descr` LIKE ?', array($sensor['sensor_id'], $sensor_value)); d_echo('State value of ' . $sensor_value . ' is ' . $state_value . "\n"); if (is_numeric($state_value)) { $sensor_value = $state_value; } } } //end if unset($mib); unset($mibdir); $sensor['new_value'] = $sensor_value; $all_sensors[] = $sensor; } } foreach ($misc_sensors as $sensor) { if ($sensor['poller_type'] == 'agent') { if (isset($agent_sensors)) { $sensor_value = $agent_sensors[$class][$sensor['sensor_type']][$sensor['sensor_index']]['current']; $sensor['new_value'] = $sensor_value; $all_sensors[] = $sensor; } else { echo "no agent data!\n"; continue; } } elseif ($sensor['poller_type'] == 'ipmi') { echo " already polled.\n"; // ipmi should probably move here from the ipmi poller file (FIXME) continue; } else { echo "unknown poller type!\n"; continue; } //end if } foreach ($all_sensors as $sensor) { $sensor_value = $sensor['new_value']; if ($sensor_value == -32768) { echo 'Invalid (-32768) '; $sensor_value = 0; } if ($sensor['sensor_divisor'] && $sensor_value !== 0) { $sensor_value = $sensor_value / $sensor['sensor_divisor']; } if ($sensor['sensor_multiplier']) { $sensor_value = $sensor_value * $sensor['sensor_multiplier']; } $rrd_name = get_sensor_rrd_name($device, $sensor); $rrd_def = 'DS:sensor:GAUGE:600:-20000:20000'; echo "{$sensor_value} {$unit}\n"; $fields = array('sensor' => $sensor_value); $tags = array('sensor_class' => $sensor['sensor_class'], 'sensor_type' => $sensor['sensor_type'], 'sensor_descr' => $sensor['sensor_descr'], 'sensor_index' => $sensor['sensor_index'], 'rrd_name' => $rrd_name, 'rrd_def' => $rrd_def); data_update($device, 'sensor', $tags, $fields); // FIXME also warn when crossing WARN level!! if ($sensor['sensor_limit_low'] != '' && $sensor['sensor_current'] > $sensor['sensor_limit_low'] && $sensor_value < $sensor['sensor_limit_low'] && $sensor['sensor_alert'] == 1) { echo 'Alerting for ' . $device['hostname'] . ' ' . $sensor['sensor_descr'] . "\n"; log_event(ucfirst($class) . ' ' . $sensor['sensor_descr'] . ' under threshold: ' . $sensor_value . " {$unit} (< " . $sensor['sensor_limit_low'] . " {$unit})", $device, $class, $sensor['sensor_id']); } elseif ($sensor['sensor_limit'] != '' && $sensor['sensor_current'] < $sensor['sensor_limit'] && $sensor_value > $sensor['sensor_limit'] && $sensor['sensor_alert'] == 1) { echo 'Alerting for ' . $device['hostname'] . ' ' . $sensor['sensor_descr'] . "\n"; log_event(ucfirst($class) . ' ' . $sensor['sensor_descr'] . ' above threshold: ' . $sensor_value . " {$unit} (> " . $sensor['sensor_limit'] . " {$unit})", $device, $class, $sensor['sensor_id']); } if ($sensor['sensor_class'] == 'state' && $sensor['sensor_current'] != $sensor_value) { log_event($class . ' sensor has changed from ' . $sensor['sensor_current'] . ' to ' . $sensor_value, $device, $class, $sensor['sensor_id']); } dbUpdate(array('sensor_current' => $sensor_value, 'sensor_prev' => $sensor['sensor_current'], 'lastupdate' => array('NOW()')), 'sensors', '`sensor_class` = ? AND `sensor_id` = ?', array($class, $sensor['sensor_id'])); } }
if ($ipmi['host'] = get_dev_attrib($device, 'ipmi_hostname')) { $ipmi['user'] = get_dev_attrib($device, 'ipmi_username'); $ipmi['password'] = get_dev_attrib($device, 'ipmi_password'); $ipmi['type'] = get_dev_attrib($device, 'ipmi_type'); echo 'Fetching IPMI sensor data...'; if ($config['own_hostname'] != $device['hostname'] || $ipmi['host'] != 'localhost') { $remote = ' -H ' . $ipmi['host'] . ' -U ' . $ipmi['user'] . ' -P ' . $ipmi['password']; } $results = external_exec($config['ipmitool'] . ' -I ' . $ipmi['type'] . ' -c ' . $remote . ' sdr 2>/dev/null'); d_echo($results); echo " done.\n"; foreach (explode("\n", $results) as $row) { list($desc, $value, $type, $status) = explode(',', $row); $ipmi_sensor[$desc][$config['ipmi_unit'][$type]]['value'] = $value; $ipmi_sensor[$desc][$config['ipmi_unit'][$type]]['unit'] = $type; } foreach ($ipmi_rows as $ipmisensors) { echo 'Updating IPMI sensor ' . $ipmisensors['sensor_descr'] . '... '; $sensor = $ipmi_sensor[$ipmisensors['sensor_descr']][$ipmisensors['sensor_class']]['value']; $unit = $ipmi_sensor[$ipmisensors['sensor_descr']][$ipmisensors['sensor_class']]['unit']; echo $sensor . " {$unit}\n"; $rrd_name = get_sensor_rrd_name($device, $ipmisensors); $rrd_def = 'DS:sensor:GAUGE:600:-20000:20000'; $fields = array('sensor' => $sensor); $tags = array('sensor_class' => $sensor['sensor_class'], 'sensor_type' => $sensor['sensor_type'], 'sensor_descr' => $sensor['sensor_descr'], 'sensor_index' => $sensor['sensor_index'], 'rrd_name' => $rrd_name, 'rrd_def' => $rrd_def); data_update($device, 'ipmi', $tags, $fields); // FIXME warnings in event & mail not done here yet! dbUpdate(array('sensor_current' => $sensor, 'lastupdate' => array('NOW()')), 'sensors', 'poller_type = ? AND sensor_class = ? AND sensor_id = ?', array('ipmi', $ipmisensors['sensor_class'], $ipmisensors['sensor_id'])); } unset($ipmi_sensor); }
function poll_sensor($device, $class, $unit) { global $config, $memcache, $agent_sensors; foreach (dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ?', array($class, $device['device_id'])) as $sensor) { echo 'Checking (' . $sensor['poller_type'] . ") {$class} " . $sensor['sensor_descr'] . '... '; $sensor_value = ''; if ($sensor['poller_type'] == 'snmp') { if ($device['os'] == 'pbn') { $mibdir = $config['mibdir'] . '/pbn' . ':' . $config['mibdir']; } else { $mibdir = $config['mibdir']; } if ($device['os'] == 'siklu') { $mib = ':RADIO-BRIDGE-MIB'; } else { $mib = ''; } if ($class == 'temperature') { if ($device['os'] == 'netapp') { include 'includes/polling/temperatures/netapp.inc.php'; } elseif ($device['os'] == 'canopy') { include 'includes/polling/temperatures/canopy.inc.php'; } elseif ($device['os'] == 'hytera') { require_once 'includes/polling/temperatures/hytera.inc.php'; } else { // Try 5 times to get a valid temp reading for ($i = 0; $i < 5; $i++) { d_echo("Attempt {$i} "); $sensor_value = trim(str_replace('"', '', snmp_get($device, $sensor['sensor_oid'], '-OUqnv', "SNMPv2-MIB{$mib}", $mibdir))); preg_match('/[\\d\\.]+/', $sensor_value, $temp_response); if (!empty($temp_response[0])) { $sensor_value = $temp_response[0]; } if (is_numeric($sensor_value) && $sensor_value != 9999) { break; // TME sometimes sends 999.9 when it is right in the middle of an update; } sleep(1); // end if } } //end if } elseif ($class == "voltage" && $device['os'] == 'hytera') { require_once "includes/polling/voltages/hytera.inc.php"; } elseif ($class == 'state') { $sensor_value = trim(str_replace('"', '', snmp_walk($device, $sensor['sensor_oid'], '-Oevq', 'SNMPv2-MIB', $mibdir))); if (!is_numeric($sensor_value)) { $state_value = dbFetchCell('SELECT `state_value` FROM `state_translations` LEFT JOIN `sensors_to_state_indexes` ON `state_translations`.`state_index_id` = `sensors_to_state_indexes`.`state_index_id` WHERE `sensors_to_state_indexes`.`sensor_id` = ? AND `state_translations`.`state_descr` LIKE ?', array($sensor['sensor_id'], $sensor_value)); d_echo('State value of ' . $sensor_value . ' is ' . $state_value . "\n"); if (is_numeric($state_value)) { $sensor_value = $state_value; } } } else { if ($class == 'signal') { $currentOS = $device['os']; include "includes/polling/signal/{$currentOS}.inc.php"; $sensor_value = trim(str_replace('"', '', snmp_get($device, $sensor['sensor_oid'], '-OUqnv', "SNMPv2-MIB{$mib}", $mibdir))); } else { if ($class == 'dbm') { $sensor_value = trim(str_replace('"', '', snmp_get($device, $sensor['sensor_oid'], '-OUqnv', "SNMPv2-MIB{$mib}", $mibdir))); //iosxr does not expose dbm values through SNMP so we convert Watts to dbm to have a nice graph to show if ($device['os'] == "iosxr") { $sensor_value = round(10 * log10($sensor_value / 1000), 3); } } else { if ($sensor['sensor_type'] == 'apc') { $sensor_value = trim(str_replace('"', '', snmp_walk($device, $sensor['sensor_oid'], '-OUqnv', "SNMPv2-MIB:PowerNet-MIB{$mib}", $mibdir))); } else { $sensor_value = trim(str_replace('"', '', snmp_get($device, $sensor['sensor_oid'], '-OUqnv', "SNMPv2-MIB{$mib}", $mibdir))); } } } } //end if unset($mib); unset($mibdir); } else { if ($sensor['poller_type'] == 'agent') { if (isset($agent_sensors)) { $sensor_value = $agent_sensors[$class][$sensor['sensor_type']][$sensor['sensor_index']]['current']; } else { echo "no agent data!\n"; continue; } } else { if ($sensor['poller_type'] == 'ipmi') { echo " already polled.\n"; // ipmi should probably move here from the ipmi poller file (FIXME) continue; } else { echo "unknown poller type!\n"; continue; } } } //end if if ($sensor_value == -32768) { echo 'Invalid (-32768) '; $sensor_value = 0; } if ($sensor['sensor_divisor'] && $sensor_value !== 0) { $sensor_value = $sensor_value / $sensor['sensor_divisor']; } if ($sensor['sensor_multiplier']) { $sensor_value = $sensor_value * $sensor['sensor_multiplier']; } $rrd_name = get_sensor_rrd_name($device, $sensor); $rrd_def = 'DS:sensor:GAUGE:600:-20000:20000'; echo "{$sensor_value} {$unit}\n"; $fields = array('sensor' => $sensor_value); $tags = array('sensor_class' => $sensor['sensor_class'], 'sensor_type' => $sensor['sensor_type'], 'sensor_descr' => $sensor['sensor_descr'], 'sensor_index' => $sensor['sensor_index'], 'rrd_name' => $rrd_name, 'rrd_def' => $rrd_def); data_update($device, 'sensor', $tags, $fields); // FIXME also warn when crossing WARN level!! if ($sensor['sensor_limit_low'] != '' && $sensor['sensor_current'] > $sensor['sensor_limit_low'] && $sensor_value < $sensor['sensor_limit_low'] && $sensor['sensor_alert'] == 1) { echo 'Alerting for ' . $device['hostname'] . ' ' . $sensor['sensor_descr'] . "\n"; log_event(ucfirst($class) . ' ' . $sensor['sensor_descr'] . ' under threshold: ' . $sensor_value . " {$unit} (< " . $sensor['sensor_limit_low'] . " {$unit})", $device, $class, $sensor['sensor_id']); } else { if ($sensor['sensor_limit'] != '' && $sensor['sensor_current'] < $sensor['sensor_limit'] && $sensor_value > $sensor['sensor_limit'] && $sensor['sensor_alert'] == 1) { echo 'Alerting for ' . $device['hostname'] . ' ' . $sensor['sensor_descr'] . "\n"; log_event(ucfirst($class) . ' ' . $sensor['sensor_descr'] . ' above threshold: ' . $sensor_value . " {$unit} (> " . $sensor['sensor_limit'] . " {$unit})", $device, $class, $sensor['sensor_id']); } } if ($sensor['sensor_class'] == 'state' && $sensor['sensor_current'] != $sensor_value) { log_event($class . ' sensor has changed from ' . $sensor['sensor_current'] . ' to ' . $sensor_value, $device, $class, $sensor['sensor_id']); } dbUpdate(array('sensor_current' => $sensor_value, 'sensor_prev' => $sensor['sensor_current'], 'lastupdate' => array('NOW()')), 'sensors', '`sensor_class` = ? AND `sensor_id` = ?', array($class, $sensor['sensor_id'])); } //end foreach }