Exemple #1
0
 if ($bgpPeerFsmEstablishedTime) {
     if (!(is_array($config['alerts']['bgp']['whitelist']) && !in_array($peer['bgpPeerRemoteAs'], $config['alerts']['bgp']['whitelist'])) && ($bgpPeerFsmEstablishedTime < $peer['bgpPeerFsmEstablishedTime'] || $bgpPeerState != $peer['bgpPeerState'])) {
         if ($peer['bgpPeerState'] == $bgpPeerState) {
             log_event('BGP Session flapped: ' . $remote_ip . ' (AS' . $peer['bgpPeerRemoteAs'] . '), time ' . formatUptime($bgpPeerFsmEstablishedTime) . ' ago', $device, 'bgpPeer', $peer['bgpPeer_id']);
         } else {
             if ($bgpPeerState == "established") {
                 log_event('BGP Session Up: ' . $remote_ip . ' (AS' . $peer['bgpPeerRemoteAs'] . '), time ' . formatUptime($bgpPeerFsmEstablishedTime) . ' ago', $device, 'bgpPeer', $peer['bgpPeer_id']);
             } else {
                 if ($peer['bgpPeerState'] == "established") {
                     log_event('BGP Session Down: ' . $remote_ip . ' (AS' . $peer['bgpPeerRemoteAs'] . '), time ' . formatUptime($bgpPeerFsmEstablishedTime) . ' ago.', $device, 'bgpPeer', $peer['bgpPeer_id']);
                 }
             }
         }
     }
 }
 check_entity('bgp_peer', $peer, array('bgpPeerState' => $bgpPeerState, 'bgpPeerAdminStatus' => $bgpPeerAdminStatus, 'bgpPeerFsmEstablishedTime' => $bgpPeerFsmEstablishedTime));
 $polled = time();
 $polled_period = $polled - $peer['bgpPeer_polled'];
 if ($debug) {
     echo "[ polled {$polled} -> period {$polled_period} ]";
 }
 $peer_rrd = 'bgp-' . $peer_ip . '.rrd';
 $create_rrd = "DS:bgpPeerOutUpdates:COUNTER:600:U:100000000000 \\\n        DS:bgpPeerInUpdates:COUNTER:600:U:100000000000 \\\n        DS:bgpPeerOutTotal:COUNTER:600:U:100000000000 \\\n        DS:bgpPeerInTotal:COUNTER:600:U:100000000000 \\\n        DS:bgpPeerEstablished:GAUGE:600:0:U ";
 rrdtool_create($device, $peer_rrd, $create_rrd);
 rrdtool_update($device, "{$peer_rrd}", "N:{$bgpPeerOutUpdates}:{$bgpPeerInUpdates}:{$bgpPeerOutTotalMessages}:{$bgpPeerInTotalMessages}:{$bgpPeerFsmEstablishedTime}");
 // Update states
 $peer['update'] = array();
 foreach (array('bgpPeerState', 'bgpPeerAdminStatus', 'bgpPeerLocalAddr', 'bgpPeerIdentifier') as $oid) {
     if (${$oid} != $peer[$oid]) {
         $peer['update'][$oid] = ${$oid};
     }
Exemple #2
0
    }
    $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']);
    $table_row[] = formatStorage($mempool['used']);
    $table_row[] = formatStorage($mempool['free']);
    $table_row[] = $mempool['perc'] . '%';
    $table_rows[] = $table_row;
    unset($table_row);
}
$headers = array('%WLabel%n', '%WType%n', '%WIndex%n', '%WTotal%n', '%WUsed%n', '%WFree%n', '%WPerc%n');
print_cli_table($table_rows, $headers);
unset($cache_mempool, $mempool, $index, $table_row, $table_rows, $table_headers);
Exemple #3
0
 if ($attribs[$ports_module] || $config[$ports_module] && !isset($attribs[$ports_module])) {
     include "port-etherlike.inc.php";
 }
 // Do ADSL MIB
 $ports_module = 'enable_ports_adsl';
 if ($attribs[$ports_module] || $config[$ports_module] && !isset($attribs[$ports_module])) {
     include "port-adsl.inc.php";
 }
 // Do PoE MIBs
 $ports_module = 'enable_ports_poe';
 if ($attribs[$ports_module] || $config[$ports_module] && !isset($attribs[$ports_module])) {
     include "port-poe.inc.php";
 }
 #    if ($debug || TRUE) { print_vars($port['alert_array']); echo(PHP_EOL); print_vars($this_port);}
 #    print_vars($port['alert_array']);
 check_entity('port', $port, $port['alert_array']);
 // Send statistics array via AMQP/JSON if AMQP is enabled globally and for the ports module
 if ($config['amqp']['enable'] == TRUE && $config['amqp']['modules']['ports']) {
     $json_data = array_merge($this_port, $port['state']);
     unset($json_data['rrd_update']);
     messagebus_send(array('attribs' => array('t' => $polled, 'device' => $device['hostname'], 'device_id' => $device['device_id'], 'e_type' => 'port', 'e_index' => $port['ifIndex']), 'data' => $json_data));
     unset($json_data);
 }
 #    // Do Alcatel Detailed Stats
 #    if ($device['os'] == "aos") { include("port-alcatel.inc.php"); }
 //if ($this_port['ifOperStatus'] == 'down' && $this_port['ifAdminStatus'] == 'up')
 if (isset($port['update']['ifOperStatus']) || isset($port['update']['ifAdminStatus'])) {
     $if_lastchange = $polled;
     // by default set ifLastChange as current polled time
     /** The value of sysUpTime at the time the interface entered
      * its current operational state. If the current state was
function poll_device($device, $options)
{
    global $config, $device, $polled_devices, $db_stats, $exec_status, $alert_rules, $alert_table, $graphs, $attribs;
    $alert_metrics = array();
    $oid_cache = array();
    $old_device_state = unserialize($device['device_state']);
    $attribs = get_entity_attribs('device', $device['device_id']);
    $pid_info = check_process_run($device);
    // This just clear stalled DB entries
    add_process_info($device);
    // Store process info
    $alert_rules = cache_alert_rules();
    $alert_table = cache_device_alert_table($device['device_id']);
    if (OBS_DEBUG > 1 && (count($alert_rules) || count($alert_table))) {
        print_vars($alert_rules);
        print_vars($alert_table);
    }
    $status = 0;
    $device_start = utime();
    // Start counting device poll time
    print_cli_heading($device['hostname'] . " [" . $device['device_id'] . "]", 1);
    print_cli_data("OS", $device['os'], 1);
    if ($config['os'][$device['os']]['group']) {
        $device['os_group'] = $config['os'][$device['os']]['group'];
        print_cli_data("OS Group", $device['os_group'], 1);
    }
    if (is_numeric($device['last_polled_timetaken'])) {
        print_cli_data("Last poll duration", $device['last_polled_timetaken'] . " seconds", 1);
    }
    print_cli_data("Last Polled", $device['last_polled'], 1);
    print_cli_data("SNMP Version", $device['snmp_version'], 1);
    //unset($poll_update); unset($poll_update_query); unset($poll_separator);
    $update_array = array();
    $host_rrd_dir = $config['rrd_dir'] . "/" . $device['hostname'];
    if (!is_dir($host_rrd_dir)) {
        mkdir($host_rrd_dir);
        echo "Created directory : {$host_rrd_dir}\n";
    }
    $flags = OBS_DNS_ALL;
    if ($device['snmp_transport'] == 'udp6' || $device['snmp_transport'] == 'tcp6') {
        $flags = $flags ^ OBS_DNS_A;
    }
    $attribs['ping_skip'] = isset($attribs['ping_skip']) && $attribs['ping_skip'];
    if ($attribs['ping_skip']) {
        $flags = $flags | OBS_PING_SKIP;
        // Add skip ping flag
    }
    $device['pingable'] = isPingable($device['hostname'], $flags);
    if ($device['pingable']) {
        $device['snmpable'] = isSNMPable($device);
        if ($device['snmpable']) {
            $ping_msg = $attribs['ping_skip'] ? '' : 'PING (' . $device['pingable'] . 'ms) and ';
            print_cli_data("Device status", "Device is reachable by " . $ping_msg . "SNMP (" . $device['snmpable'] . "ms)", 1);
            $status = "1";
            $status_type = '';
        } else {
            print_cli_data("Device status", "Device is not responding to SNMP requests", 1);
            $status = "0";
            $status_type = 'snmp';
        }
    } else {
        print_cli_data("Device status", "Device is not responding to PINGs", 1);
        $status = "0";
        $status_type = 'ping';
    }
    if ($device['status'] != $status) {
        dbUpdate(array('status' => $status), 'devices', 'device_id = ?', array($device['device_id']));
        // dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "Device is " .($status == '1' ? 'up' : 'down')), 'alerts');
        $event_msg = 'Device status changed to ';
        if ($status == '1') {
            // Device Up, Severity Warning (4)
            $event_msg .= 'Up';
            $event_severity = 4;
        } else {
            // Device Down, Severity Error (3)!
            $event_msg .= 'Down';
            $event_severity = 3;
        }
        if ($status_type != '') {
            $event_msg .= ' (' . $status_type . ')';
        }
        log_event($event_msg, $device, 'device', $device['device_id'], $event_severity);
    }
    rrdtool_update_ng($device, 'status', array('status' => $status));
    if (!$attribs['ping_skip']) {
        // Ping response RRD database.
        rrdtool_update_ng($device, 'ping', array('ping' => $device['pingable'] ? $device['pingable'] : 'U'));
    }
    // SNMP response RRD database.
    rrdtool_update_ng($device, 'ping_snmp', array('ping_snmp' => $device['snmpable'] ? $device['snmpable'] : 'U'));
    $alert_metrics['device_status'] = $status;
    $alert_metrics['device_status_type'] = $status_type;
    $alert_metrics['device_ping'] = $device['pingable'];
    // FIXME, when ping skipped, here always 0.001
    $alert_metrics['device_snmp'] = $device['snmpable'];
    if ($status == "1") {
        // Arrays for store and check enabled/disabled graphs
        $graphs = array();
        $graphs_db = array();
        foreach (dbFetchRows("SELECT * FROM `device_graphs` WHERE `device_id` = ?", array($device['device_id'])) as $entry) {
            $graphs_db[$entry['graph']] = isset($entry['enabled']) ? (bool) $entry['enabled'] : TRUE;
        }
        if (!$attribs['ping_skip']) {
            // Enable Ping graphs
            $graphs['ping'] = TRUE;
        }
        // Enable SNMP graphs
        $graphs['ping_snmp'] = TRUE;
        // Run these base modules always and before all other modules!
        $poll_modules = array('system', 'os');
        $mods_disabled_global = array();
        $mods_disabled_device = array();
        $mods_excluded = array();
        if ($options['m']) {
            foreach (explode(',', $options['m']) as $module) {
                $module = trim($module);
                if (in_array($module, $poll_modules)) {
                    continue;
                }
                // Skip already added modules
                if ($module == 'unix-agent') {
                    array_unshift($poll_modules, $module);
                    // Add 'unix-agent' before all
                    continue;
                }
                if (is_file($config['install_dir'] . "/includes/polling/{$module}.inc.php")) {
                    $poll_modules[] = $module;
                }
            }
        } else {
            foreach ($config['poller_modules'] as $module => $module_status) {
                if (in_array($module, $poll_modules)) {
                    continue;
                }
                // Skip already added modules
                if ($attribs['poll_' . $module] || $module_status && !isset($attribs['poll_' . $module])) {
                    if (poller_module_excluded($device, $module)) {
                        $mods_excluded[] = $module;
                        //print_warning("Module [ $module ] excluded for device.");
                        continue;
                    }
                    if ($module == 'unix-agent') {
                        array_unshift($poll_modules, $module);
                        // Add 'unix-agent' before all
                        continue;
                    }
                    if (is_file($config['install_dir'] . "/includes/polling/{$module}.inc.php")) {
                        $poll_modules[] = $module;
                    }
                } elseif (isset($attribs['poll_' . $module]) && !$attribs['poll_' . $module]) {
                    $mods_disabled_device[] = $module;
                    //print_warning("Module [ $module ] disabled on device.");
                } else {
                    $mods_disabled_global[] = $module;
                    //print_warning("Module [ $module ] disabled globally.");
                }
            }
        }
        if (count($mods_excluded)) {
            print_cli_data("Modules Excluded", implode(", ", $mods_excluded), 1);
        }
        if (count($mods_disabled_global)) {
            print_cli_data("Disabled Globally", implode(", ", $mods_disabled_global), 1);
        }
        if (count($mods_disabled_device)) {
            print_cli_data("Disabled Device", implode(", ", $mods_disabled_global), 1);
        }
        if (count($poll_modules)) {
            print_cli_data("Modules Enabled", implode(", ", $poll_modules), 1);
        }
        echo PHP_EOL;
        foreach ($poll_modules as $module) {
            print_debug(PHP_EOL . "including: includes/polling/{$module}.inc.php");
            print_cli_heading("Module Start: %R" . $module . "");
            $m_start = utime();
            include $config['install_dir'] . "/includes/polling/{$module}.inc.php";
            $m_end = utime();
            $m_run = round($m_end - $m_start, 4);
            $device_state['poller_mod_perf'][$module] = $m_run;
            print_cli_data("Module time", number_format($m_run, 4) . "s");
            echo PHP_EOL;
        }
        print_cli_heading($device['hostname'] . " [" . $device['device_id'] . "] completed poller modules at " . date("Y-m-d H:i:s"), 1);
        // Check and update graphs DB
        $graphs_stat = array();
        if (!isset($options['m'])) {
            // Hardcoded poller performance
            $graphs['poller_perf'] = TRUE;
            // Delete not exists graphs from DB (only if poller run without modules option)
            foreach ($graphs_db as $graph => $value) {
                if (!isset($graphs[$graph])) {
                    dbDelete('device_graphs', "`device_id` = ? AND `graph` = ?", array($device['device_id'], $graph));
                    unset($graphs_db[$graph]);
                    $graphs_stat['deleted'][] = $graph;
                }
            }
        }
        // Add or update graphs in DB
        foreach ($graphs as $graph => $value) {
            if (!isset($graphs_db[$graph])) {
                dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph, 'enabled' => $value), 'device_graphs');
                $graphs_stat['added'][] = $graph;
            } else {
                if ($value != $graphs_db[$graph]) {
                    dbUpdate(array('enabled' => $value), 'device_graphs', '`device_id` = ? AND `graph` = ?', array($device['device_id'], $graph));
                    $graphs_stat['updated'][] = $graph;
                } else {
                    $graphs_stat['checked'][] = $graph;
                }
            }
        }
        // Print graphs stats
        foreach ($graphs_stat as $key => $stat) {
            if (count($stat)) {
                print_cli_data('Graphs [' . $key . ']', implode(', ', $stat), 1);
            }
        }
        $device_end = utime();
        $device_run = $device_end - $device_start;
        $device_time = round($device_run, 4);
        $update_array['last_polled'] = array('NOW()');
        $update_array['last_polled_timetaken'] = $device_time;
        $update_array['device_state'] = serialize($device_state);
        #echo("$device_end - $device_start; $device_time $device_run");
        print_cli_data("Poller time", $device_time . " seconds", 1);
        //print_message(PHP_EOL."Polled in $device_time seconds");
        // Only store performance data if we're not doing a single-module poll
        if (!$options['m']) {
            dbInsert(array('device_id' => $device['device_id'], 'operation' => 'poll', 'start' => $device_start, 'duration' => $device_run), 'devices_perftimes');
            rrdtool_update_ng($device, 'perf-poller', array('val' => $device_time));
        }
        if (OBS_DEBUG) {
            echo "Updating " . $device['hostname'] . " - ";
            print_vars($update_array);
            echo " \n";
        }
        $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id']));
        if ($updated) {
            print_cli_data("Updated Data", implode(", ", array_keys($update_array)), 1);
        }
        $alert_metrics['device_uptime'] = $device['uptime'];
        $alert_metrics['device_rebooted'] = $rebooted;
        // 0 - not rebooted, 1 - rebooted
        $alert_metrics['device_duration_poll'] = $device['last_polled_timetaken'];
        unset($cache_storage);
        // Clear cache of hrStorage ** MAYBE FIXME? ** (ok, later)
        unset($cache);
        // Clear cache (unify all things here?)
    }
    check_entity('device', $device, $alert_metrics);
    echo PHP_EOL;
    // Clean
    del_process_info($device);
    // Remove process info
    unset($alert_metrics);
}
Exemple #5
0
     } else {
         $sla_state['sla_id'] = $sla['sla_id'];
         dbInsert($sla_state, 'slas-state');
     }
     // Check alerts
     $metrics = array();
     $metrics['rtt_value'] = $sla_state['rtt_value'];
     $metrics['rtt_sense'] = $sla_state['rtt_sense'];
     $metrics['rtt_sense_uptime'] = $sla_polled_time - $sla_state['rtt_last_change'];
     $metrics['rtt_event'] = $sla_state['rtt_event'];
     $metrics['rtt_minimum'] = $sla_state['rtt_minimum'];
     $metrics['rtt_maximum'] = $sla_state['rtt_maximum'];
     $metrics['rtt_success'] = $sla_state['rtt_success'];
     $metrics['rtt_loss'] = $sla_state['rtt_loss'];
     $metrics['rtt_loss_percent'] = $rtt_loss_percent;
     check_entity('sla', $sla, $metrics);
     //echo("SLA " . $sla['sla_index'] . ": " . $sla['rtt_type'] . " " . $sla['sla_owner'] . " " . $sla['sla_tag']. "... ");
     //echo($sla_state['rtt_value'] . 'ms at ' . format_unixtime($sla_state['rtt_unixtime']) . ', Sense code - "' . $sla_state['rtt_sense'] . '"');
     $table_row = array();
     $table_row[] = "SLA " . $sla['sla_index'];
     $table_row[] = $sla['sla_mib'];
     $table_row[] = $sla['rtt_type'];
     $table_row[] = $sla['sla_owner'];
     $table_row[] = $sla['sla_tag'];
     $table_row[] = $sla_state['rtt_sense'];
     $table_row[] = $sla_state['rtt_value'] . 'ms';
     $table_rows[] = $table_row;
     unset($table_row);
 } else {
     echo 'NaN';
     $rrd_value = 'U';
             } else {
                 $rrdupdate .= ":U";
             }
         }
         echo str_pad($svc['svcServiceName'], 25) . " | " . str_pad($svc['svcServiceType'], 15) . " | " . str_pad($svc['svcState'], 6) . " | " . str_pad($svc['svcIpAddress'], 16) . " | " . str_pad($svc['svcPort'], 5);
         echo " | " . str_pad($svc['svcRequestRate'], 8) . " | " . str_pad($svc['svcRxBytesRate'] . "B/s", 8) . " | " . str_pad($svc['svcTxBytesRate'] . "B/s", 8);
         $db_update = array('svc_label' => $svc['label'], 'svc_fullname' => $svc['svcServiceFullName'], 'svc_ip' => $svc['svcIpAddress'], 'svc_port' => $svc['svcPort'], 'svc_state' => $svc['svcState'], 'svc_type' => $svc['svcServiceType'], 'svc_req_rate' => $svc['RequestRate'], 'svc_bps_in' => $svc['svcRxBytesRate'], 'svc_bps_out' => $svc['svcTxBytesRate']);
         if (!is_array($svcs[$svc['svcServiceName']])) {
             $db_insert = array_merge(array('device_id' => $device['device_id'], 'svc_name' => $svc['svcServiceName']), $db_update);
             $svc_id = dbInsert($db_insert, 'netscaler_services');
             echo " +";
         } else {
             $updated = dbUpdate($db_update, 'netscaler_services', '`svc_id` = ?', array($svcs[$svc['svcServiceName']]['svc_id']));
             echo " U";
             // Check Alerts
             check_entity('netscaler_svc', $svcs[$svc['svcServiceName']], array('svc_state' => $svc['svcState'], 'svc_bps_in' => $svc['svcRxBytesRate'], 'svc_bps_out' => $svc['svcTxBytesRate']));
         }
         rrdtool_create($device, $rrd_file, $rrd_create);
         rrdtool_update($device, $rrd_file, $rrdupdate);
         echo "\n";
     }
 }
 if (OBS_DEBUG && count($svc_exist)) {
     print_vars($svc_exist);
 }
 foreach ($svcs as $db_name => $db_id) {
     if (!$svc_exist[$db_name]) {
         echo "-" . $db_name;
         dbDelete('netscaler_services', "`svc_id` =  ?", array($db_id));
     }
 }
        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));
}
             } else {
                 $rrd_update .= ":U";
             }
         }
         echo str_pad($svcgrpmem['svcGrpMemberName'], 25) . " | " . str_pad($svcgrpmem['svcGrpMemberServiceType'], 15) . " | " . str_pad($svcgrpmem['svcGrpMemberState'], 12) . " | " . str_pad($svcgrpmem['svcGrpMemberPrimaryIPAddress'], 16) . " | " . str_pad($svcgrpmem['svcGrpMemberPrimaryPort'], 5);
         echo " | " . str_pad($svcgrpmem['svcGrpMemberRequestRate'], 8) . " | " . str_pad($svcgrpmem['svcGrpMemberRxBytesRate'] . "B/s", 8) . " | " . str_pad($svcgrpmem['svcGrpMemberTxBytesRate'] . "B/s", 8);
         $db_update = array('svc_label' => $svcgrpmem['label'], 'svc_fullname' => $svcgrpmem['svcGrpMemberFullName'], 'svc_ip' => $svcgrpmem['svcGrpMemberPrimaryIPAddress'], 'svc_port' => $svcgrpmem['svcGrpMemberPrimaryPort'], 'svc_state' => $svcgrpmem['svcGrpMemberState'], 'svc_type' => $svcgrpmem['svcGrpMemberType'], 'svc_req_rate' => $svcgrpmem['svcGrpMemberRequestRate'], 'svc_bps_in' => $svcgrpmem['svcGrpMemberRxBytesRate'], 'svc_bps_out' => $svcgrpmem['svcGrpMemberTxBytesRate']);
         if (!is_array($svcgrpmems[$svcgrpmem['svcGrpMemberName']])) {
             $db_insert = array_merge(array('device_id' => $device['device_id'], 'svc_name' => $svcgrpmem['svcGrpMemberName']), $db_update);
             $svcgrpmem_id = dbInsert($db_insert, 'netscaler_servicegroupmembers');
             echo " +";
         } else {
             $updated = dbUpdate($db_update, 'netscaler_servicegroupmembers', '`svc_id` = ?', array($svcgrpmems[$svcgrpmem['svcGrpMemberName']]['svc_id']));
             echo " U";
             // Check Alerts
             check_entity('netscalersvcgrpmem', $svcgrpmems[$svcgrpmem['svcGrpMemberName']], array('svc_state' => $svcgrpmem['svcGrpMemberState'], 'svc_conn_active' => $svcgrpmem['svcGrpMemberActiveConn'], 'svc_trans_active' => $svcgrpmem['svcGrpMemberActiveTransactions'], 'svc_trans_avgtime' => $svcgrpmem['svcGrpMemberAvgTransactionTime'], 'svc_svr_avgttfb' => $svcgrpmem['svcGrpMemberAvgSvrTTFB'], 'svc_conn_client' => $svcgrpmem['svcGrpMemberCurClntConnections'], 'svc_bps_in' => $svcgrpmem['svcGrpMemberRxBytesRate'], 'svc_bps_out' => $svcgrpmem['svcGrpMemberTxBytesRate']));
         }
         rrdtool_create($device, $rrd_file, $rrd_create);
         rrdtool_update($device, $rrd_file, $rrd_update);
         echo "\n";
     }
 }
 if (OBS_DEBUG && count($svcgrpmem_exist)) {
     print_vars($svcgrpmem_exist);
 }
 foreach ($svcgrpmems as $db_name => $db_svcgrp) {
     if (!$svcgrpmem_exist[$db_name]) {
         echo "-" . $db_name . "\n";
         dbDelete('netscaler_servicegroupmembers', "`svc_id` =  ?", array($db_svcgrp['svc_id']));
     }
 }
        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;
}
unset($storage);
// EOF
         } else {
             // If status not changed, leave old last_change
             $pw_poll['last_change'] = $pw['last_change'];
         }
     }
 }
 // Metrics
 $metrics = array();
 $metrics['pwOperStatus'] = $pw_poll[$oids['OperStatus']['oid']];
 $metrics['pwRemoteStatus'] = $pw_poll[$oids['RemoteStatus']['oid']];
 $metrics['pwLocalStatus'] = $pw_poll[$oids['LocalStatus']['oid']];
 $metrics['event'] = $pw_poll['event'];
 $metrics['pwUptime'] = $pw_uptime;
 $metrics['last_change'] = $pw_poll['last_change'];
 // Check entity
 check_entity('pseudowire', $pw, $metrics);
 // Update SQL State
 if (is_numeric($pw['pwUptime'])) {
     dbUpdate($metrics, 'pseudowires-state', '`pseudowire_id` = ?', array($pw['pseudowire_id']));
 } else {
     $metrics['pseudowire_id'] = $pw['pseudowire_id'];
     dbInsert($metrics, 'pseudowires-state');
 }
 // Add table row
 $table_row = array();
 $table_row[] = $pw['pwID'];
 $table_row[] = $pw['mib'];
 $table_row[] = $pw['pwType'];
 $table_row[] = $pw['pwPsnType'];
 $table_row[] = $pw['peer_addr'];
 $table_row[] = $metrics['pwOperStatus'];
            }
            break;
        case 'wastetoner':
            $old_rrd = 'wastebox.rrd';
            rename_rrd($device, $old_rrd, $supply_rrd);
            break;
        case 'fuser':
            $old_rrd = 'fuser.rrd';
            rename_rrd($device, $old_rrd, $supply_rrd);
            break;
    }
    // END CLEANME
    echo $supplyperc . " %\n";
    rrdtool_update_ng($device, 'toner', array('level' => $supplyperc), $supply['supply_index']);
    if ($supplyperc > $supply['supply_value']) {
        log_event('Printer supply ' . $supply['supply_descr'] . ' (type ' . nicecase($supply['supply_type']) . ') was replaced (new level: ' . $supplyperc . '%)', $device, 'toner', $supply['supply_id']);
    }
    dbUpdate(array('supply_value' => $supplyperc, 'supply_capacity' => $supply['supply_capacity']), 'printersupplies', '`supply_id` = ?', array($supply['supply_id']));
    check_entity('printersupply', $supply, array('supply_value' => $supplyperc));
    $graphs['printersupplies'] = TRUE;
}
// Old stuff, to replace?
$oid = get_dev_attrib($device, 'pagecount_oid');
if ($oid) {
    echo "Checking page count... ";
    $pages = snmp_get($device, $oid, "-OUqnv");
    set_dev_attrib($device, "pagecounter", $pages);
    rrdtool_update_ng($device, 'pagecount', array('pagecount' => $pages));
    echo "{$pages}\n";
}
// EOF
Exemple #12
0
 *
 */
if ($config['enable_printers']) {
    $toner_data = dbFetchRows("SELECT * FROM toner WHERE device_id = ?", array($device['device_id']));
    foreach ($toner_data as $toner) {
        echo "Checking toner " . $toner['toner_descr'] . "... ";
        $tonerperc = round(snmp_get($device, $toner['toner_oid'], "-OUqnv") / $toner['toner_capacity'] * 100);
        $tonerrrd = "toner-" . $toner['toner_index'] . ".rrd";
        rrdtool_create($device, $tonerrrd, " \\\n      DS:toner:GAUGE:600:0:20000 ");
        echo $tonerperc . " %\n";
        rrdtool_update($device, $tonerrrd, "N:{$tonerperc}");
        if ($tonerperc > $toner['toner_current']) {
            log_event('Toner ' . $toner['toner_descr'] . ' was replaced (new level: ' . $tonerperc . '%)', $device, 'toner', $toner['toner_id']);
        }
        dbUpdate(array('toner_current' => $tonerperc, 'toner_capacity' => $toner['toner_capacity']), 'toner', '`toner_id` = ?', array($toner['toner_id']));
        check_entity('toner', $toner, array('toner_current' => $tonerperc));
    }
    if ($device['type'] == 'printer') {
        $oid = get_dev_attrib($device, 'pagecount_oid');
        if ($oid) {
            echo "Checking page count... ";
            $pages = snmp_get($device, $oid, "-OUqnv");
            $pagecountrrd = "pagecount.rrd";
            rrdtool_create($device, $pagecountrrd, " \\\n        DS:pagecount:GAUGE:600:0:U ");
            set_dev_attrib($device, "pagecounter", $pages);
            rrdtool_update($device, $pagecountrrd, "N:{$pages}");
            echo "{$pages}\n";
        }
        $oid = get_dev_attrib($device, 'imagingdrum_oid');
        if ($oid) {
            echo "Checking Imaging Drum... ";
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_device($device, $options)
{
    global $config, $device, $polled_devices, $db_stats, $memcache, $exec_status, $alert_rules, $alert_table, $graphs, $attribs;
    $alert_metrics = array();
    $oid_cache = array();
    $old_device_state = unserialize($device['device_state']);
    $attribs = get_dev_attribs($device['device_id']);
    $alert_rules = cache_alert_rules();
    $alert_table = cache_device_alert_table($device['device_id']);
    if (OBS_DEBUG > 1 && (count($alert_rules) || count($alert_table))) {
        print_vars($alert_rules);
        print_vars($alert_table);
    }
    $status = 0;
    unset($array);
    $device_start = utime();
    // Start counting device poll time
    echo $device['hostname'] . " " . $device['device_id'] . " " . $device['os'] . " ";
    if ($config['os'][$device['os']]['group']) {
        $device['os_group'] = $config['os'][$device['os']]['group'];
        echo "(" . $device['os_group'] . ")";
    }
    echo "\n";
    unset($poll_update);
    unset($poll_update_query);
    unset($poll_separator);
    $poll_update_array = array();
    $host_rrd_dir = $config['rrd_dir'] . "/" . $device['hostname'];
    if (!is_dir($host_rrd_dir)) {
        mkdir($host_rrd_dir);
        echo "创建的目录 : {$host_rrd_dir}\n";
    }
    $try_a = !($device['snmp_transport'] == 'udp6' || $device['snmp_transport'] == 'tcp6');
    // Use IPv6 only if transport 'udp6' or 'tcp6'
    $device['pingable'] = isPingable($device['hostname'], $try_a);
    if ($device['pingable']) {
        $device['snmpable'] = isSNMPable($device);
        if ($device['snmpable']) {
            $status = "1";
            $status_type = '';
        } else {
            echo "SNMP 无法访问";
            $status = "0";
            $status_type = 'snmp';
        }
    } else {
        echo "Unpingable";
        $status = "0";
        $status_type = 'ping';
    }
    if ($device['status'] != $status) {
        $poll_update .= $poll_separator . "`status` = '{$status}'";
        $poll_separator = ", ";
        dbUpdate(array('status' => $status), 'devices', 'device_id = ?', array($device['device_id']));
        dbInsert(array('importance' => '0', 'device_id' => $device['device_id'], 'message' => "设备的 " . ($status == '1' ? 'up' : 'down')), 'alerts');
        $event_msg = '设备状态变更为 ';
        if ($status == '1') {
            // Device Up, Severity Warning (4)
            $event_msg .= 'Up';
            $event_severity = 4;
        } else {
            // Device Down, Severity Error (3)!
            $event_msg .= 'Down';
            $event_severity = 3;
        }
        if ($status_type != '') {
            $event_msg .= ' (' . $status_type . ')';
        }
        log_event($event_msg, $device, 'device', $device['device_id'], $event_severity);
    }
    $rrd_filename = "status.rrd";
    rrdtool_create($device, $rrd_filename, "DS:status:GAUGE:600:0:1 ");
    if ($status == "1" || $status == "0") {
        rrdtool_update($device, $rrd_filename, "N:" . $status);
    } else {
        rrdtool_update($device, $rrd_filename, "N:U");
    }
    // Ping response RRD database.
    $ping_rrd = 'ping.rrd';
    rrdtool_create($device, $ping_rrd, "DS:ping:GAUGE:600:0:65535 ");
    if ($device['pingable']) {
        rrdtool_update($device, $ping_rrd, "N:" . $device['pingable']);
    } else {
        rrdtool_update($device, $ping_rrd, "N:U");
    }
    // SNMP response RRD database.
    $ping_snmp_rrd = 'ping_snmp.rrd';
    rrdtool_create($device, $ping_snmp_rrd, "DS:ping_snmp:GAUGE:600:0:65535 ");
    if ($device['snmpable']) {
        rrdtool_update($device, $ping_snmp_rrd, "N:" . $device['snmpable']);
    } else {
        rrdtool_update($device, $ping_snmp_rrd, "N:U");
    }
    $alert_metrics['device_status'] = $status;
    $alert_metrics['device_status_type'] = $status_type;
    $alert_metrics['device_ping'] = $device['pingable'];
    $alert_metrics['device_snmp'] = $device['snmpable'];
    if ($status == "1") {
        // Arrays for store and check enabled/disabled graphs
        $graphs = array();
        $graphs_db = array();
        foreach (dbFetchRows("SELECT * FROM `device_graphs` WHERE `device_id` = ?", array($device['device_id'])) as $entry) {
            $graphs_db[$entry['graph']] = isset($entry['enabled']) ? (bool) $entry['enabled'] : TRUE;
        }
        // Enable Ping graphs
        $graphs['ping'] = TRUE;
        // Enable SNMP graphs
        $graphs['ping_snmp'] = TRUE;
        // Run these base modules always and before all other modules!
        $poll_modules = array('system', 'os');
        if ($options['m']) {
            foreach (explode(',', $options['m']) as $module) {
                $module = trim($module);
                if (in_array($module, $poll_modules)) {
                    continue;
                }
                // Skip already added modules
                if ($module == 'unix-agent') {
                    array_unshift($poll_modules, $module);
                    // Add 'unix-agent' before all
                    continue;
                }
                if (is_file($config['install_dir'] . "/includes/polling/{$module}.inc.php")) {
                    $poll_modules[] = $module;
                }
            }
        } else {
            foreach ($config['poller_modules'] as $module => $module_status) {
                if (in_array($module, $poll_modules)) {
                    continue;
                }
                // Skip already added modules
                if ($attribs['poll_' . $module] || $module_status && !isset($attribs['poll_' . $module])) {
                    if (poller_module_excluded($device, $module)) {
                        print_warning("模块 [ {$module} ] 排除设备.");
                        continue;
                    }
                    if ($module == 'unix-agent') {
                        array_unshift($poll_modules, $module);
                        // Add 'unix-agent' before all
                        continue;
                    }
                    if (is_file($config['install_dir'] . "/includes/polling/{$module}.inc.php")) {
                        $poll_modules[] = $module;
                    }
                } elseif (isset($attribs['poll_' . $module]) && !$attribs['poll_' . $module]) {
                    print_warning("模块 [ {$module} ] 禁用设备.");
                } else {
                    print_warning("模块 [ {$module} ] 禁用全局.");
                }
            }
        }
        foreach ($poll_modules as $module) {
            print_debug(PHP_EOL . "including: includes/polling/{$module}.inc.php");
            $m_start = utime();
            include $config['install_dir'] . "/includes/polling/{$module}.inc.php";
            $m_end = utime();
            $m_run = round($m_end - $m_start, 4);
            $device_state['poller_mod_perf'][$module] = number_format($m_run, 4);
            print_message("Module [ {$module} ] time: {$m_run}" . "s");
        }
        // Fields to notify about in event log - FIXME should move to definitions?
        $update_fields = array('version', 'features', 'hardware', 'serial', 'kernel', 'distro', 'distro_ver', 'arch', 'asset_tag');
        // Log changed variables
        foreach ($update_fields as $field) {
            if (isset(${$field}) && ${$field} != $device[$field]) {
                $update_array[$field] = ${$field};
                log_event(ucfirst($field) . " -> " . $update_array[$field], $device, 'device', $device['device_id']);
            }
        }
        // Here additional fields, change only if not set already
        foreach (array('type', 'icon') as $field) {
            if (isset(${$field}) && ($device[$field] == "unknown" || $device[$field] == '')) {
                $update_array[$field] = ${$field};
                log_event(ucfirst($field) . " -> " . $update_array[$field], $device, 'device', $device['device_id']);
            }
        }
        // Check and update graphs DB
        $graphs_stat = array();
        if (!isset($options['m'])) {
            // Hardcoded poller performance
            $graphs['poller_perf'] = TRUE;
            // Delete not exists graphs from DB (only if poller run without modules option)
            foreach ($graphs_db as $graph => $value) {
                if (!isset($graphs[$graph])) {
                    dbDelete('device_graphs', "`device_id` = ? AND `graph` = ?", array($device['device_id'], $graph));
                    unset($graphs_db[$graph]);
                    $graphs_stat['deleted'][] = $graph;
                }
            }
        }
        // Add or update graphs in DB
        foreach ($graphs as $graph => $value) {
            if (!isset($graphs_db[$graph])) {
                dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph, 'enabled' => $value), 'device_graphs');
                $graphs_stat['added'][] = $graph;
            } else {
                if ($value != $graphs_db[$graph]) {
                    dbUpdate(array('enabled' => $value), 'device_graphs', '`device_id` = ? AND `graph` = ?', array($device['device_id'], $graph));
                    $graphs_stat['updated'][] = $graph;
                } else {
                    $graphs_stat['checked'][] = $graph;
                }
            }
        }
        // Print graphs stats
        foreach ($graphs_stat as $key => $stat) {
            if (count($stat)) {
                echo ' Graphs [' . $key . ']: ' . implode(', ', $stat) . PHP_EOL;
            }
        }
        $device_end = utime();
        $device_run = $device_end - $device_start;
        $device_time = round($device_run, 4);
        $update_array['last_polled'] = array('NOW()');
        $update_array['last_polled_timetaken'] = $device_time;
        $update_array['device_state'] = serialize($device_state);
        #echo("$device_end - $device_start; $device_time $device_run");
        print_message(PHP_EOL . "Polled in {$device_time} seconds");
        // Only store performance data if we're not doing a single-module poll
        if (!$options['m']) {
            dbInsert(array('device_id' => $device['device_id'], 'operation' => 'poll', 'start' => $device_start, 'duration' => $device_run), 'devices_perftimes');
            $poller_rrd = "perf-poller.rrd";
            rrdtool_create($device, $poller_rrd, "DS:val:GAUGE:600:0:38400 ");
            rrdtool_update($device, $poller_rrd, "N:" . $device_time);
        }
        if (OBS_DEBUG) {
            echo "更新 " . $device['hostname'] . " - ";
            print_vars($update_array);
            echo " \n";
        }
        $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id']));
        if ($updated) {
            echo "已更新!\n";
        }
        $alert_metrics['device_uptime'] = $device['uptime'];
        $alert_metrics['device_rebooted'] = $rebooted;
        // 0 - not rebooted, 1 - rebooted
        $alert_metrics['device_duration_poll'] = $device['last_polled_timetaken'];
        unset($cache_storage);
        // Clear cache of hrStorage ** MAYBE FIXME? ** (ok, later)
        unset($cache);
        // Clear cache (unify all things here?)
    }
    check_entity('device', $device, $alert_metrics);
    unset($alert_metrics);
}
             } else {
                 $rrdupdate .= ":U";
             }
         }
         echo str_pad($svc['svcServiceName'], 25) . " | " . str_pad($svc['svcServiceType'], 15) . " | " . str_pad($svc['svcState'], 6) . " | " . str_pad($svc['svcIpAddress'], 16) . " | " . str_pad($svc['svcPort'], 5);
         echo " | " . str_pad($svc['svcRequestRate'], 8) . " | " . str_pad($svc['svcRxBytesRate'] . "B/s", 8) . " | " . str_pad($svc['svcTxBytesRate'] . "B/s", 8);
         $db_update = array('svc_label' => $svc['label'], 'svc_fullname' => $svc['svcServiceFullName'], 'svc_ip' => $svc['svcIpAddress'], 'svc_port' => $svc['svcPort'], 'svc_state' => $svc['svcState'], 'svc_type' => $svc['svcServiceType'], 'svc_req_rate' => $svc['RequestRate'], 'svc_bps_in' => $svc['svcRxBytesRate'], 'svc_bps_out' => $svc['svcTxBytesRate']);
         if (!is_array($svcs[$svc['svcServiceName']])) {
             $db_insert = array_merge(array('device_id' => $device['device_id'], 'svc_name' => $svc['svcServiceName']), $db_update);
             $svc_id = dbInsert($db_insert, 'netscaler_services');
             echo " +";
         } else {
             $updated = dbUpdate($db_update, 'netscaler_services', '`svc_id` = ?', array($svcs[$svc['svcServiceName']]['svc_id']));
             echo " U";
             // Check Alerts
             check_entity('netscaler_svc', $svcs[$svc['svcServiceName']], array('svc_state' => $svc['svcState']));
         }
         if (!file_exists($rrd_file)) {
             rrdtool_create($rrd_file, $rrd_create);
         }
         rrdtool_update($rrd_file, $rrdupdate);
         echo "\n";
     }
 }
 if ($debug) {
     print_vars($svc_exist);
 }
 foreach ($svcs as $db_name => $db_id) {
     if (!$svc_exist[$db_name]) {
         echo "-" . $db_name;
         dbDelete('netscaler_services', "`svc_id` =  ?", array($db_id));