function process_port_etherlike(&$this_port, $device) { // Used to loop below for StatsD $etherlike_oids = array('dot3StatsAlignmentErrors', 'dot3StatsFCSErrors', 'dot3StatsSingleCollisionFrames', 'dot3StatsMultipleCollisionFrames', 'dot3StatsSQETestErrors', 'dot3StatsDeferredTransmissions', 'dot3StatsLateCollisions', 'dot3StatsExcessiveCollisions', 'dot3StatsInternalMacTransmitErrors', 'dot3StatsCarrierSenseErrors', 'dot3StatsFrameTooLongs', 'dot3StatsInternalMacReceiveErrors', 'dot3StatsSymbolErrors'); // Overwrite ifDuplex with dot3StatsDuplexStatus if it exists if (isset($this_port['dot3StatsDuplexStatus'])) { // echo("dot3Duplex, "); $this_port['ifDuplex'] = $this_port['dot3StatsDuplexStatus']; } if ($this_port['ifType'] == "ethernetCsmacd" && isset($this_port['dot3StatsIndex'])) { // Check to make sure Port data is cached. rrdtool_update_ng($device, 'port-dot3', array('dot3StatsAlignmentErrors' => $this_port['dot3StatsAlignmentErrors'], 'dot3StatsFCSErrors' => $this_port['dot3StatsFCSErrors'], 'dot3StatsSingleCollisionFrames' => $this_port['dot3StatsSingleCollisionFrames'], 'dot3StatsMultipleCollisionFrames' => $this_port['dot3StatsMultipleCollisionFrames'], 'dot3StatsSQETestErrors' => $this_port['dot3StatsSQETestErrors'], 'dot3StatsDeferredTransmissions' => $this_port['dot3StatsDeferredTransmissions'], 'dot3StatsLateCollisions' => $this_port['dot3StatsLateCollisions'], 'dot3StatsExcessiveCollisions' => $this_port['dot3StatsExcessiveCollisions'], 'dot3StatsInternalMacTransmitErrors' => $this_port['dot3StatsInternalMacTransmitErrors'], 'dot3StatsCarrierSenseErrors' => $this_port['dot3StatsCarrierSenseErrors'], 'dot3StatsFrameTooLongs' => $this_port['dot3StatsFrameTooLongs'], 'dot3StatsInternalMacReceiveErrors' => $this_port['dot3StatsInternalMacReceiveErrors'], 'dot3StatsSymbolErrors' => $this_port['dot3StatsSymbolErrors']), get_port_rrdindex($this_port)); if ($GLOBALS['config']['statsd']['enable'] == TRUE) { foreach ($etherlike_oids as $oid) { // Update StatsD/Carbon StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'port' . '.' . $this_port['ifIndex'] . '.' . $oid, $this_port[$oid]); } } } }
function process_port_adsl(&$this_port, $device, $port) { // Check to make sure Port data is cached. if (!isset($this_port['adslLineCoding'])) { return; } // Used below for StatsD only $adsl_oids = array('adslAtucCurrSnrMgn', 'adslAtucCurrAtn', 'adslAtucCurrOutputPwr', 'adslAtucCurrAttainableRate', 'adslAtucChanCurrTxRate', 'adslAturCurrSnrMgn', 'adslAturCurrAtn', 'adslAturCurrOutputPwr', 'adslAturCurrAttainableRate', 'adslAturChanCurrTxRate', 'adslAtucPerfLofs', 'adslAtucPerfLoss', 'adslAtucPerfLprs', 'adslAtucPerfESs', 'adslAtucPerfInits', 'adslAturPerfLofs', 'adslAturPerfLoss', 'adslAturPerfLprs', 'adslAturPerfESs', 'adslAtucChanCorrectedBlks', 'adslAtucChanUncorrectBlks', 'adslAturChanCorrectedBlks', 'adslAturChanUncorrectBlks'); $adsl_db_oids = array('adslLineCoding', 'adslLineType', 'adslAtucInvVendorID', 'adslAtucInvVersionNumber', 'adslAtucCurrSnrMgn', 'adslAtucCurrAtn', 'adslAtucCurrOutputPwr', 'adslAtucCurrAttainableRate', 'adslAturInvSerialNumber', 'adslAturInvVendorID', 'adslAturInvVersionNumber', 'adslAtucChanCurrTxRate', 'adslAturChanCurrTxRate', 'adslAturCurrSnrMgn', 'adslAturCurrAtn', 'adslAturCurrOutputPwr', 'adslAturCurrAttainableRate'); $adsl_tenth_oids = array('adslAtucCurrSnrMgn', 'adslAtucCurrAtn', 'adslAtucCurrOutputPwr', 'adslAturCurrSnrMgn', 'adslAturCurrAtn', 'adslAturCurrOutputPwr'); foreach ($adsl_tenth_oids as $oid) { if (isset($this_port[$oid])) { $this_port[$oid] = $this_port[$oid] / 10; } } if (dbFetchCell("SELECT COUNT(*) FROM `ports_adsl` WHERE `port_id` = ?", array($port['port_id'])) == "0") { dbInsert(array('port_id' => $port['port_id']), 'ports_adsl'); } $adsl_update = array('port_adsl_updated' => array('NOW()')); foreach ($adsl_db_oids as $oid) { $adsl_update[$oid] = $this_port[$oid]; } dbUpdate($adsl_update, 'ports_adsl', '`port_id` = ?', array($port['port_id'])); if ($this_port['adslAtucCurrSnrMgn'] > "1280") { $this_port['adslAtucCurrSnrMgn'] = "U"; } if ($this_port['adslAturCurrSnrMgn'] > "1280") { $this_port['adslAturCurrSnrMgn'] = "U"; } rrdtool_update_ng($device, 'port-adsl', array('AtucCurrSnrMgn' => $this_port['adslAtucCurrSnrMgn'], 'AtucCurrAtn' => $this_port['adslAtucCurrAtn'], 'AtucCurrOutputPwr' => $this_port['adslAtucCurrOutputPwr'], 'AtucCurrAttainableR' => $this_port['adslAtucCurrAttainableR'], 'AtucChanCurrTxRate' => $this_port['adslAtucChanCurrTxRate'], 'AturCurrSnrMgn' => $this_port['adslAturCurrSnrMgn'], 'AturCurrAtn' => $this_port['adslAturCurrAtn'], 'AturCurrOutputPwr' => $this_port['adslAturCurrOutputPwr'], 'AturCurrAttainableR' => $this_port['adslAturCurrAttainableR'], 'AturChanCurrTxRate' => $this_port['adslAturChanCurrTxRate'], 'AtucPerfLofs' => $this_port['adslAtucPerfLofs'], 'AtucPerfLoss' => $this_port['adslAtucPerfLoss'], 'AtucPerfLprs' => $this_port['adslAtucPerfLprs'], 'AtucPerfESs' => $this_port['adslAtucPerfESs'], 'AtucPerfInits' => $this_port['adslAtucPerfInits'], 'AturPerfLofs' => $this_port['adslAturPerfLofs'], 'AturPerfLoss' => $this_port['adslAturPerfLoss'], 'AturPerfLprs' => $this_port['adslAturPerfLprs'], 'AturPerfESs' => $this_port['adslAturPerfESs'], 'AtucChanCorrectedBl' => $this_port['adslAtucChanCorrectedBl'], 'AtucChanUncorrectBl' => $this_port['adslAtucChanUncorrectBl'], 'AturChanCorrectedBl' => $this_port['adslAturChanCorrectedBl'], 'AturChanUncorrectBl' => $this_port['adslAturChanUncorrectBl']), get_port_rrdindex($port)); if ($GLOBALS['config']['statsd']['enable']) { foreach ($adsl_oids as $oid) { // Update StatsD/Carbon StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'port' . '.' . $port['ifIndex'] . '.' . $oid, $this_port[$oid]); } } //echo("ADSL (".$this_port['adslLineCoding']."/".formatRates($this_port['adslAtucChanCurrTxRate'])."/".formatRates($this_port['adslAturChanCurrTxRate']).")"); }
$mempool['total'] = 100; $mempool['used'] = $mempool['perc']; } else { // Hrrmm.. it looks like empty snmp walk continue; } } } } } $mempool['free'] = $mempool['total'] - $mempool['used']; $hc = $mempool['mempool_hc'] ? ' (HC)' : ''; // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'mempool' . '.' . $mempool['mempool_mib'] . "." . $mempool['mempool_index'] . ".used", $mempool['used']); StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'mempool' . '.' . $mempool['mempool_mib'] . "." . $mempool['mempool_index'] . ".free", $mempool['free']); } rrdtool_update_ng($device, 'mempool', array('used' => $mempool['used'], 'free' => $mempool['free']), $mempool['mempool_mib'] . "-" . $mempool['mempool_index']); if (!is_numeric($mempool['mempool_polled'])) { dbInsert(array('mempool_id' => $mempool['mempool_id']), 'mempools-state'); } $mempool['state'] = array('mempool_polled' => time(), 'mempool_used' => $mempool['used'], 'mempool_perc' => $mempool['perc'], 'mempool_free' => $mempool['free'], 'mempool_total' => $mempool['total']); dbUpdate($mempool['state'], 'mempools-state', '`mempool_id` = ?', array($mempool['mempool_id'])); $graphs['mempool'] = TRUE; check_entity('mempool', $mempool, array('mempool_perc' => $mempool['perc'], 'mempool_free' => $mempool['free'], 'mempool_used' => $mempool['used'])); // print_message("Mempool ". $mempool['mempool_descr'] . ': '.$mempool['perc'].'%%'.$hc); $table_row = array(); $table_row[] = $mempool['mempool_descr']; $table_row[] = $mempool['mempool_mib']; $table_row[] = $mempool['mempool_index']; $table_row[] = formatStorage($mempool['total']);
if ($port_stats[$port['ifIndex']] && $port['ifType'] == "ethernetCsmacd" && isset($port_stats[$port['ifIndex']]['dot3StatsIndex'])) { // Check to make sure Port data is cached. $this_port =& $port_stats[$port['ifIndex']]; // TODO: remove $old_rrdfile? $old_rrdfile = $config['rrd_dir'] . "/" . $device['hostname'] . "/" . safename("etherlike-" . $port['ifIndex'] . ".rrd"); $rrdfile = get_port_rrdfilename($device, $port, "dot3"); if (!file_exists($rrdfile)) { if (file_exists($old_rrdfile)) { rename($old_rrdfile, $rrd_file); } else { foreach ($etherlike_oids as $oid) { $oid = truncate(str_replace("dot3Stats", "", $oid), 19, ''); $rrd_create .= " DS:{$oid}:COUNTER:600:U:100000000000"; } rrdtool_create($rrdfile, $rrd_create); } } if ($config['statsd']['enable'] == TRUE) { foreach ($etherlike_oids as $oid) { // Update StatsD/Carbon StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'port' . '.' . $port['ifIndex'] . '.' . $oid, $this_port[$oid]); } } $rrdupdate = "N"; foreach ($etherlike_oids as $oid) { $data = $this_port[$oid] + 0; $rrdupdate .= ":{$data}"; } rrdtool_update($rrdfile, $rrdupdate); echo "EtherLike "; }
$peer['c_update']['PrefixAdminLimit'] = $cbgpPeerPrefixAdminLimit; $peer['c_update']['PrefixThreshold'] = $cbgpPeerPrefixThreshold; $peer['c_update']['PrefixClearThreshold'] = $cbgpPeerPrefixClearThreshold; $peer['c_update']['AdvertisedPrefixes'] = $cbgpPeerAdvertisedPrefixes; $peer['c_update']['SuppressedPrefixes'] = $cbgpPeerSuppressedPrefixes; $peer['c_update']['WithdrawnPrefixes'] = $cbgpPeerWithdrawnPrefixes; if (dbFetchCell('SELECT COUNT(cbgp_id) FROM `bgpPeers_cbgp-state` WHERE `cbgp_id` = ?', array($peer_afi['cbgp_id'])) == 0) { dbInsert(array('cbgp_id' => $peer_afi['cbgp_id']), 'bgpPeers_cbgp-state'); } dbUpdate($peer['c_update'], 'bgpPeers_cbgp-state', '`cbgp_id` = ?', array($peer_afi['cbgp_id'])); // Update cbgp StatsD if ($config['statsd']['enable'] == TRUE) { foreach (array('AcceptedPrefixes', 'DeniedPrefixes', 'AdvertisedPrefixes', 'SuppressedPrefixes', 'WithdrawnPrefixes') as $oid) { // Update StatsD/Carbon $r_oid = 'cbgpPeer' . $oid; StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'bgp' . '.' . str_replace(".", "_", $peer_ip) . ".{$afi}.{$safi}" . '.' . $oid, ${$r_oid}); } } // Update cbgp RRD $cbgp_rrd = "cbgp-{$peer_ip}.{$afi}.{$safi}.rrd"; $rrd_create = "DS:AcceptedPrefixes:GAUGE:600:U:100000000000 \\\n DS:DeniedPrefixes:GAUGE:600:U:100000000000 \\\n DS:AdvertisedPrefixes:GAUGE:600:U:100000000000 \\\n DS:SuppressedPrefixes:GAUGE:600:U:100000000000 \\\n DS:WithdrawnPrefixes:GAUGE:600:U:100000000000 "; rrdtool_create($device, $cbgp_rrd, $rrd_create); rrdtool_update($device, $cbgp_rrd, "N:{$cbgpPeerAcceptedPrefixes}:{$cbgpPeerDeniedPrefixes}:{$cbgpPeerAdvertisedPrefixes}:{$cbgpPeerSuppressedPrefixes}:{$cbgpPeerWithdrawnPrefixes}"); } # while } # os_group=cisco | vendors echo PHP_EOL; } // End While loop on peers }
function poll_status($device) { global $config, $agent_sensors, $ipmi_sensors, $graphs, $oid_cache; $sql = "SELECT * FROM `status`"; $sql .= " LEFT JOIN `status-state` USING(`status_id`)"; $sql .= " WHERE `device_id` = ?"; foreach (dbFetchRows($sql, array($device['device_id'])) as $status_db) { //print_cli_heading("Status: ".$status_db['status_descr']. "(".$status_db['poller_type'].")", 3); print_debug("Checking (" . $status_db['poller_type'] . ") " . $status_db['status_descr'] . " "); // $status_poll = $status_db; // Cache non-humanized status array for use as new status state if ($status_db['poller_type'] == "snmp") { // Check if a specific poller file exists for this status, else collect via SNMP. $file = $config['install_dir'] . "/includes/polling/status/" . $status_db['status_type'] . ".inc.php"; if (is_file($file)) { include $file; } else { // Take value from $oid_cache if we have it, else snmp_get it if (is_numeric($oid_cache[$status_db['status_oid']])) { print_debug("value taken from oid_cache"); $status_value = $oid_cache[$status_db['status_oid']]; } else { $status_value = snmp_get($device, $status_db['status_oid'], "-OUqnv", "SNMPv2-MIB"); } $status_value = snmp_fix_numeric($status_value); } } else { if ($status_db['poller_type'] == "agent") { if (isset($agent_sensors['state'])) { $status_value = $agent_sensors['state'][$status_db['status_type']][$status_db['status_index']]['current']; } else { print_warning("No agent status data available."); continue; } } else { if ($status_db['poller_type'] == "ipmi") { if (isset($ipmi_sensors['state'])) { $status_value = $ipmi_sensors['state'][$status_db['status_type']][$status_db['status_index']]['current']; } else { print_warning("No IPMI status data available."); continue; } } else { print_warning("Unknown status poller type."); continue; } } } $status_polled_time = time(); // Store polled time for current status // Write new value and humanize (for alert checks) $state_array = get_state_array($status_db['status_type'], $status_value, $status_db['poller_type']); $status_poll['status_value'] = $state_array['value']; $status_poll['status_name'] = $state_array['name']; if ($status_db['status_ignore'] || $status_db['status_disable']) { $status_poll['status_event'] = 'ignore'; } else { $status_poll['status_event'] = $state_array['event']; } // If last change never set, use current time if (empty($status_db['status_last_change'])) { $status_db['status_last_change'] = $status_polled_time; } if ($status_poll['status_event'] != $status_db['status_event']) { // Status event changed, log and set status_last_change $status_poll['status_last_change'] = $status_polled_time; if ($status_poll['status_event'] == 'ignore') { print_message("[%ystatus Ignored%n]", 'color'); } else { if ($status_db['status_event'] != '') { // If old state not empty and new state not equals to new state $msg = 'Status '; switch ($status_poll['status_event']) { case 'alert': // New state alerted $msg .= "Alert: " . $device['hostname'] . " " . $status_db['status_descr'] . " entered ALERT state: " . $status_poll['status_name'] . " (previous: " . $status_db['status_name'] . ")"; log_event($msg, $device, 'status', $status_db['status_id'], 'warning'); break; case 'warning': // New state warned $msg .= "Warning: " . $device['hostname'] . " " . $status_db['status_descr'] . " entered WARNING state: " . $status_poll['status_name'] . " (previous: " . $status_db['status_name'] . ")"; log_event($msg, $device, 'status', $status_db['status_id']); break; case 'ok': // New state ok $msg .= "Ok: " . $device['hostname'] . " " . $status_db['status_descr'] . " entered OK state: " . $status_poll['status_name'] . " (previous: " . $status_db['status_name'] . ")"; log_event($msg, $device, 'status', $status_db['status_id'], 'warning'); break; } } } } else { // If status not changed, leave old last_change $status_poll['status_last_change'] = $status_db['status_last_change']; } if (OBS_DEBUG > 1) { print_vars($status_poll); } // Send statistics array via AMQP/JSON if AMQP is enabled globally and for the ports module if ($config['amqp']['enable'] == TRUE && $config['amqp']['modules']['status']) { $json_data = array('value' => $status_value); messagebus_send(array('attribs' => array('t' => time(), 'device' => $device['hostname'], 'device_id' => $device['device_id'], 'e_type' => 'status', 'e_type' => $status_db['status_type'], 'e_index' => $status_db['status_index']), 'data' => $json_data)); } // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'status' . '.' . $status_db['status_class'] . '.' . $status_db['status_type'] . '.' . $status_db['status_index'], $status_value); } // Update RRD - FIXME - can't convert to NG because filename is dynamic! new function should return index instead of filename. $rrd_file = get_status_rrd($device, $status_db); rrdtool_create($device, $rrd_file, "DS:status:GAUGE:600:-20000:U"); rrdtool_update($device, $rrd_file, "N:{$status_value}"); // Enable graph $graphs[$sensor_db['status']] = TRUE; // Check alerts $metrics = array(); $metrics['status_value'] = $status_value; $metrics['status_name'] = $status_poll['status_name']; $metrics['status_name_uptime'] = $status_polled_time - $status_poll['status_last_change']; $metrics['status_event'] = $status_poll['status_event']; //print_cli_data("Event (State)", $status_poll['status_event'] ." (".$status_poll['status_name'].")", 3); $GLOBALS['table_rows'][] = array($status_db['status_descr'], $status_db['status_type'], $status_db['status_index'], $status_db['poller_type'], $status_poll['status_name'], $status_poll['status_event'], format_unixtime($status_poll['status_last_change'])); check_entity('status', $status_db, $metrics); // Update SQL State if (is_numeric($status_db['status_polled'])) { dbUpdate(array('status_value' => $status_value, 'status_name' => $status_poll['status_name'], 'status_event' => $status_poll['status_event'], 'status_last_change' => $status_poll['status_last_change'], 'status_polled' => $status_polled_time), 'status-state', '`status_id` = ?', array($status_db['status_id'])); } else { dbInsert(array('status_id' => $status_db['status_id'], 'status_value' => $status_value, 'status_name' => $status_poll['status_name'], 'status_event' => $status_poll['status_event'], 'status_last_change' => $status_poll['status_last_change'], 'status_polled' => $status_polled_time), 'status-state'); } } }
$stats['ipSystemStatsOutForwDatagrams'] = $stats['ipSystemStatsHCOutForwDatagrams']; } unset($snmpstring, $rrdupdate, $snmpdata, $snmpdata_cmd, $rrd_create); $rrdfile = "ipSystemStats-{$af}.rrd"; $rrdupdate = "N"; foreach ($oids as $oid) { $oid_ds = str_replace("ipSystemStats", "", $oid); $oid_ds = truncate($oid_ds, 19, ''); $rrd_create .= " DS:{$oid_ds}:COUNTER:600:U:100000000000"; if (strstr($stats[$oid], "No") || strstr($stats[$oid], "d") || strstr($stats[$oid], "s")) { $stats[$oid] = "0"; } $rrdupdate .= ":" . $stats[$oid]; // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE && !strpos($oid, "HC")) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'system' . '.' . $oid, $stats[$oid]); } } rrdtool_create($device, $rrdfile, $rrd_create); rrdtool_update($device, $rrdfile, $rrdupdate); unset($rrdupdate, $rrd_create); // FIXME per-AF? $graphs['ipsystemstats_' . $af] = TRUE; $graphs['ipsystemstats_' . $af . '_frag'] = TRUE; $show_graphs[] = 'ipsystemstats_' . $af; $show_graphs[] = 'ipsystemstats_' . $af . '_frag'; } echo PHP_EOL; print_cli_data("Graphs", implode($show_graphs, " "), 2); } }
/** * @todo Implement testGaugeStats(). */ public function testGaugeStats() { $this->statsd->gauge("test.gaugeStat", "5", "1"); }
include $file; } else { $proc = snmp_get($device, $processor['processor_oid'], "-O Uqnv", "\"\""); } $procrrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/" . safename("processor-" . $processor['processor_type'] . "-" . $processor['processor_index'] . ".rrd"); if (!is_file($procrrd)) { rrdtool_create($procrrd, " \\\n DS:usage:GAUGE:600:-273:1000 "); } $proc = trim(str_replace("\"", "", $proc)); list($proc) = preg_split("@\\ @", $proc); if (!$processor['processor_precision']) { $processor['processor_precision'] = "1"; } $proc = round($proc / $processor['processor_precision'], 2); $graphs['processor'] = TRUE; echo $proc . "%\n"; // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'processor' . '.' . $processor['processor_type'] . "-" . $processor['processor_index'], $proc); } // Update RRD rrdtool_update($procrrd, "N:{$proc}"); // Update SQL State if (is_numeric($processor['processor_polled'])) { dbUpdate(array('processor_usage' => $proc, 'processor_polled' => time()), 'processors-state', '`processor_id` = ?', array($processor['processor_id'])); } else { dbInsert(array('processor_id' => $processor['processor_id'], 'processor_usage' => $proc, 'processor_polled' => time()), 'processors-state'); } // Check alerts check_entity('processor', $processor, array('processor_usage' => $proc)); }
continue; } if (OBS_DEBUG && count($storage)) { print_vars($storage); } if ($storage['size']) { $percent = round($storage['used'] / $storage['size'] * 100, 2); } else { $percent = 0; } $hc = $storage['storage_hc'] ? ' (HC)' : ''; print_message("存储 " . $storage['storage_descr'] . ': ' . $percent . '%%' . $hc); // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'storage' . '.' . $storage['storage_mib'] . "-" . safename($storage['storage_descr']) . ".used", $storage['used']); StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'storage' . '.' . $storage['storage_mib'] . "-" . safename($storage['storage_descr']) . ".free", $storage['free']); } // Update RRD rrdtool_update($device, $storage_rrd, "N:" . $storage['used'] . ":" . $storage['free']); if (!is_numeric($storage['storage_polled'])) { dbInsert(array('storage_id' => $storage['storage_id'], 'storage_polled' => time(), 'storage_used' => $storage['used'], 'storage_free' => $storage['free'], 'storage_size' => $storage['size'], 'storage_units' => $storage['units'], 'storage_perc' => $percent), 'storage-state'); } else { $update = dbUpdate(array('storage_polled' => time(), 'storage_used' => $storage['used'], 'storage_free' => $storage['free'], 'storage_size' => $storage['size'], 'storage_units' => $storage['units'], 'storage_perc' => $percent), 'storage-state', '`storage_id` = ?', array($storage['storage_id'])); if ($storage_size != $storage['storage_size']) { log_event('存储容量变更: ' . formatStorage($storage_size) . ' -> ' . formatStorage($storage['storage_size']) . ' (' . $storage['storage_descr'] . ')', $device, 'storage', $storage['storage_id']); } } $graphs['storage'] = TRUE; // Check alerts check_entity('storage', $storage, array('storage_perc' => $percent, 'storage_free' => $storage['free'], 'storage_used' => $storage['used'])); echo PHP_EOL;
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; } // 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 ($sensor['sensor_divisor'] == 9 && $sensor['sensor_multiplier'] == 5) { $sensor_value -= 32; } } 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."); 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."); continue; } } else { print_warning("Unknown sensor poller type."); continue; } } } if (!$sensor['sensor_state']) { if ($sensor_value == -32768) { echo "Invalid (-32768) "; $sensor_value = 0; } if ($sensor['sensor_divisor']) { $sensor_value = $sensor_value / $sensor['sensor_divisor']; } if ($sensor['sensor_multiplier']) { $sensor_value = $sensor_value * $sensor['sensor_multiplier']; } } $rrd_file = get_sensor_rrd($device, $sensor); if (!is_file($rrd_file)) { rrdtool_create($rrd_file, "DS:sensor:GAUGE:600:-20000:U"); //DS:sensor:GAUGE:600:-20000:20000 "); } echo "{$sensor_value} {$unit} "; // Write new value and humanize (for alert checks) $sensor_new['sensor_value'] = $sensor_value; humanize_sensor($sensor_new); // FIXME also warn when crossing WARN level!! 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})"; notify($device, ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); print_message("[%rAlerting for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "%n\n", 'color'); 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})"; notify($device, ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); print_message("[%rAlerting for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "%n\n", 'color'); log_event(ucfirst($class) . ' ' . $sensor['sensor_descr'] . " above threshold: " . $sensor_value . " {$unit} (> " . $sensor['sensor_limit'] . " {$unit})", $device, $class, $sensor['sensor_id']); } } } else { if ($sensor_value != $sensor['sensor_value'] && $sensor['state_value'] != '') { $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'] . ")"; print_message("[%rSensor ALARM for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "%n\n", 'color'); notify($device, ucfirst($class) . " Alarm: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); 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'] . ")"; print_message("[%rSensor WARNING for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "%n\n", 'color'); 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'] . ")"; print_message("[%rSensor UP for " . $device['hostname'] . " " . $sensor['sensor_descr'] . "%n\n", 'color'); if ($sensor['state_event'] != 'warning') { notify($device, ucfirst($class) . " Up: " . $device['hostname'] . " " . $sensor['sensor_descr'], $msg); } 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($rrd_file, "N:{$sensor_value}"); // Check alerts if (!$sensor['sensor_state']) { check_entity('sensor', $sensor, array('sensor_value' => $sensor_new['sensor_value'])); } check_entity('sensor', $sensor, array('sensor_event' => $sensor_new['state_event'])); // 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'); } } }
function poll_status($device) { global $config, $agent_status, $ipmi_status, $graphs, $oid_cache; $sql = "SELECT *, `status`.`status_id` AS `status_id`"; $sql .= " FROM `status`"; $sql .= " LEFT JOIN `status-state` ON `status`.status_id = `status-state`.status_id"; $sql .= " WHERE `device_id` = ?"; foreach (dbFetchRows($sql, array($device['device_id'])) as $status_db) { print_debug("Checking (" . $status_db['poller_type'] . ") " . $status_db['status_descr'] . " "); // $status_poll = $status_db; // Cache non-humanized status array for use as new status state if ($status_db['poller_type'] == "snmp") { // Check if a specific poller file exists for this status, else collect via SNMP. $file = $config['install_dir'] . "/includes/polling/status/" . $status_db['status_type'] . ".inc.php"; if (is_file($file)) { include $file; } else { // Take value from $oid_cache if we have it, else snmp_get it if (is_numeric($oid_cache[$status_db['status_oid']])) { print_debug("value taken from oid_cache"); $status_value = $oid_cache[$status_db['status_oid']]; } else { $status_value = snmp_fix_numeric(snmp_get($device, $status_db['status_oid'], "-OUqnv", "SNMPv2-MIB", mib_dirs())); } } } else { if ($status_db['poller_type'] == "agent") { if (isset($agent_status)) { $status_value = $agent_status[$class][$status_db['status_type']][$status_db['status_index']]['current']; // FIXME pass unit? } else { print_warning("No agent status data available."); continue; } } else { if ($status_db['poller_type'] == "ipmi") { if (isset($ipmi_status)) { $status_value = $ipmi_status[$class][$status_db['status_type']][$status_db['status_index']]['current']; $unit = $ipmi_status[$class][$status_db['status_type']][$status_db['status_index']]['unit']; } else { print_warning("No IPMI status data available."); continue; } } else { print_warning("Unknown status poller type."); continue; } } } $rrd_file = get_status_rrd($device, $status_db); rrdtool_create($device, $rrd_file, "DS:status:GAUGE:600:-20000:U"); echo "{$status_value} {$unit} "; // Write new value and humanize (for alert checks) $status_poll['status_value'] = $status_value; // Set status_event and status_name if they're not already set. if (isset($config['status_states'][$status_db['status_type']]) && !isset($status['status_event'])) { $status_poll['status_value'] = (int) $status_poll['status_value']; $status_poll['status_name'] = $config['status_states'][$status_db['status_type']][$status_poll['status_value']]['name']; if ($status_poll['status_ignore'] || $status['status_disable']) { $status_poll['status_event'] = 'ignore'; } else { $status_poll['status_event'] = $config['status_states'][$status_db['status_type']][$status_poll['status_value']]['event']; } } // FIXME I left the eventlog code for now, as soon as alerts send an entry to the eventlog this can go. if ($status_poll['status_event'] != 'ignore') { if ($status_db['status_event'] != '' && $status_poll['status_event'] != $status_db['status_event']) { // If old state not empty and new state not equals to new state switch ($status_poll['status_event']) { case 'alert': // New state alerted $msg = "Alarm: " . $device['hostname'] . " " . $status_db['status_descr'] . " entered ALERT state: " . $status_poll['status_name'] . " (previous: " . $status_db['status_name'] . ")"; log_event($msg, $device, 'status', $status_db['status_id'], 'warning'); break; case 'warning': // New state warned $msg = "Warning: " . $device['hostname'] . " " . $status_db['status_descr'] . " entered WARNING state: " . $status_poll['status_name'] . " (previous: " . $status_db['status_name'] . ")"; log_event($msg, $device, 'status', $status_db['status_id']); break; case 'up': // New state ok $msg = "Up: " . $device['hostname'] . " " . $status_db['status_descr'] . " entered NORMAL state: " . $status_poll['status_name'] . " (previous: " . $status_db['status_name'] . ")"; log_event('Status ' . $msg, $device, 'status', $status_db['status_id'], 'warning'); break; } } } else { print_message("[%ystatus Ignored%n]", 'color'); } print_r($status_poll); 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']['status']) { $json_data = array('value' => $status_value); messagebus_send(array('attribs' => array('t' => time(), 'device' => $device['hostname'], 'device_id' => $device['device_id'], 'e_type' => 'status', 'e_type' => $status_db['status_type'], 'e_index' => $status_db['status_index']), 'data' => $json_data)); } // Update StatsD/Carbon if ($config['statsd']['enable'] == TRUE) { StatsD::gauge(str_replace(".", "_", $device['hostname']) . '.' . 'status' . '.' . $status_db['status_class'] . '.' . $status_db['status_type'] . '.' . $status_db['status_index'], $status_value); } // Update RRD rrdtool_update($device, $rrd_file, "N:{$status_value}"); // Enable graph $graphs[$status_db['status_class']] = TRUE; // Check alerts $metrics = array(); $metrics['status_value'] = $status_value; $metrics['status_name'] = $status_poll['status_name']; $metrics['status_event'] = $status_poll['status_event']; check_entity('status', $status_db, $metrics); // Update SQL State if (is_numeric($status_db['status_polled'])) { dbUpdate(array('status_value' => $status_value, 'status_name' => $status_poll['status_name'], 'status_event' => $status_poll['status_event'], 'status_polled' => time()), 'status-state', '`status_id` = ?', array($status_db['status_id'])); } else { dbInsert(array('status_id' => $status_db['status_id'], 'status_value' => $status_value, 'status_name' => $status_poll['status_name'], 'status_event' => $status_poll['status_event'], 'status_polled' => time()), 'status-state'); } } }