Example #1
0
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);
}
function poll_device($device, $options)
{
    global $config, $debug, $device, $polled_devices, $db_stats, $memcache, $exec_status, $alert_rules, $alert_table;
    $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 ($debug && (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 = $config['rrd_dir'] . "/" . $device['hostname'];
    if (!is_dir($host_rrd)) {
        mkdir($host_rrd);
        echo "Created directory : {$host_rrd}\n";
    }
    $device['pingable'] = isPingable($device['hostname']);
    if ($device['pingable']) {
        $device['snmpable'] = isSNMPable($device);
        if ($device['snmpable']) {
            $status = "1";
            $status_type = '';
        } else {
            echo "SNMP Unreachable";
            $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' => "Device is " . ($status == '1' ? 'up' : 'down')), 'alerts');
        log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down') . $status_type, $device, 'system');
        notify($device, "Device " . ($status == '1' ? 'Up' : 'Down') . ": " . $device['hostname'] . $status_type, "Device " . ($status == '1' ? 'up' : 'down') . ": " . $device['hostname']);
    }
    $rrd = $config['rrd_dir'] . "/" . $device['hostname'] . "/status.rrd";
    if (!is_file($rrd)) {
        rrdtool_create($rrd, "DS:status:GAUGE:600:0:1 ");
    }
    if ($status == "1" || $status == "0") {
        rrdtool_update($rrd, "N:" . $status);
    } else {
        rrdtool_update($rrd, "N:U");
    }
    // Ping response RRD database.
    $ping_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping.rrd';
    if (!is_file($ping_rrd)) {
        rrdtool_create($ping_rrd, "DS:ping:GAUGE:600:0:65535 ");
    }
    if ($device['pingable']) {
        rrdtool_update($ping_rrd, "N:" . $device['pingable']);
    } else {
        rrdtool_update($ping_rrd, "N:U");
    }
    // SNMP response RRD database.
    $ping_snmp_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/ping_snmp.rrd';
    if (!is_file($ping_snmp_rrd)) {
        rrdtool_create($ping_snmp_rrd, "DS:ping_snmp:GAUGE:600:0:65535 ");
    }
    if ($device['snmpable']) {
        rrdtool_update($ping_snmp_rrd, "N:" . $device['snmpable']);
    } else {
        rrdtool_update($ping_snmp_rrd, "N:U");
    }
    if ($status == "1") {
        $graphs = array();
        $oldgraphs = array();
        // Enable Ping graphs
        $graphs['ping'] = TRUE;
        // Enable SNMP graphs
        $graphs['ping_snmp'] = TRUE;
        // Run this 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 [ {$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]) {
                    print_warning("Module [ {$module} ] disabled on device.");
                } else {
                    print_warning("Module [ {$module} ] disabled globally.");
                }
            }
        }
        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 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', 'icon');
        // 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, 'system');
            }
        }
        if (!isset($options['m'])) {
            // FIXME EVENTLOGGING -- MAKE IT SO WE DO THIS PER-MODULE?
            // This code cycles through the graphs already known in the database and the ones we've defined as being polled here
            // If there any don't match, they're added/deleted from the database.
            // Ideally we should hold graphs for xx days/weeks/polls so that we don't needlessly hide information.
            // Hardcoded poller performance
            $graphs['poller_perf'] = TRUE;
            foreach (dbFetch("SELECT `graph` FROM `device_graphs` WHERE `device_id` = ?", array($device['device_id'])) as $graph) {
                if (!isset($graphs[$graph["graph"]])) {
                    dbDelete('device_graphs', "`device_id` = ? AND `graph` = ?", array($device['device_id'], $graph["graph"]));
                } else {
                    $oldgraphs[$graph["graph"]] = TRUE;
                }
            }
            foreach ($graphs as $graph => $value) {
                if (!isset($oldgraphs[$graph])) {
                    echo "+";
                    dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph), 'device_graphs');
                }
                echo $graph . " ";
            }
        }
        $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");
        echo "Polled in {$device_time} seconds\n";
        // 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 = $config['rrd_dir'] . "/" . $device['hostname'] . "/perf-poller.rrd";
            if (!is_file($poller_rrd)) {
                rrdtool_create($poller_rrd, "DS:val:GAUGE:600:0:38400 ");
            }
            rrdtool_update($poller_rrd, "N:" . $device_time);
        }
        if ($debug) {
            echo "Updating " . $device['hostname'] . " - ";
            print_vars($update_array);
            echo " \n";
        }
        $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id']));
        if ($updated) {
            echo "UPDATED!\n";
        }
        unset($cache_storage);
        // Clear cache of hrStorage ** MAYBE FIXME? ** (ok, later)
        unset($cache);
        // Clear cache (unify all things here?)
    }
}
Example #3
0
      <th style="width: 80px;"></th>
    </tr>
  </thead>
  <tbody>

<?php 
foreach ($config['poller_modules'] as $module => $module_status) {
    $attrib_set = isset($attribs['poll_' . $module]);
    echo '<tr><td><strong>' . $module . '</strong></td><td>';
    echo $module_status ? '<span class="text-success">enabled</span>' : '<span class="text-danger">disabled</span>';
    echo '</td><td>';
    $attrib_status = '<span class="text-danger">disabled</span>';
    $toggle = 'Enable';
    $btn_class = 'btn-success';
    $btn_toggle = 'value="Toggle"';
    if (poller_module_excluded($device, $module)) {
        $attrib_status = '<span class="text-disabled">excluded</span>';
        $toggle = "Excluded";
        $btn_class = '';
        $btn_toggle = 'disabled="disabled"';
    } elseif ($attrib_set && $attribs['poll_' . $module] || !$attrib_set && $module_status) {
        $attrib_status = '<span class="text-success">enabled</span>';
        $toggle = "Disable";
        $btn_class = "btn-danger";
    }
    echo $attrib_status . '</td><td>';
    echo '<form id="toggle_poller" name="toggle_poller" method="post" action="">
  <input type="hidden" name="toggle_poller" value="' . $module . '">
  <button type="submit" class="btn btn-mini ' . $btn_class . '" name="Submit" ' . $btn_toggle . '>' . $toggle . '</button>
</form>';
    echo '</td></tr>';
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);
}