Пример #1
0
function device_by_id_cache($device_id, $refresh = '0')
{
    global $cache;
    if (!$refresh && isset($cache['devices']['id'][$device_id]) && is_array($cache['devices']['id'][$device_id])) {
        $device = $cache['devices']['id'][$device_id];
    } else {
        $device = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($device_id));
        $device['ip'] = inet6_ntop($device['ip']);
        $cache['devices']['id'][$device_id] = $device;
    }
    return $device;
}
Пример #2
0
function device_by_id_cache($device_id, $refresh = '0')
{
    global $cache;
    if (!$refresh && isset($cache['devices']['id'][$device_id]) && is_array($cache['devices']['id'][$device_id])) {
        $device = $cache['devices']['id'][$device_id];
    } else {
        $device = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($device_id));
        //order vrf_lite_cisco with context, this will help to get the vrf_name and instance_name all the time
        $vrfs_lite_cisco = dbFetchRows("SELECT * FROM `vrf_lite_cisco` WHERE `device_id` = ?", array($device_id));
        $device['vrf_lite_cisco'] = array();
        if (!empty($vrfs_lite_cisco)) {
            foreach ($vrfs_lite_cisco as $vrf) {
                $device['vrf_lite_cisco'][$vrf['context_name']] = $vrf;
            }
        }
        $device['ip'] = inet6_ntop($device['ip']);
        $cache['devices']['id'][$device_id] = $device;
    }
    return $device;
}
Пример #3
0
function list_devices()
{
    // This will return a list of devices
    global $config;
    $app = \Slim\Slim::getInstance();
    $order = $_GET['order'];
    $type = $_GET['type'];
    $query = mres($_GET['query']);
    $param = array();
    $join = '';
    if (empty($order)) {
        $order = 'hostname';
    }
    if (stristr($order, ' desc') === false && stristr($order, ' asc') === false) {
        $order = '`' . $order . '` ASC';
    }
    if ($type == 'all' || empty($type)) {
        $sql = '1';
    } elseif ($type == 'ignored') {
        $sql = "`ignore`='1' AND `disabled`='0'";
    } elseif ($type == 'up') {
        $sql = "`status`='1' AND `ignore`='0' AND `disabled`='0'";
    } elseif ($type == 'down') {
        $sql = "`status`='0' AND `ignore`='0' AND `disabled`='0'";
    } elseif ($type == 'disabled') {
        $sql = "`disabled`='1'";
    } elseif ($type == 'mac') {
        $join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv4_mac` ON `ports`.`port_id`=`ipv4_mac`.`port_id` ";
        $sql = "`ipv4_mac`.`mac_address`=?";
        $param[] = $query;
    } elseif ($type == 'ipv4') {
        $join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv4_addresses` ON `ports`.`port_id`=`ipv4_addresses`.`port_id` ";
        $sql = "`ipv4_addresses`.`ipv4_address`=?";
        $param[] = $query;
    } elseif ($type == 'ipv6') {
        $join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv6_addresses` ON `ports`.`port_id`=`ipv6_addresses`.`port_id` ";
        $sql = "`ipv6_addresses`.`ipv6_address`=? OR `ipv6_addresses`.`ipv6_compressed`=?";
        $param = array($query, $query);
    } else {
        $sql = '1';
    }
    $devices = array();
    foreach (dbFetchRows("SELECT * FROM `devices` {$join} WHERE {$sql} ORDER by {$order}", $param) as $device) {
        $device['ip'] = inet6_ntop($device['ip']);
        $devices[] = $device;
    }
    $count = count($devices);
    $output = array('status' => 'ok', 'count' => $count, 'devices' => $devices);
    $app->response->headers->set('Content-Type', 'application/json');
    echo _json_encode($output);
}
Пример #4
0
function poll_device($device, $options)
{
    global $config, $device, $polled_devices, $db_stats, $memcache;
    $attribs = get_dev_attribs($device['device_id']);
    $status = 0;
    unset($array);
    $device_start = microtime(true);
    // Start counting device poll time
    echo $device['hostname'] . ' ' . $device['device_id'] . ' ' . $device['os'] . ' ';
    $ip = dnslookup($device);
    if (!empty($ip) && $ip != inet6_ntop($device['ip'])) {
        log_event('Device IP changed to ' . $ip, $device, 'system');
        $db_ip = inet_pton($ip);
        dbUpdate(array('ip' => $db_ip), 'devices', 'device_id=?', array($device['device_id']));
    }
    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();
    $update_array = array();
    $host_rrd = $config['rrd_dir'] . '/' . $device['hostname'];
    if (!is_dir($host_rrd)) {
        mkdir($host_rrd);
        echo "Created directory : {$host_rrd}\n";
    }
    $address_family = snmpTransportToAddressFamily($device['transport']);
    $ping_response = isPingable($device['hostname'], $address_family, $attribs);
    $device_perf = $ping_response['db'];
    $device_perf['device_id'] = $device['device_id'];
    $device_perf['timestamp'] = array('NOW()');
    if (can_ping_device($attribs) === true && is_array($device_perf)) {
        dbInsert($device_perf, 'device_perf');
    }
    $device['pingable'] = $ping_response['result'];
    $ping_time = $ping_response['last_ping_timetaken'];
    $response = array();
    $status_reason = '';
    if ($device['pingable']) {
        $device['snmpable'] = isSNMPable($device);
        if ($device['snmpable']) {
            $status = '1';
            $response['status_reason'] = '';
        } else {
            echo 'SNMP Unreachable';
            $status = '0';
            $response['status_reason'] = 'snmp';
        }
    } else {
        echo 'Unpingable';
        $status = '0';
        $response['status_reason'] = 'icmp';
    }
    if ($device['status'] != $status) {
        $poll_update .= $poll_separator . "`status` = '{$status}'";
        $poll_separator = ', ';
        dbUpdate(array('status' => $status, 'status_reason' => $response['status_reason']), 'devices', 'device_id=?', array($device['device_id']));
        log_event('Device status changed to ' . ($status == '1' ? 'Up' : 'Down'), $device, $status == '1' ? 'up' : 'down');
    }
    if ($status == '1') {
        $graphs = array();
        $oldgraphs = array();
        // we always want the core module to be included
        include 'includes/polling/core.inc.php';
        if ($options['m']) {
            foreach (explode(',', $options['m']) as $module) {
                if (is_file('includes/polling/' . $module . '.inc.php')) {
                    include 'includes/polling/' . $module . '.inc.php';
                }
            }
        } else {
            foreach ($config['poller_modules'] as $module => $module_status) {
                if ($attribs['poll_' . $module] || $module_status && !isset($attribs['poll_' . $module])) {
                    $module_start = microtime(true);
                    include 'includes/polling/' . $module . '.inc.php';
                    $module_time = microtime(true) - $module_start;
                    echo "Runtime for polling module '{$module}': {$module_time}\n";
                    // save per-module poller stats
                    $tags = array('module' => $module, 'rrd_def' => 'DS:poller:GAUGE:600:0:U', 'rrd_name' => array('poller-perf', $module));
                    $fields = array('poller' => $module_time);
                    data_update($device, 'poller-perf', $tags, $fields);
                    // remove old rrd
                    $oldrrd = rrd_name($device['hostname'], array('poller', $module, 'perf'));
                    if (is_file($oldrrd)) {
                        unlink($oldrrd);
                    }
                } else {
                    if (isset($attribs['poll_' . $module]) && $attribs['poll_' . $module] == '0') {
                        echo "Module [ {$module} ] disabled on host.\n";
                    } else {
                        echo "Module [ {$module} ] disabled globally.\n";
                    }
                }
            }
        }
        //end if
        if (!$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.
            foreach (dbFetch('SELECT `graph` FROM `device_graphs` WHERE `device_id` = ?', array($device['device_id'])) as $graph) {
                if (isset($graphs[$graph['graph']])) {
                    $oldgraphs[$graph['graph']] = true;
                } else {
                    dbDelete('device_graphs', '`device_id` = ? AND `graph` = ?', array($device['device_id'], $graph['graph']));
                }
            }
            foreach ($graphs as $graph => $value) {
                if (!isset($oldgraphs[$graph])) {
                    echo '+';
                    dbInsert(array('device_id' => $device['device_id'], 'graph' => $graph), 'device_graphs');
                }
                echo $graph . ' ';
            }
        }
        //end if
        $device_end = microtime(true);
        $device_run = $device_end - $device_start;
        $device_time = substr($device_run, 0, 5);
        // Poller performance
        if (!empty($device_time)) {
            $tags = array('rrd_def' => 'DS:poller:GAUGE:600:0:U', 'module' => 'ALL');
            $fields = array('poller' => $device_time);
            data_update($device, 'poller-perf', $tags, $fields);
        }
        // Ping response
        if (can_ping_device($attribs) === true && !empty($ping_time)) {
            $tags = array('rrd_def' => 'DS:ping:GAUGE:600:0:65535');
            $fields = array('ping' => $ping_time);
            $update_array['last_ping'] = array('NOW()');
            $update_array['last_ping_timetaken'] = $ping_time;
            data_update($device, 'ping-perf', $tags, $fields);
        }
        $update_array['last_polled'] = array('NOW()');
        $update_array['last_polled_timetaken'] = $device_time;
        // echo("$device_end - $device_start; $device_time $device_run");
        echo "Polled in {$device_time} seconds\n";
        d_echo('Updating ' . $device['hostname'] . "\n");
        d_echo($update_array);
        $updated = dbUpdate($update_array, 'devices', '`device_id` = ?', array($device['device_id']));
        if ($updated) {
            echo "UPDATED!\n";
        }
        unset($storage_cache);
        // Clear cache of hrStorage ** MAYBE FIXME? **
        unset($cache);
        // Clear cache (unify all things here?)
    }
    //end if
}
Пример #5
0
}
echo '</div>
      <table class="table table-hover table-condensed table-striped">';
$uptime = $device['uptime'];
if ($device['os'] == 'ios') {
    formatCiscoHardware($device);
}
if ($device['features']) {
    $device['features'] = '(' . $device['features'] . ')';
}
$device['os_text'] = $config['os'][$device['os']]['text'];
echo '<tr>
        <td>System Name</td>
        <td>' . $device['sysName'] . ' </td>
      </tr>';
if ($ip = inet6_ntop($device['ip'])) {
    echo '<tr>
             <td>Resolved IP</td>
             <td>' . $ip . '</td>
         </tr>';
}
if ($device['hardware']) {
    echo '<tr>
        <td>Hardware</td>
        <td>' . $device['hardware'] . '</td>
      </tr>';
}
echo '<tr>
        <td>Operating System</td>
        <td>' . $device['os_text'] . ' ' . $device['version'] . ' ' . $device['features'] . ' </td>
      </tr>';
Пример #6
0
/**
 * Run all rules for a device
 * @param int $device Device-ID
 * @return void
 */
function RunRules($device)
{
    if (IsMaintenance($device) > 0) {
        echo "Under Maintenance, Skipping alerts.\r\n";
        return false;
    }
    foreach (GetRules($device) as $rule) {
        c_echo('Rule %p#' . $rule['id'] . ' (' . $rule['name'] . '):%n ');
        $inv = json_decode($rule['extra'], true);
        if (isset($inv['invert'])) {
            $inv = (bool) $inv['invert'];
        } else {
            $inv = false;
        }
        d_echo(PHP_EOL);
        $chk = dbFetchRow("SELECT state FROM alerts WHERE rule_id = ? && device_id = ? ORDER BY id DESC LIMIT 1", array($rule['id'], $device));
        $sql = GenSQL($rule['rule']);
        $qry = dbFetchRows($sql, array($device));
        if (isset($qry[0]['ip'])) {
            $qry[0]['ip'] = inet6_ntop($qry[0]['ip']);
        }
        $s = sizeof($qry);
        if ($s == 0 && $inv === false) {
            $doalert = false;
        } elseif ($s > 0 && $inv === false) {
            $doalert = true;
        } elseif ($s == 0 && $inv === true) {
            $doalert = true;
        } else {
            //( $s > 0 && $inv == false ) {
            $doalert = false;
        }
        if ($doalert) {
            if ($chk['state'] === "2") {
                c_echo('Status: %ySKIP');
            } elseif ($chk['state'] >= "1") {
                c_echo('Status: %bNOCHG');
            } else {
                $extra = gzcompress(json_encode(array('contacts' => GetContacts($qry), 'rule' => $qry)), 9);
                if (dbInsert(array('state' => 1, 'device_id' => $device, 'rule_id' => $rule['id'], 'details' => $extra), 'alert_log')) {
                    if (!dbUpdate(array('state' => 1, 'open' => 1), 'alerts', 'device_id = ? && rule_id = ?', array($device, $rule['id']))) {
                        dbInsert(array('state' => 1, 'device_id' => $device, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0), 'alerts');
                    }
                    c_echo(PHP_EOL . 'Status: %rALERT');
                }
            }
        } else {
            if ($chk['state'] === "0") {
                c_echo('Status: %bNOCHG');
            } else {
                if (dbInsert(array('state' => 0, 'device_id' => $device, 'rule_id' => $rule['id']), 'alert_log')) {
                    if (!dbUpdate(array('state' => 0, 'open' => 1), 'alerts', 'device_id = ? && rule_id = ?', array($device, $rule['id']))) {
                        dbInsert(array('state' => 0, 'device_id' => $device, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0), 'alerts');
                    }
                    c_echo(PHP_EOL . 'Status: %gOK');
                }
            }
        }
        c_echo('%n' . PHP_EOL);
    }
}