Example #1
0
        }
    }
    if ($old_ifSpeed_bool && $new_ifSpeed_bool) {
        // Both set, compare values
        if ($vars['ifSpeed_custom_' . $port_id] != $port['ifSpeed_custom']) {
            //r($vars['ifSpeed_custom_' . $port_id]); r($port['ifSpeed_custom']);
            set_entity_attrib('port', $port_id, 'ifSpeed_custom', $vars['ifSpeed_custom_' . $port_id]);
            $updated = TRUE;
        }
    } else {
        if ($old_ifSpeed_bool !== $new_ifSpeed_bool) {
            // Added or removed
            if ($old_ifSpeed_bool) {
                del_entity_attrib('port', $port_id, 'ifSpeed_custom');
            } else {
                set_entity_attrib('port', $port_id, 'ifSpeed_custom', $vars['ifSpeed_custom_' . $port_id]);
            }
            $updated = TRUE;
        }
    }
    // Count updates
    if ($updated) {
        $rows_updated++;
    }
}
if ($rows_updated > 0) {
    $update_message = $rows_updated . " Port entries updated.";
    $updated = 1;
} else {
    $update_message = "Port entries unchanged. No update necessary.";
    $updated = -1;
Example #2
0
        }
    } else {
        if (isset($ports[$port['ifIndex']]) && $ports[$port['ifIndex']]['deleted'] != '1' && $allow_delete_ports) {
            dbUpdate(array('deleted' => '1', 'ifLastChange' => date('Y-m-d H:i:s', $polled)), 'ports', '`port_id` = ?', array($ports[$ifIndex]['port_id']));
            log_event("Interface was marked as DELETED", $device, 'port', $ports[$ifIndex]);
            $ports[$ifIndex]['deleted'] = "1";
        }
        $ports_ignored_count++;
        // Counting ignored ports
    }
}
if (!$allow_delete_ports) {
    log_event("WARNING! Ports snmpwalk did not complete. Try to increase SNMP timeout on the device properties page.", $device, 'device', $device['device_id'], 7);
}
if ($ports_ignored_count !== $ports_ignored_count_db) {
    set_entity_attrib('device', $device, 'ports_ignored_count', $ports_ignored_count);
}
// End New interface detection
echo PHP_EOL . PHP_EOL;
// Loop ports in the DB and update where necessary
foreach ($ports as $port) {
    // Notes:
    // $port_stats - array of ports from snmpwalks
    // $this_port  - link to port array from snmpwalk
    // $ports      - array of ports based on current db entries
    // $port       - current port array from db
    if ($port['deleted']) {
        continue;
    }
    // Skip updating RRDs and DB if interface marked as DELETED (also skipped bad_if's)
    if ($port_stats[$port['ifIndex']] && $port['disabled'] != "1") {
Example #3
0
function discover_new_device($hostname, $source = 'xdp', $protocol = NULL, $device = NULL, $snmp_port = 161)
{
    global $config;
    $source = strtolower($source);
    // Check if source is enabled for autodiscovery
    if ($config['autodiscovery'][$source]) {
        $flags = OBS_DNS_ALL;
        if (!$protocol) {
            $protocol = strtoupper($source);
        }
        print_cli_data("Try discovering host", "{$hostname} through {$protocol}", 3);
        // By first detect hostname is IP or domain name (IPv4/6 == 4/6, hostname == FALSE)
        $ip_version = get_ip_version($hostname);
        if ($ip_version) {
            // Hostname is IPv4/IPv6
            $use_ip = TRUE;
            $ip = $hostname;
        } else {
            $use_ip = FALSE;
            // Add "mydomain" configuration if this resolves, converts switch1 -> switch1.mydomain.com
            if (!empty($config['mydomain']) && isDomainResolves($hostname . '.' . $config['mydomain'], $flags)) {
                $hostname .= '.' . $config['mydomain'];
            }
            // Determine v4 vs v6
            $ip = gethostbyname6($hostname, $flags);
            if ($ip) {
                $ip_version = get_ip_version($ip);
                print_debug("Host {$hostname} resolved as {$ip}");
            } else {
                // No DNS records
                print_debug("Host {$hostname} not resolved, autodiscovery fails.");
                return FALSE;
            }
        }
        if ($ip_version == 6) {
            $flags = $flags ^ OBS_DNS_A;
            // Exclude IPv4
        }
        if (isset($config['autodiscovery']['ping_skip']) && $config['autodiscovery']['ping_skip']) {
            $flags = $flags | OBS_PING_SKIP;
            // Add skip pings flag
        }
        if (match_network($ip, $config['autodiscovery']['ip_nets'])) {
            print_debug("Host {$hostname} ({$ip}) founded inside configured nets, trying to add:");
            // By first check if pingable
            $pingable = isPingable($ip, $flags);
            if (!$pingable && (isset($config['autodiscovery']['ping_skip']) && $config['autodiscovery']['ping_skip'])) {
                $flags = $flags | OBS_PING_SKIP;
                // Add skip pings flag if allowed in config
                $pingable = TRUE;
            }
            if ($pingable) {
                // Check if device duplicated by IP
                $ip = $ip_version == 4 ? $ip : Net_IPv6::uncompress($ip, TRUE);
                $db = dbFetchRow('SELECT D.`hostname` FROM ipv' . $ip_version . '_addresses AS A
                         LEFT JOIN `ports`   AS P ON A.`port_id`   = P.`port_id`
                         LEFT JOIN `devices` AS D ON D.`device_id` = P.`device_id`
                         WHERE D.`disabled` = 0 AND A.`ipv' . $ip_version . '_address` = ?', array($ip));
                if ($db) {
                    print_debug('Already have device ' . $db['hostname'] . " with IP {$ip}");
                    return FALSE;
                }
                // Detect snmp transport, net-snmp needs udp6 for ipv6
                $snmp_transport = $ip_version == 4 ? 'udp' : 'udp6';
                $new_device = detect_device_snmpauth($ip, $snmp_port, $snmp_transport);
                if ($new_device) {
                    if ($use_ip) {
                        // Detect FQDN hostname
                        // by sysName
                        $snmphost = snmp_get($new_device, 'sysName.0', '-Oqv', 'SNMPv2-MIB');
                        if ($snmphost) {
                            $snmp_ip = gethostbyname6($snmphost, $flags);
                        }
                        if ($snmp_ip == $ip) {
                            $hostname = $snmphost;
                        } else {
                            // by PTR
                            $ptr = gethostbyaddr6($ip);
                            if ($ptr) {
                                $ptr_ip = gethostbyname6($ptr, $flags);
                            }
                            if ($ptr && $ptr_ip == $ip) {
                                $hostname = $ptr;
                            } else {
                                if ($config['autodiscovery']['require_hostname']) {
                                    print_debug("Device IP {$ip} does not seem to have FQDN.");
                                    return FALSE;
                                } else {
                                    $hostname = $ip_version == 4 ? $ip : Net_IPv6::compress($hostname, TRUE);
                                    // Always use compressed IPv6 name
                                }
                            }
                        }
                        print_debug("Device IP {$ip} linked to FQDN name: {$hostname}");
                    }
                    $new_device['hostname'] = $hostname;
                    if (!check_device_duplicated($new_device)) {
                        $snmp_v3 = array();
                        if ($new_device['snmp_version'] === 'v3') {
                            $snmp_v3['snmp_authlevel'] = $new_device['snmp_authlevel'];
                            $snmp_v3['snmp_authname'] = $new_device['snmp_authname'];
                            $snmp_v3['snmp_authpass'] = $new_device['snmp_authpass'];
                            $snmp_v3['snmp_authalgo'] = $new_device['snmp_authalgo'];
                            $snmp_v3['snmp_cryptopass'] = $new_device['snmp_cryptopass'];
                            $snmp_v3['snmp_cryptoalgo'] = $new_device['snmp_cryptoalgo'];
                        }
                        $remote_device_id = createHost($new_device['hostname'], $new_device['snmp_community'], $new_device['snmp_version'], $new_device['snmp_port'], $new_device['snmp_transport'], $snmp_v3);
                        if ($remote_device_id) {
                            if (is_flag_set(OBS_PING_SKIP, $flags)) {
                                set_entity_attrib('device', $remote_device_id, 'ping_skip', 1);
                            }
                            $remote_device = device_by_id_cache($remote_device_id, 1);
                            if ($port) {
                                humanize_port($port);
                                log_event("Device autodiscovered through {$protocol} on " . $device['hostname'] . " (port " . $port['port_label'] . ")", $remote_device_id, 'port', $port['port_id']);
                            } else {
                                log_event("Device autodiscovered through {$protocol} on " . $device['hostname'], $remote_device_id, $protocol);
                            }
                            //array_push($GLOBALS['devices'], $remote_device); // createHost() already puth this
                            return $remote_device_id;
                        }
                    }
                }
            }
        } else {
            print_debug("IP {$ip} ({$hostname}) not permitted inside \$config['autodiscovery']['ip_nets'] in config.php");
        }
        print_debug('Autodiscovery for host ' . $hostname . ' failed.');
    } else {
        print_debug('Autodiscovery for protocol ' . $protocol . ' disabled.');
    }
    return FALSE;
}
Example #4
0
function force_discovery($device, $modules = array())
{
    $return = FALSE;
    if (count($modules) == 0) {
        // Modules not passed, just full rediscover device
        $return = dbUpdate(array('force_discovery' => 1), 'devices', '`device_id` = ?', array($device['device_id']));
    } else {
        // Modules passed, check if modules valid and enabled
        $modules = (array) $modules;
        $forced_modules = get_entity_attrib('device', $device['device_id'], 'force_discovery_modules');
        if ($forced_modules) {
            // Already forced modules exist, merge it with new
            $modules = array_unique(array_merge($modules, json_decode($forced_modules, TRUE)));
        }
        $valid_modules = array();
        foreach ($GLOBALS['config']['discovery_modules'] as $module => $ok) {
            // Filter by valid and enabled modules
            if ($ok && in_array($module, $modules)) {
                $valid_modules[] = $module;
            }
        }
        if (count($valid_modules)) {
            $return = dbUpdate(array('force_discovery' => 1), 'devices', '`device_id` = ?', array($device['device_id']));
            set_entity_attrib('device', $device['device_id'], 'force_discovery_modules', json_encode($valid_modules));
        }
    }
    return $return;
}
Example #5
0
          foreach($tmp_diff as $entry)
          {
            $tmp_rows[] = array($entry, 'MIB unused');
          }
          echo("\n");
          print_cli_table($tmp_rows, array('%WOID%n', '%WStatus%n'));
        }
        */
 } else {
     echo '<empty>';
 }
 echo PHP_EOL;
 // Set device attribute if we found any new MIBs, else delete the attribute
 if (count($sysORID_mibs)) {
     $sysORID_db = json_decode(get_entity_attrib('device', $device, 'sysORID'), TRUE);
     set_entity_attrib('device', $device, 'sysORID', json_encode($sysORID_mibs));
     $update_array = array_diff($sysORID_mibs, (array) $sysORID_db);
     //print_vars($sysORID_db);
     //print_vars($sysORID_mibs);
     //print_vars($update_array);
     if (count($update_array)) {
         log_event("MIBs discovered through sysORID: '" . implode("', '", $update_array) . "'", $device, 'device', $device['device_id']);
     }
 } else {
     del_entity_attrib('device', $device, 'sysORID');
 }
 unset($sysORID_mibs, $device_sysORID, $device_mibs, $device_mibs_bl, $advertised_mibs, $capabilities_mibs, $capabilities_unused, $found_mibs, $identity_found, $update_array);
 if (count($table_rows)) {
     echo PHP_EOL;
     $table_headers = array('%WOID%n', '%WMIB%n', '%WStatus%n');
     print_cli_table($table_rows, $table_headers);
Example #6
0
 }
 if (get_entity_attrib('device', $device, 'override_sysLocation_bool') != $override_sysLocation_bool || get_entity_attrib('device', $device, 'override_sysLocation_string') != $override_sysLocation_string) {
     $updated = 2;
 }
 if ($override_sysLocation_bool) {
     set_entity_attrib('device', $device, 'override_sysLocation_bool', '1');
 } else {
     del_entity_attrib('device', $device, 'override_sysLocation_bool');
 }
 if (isset($override_sysLocation_string)) {
     set_entity_attrib('device', $device, 'override_sysLocation_string', $override_sysLocation_string);
 }
 $ping_skip_set = isset($vars['ping_skip']) && ($vars['ping_skip'] == 'on' || $vars['ping_skip'] == '1');
 if ($ping_skip != $ping_skip_set) {
     if ($ping_skip_set) {
         set_entity_attrib('device', $device, 'ping_skip', '1');
     } else {
         del_entity_attrib('device', $device, 'ping_skip');
     }
     $ping_skip = get_entity_attrib('device', $device, 'ping_skip');
     $updated++;
 }
 # FIXME needs more sanity checking! and better feedback
 # FIXME -- update location too? Need to trigger geolocation!
 $param = array('purpose' => $vars['descr'], 'type' => $vars['type'], 'ignore' => $vars['ignore'], 'disabled' => $vars['disabled']);
 $rows_updated = dbUpdate($param, 'devices', '`device_id` = ?', array($device['device_id']));
 if ($rows_updated > 0 || $updated) {
     if ((bool) $vars['ignore'] != (bool) $device['ignore']) {
         log_event('Device ' . ((bool) $vars['ignore'] ? 'ignored' : 'attended') . ': ' . $device['hostname'], $device['device_id'], 'device', $device['device_id'], 5);
     }
     if ((bool) $vars['disabled'] != (bool) $device['disabled']) {
Example #7
0
function set_dev_attrib($device, $attrib_type, $attrib_value)
{
    // Call to new function
    return set_entity_attrib('device', $device, $attrib_type, $attrib_value);
}
Example #8
0
foreach (get_device_mibs($device) as $mib) {
    $mibs[$mib]++;
}
// Sort alphabetically
ksort($mibs);
$attribs = get_entity_attribs('device', $device['device_id']);
if ($vars['submit']) {
    if ($readonly) {
        print_error_permission('You have insufficient permissions to edit settings.');
    } else {
        if ($vars['toggle_mib'] && isset($mibs[$vars['toggle_mib']])) {
            $mib = $vars['toggle_mib'];
            if (isset($attribs['mib_' . $mib])) {
                del_entity_attrib('device', $device, 'mib_' . $mib);
            } else {
                set_entity_attrib('device', $device, 'mib_' . $mib, "0");
            }
            // reload attribs
            $attribs = get_entity_attribs('device', $device['device_id']);
        }
    }
}
//$poll_period = 300;
$error_codes = $GLOBALS['config']['snmp']['errorcodes'];
$poll_period = $GLOBALS['config']['rrd']['step'];
// Count critical errors into DB (only for poller)
$snmp_errors = array();
$sql = 'SELECT * FROM `snmp_errors` WHERE `device_id` = ?;';
foreach (dbFetchRows($sql, array($device['device_id'])) as $entry) {
    $timediff = $entry['updated'] - $entry['added'];
    $poll_count = round($timediff / $poll_period) + 1;