function float_sort($a, $b) { if (float_gtr($a[0], $b[0])) { return 1; } if (float_cmp($a[0], $b[0])) { return 0; } return -1; }
switch ($entry['alarmCfgTripSelect']) { case 'tripBelow': if (isset($geist_alarms[$entry['alarmCfgReadingID']]['limit_low'])) { if (float_cmp($entry['alarmCfgThreshold'] * $scale, $geist_alarms[$entry['alarmCfgReadingID']]['limit_low']) === 1) { $geist_alarms[$entry['alarmCfgReadingID']]['limit_low_warn'] = $geist_alarms[$entry['alarmCfgReadingID']]['low_limit']; $limit_type = 'limit_low'; } else { $limit_type = 'limit_low_warn'; } } else { $limit_type = 'limit_low'; } break; case 'tripAbove': if (isset($geist_alarms[$entry['alarmCfgReadingID']]['limit_high'])) { if (float_cmp($entry['alarmCfgThreshold'] * $scale, $geist_alarms[$entry['alarmCfgReadingID']]['limit_high']) === 1) { $geist_alarms[$entry['alarmCfgReadingID']]['limit_high_warn'] = $geist_alarms[$entry['alarmCfgReadingID']]['limit_high']; $limit_type = 'limit_high'; } else { $limit_type = 'limit_high_warn'; } } else { $limit_type = 'limit_high'; } break; } // Loads up array like $geist_alarms['digitalSensorDigital.1']['tripBelow'] = -999 $geist_alarms[$entry['alarmCfgReadingID']][$limit_type] = $entry['alarmCfgThreshold'] * $scale; } // GEIST-MIB-V3::ctrl3ChIECSerial.1 = STRING: 0000777654567777 // GEIST-MIB-V3::ctrl3ChIECName.1 = STRING: my-geist-pdu01
/** * Discover a new sensor on a device * * This function adds a status sensor to a device, if it does not already exist. * Data on the sensor is updated if it has changed, and an event is logged with regards to the changes. * * Status sensors are handed off to discover_status(). * Current sensor values are rectified in case they are broken (added spaces, etc). * * @param array &$valid Array of currently valid sensors for poller type (used to delete later) * @param string $class Class of sensor (voltage, temperature, etc.) * @param array $device Device array sensor is being discovered on * @param string $oid SNMP OID of sensor * @param string $index SNMP index of sensor * @param string $type Type of sensor * @param string $sensor_descr Description of sensor * @param int $scale Scale of sensor (0.1 for 1:10 scale, 10 for 10:1 scale, etc) * @param string $value Current sensor value * @param array $options Options (sensor_unit, limit_auto, limit*) * @param string $poller_type Type of poller being used (SNMP, Agent, etc) - Used to check valid sensors per poller type * @return bool */ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $sensor_descr, $scale = 1, $value = NULL, $options = array(), $poller_type = 'snmp') { global $config; // If this is actually a status indicator, pass it off to discover_status() then return. if ($class == 'state' || $class == 'status') { print_debug("Redirecting call to discover_status()."); return discover_status($device, $oid, $index, $type, $sensor_descr, $value, $options, $poller_type); } // Init main $param_main = array('oid' => 'sensor_oid', 'sensor_descr' => 'sensor_descr', 'scale' => 'sensor_multiplier'); // Init numeric values if (!is_numeric($scale) || $scale == 0) { $scale = 1; } // Skip discovery sensor if value not numeric or null (default) if (strlen($value)) { // Some retarded devices report data with spaces and commas // STRING: " 20,4" $value = snmp_fix_numeric($value); } if (is_numeric($value)) { $value *= $scale; // Scale before unit conversion $value = value_to_si($value, $options['sensor_unit'], $class); // Convert if not SI unit } else { print_debug("Sensor skipped by not numeric value: '{$value}', '{$sensor_descr}'"); if (strlen($value)) { print_debug("Perhaps this is named sensor, use discover_status() instead."); } return FALSE; } $param_limits = array('limit_high' => 'sensor_limit', 'limit_high_warn' => 'sensor_limit_warn', 'limit_low' => 'sensor_limit_low', 'limit_low_warn' => 'sensor_limit_low_warn'); foreach ($param_limits as $key => $column) { // Set limits vars and unit convert if required ${$key} = is_numeric($options[$key]) ? value_to_si($options[$key], $options['sensor_unit'], $class) : NULL; } // Auto calculate high/low limits if not passed $limit_auto = !isset($options['limit_auto']) || (bool) $options['limit_auto']; // Init optional $param_opt = array('entPhysicalIndex', 'entPhysicalClass', 'entPhysicalIndex_measured', 'measured_class', 'measured_entity', 'sensor_unit'); foreach ($param_opt as $key) { ${$key} = $options[$key] ? $options[$key] : NULL; } print_debug("Discover sensor: {$class}, " . $device['hostname'] . ", {$oid}, {$index}, {$type}, {$sensor_descr}, SCALE: {$scale}, LIMITS: ({$limit_low}, {$limit_low_warn}, {$limit_high_warn}, {$limit_high}), CURRENT: {$value}, {$poller_type}, {$entPhysicalIndex}, {$entPhysicalClass}"); // Check sensor ignore filters foreach ($config['ignore_sensor'] as $bi) { if (strcasecmp($bi, $sensor_descr) == 0) { print_debug("Skipped by equals: {$bi}, {$sensor_descr} "); return FALSE; } } foreach ($config['ignore_sensor_string'] as $bi) { if (stripos($sensor_descr, $bi) !== FALSE) { print_debug("Skipped by strpos: {$bi}, {$sensor_descr} "); return FALSE; } } foreach ($config['ignore_sensor_regexp'] as $bi) { if (preg_match($bi, $sensor_descr) > 0) { print_debug("Skipped by regexp: {$bi}, {$sensor_descr} "); return FALSE; } } if (!is_null($limit_low_warn) && !is_null($limit_high_warn) && $limit_low_warn > $limit_high_warn) { // Fix high/low thresholds (i.e. on negative numbers) list($limit_high_warn, $limit_low_warn) = array($limit_low_warn, $limit_high_warn); } if (dbFetchCell('SELECT COUNT(`sensor_id`) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', array($poller_type, $class, $device['device_id'], $type, $index)) == '0') { if (!$limit_high) { $limit_high = sensor_limit_high($class, $value, $limit_auto); } if (!$limit_low) { $limit_low = sensor_limit_low($class, $value, $limit_auto); } if (!is_null($limit_low) && !is_null($limit_high) && $limit_low > $limit_high) { // Fix high/low thresholds (i.e. on negative numbers) list($limit_high, $limit_low) = array($limit_low, $limit_high); print_debug("High/low limits swapped."); } $sensor_insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_index' => $index, 'sensor_type' => $type); foreach ($param_main as $key => $column) { $sensor_insert[$column] = ${$key}; } foreach ($param_limits as $key => $column) { // Convert strings/numbers to (float) or to array('NULL') ${$key} = ${$key} === NULL ? array('NULL') : (double) ${$key}; $sensor_insert[$column] = ${$key}; } foreach ($param_opt as $key) { if (is_null(${$key})) { ${$key} = array('NULL'); } $sensor_insert[$key] = ${$key}; } $sensor_id = dbInsert($sensor_insert, 'sensors'); $state_insert = array('sensor_id' => $sensor_id, 'sensor_value' => $value, 'sensor_polled' => 'NOW()'); dbInsert($state_insert, 'sensors-state'); print_debug("( {$sensor_id} inserted )"); echo '+'; log_event("Sensor added: {$class} {$type} {$index} {$sensor_descr}", $device, 'sensor', $sensor_id); } else { $sensor_entry = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?", array($class, $device['device_id'], $type, $index)); // Limits if (!$sensor_entry['sensor_custom_limit']) { if (!is_numeric($limit_high)) { if ($sensor_entry['sensor_limit'] !== '') { // Calculate a reasonable limit $limit_high = sensor_limit_high($class, $value, $limit_auto); } else { // Use existing limit. (this is wrong! --mike) $limit_high = $sensor_entry['sensor_limit']; } } if (!is_numeric($limit_low)) { if ($sensor_entry['sensor_limit_low'] !== '') { // Calculate a reasonable limit $limit_low = sensor_limit_low($class, $value, $limit_auto); } else { // Use existing limit. (this is wrong! --mike) $limit_low = $sensor_entry['sensor_limit_low']; } } // Fix high/low thresholds (i.e. on negative numbers) if (!is_null($limit_low) && !is_null($limit_high) && $limit_low > $limit_high) { list($limit_high, $limit_low) = array($limit_low, $limit_high); print_debug("High/low limits swapped."); } // Update limits $update = array(); $update_msg = array(); $debug_msg = 'Current sensor value: "' . $value . '", scale: "' . $scale . '"' . PHP_EOL; foreach ($param_limits as $key => $column) { // $key - param name, $$key - param value, $column - column name in DB for $key $debug_msg .= ' ' . $key . ': "' . $sensor_entry[$column] . '" -> "' . ${$key} . '"' . PHP_EOL; //convert strings/numbers to identical type (float) or to array('NULL') for correct comparison ${$key} = ${$key} === NULL ? array('NULL') : (double) ${$key}; $sensor_entry[$column] = $sensor_entry[$column] === NULL ? array('NULL') : (double) $sensor_entry[$column]; if (float_cmp(${$key}, $sensor_entry[$column], 0.1) !== 0) { $update[$column] = ${$key}; $update_msg[] = $key . ' -> "' . (is_array(${$key}) ? 'NULL' : ${$key}) . '"'; } } if (count($update)) { echo "L"; print_debug($debug_msg); log_event('Sensor updated (limits): ' . implode(', ', $update_msg), $device, 'sensor', $sensor_entry['sensor_id']); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); } } $update = array(); foreach ($param_main as $key => $column) { if (float_cmp(${$key}, $sensor_entry[$column]) !== 0) { $update[$column] = ${$key}; } } foreach ($param_opt as $key) { if (${$key} != $sensor_entry[$key]) { $update[$key] = ${$key}; } } if (count($update)) { $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); echo 'U'; log_event("Sensor updated: {$class} {$type} {$index} {$sensor_descr}", $device, 'sensor', $sensor_entry['sensor_id']); } else { echo '.'; } } $valid[$class][$type][$index] = 1; }
function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $sensor_descr, $scale = 1, $current = NULL, $options = array(), $poller_type = 'snmp') { global $config; // Init main $param_main = array('oid' => 'sensor_oid', 'sensor_descr' => 'sensor_descr', 'scale' => 'sensor_multiplier'); // Init numeric values if (!is_numeric($scale) || $scale == 0) { $scale = 1; } $param_limits = array('limit_high' => 'sensor_limit', 'limit_high_warn' => 'sensor_limit_warn', 'limit_low_warn' => 'sensor_limit_low_warn', 'limit_low' => 'sensor_limit_low'); foreach ($param_limits as $key => $column) { ${$key} = is_numeric($options[$key]) ? $options[$key] : NULL; } // Init optional $param_opt = array('entPhysicalIndex', 'entPhysicalClass', 'entPhysicalIndex_measured', 'measured_class', 'measured_entity'); foreach ($param_opt as $key) { ${$key} = $options[$key] ? $options[$key] : NULL; } print_debug("Discover sensor: {$class}, " . $device['hostname'] . ", {$oid}, {$index}, {$type}, {$sensor_descr}, {$scale}, {$limit_low}, {$limit_low_warn}, {$limit_high_warn}, {$limit_high}, {$current}, {$poller_type}, {$entPhysicalIndex}, {$entPhysicalClass}"); // Check sensor ignore filters foreach ($config['ignore_sensor'] as $bi) { if (strcasecmp($bi, $sensor_descr) == 0) { print_debug("Skipped by equals: {$bi}, {$sensor_descr} "); return FALSE; } } foreach ($config['ignore_sensor_string'] as $bi) { if (stripos($sensor_descr, $bi) !== FALSE) { print_debug("Skipped by strpos: {$bi}, {$sensor_descr} "); return FALSE; } } foreach ($config['ignore_sensor_regexp'] as $bi) { if (preg_match($bi, $sensor_descr) > 0) { print_debug("Skipped by regexp: {$bi}, {$sensor_descr} "); return FALSE; } } if (!is_null($limit_low_warn) && !is_null($limit_high_warn) && $limit_low_warn > $limit_high_warn) { // Fix high/low thresholds (i.e. on negative numbers) list($limit_high_warn, $limit_low_warn) = array($limit_low_warn, $limit_high_warn); } if (dbFetchCell('SELECT COUNT(`sensor_id`) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', array($poller_type, $class, $device['device_id'], $type, $index)) == '0') { if (!isset($config['sensor_states'][$type])) { if (!$limit_high) { $limit_high = sensor_limit_high($class, $current); } if (!$limit_low) { $limit_low = sensor_limit_low($class, $current); } if (!is_null($limit_low) && !is_null($limit_high) && $limit_low > $limit_high) { // Fix high/low thresholds (i.e. on negative numbers) list($limit_high, $limit_low) = array($limit_low, $limit_high); } } else { // For state sensors limits always is NULL $limit_high = NULL; $limit_low = NULL; $limit_high_warn = NULL; $limit_low_warn = NULL; } $sensor_insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_index' => $index, 'sensor_type' => $type); foreach ($param_main as $key => $column) { $sensor_insert[$column] = ${$key}; } foreach ($param_limits as $key => $column) { // Convert strings/numbers to (float) or to array('NULL') ${$key} = ${$key} === NULL ? array('NULL') : (double) ${$key}; $sensor_insert[$column] = ${$key}; } foreach ($param_opt as $key) { if (is_null(${$key})) { ${$key} = array('NULL'); } $sensor_insert[$key] = ${$key}; } $sensor_id = dbInsert($sensor_insert, 'sensors'); $state_insert = array('sensor_id' => $sensor_id, 'sensor_value' => $current, 'sensor_polled' => 'NOW()'); dbInsert($state_insert, 'sensors-state'); print_debug("( {$sensor_id} inserted )"); echo "+"; log_event("Sensor added: {$class} {$type} {$index} {$sensor_descr}", $device, 'sensor', $sensor_id); } else { $sensor_entry = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?", array($class, $device['device_id'], $type, $index)); // Limits if (!$sensor_entry['sensor_custom_limit']) { if (!isset($config['sensor_states'][$type])) { if (!is_numeric($limit_high) && !is_numeric($limit_low)) { if (!$sensor_entry['sensor_limit']) { // Calculate a reasonable limit $limit_high = sensor_limit_high($class, $current); } else { // Use existing limit. (this is wrong! --mike) $limit_high = $sensor_entry['sensor_limit']; } if (!$sensor_entry['sensor_limit_low']) { // Calculate a reasonable limit $limit_low = sensor_limit_low($class, $current); } else { // Use existing limit. (this is wrong! --mike) $limit_low = $sensor_entry['sensor_limit_low']; } } // Fix high/low thresholds (i.e. on negative numbers) if (!is_null($limit_low) && !is_null($limit_high) && $limit_low > $limit_high) { list($limit_high, $limit_low) = array($limit_low, $limit_high); } } else { // For state sensors limits always is NULL $limit_high = NULL; $limit_low = NULL; $limit_high_warn = NULL; $limit_low_warn = NULL; } // Update limits $update = array(); $msg_debug = 'Current sensor value: "' . $current . '", scale: "' . $scale . '"' . PHP_EOL; foreach ($param_limits as $v => $k) { $msg_debug .= ' ' . $v . ': "' . $sensor_entry[$k] . '" -> "' . ${$v} . '"' . PHP_EOL; //convert strings/numbers to identical type (float) or to array('NULL') for correct comparison ${$v} = ${$v} === NULL ? array('NULL') : (double) ${$v}; $sensor_entry[$k] = $sensor_entry[$k] === NULL ? array('NULL') : (double) $sensor_entry[$k]; if (float_cmp(${$v}, $sensor_entry[$k]) !== 0) { $update[$k] = ${$v}; } } if (count($update)) { echo "L"; print_debug($msg_debug); $msg = 'Sensor updated (limits): ' . $class . ' ' . $type . ' ' . $index . ' ' . $sensor_descr . ' '; foreach (array('L' => 'sensor_limit_low', 'Lw' => 'sensor_limit_low_warn', 'Hw' => 'sensor_limit_warn', 'H' => 'sensor_limit') as $v => $k) { if (isset($update[$k])) { $msg .= is_array($update[$k]) ? "[{$v}: " . $update[$k][0] . "]" : "[{$v}: " . $update[$k] . "]"; } } log_event($msg, $device, 'sensor', $sensor_entry['sensor_id']); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); } } $update = array(); foreach ($param_main as $key => $column) { if (float_cmp(${$key}, $sensor_entry[$column]) !== 0) { $update[$column] = ${$key}; } } foreach ($param_opt as $key) { if (${$key} != $sensor_entry[$key]) { $update[$key] = ${$key}; } } if (count($update)) { $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); echo "U"; log_event("Sensor updated: {$class} {$type} {$index} {$sensor_descr}", $device, 'sensor', $sensor_entry['sensor_id']); } else { echo "."; } } $valid[$class][$type][$index] = 1; }
/** * @dataProvider providerFloatCompare * @group numbers */ public function testFloatCompare($a, $b, $epsilon, $result) { $this->assertSame($result, float_cmp($a, $b, $epsilon)); }
continue; } if ($t_entry['entSensorThresholdSeverity'] == "major" && $t_entry['entSensorThresholdRelation'] == "greaterOrEqual") { $limits['limit_high'] = $t_entry['entSensorThresholdValue'] * $scale; } if ($t_entry['entSensorThresholdSeverity'] == "major" && $t_entry['entSensorThresholdRelation'] == "lessOrEqual") { $limits['limit_low'] = $t_entry['entSensorThresholdValue'] * $scale; } if ($t_entry['entSensorThresholdSeverity'] == "minor" && $t_entry['entSensorThresholdRelation'] == "greaterOrEqual") { $limits['limit_high_warn'] = $t_entry['entSensorThresholdValue'] * $scale; } if ($t_entry['entSensorThresholdSeverity'] == "minor" && $t_entry['entSensorThresholdRelation'] == "lessOrEqual") { $limits['limit_low_warn'] = $t_entry['entSensorThresholdValue'] * $scale; } } if (float_cmp($limits['limit_high'], $limits['limit_low']) === 0 && float_cmp($limits['limit_high_warn'], $limits['limit_low_warn']) === 0 && float_cmp($limits['limit_high'], $limits['limit_high_warn']) === 0) { // Some Cisco sensors have all limits as same value (f.u. cisco), than leave only one limit unset($limits['limit_high_warn'], $limits['limit_low_warn'], $limits['limit_low']); } // End Threshold code } if ($descr == "") { $ok = FALSE; } // Invalid description. Lots of these on Nexus if ($ok) { $options = array_merge($limits, $options); discover_sensor($valid['sensor'], $type, $device, $oid, $index, $sensor_type, $descr, $scale, $value, $options); } } }
function __api_gain_mutes($gain) { if (strpos($gain, ',') !== FALSE) { $does_mute = TRUE; $gains = explode(',', $gain); $z = sizeof($gains) / 2; for ($i = 0; $i < $z; $i++) { $does_mute = float_cmp(floatval($gains[1 + $i * 2]), FLOAT_ZERO); if (!$does_mute) { break; } } } else { $does_mute = float_cmp(floatval($gain), FLOAT_ZERO); } return $does_mute; }
function poll_sensor($device, $class, $unit, &$oid_cache) { global $config, $agent_sensors, $ipmi_sensors, $graphs; $sql = "SELECT *, `sensors`.`sensor_id` AS `sensor_id`"; $sql .= " FROM `sensors`"; $sql .= " LEFT JOIN `sensors-state` ON `sensors`.sensor_id = `sensors-state`.sensor_id"; $sql .= " WHERE `sensor_class` = ? AND `device_id` = ?"; foreach (dbFetchRows($sql, array($class, $device['device_id'])) as $sensor_db) { $sensor_poll = array(); if (OBS_DEBUG) { echo "检测中 (" . $sensor_db['poller_type'] . ") {$class} " . $sensor_db['sensor_descr'] . " "; print_r($sensor_db); } if ($sensor_db['poller_type'] == "snmp") { # if ($class == "temperature" && $device['os'] == "papouch") // Why all temperature? if ($class == "temperature") { for ($i = 0; $i < 5; $i++) { // Take value from $oid_cache if we have it, else snmp_get it if (is_numeric($oid_cache[$sensor_db['sensor_oid']])) { print_debug("取值来自 oid_cache"); $sensor_poll['sensor_value'] = $oid_cache[$sensor_db['sensor_oid']]; } else { $sensor_poll['sensor_value'] = snmp_fix_numeric(snmp_get($device, $sensor_db['sensor_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs())); } if (is_numeric($sensor_poll['sensor_value']) && $sensor_poll['sensor_value'] != 9999) { break; } // Papouch TME sometimes sends 999.9 when it is right in the middle of an update; sleep(1); // Give the TME some time to reset } // If we received 999.9 degrees still, reset to Unknown. if ($sensor_poll['sensor_value'] == 9999) { $sensor_poll['sensor_value'] = "U"; } } else { if ($class == "runtime") { if (isset($oid_cache[$sensor_db['sensor_oid']])) { print_debug("value taken from oid_cache"); $sensor_poll['sensor_value'] = $oid_cache[$sensor_db['sensor_oid']]; } else { $sensor_poll['sensor_value'] = snmp_get($device, $sensor_db['sensor_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs()); } if (strpos($sensor_poll['sensor_value'], ':') !== FALSE) { // Use timetick conversion only when snmpdata is formatted as timetick 0:0:21:00.00 $sensor_poll['sensor_value'] = timeticks_to_sec($sensor_poll['sensor_value']); } } else { // Take value from $oid_cache if we have it, else snmp_get it if (is_numeric($oid_cache[$sensor_db['sensor_oid']])) { print_debug("取值来自 oid_cache"); $sensor_poll['sensor_value'] = $oid_cache[$sensor_db['sensor_oid']]; } else { $sensor_poll['sensor_value'] = snmp_fix_numeric(snmp_get($device, $sensor_db['sensor_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs())); } } } } else { if ($sensor_db['poller_type'] == "agent") { if (isset($agent_sensors)) { $sensor_poll['sensor_value'] = $agent_sensors[$class][$sensor_db['sensor_type']][$sensor_db['sensor_index']]['current']; // FIXME pass unit? } else { print_warning("传感器中的数据无可用的代理."); continue; } } else { if ($sensor_db['poller_type'] == "ipmi") { if (isset($ipmi_sensors)) { $sensor_poll['sensor_value'] = $ipmi_sensors[$class][$sensor_db['sensor_type']][$sensor_db['sensor_index']]['current']; $unit = $ipmi_sensors[$class][$sensor_db['sensor_type']][$sensor_db['sensor_index']]['unit']; } else { print_warning("IPMI传感器数据不可用."); continue; } } else { print_warning("未知的系缆柱型传感器."); continue; } } } if (OBS_DEBUG) { print_r($sensor_poll); } if ($sensor_poll['sensor_value'] == -32768) { print_debug("Invalid (-32768) "); $sensor_poll['sensor_value'] = 0; } /// FIXME. This is old pre 'scale' method, remove in r7000 if (isset($sensor_db['sensor_divisor']) && $sensor_db['sensor_divisor'] > 1) { /// This is fix for r5351 if ($sensor_db['sensor_multiplier'] >= 1) { $sensor_poll['sensor_value'] = $sensor_poll['sensor_value'] / $sensor_db['sensor_divisor']; } } if (isset($sensor_db['sensor_multiplier']) && $sensor_db['sensor_multiplier'] != 0) { $f2c = FALSE; if ($class == "temperature") { // This is weird hardcode for convert Fahrenheit to Celsius foreach (array(1, 0.1) as $scale_tmp) { if (float_cmp($sensor_db['sensor_multiplier'], $scale_tmp * 5 / 9) === 0) { $sensor_db['sensor_multiplier'] = $scale_tmp; $f2c = TRUE; break; } } } $sensor_poll['sensor_value'] *= $sensor_db['sensor_multiplier']; if ($f2c) { $sensor_poll['sensor_value'] = f2c($sensor_poll['sensor_value']); print_debug('TEMPERATURE sensor: Fahrenheit -> Celsius'); } } $rrd_file = get_sensor_rrd($device, $sensor_db); rrdtool_create($device, $rrd_file, "DS:sensor:GAUGE:600:-20000:U"); echo $sensor_poll['sensor_value'] . "{$unit} "; // FIXME this block and the other block below it are kinda retarded. They should be merged and simplified. if ($sensor_poll['sensor_ignore'] || $sensor_poll['sensor_disable']) { $sensor_poll['sensor_event'] = 'ignore'; } else { if ($sensor_db['sensor_limit_low_warn'] != '' && $sensor_poll['sensor_value'] < $sensor_db['sensor_limit_low_warn'] || $sensor_db['sensor_limit_warn'] != '' && $sensor_poll['sensor_value'] > $sensor_db['sensor_limit_warn']) { $sensor_poll['sensor_event'] = 'warning'; $sensor_poll['sensor_status'] = 'Sensor warning thresholds exceeded.'; // FIXME - be more specific } else { if (($sensor_db['sensor_limit_low'] != '' && $sensor_poll['sensor_value'] < $sensor_db['sensor_limit_low'] || $sensor_db['sensor_limit'] != '' && $sensor_poll['sensor_value'] > $sensor_db['sensor_limit']) && $sensor_db['sensor_value'] != '') { $sensor_poll['sensor_event'] = 'alert'; $sensor_poll['sensor_status'] = 'Sensor critical thresholds exceeded.'; // FIXME - be more specific } else { $sensor_poll['sensor_event'] = 'up'; $sensor_poll['sensor_status'] = ''; //if ($sensor_db['sensor_event'] != 'up' && $sensor_db['sensor_event'] != '') //{ // $sensor_poll['sensor_status'] = 'Sensor thresholds cleared.'; // FIXME - be more specific //} } } } // FIXME I left the eventlog code for now, as soon as alerts send an entry to the eventlog this can go. if ($sensor_db['sensor_event'] != 'ignore') { if ($sensor_db['sensor_limit_low'] != "" && $sensor_db['sensor_value'] >= $sensor_db['sensor_limit_low'] && $sensor_poll['sensor_value'] < $sensor_db['sensor_limit_low']) { // If old value greater than low limit and new value less than low limit $msg = ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor_db['sensor_descr'] . " is under threshold: " . $sensor_poll['sensor_value'] . "{$unit} (< " . $sensor_db['sensor_limit_low'] . "{$unit})"; log_event(ucfirst($class) . ' ' . $sensor_db['sensor_descr'] . " under threshold: " . $sensor_poll['sensor_value'] . " {$unit} (< " . $sensor_db['sensor_limit_low'] . " {$unit})", $device, 'sensor', $sensor_db['sensor_id'], 'warning'); } else { if ($sensor_db['sensor_limit'] != "" && $sensor_db['sensor_value'] <= $sensor_db['sensor_limit'] && $sensor_poll['sensor_value'] > $sensor_db['sensor_limit']) { // If old value less than high limit and new value greater than high limit $msg = ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor_db['sensor_descr'] . " is over threshold: " . $sensor_poll['sensor_value'] . "{$unit} (> " . $sensor_db['sensor_limit'] . "{$unit})"; log_event(ucfirst($class) . ' ' . $sensor_db['sensor_descr'] . " above threshold: " . $sensor_poll['sensor_value'] . " {$unit} (> " . $sensor_db['sensor_limit'] . " {$unit})", $device, 'sensor', $sensor_db['sensor_id'], 'warning'); } } } else { print_message("[%ySensor Ignored%n]", 'color'); } echo PHP_EOL; // Send statistics array via AMQP/JSON if AMQP is enabled globally and for the ports module if ($config['amqp']['enable'] == TRUE && $config['amqp']['modules']['sensors']) { $json_data = array('value' => $sensor_poll['sensor_value']); messagebus_send(array('attribs' => array('t' => time(), 'device' => $device['hostname'], 'device_id' => $device['device_id'], 'e_type' => 'sensor', 'e_class' => $sensor_db['sensor_class'], 'e_type' => $sensor_db['sensor_type'], 'e_index' => $sensor_db['sensor_index']), 'data' => $json_data)); } // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'sensor' . '.' . $sensor_db['sensor_class'] . '.' . $sensor_db['sensor_type'] . '.' . $sensor_db['sensor_index'], $sensor_poll['sensor_value']); } // Update RRD rrdtool_update($device, $rrd_file, "N:" . $sensor_poll['sensor_value']); // Enable graph $graphs[$sensor_db['sensor_class']] = TRUE; // Check alerts $metrics = array(); $metrics['sensor_value'] = $sensor_poll['sensor_value']; $metrics['sensor_event'] = $sensor_poll['sensor_event']; $metrics['sensor_status'] = $sensor_poll['sensor_status']; check_entity('sensor', $sensor_db, $metrics); // Update SQL State if (is_numeric($sensor_db['sensor_polled'])) { dbUpdate(array('sensor_value' => $sensor_poll['sensor_value'], 'sensor_event' => $sensor_poll['sensor_event'], 'sensor_status' => $sensor_poll['sensor_status'], 'sensor_polled' => time()), 'sensors-state', '`sensor_id` = ?', array($sensor_db['sensor_id'])); } else { dbInsert(array('sensor_id' => $sensor_db['sensor_id'], 'sensor_value' => $sensor_poll['sensor_value'], 'sensor_event' => $sensor_poll['sensor_event'], 'sensor_status' => $sensor_poll['sensor_status'], 'sensor_polled' => time()), 'sensors-state'); } } }
function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $sensor_descr, $scale = 1, $current = NULL, $options = array(), $poller_type = 'snmp') { global $config; // If this is actually a status indicator, pass it off to discover_status() then return. if ($class == 'state' || $class == 'status') { print_debug("Redirect call to discover_status()."); $return = discover_status($device, $oid, $index, $type, $sensor_descr, $current, $options, $poller_type); return $return; } // Init main $param_main = array('oid' => 'sensor_oid', 'sensor_descr' => 'sensor_descr', 'scale' => 'sensor_multiplier'); // Init numeric values if (!is_numeric($scale) || $scale == 0) { $scale = 1; } // Skip discovery sensor if value not numeric or null (default) if ($current !== NULL) { // Some retarded devices report data with spaces and commas // STRING: " 20,4" $current = snmp_fix_numeric($current); } if (is_numeric($current)) { $f2c = FALSE; if ($class == 'temperature') { // This is weird hardcode for convert Fahrenheit to Celsius foreach (array(1, 0.1) as $scale_f2c) { if (float_cmp($scale, $scale_f2c * 5 / 9) === 0) { //$scale = $scale_tmp; $f2c = TRUE; break; } } } if ($f2c) { $current = f2c($current * $scale_f2c); print_debug('TEMPERATURE sensor: Fahrenheit -> Celsius'); } else { $current *= $scale; } } else { if ($current !== NULL) { print_debug("Sensor skipped by not numeric value: {$current}, {$sensor_descr} "); return FALSE; } } $param_limits = array('limit_high' => 'sensor_limit', 'limit_high_warn' => 'sensor_limit_warn', 'limit_low' => 'sensor_limit_low', 'limit_low_warn' => 'sensor_limit_low_warn'); foreach ($param_limits as $key => $column) { ${$key} = is_numeric($options[$key]) ? $options[$key] : NULL; } // Init optional $param_opt = array('entPhysicalIndex', 'entPhysicalClass', 'entPhysicalIndex_measured', 'measured_class', 'measured_entity'); foreach ($param_opt as $key) { ${$key} = $options[$key] ? $options[$key] : NULL; } print_debug("发现传感器: {$class}, " . $device['hostname'] . ", {$oid}, {$index}, {$type}, {$sensor_descr}, SCALE: {$scale}, LIMITS: ({$limit_low}, {$limit_low_warn}, {$limit_high_warn}, {$limit_high}), CURRENT: {$current}, {$poller_type}, {$entPhysicalIndex}, {$entPhysicalClass}"); // Check sensor ignore filters foreach ($config['ignore_sensor'] as $bi) { if (strcasecmp($bi, $sensor_descr) == 0) { print_debug("Skipped by equals: {$bi}, {$sensor_descr} "); return FALSE; } } foreach ($config['ignore_sensor_string'] as $bi) { if (stripos($sensor_descr, $bi) !== FALSE) { print_debug("Skipped by strpos: {$bi}, {$sensor_descr} "); return FALSE; } } foreach ($config['ignore_sensor_regexp'] as $bi) { if (preg_match($bi, $sensor_descr) > 0) { print_debug("Skipped by regexp: {$bi}, {$sensor_descr} "); return FALSE; } } if (!is_null($limit_low_warn) && !is_null($limit_high_warn) && $limit_low_warn > $limit_high_warn) { // Fix high/low thresholds (i.e. on negative numbers) list($limit_high_warn, $limit_low_warn) = array($limit_low_warn, $limit_high_warn); } if (dbFetchCell('SELECT COUNT(`sensor_id`) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', array($poller_type, $class, $device['device_id'], $type, $index)) == '0') { if (!$limit_high) { $limit_high = sensor_limit_high($class, $current); } if (!$limit_low) { $limit_low = sensor_limit_low($class, $current); } if (!is_null($limit_low) && !is_null($limit_high) && $limit_low > $limit_high) { // Fix high/low thresholds (i.e. on negative numbers) list($limit_high, $limit_low) = array($limit_low, $limit_high); print_debug("High/low limits swapped."); } $sensor_insert = array('poller_type' => $poller_type, 'sensor_class' => $class, 'device_id' => $device['device_id'], 'sensor_index' => $index, 'sensor_type' => $type); foreach ($param_main as $key => $column) { $sensor_insert[$column] = ${$key}; } foreach ($param_limits as $key => $column) { // Convert strings/numbers to (float) or to array('NULL') ${$key} = ${$key} === NULL ? array('NULL') : (double) ${$key}; $sensor_insert[$column] = ${$key}; } foreach ($param_opt as $key) { if (is_null(${$key})) { ${$key} = array('NULL'); } $sensor_insert[$key] = ${$key}; } $sensor_id = dbInsert($sensor_insert, 'sensors'); $state_insert = array('sensor_id' => $sensor_id, 'sensor_value' => $current, 'sensor_polled' => 'NOW()'); dbInsert($state_insert, 'sensors-state'); print_debug("( {$sensor_id} inserted )"); echo "+"; if ($poller_type != 'ipmi') { // Suppress events for IPMI, see: http://jira.observium.org/browse/OBSERVIUM-959 log_event("传感器已添加: {$class} {$type} {$index} {$sensor_descr}", $device, 'sensor', $sensor_id); } } else { $sensor_entry = dbFetchRow("SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?", array($class, $device['device_id'], $type, $index)); // Limits if (!$sensor_entry['sensor_custom_limit']) { if (!is_numeric($limit_high) && !is_numeric($limit_low)) { if ($sensor_entry['sensor_limit'] !== '') { // Calculate a reasonable limit $limit_high = sensor_limit_high($class, $current); } else { // Use existing limit. (this is wrong! --mike) $limit_high = $sensor_entry['sensor_limit']; } if ($sensor_entry['sensor_limit_low'] !== '') { // Calculate a reasonable limit $limit_low = sensor_limit_low($class, $current); } else { // Use existing limit. (this is wrong! --mike) $limit_low = $sensor_entry['sensor_limit_low']; } } // Fix high/low thresholds (i.e. on negative numbers) if (!is_null($limit_low) && !is_null($limit_high) && $limit_low > $limit_high) { list($limit_high, $limit_low) = array($limit_low, $limit_high); print_debug("High/low limits swapped."); } // Update limits $update = array(); $update_msg = array(); $debug_msg = 'Current sensor value: "' . $current . '", scale: "' . $scale . '"' . PHP_EOL; foreach ($param_limits as $key => $column) { // $key - param name, $$key - param value, $column - column name in DB for $key $debug_msg .= ' ' . $key . ': "' . $sensor_entry[$column] . '" -> "' . ${$key} . '"' . PHP_EOL; //convert strings/numbers to identical type (float) or to array('NULL') for correct comparison ${$key} = ${$key} === NULL ? array('NULL') : (double) ${$key}; $sensor_entry[$column] = $sensor_entry[$column] === NULL ? array('NULL') : (double) $sensor_entry[$column]; if (float_cmp(${$key}, $sensor_entry[$column], 0.1) !== 0) { $update[$column] = ${$key}; $update_msg[] = $key . ' -> "' . (is_array(${$key}) ? 'NULL' : ${$key}) . '"'; } } if (count($update)) { echo "L"; print_debug($debug_msg); log_event('传感器已更新(限制): ' . implode(', ', $update_msg), $device, 'sensor', $sensor_entry['sensor_id']); $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); } } $update = array(); foreach ($param_main as $key => $column) { if (float_cmp(${$key}, $sensor_entry[$column]) !== 0) { $update[$column] = ${$key}; } } foreach ($param_opt as $key) { if (${$key} != $sensor_entry[$key]) { $update[$key] = ${$key}; } } if (count($update)) { $updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', array($sensor_entry['sensor_id'])); echo "U"; log_event("传感器已更新: {$class} {$type} {$index} {$sensor_descr}", $device, 'sensor', $sensor_entry['sensor_id']); } else { echo "."; } } $valid[$class][$type][$index] = 1; }
<?php /** * Observium * * This file is part of Observium. * * @package observium * @subpackage update * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2016 Observium Limited * */ echo 'Clean incorrect manual geolocation: '; foreach (dbFetchRows("SELECT * FROM `devices_locations` WHERE `location_manual` = '1';") as $entry) { if (float_cmp($entry['location_lat'], $config['geocoding']['default']['lat'], 0.001) == 0 && float_cmp($entry['location_lon'], $config['geocoding']['default']['lon'], 0.001) == 0) { dbDelete('devices_locations', 'location_id = ?', array($entry['location_id'])); echo '.'; } } // EOF
function poll_sensor($device, $class, $unit, &$oid_cache) { global $config, $agent_sensors, $ipmi_sensors; $sql = "SELECT *, `sensors`.`sensor_id` AS `sensor_id`"; $sql .= " FROM `sensors`"; $sql .= " LEFT JOIN `sensors-state` ON `sensors`.sensor_id = `sensors-state`.sensor_id"; $sql .= " WHERE `sensor_class` = ? AND `device_id` = ?"; foreach (dbFetchRows($sql, array($class, $device['device_id'])) as $sensor) { echo "Checking (" . $sensor['poller_type'] . ") {$class} " . $sensor['sensor_descr'] . " "; $sensor_new = $sensor; // Cache non-humanized sensor array humanize_sensor($sensor); if ($sensor['poller_type'] == "snmp") { # if ($class == "temperature" && $device['os'] == "papouch") // Why all temperature? if ($class == "temperature" && !$sensor['sensor_state']) { for ($i = 0; $i < 5; $i++) { // Take value from $oid_cache if we have it, else snmp_get it if (is_numeric($oid_cache[$sensor['sensor_oid']])) { print_debug("value taken from oid_cache"); $sensor_value = $oid_cache[$sensor['sensor_oid']]; } else { $sensor_value = preg_replace("/[^0-9\\-\\.]/", "", snmp_get($device, $sensor['sensor_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs())); } if (is_numeric($sensor_value) && $sensor_value != 9999) { break; } // Papouch TME sometimes sends 999.9 when it is right in the middle of an update; sleep(1); // Give the TME some time to reset } // Also reduce value by 32 if sensor in Fahrenheit unit if (float_cmp($sensor['sensor_multiplier'], 5 / 9) === 0) { $sensor_value -= 32; } // If we received 999.9 degrees still, reset to Unknown. if ($sensor_value == 9999) { $sensor_value = "U"; } } else { if ($class == "runtime" && !$sensor['sensor_state']) { if (isset($oid_cache[$sensor['sensor_oid']])) { print_debug("value taken from oid_cache"); $sensor_value = timeticks_to_sec($oid_cache[$sensor['sensor_oid']]); } else { $sensor_value = trim(str_replace("\"", "", snmp_get($device, $sensor['sensor_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs()))); $sensor_value = timeticks_to_sec($sensor_value); } } else { // Take value from $oid_cache if we have it, else snmp_get it if (is_numeric($oid_cache[$sensor['sensor_oid']])) { print_debug("value taken from oid_cache"); $sensor_value = $oid_cache[$sensor['sensor_oid']]; } else { $sensor_value = trim(str_replace("\"", "", snmp_get($device, $sensor['sensor_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs()))); } } } } else { if ($sensor['poller_type'] == "agent") { if (isset($agent_sensors)) { $sensor_value = $agent_sensors[$class][$sensor['sensor_type']][$sensor['sensor_index']]['current']; // FIXME pass unit? } else { print_warning("No agent sensor data available."); continue; } } else { if ($sensor['poller_type'] == "ipmi") { if (isset($ipmi_sensors)) { $sensor_value = $ipmi_sensors[$class][$sensor['sensor_type']][$sensor['sensor_index']]['current']; $unit = $ipmi_sensors[$class][$sensor['sensor_type']][$sensor['sensor_index']]['unit']; } else { print_warning("No IPMI sensor data available."); continue; } } else { print_warning("Unknown sensor poller type."); continue; } } } if (!$sensor['sensor_state']) { if ($sensor_value == -32768) { echo "Invalid (-32768) "; $sensor_value = 0; } if (isset($sensor['sensor_divisor']) && $sensor['sensor_divisor'] > 1) { /// This is fix for r5351 if ($sensor['sensor_multiplier'] >= 1) { $sensor_value = $sensor_value / $sensor['sensor_divisor']; } } if (isset($sensor['sensor_multiplier']) && $sensor['sensor_multiplier'] != 0) { $sensor_value = $sensor_value * $sensor['sensor_multiplier']; } } $rrd_file = get_sensor_rrd($device, $sensor); rrdtool_create($device, $rrd_file, "DS:sensor:GAUGE:600:-20000:U"); echo "{$sensor_value} {$unit} "; // Write new value and humanize (for alert checks) $sensor_new['sensor_value'] = $sensor_value; humanize_sensor($sensor_new); // FIXME I left the eventlog code for now, as soon as alerts send an entry to the eventlog this can go. if ($sensor['state_event'] != 'ignore') { if (!$sensor['sensor_state']) { if ($sensor['sensor_limit_low'] != "" && $sensor['sensor_value'] >= $sensor['sensor_limit_low'] && $sensor_value < $sensor['sensor_limit_low']) { $msg = ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " is under threshold: " . $sensor_value . "{$unit} (< " . $sensor['sensor_limit_low'] . "{$unit})"; 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_value'] <= $sensor['sensor_limit'] && $sensor_value > $sensor['sensor_limit']) { $msg = ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " is over threshold: " . $sensor_value . "{$unit} (> " . $sensor['sensor_limit'] . "{$unit})"; log_event(ucfirst($class) . ' ' . $sensor['sensor_descr'] . " above threshold: " . $sensor_value . " {$unit} (> " . $sensor['sensor_limit'] . " {$unit})", $device, $class, $sensor['sensor_id']); } } } else { if ($sensor_new['state_event'] != $sensor['state_event'] && $sensor['state_event'] != '') { $sensor_state_name = $sensor_new['state_name']; $sensor_state_event = $sensor_new['state_event']; switch ($sensor_state_event) { case 'alert': $msg = ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " is under ALERT state: " . $sensor_state_name . " (previous state: " . $sensor['state_name'] . ")"; log_event($msg, $device, $class, $sensor['sensor_id']); break; case 'warning': $msg = ucfirst($class) . " Warning: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " in WARNING state: " . $sensor_state_name . " (previous state: " . $sensor['state_name'] . ")"; log_event($msg, $device, $class, $sensor['sensor_id']); break; case 'up': $msg = ucfirst($class) . " Up: " . $device['hostname'] . " " . $sensor['sensor_descr'] . " in NORMAL state: " . $sensor_state_name . " (previous state: " . $sensor['state_name'] . ")"; log_event($msg, $device, $class, $sensor['sensor_id']); break; } } } } else { print_message("[%ySensor Ignored%n]", 'color'); } echo "\n"; // Send statistics array via AMQP/JSON if AMQP is enabled globally and for the ports module if ($config['amqp']['enable'] == TRUE && $config['amqp']['modules']['sensors']) { $json_data = array('value' => $sensor_value); messagebus_send(array('attribs' => array('t' => time(), 'device' => $device['hostname'], 'device_id' => $device['device_id'], 'e_type' => 'sensor', 'e_class' => $sensor['sensor_class'], 'e_type' => $sensor['sensor_type'], 'e_index' => $sensor['sensor_index']), 'data' => $json_data)); } // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'sensor' . '.' . $sensor['sensor_class'] . '.' . $sensor['sensor_type'] . '.' . $sensor['sensor_index'], $sensor_value); } // Update RRD rrdtool_update($device, $rrd_file, "N:{$sensor_value}"); // Check alerts $metrics = array(); if (!$sensor['sensor_state']) { $metrics['sensor_value'] = $sensor_new['sensor_value']; } $metrics['sensor_event'] = $sensor_new['state_event']; check_entity('sensor', $sensor, $metrics); // Update SQL State if (is_numeric($sensor['sensor_polled'])) { dbUpdate(array('sensor_value' => $sensor_value, 'sensor_polled' => time()), 'sensors-state', '`sensor_id` = ?', array($sensor['sensor_id'])); } else { dbInsert(array('sensor_id' => $sensor['sensor_id'], 'sensor_value' => $sensor_value, 'sensor_polled' => time()), 'sensors-state'); } } }