mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-30 08:04:41 +02:00
Merge pull request #2797 from BarbarossaTM/issue-386-v2
Ability to use ifIndex,ifName and ifDescr to create and update port rrd files
This commit is contained in:
@@ -39,11 +39,11 @@
|
||||
// adslAturPerfESs.1 = 0 seconds
|
||||
// adslAturPerfValidIntervals.1 = 0
|
||||
// adslAturPerfInvalidIntervals.1 = 0
|
||||
if (isset($port_stats[$port['ifIndex']]['adslLineCoding'])) {
|
||||
if (isset($port_stats[$port_id]['adslLineCoding'])) {
|
||||
// Check to make sure Port data is cached.
|
||||
$this_port = &$port_stats[$port['ifIndex']];
|
||||
$this_port = &$port_stats[$port_id];
|
||||
|
||||
$rrdfile = $config['rrd_dir'].'/'.$device['hostname'].'/'.safename('port-'.$port['ifIndex'].'-adsl.rrd');
|
||||
$rrdfile = get_port_rrdfile_path ($device['hostname'], $port_id, 'adsl');
|
||||
|
||||
$rrd_create = ' --step 300';
|
||||
$rrd_create .= ' DS:AtucCurrSnrMgn:GAUGE:600:0:635';
|
||||
@@ -130,8 +130,8 @@ if (isset($port_stats[$port['ifIndex']]['adslLineCoding'])) {
|
||||
$this_port[$oid] = ($this_port[$oid] / 10);
|
||||
}
|
||||
|
||||
if (dbFetchCell('SELECT COUNT(*) FROM `ports_adsl` WHERE `port_id` = ?', array($port['port_id'])) == '0') {
|
||||
dbInsert(array('port_id' => $port['port_id']), 'ports_adsl');
|
||||
if (dbFetchCell('SELECT COUNT(*) FROM `ports_adsl` WHERE `port_id` = ?', array($port_id)) == '0') {
|
||||
dbInsert(array('port_id' => $port_id), 'ports_adsl');
|
||||
}
|
||||
|
||||
$port['adsl_update'] = array('port_adsl_updated' => array('NOW()'));
|
||||
@@ -141,7 +141,7 @@ if (isset($port_stats[$port['ifIndex']]['adslLineCoding'])) {
|
||||
$port['adsl_update'][$oid] = $data;
|
||||
}
|
||||
|
||||
dbUpdate($port['adsl_update'], 'ports_adsl', '`port_id` = ?', array($port['port_id']));
|
||||
dbUpdate($port['adsl_update'], 'ports_adsl', '`port_id` = ?', array($port_id));
|
||||
|
||||
if ($this_port['adslAtucCurrSnrMgn'] > '1280') {
|
||||
$this_port['adslAtucCurrSnrMgn'] = 'U';
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
<?php
|
||||
|
||||
if ($port_stats[$port['ifIndex']] &&
|
||||
if ($port_stats[$port_id] &&
|
||||
$port['ifType'] == 'ethernetCsmacd' &&
|
||||
isset($port_stats[$port['ifIndex']]['dot3StatsIndex'])) {
|
||||
isset($port_stats[$port_id]['dot3StatsIndex'])) {
|
||||
// Check to make sure Port data is cached.
|
||||
$this_port = &$port_stats[$port[ifIndex]];
|
||||
$this_port = &$port_stats[$port_id];
|
||||
|
||||
// TODO: remove legacy check?
|
||||
$old_rrdfile = $config['rrd_dir'].'/'.$device['hostname'].'/'.safename('etherlike-'.$port['ifIndex'].'.rrd');
|
||||
$rrdfile = $config['rrd_dir'].'/'.$device['hostname'].'/'.safename('port-'.$port['ifIndex'].'-dot3.rrd');
|
||||
$rrd_file = get_port_rrdfile_path ($device['hostname'], $port_id, 'dot3');
|
||||
|
||||
$rrd_create = $config['rrd_rra'];
|
||||
|
||||
|
||||
@@ -35,14 +35,13 @@ $peth_oids = array(
|
||||
'pethMainPseConsumptionPower',
|
||||
);
|
||||
|
||||
if ($port_stats[$port['ifIndex']]
|
||||
if ($port_stats[$port_id]
|
||||
&& $port['ifType'] == 'ethernetCsmacd'
|
||||
&& isset($port_stats[$port['ifIndex']]['dot3StatsIndex'])) {
|
||||
&& isset($port_stats[$port_id]['dot3StatsIndex'])) {
|
||||
// Check to make sure Port data is cached.
|
||||
$this_port = &$port_stats[$port['ifIndex']];
|
||||
|
||||
$rrdfile = $config['rrd_dir'].'/'.$device['hostname'].'/'.safename('port-'.$port['ifIndex'].'-poe.rrd');
|
||||
$this_port = &$port_stats[$port_id];
|
||||
|
||||
$rrdfile = get_port_rrdfile_path ($device['hostname'], $port_id, 'poe');
|
||||
if (!file_exists($rrdfile)) {
|
||||
$rrd_create .= $config['rrd_rra'];
|
||||
|
||||
|
||||
@@ -191,50 +191,91 @@ $polled = time();
|
||||
// End Building SNMP Cache Array
|
||||
d_echo($port_stats);
|
||||
|
||||
// Build array of ports in the database
|
||||
// FIXME -- this stuff is a little messy, looping the array to make an array just seems wrong. :>
|
||||
// -- i can make it a function, so that you don't know what it's doing.
|
||||
// -- $ports = adamasMagicFunction($ports_db); ?
|
||||
// select * doesn't do what we want if multiple tables have the same column name -- last one wins :/
|
||||
$ports_db = dbFetchRows('SELECT *, `ports_statistics`.`port_id` AS `ports_statistics_port_id`, `ports`.`port_id` AS `port_id` FROM `ports` LEFT OUTER JOIN `ports_statistics` ON `ports`.`port_id` = `ports_statistics`.`port_id` WHERE `ports`.`device_id` = ?', array($device['device_id']));
|
||||
// By default libreNMS uses the ifIndex to associate ports on devices with ports discoverd/polled
|
||||
// before and stored in the database. On Linux boxes this is a problem as ifIndexes may be
|
||||
// unstable between reboots or (re)configuration of tunnel interfaces (think: GRE/OpenVPN/Tinc/...)
|
||||
// The port association configuration allows to choose between association via ifIndex, ifName,
|
||||
// or maybe other means in the future. The default port association mode still is ifIndex for
|
||||
// compatibility reasons.
|
||||
$port_association_mode = $config['default_port_association_mode'];
|
||||
if ($device['port_association_mode'])
|
||||
$port_association_mode = get_port_assoc_mode_name ($device['port_association_mode']);
|
||||
|
||||
foreach ($ports_db as $port) {
|
||||
$ports[$port['ifIndex']] = $port;
|
||||
// Query known ports and mapping table in order of discovery to make sure
|
||||
// the latest discoverd/polled port is in the mapping tables.
|
||||
$ports_mapped = get_ports_mapped ($device['device_id'], true);
|
||||
$ports = $ports_mapped['ports'];
|
||||
|
||||
//
|
||||
// Rename any old RRD files still named after the previous ifIndex based naming schema.
|
||||
foreach ($ports_mapped['maps']['ifIndex'] as $ifIndex => $port_id) {
|
||||
foreach (array ('', 'adsl', 'dot3') as $suffix) {
|
||||
$suffix_tmp = '';
|
||||
if ($suffix)
|
||||
$suffix_tmp = "-$suffix";
|
||||
|
||||
$old_rrd_path = trim ($config['rrd_dir']) . '/' . $device['hostname'] . "/port-$ifIndex$suffix_tmp.rrd";
|
||||
$new_rrd_path = get_port_rrdfile_path ($device['hostname'], $port_id, $suffix);
|
||||
|
||||
if (is_file ($old_rrd_path)) {
|
||||
rename ($old_rrd_path, $new_rrd_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// New interface detection
|
||||
foreach ($port_stats as $ifIndex => $port) {
|
||||
// Store ifIndex in port entry and prefetch ifName as we'll need it multiple times
|
||||
$port['ifIndex'] = $ifIndex;
|
||||
$ifName = $port['ifName'];
|
||||
|
||||
// Get port_id according to port_association_mode used for this device
|
||||
$port_id = get_port_id ($ports_mapped, $port, $port_association_mode);
|
||||
|
||||
if (is_port_valid($port, $device)) {
|
||||
echo 'valid';
|
||||
if (!is_array($ports[$port['ifIndex']])) {
|
||||
$port_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports');
|
||||
|
||||
// Port newly discovered?
|
||||
if (! $ports[$port_id]) {
|
||||
$port_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex, 'ifName' => $ifName), 'ports');
|
||||
dbInsert(array('port_id' => $port_id), 'ports_statistics');
|
||||
$ports[$port['ifIndex']] = dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', array($port_id));
|
||||
echo 'Adding: '.$port['ifName'].'('.$ifIndex.')('.$ports[$port['ifIndex']]['port_id'].')';
|
||||
$ports[$port_id] = dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', array($port_id));
|
||||
echo 'Adding: '.$ifName.'('.$ifIndex.')('.$port_id.')';
|
||||
// print_r($ports);
|
||||
}
|
||||
else if ($ports[$ifIndex]['deleted'] == '1') {
|
||||
dbUpdate(array('deleted' => '0'), 'ports', '`port_id` = ?', array($ports[$ifIndex]['port_id']));
|
||||
$ports[$ifIndex]['deleted'] = '0';
|
||||
}
|
||||
if ($ports[$ifIndex]['ports_statistics_port_id'] === null) {
|
||||
// in case the port was created before we created the table
|
||||
dbInsert(array('port_id' => $ports[$ifIndex]['port_id']), 'ports_statistics');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($ports[$port['ifIndex']]['deleted'] != '1') {
|
||||
dbUpdate(array('deleted' => '1'), 'ports', '`port_id` = ?', array($ports[$ifIndex]['port_id']));
|
||||
$ports[$ifIndex]['deleted'] = '1';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// End New interface detection
|
||||
// Port re-discovered after previous deletion?
|
||||
else if ($ports[$port_id]['deleted'] == 1) {
|
||||
dbUpdate(array('deleted' => '0'), 'ports', '`port_id` = ?', array($port_id));
|
||||
$ports[$port_id]['deleted'] = '0';
|
||||
}
|
||||
if ($ports[$port_id]['ports_statistics_port_id'] === null) {
|
||||
// in case the port was created before we created the table
|
||||
dbInsert(array('port_id' => $port_id), 'ports_statistics');
|
||||
}
|
||||
|
||||
// Assure stable mapping
|
||||
$port_stats[$ifIndex]['port_id'] = $port_id;
|
||||
$ports[$port_id]['ifIndex'] = $ifIndex;
|
||||
}
|
||||
|
||||
// Port vanished (mark as deleted)
|
||||
else {
|
||||
if ($ports[$port_id]['deleted'] != '1') {
|
||||
dbUpdate(array('deleted' => '1'), 'ports', '`port_id` = ?', array($port_id));
|
||||
$ports[$port_id]['deleted'] = '1';
|
||||
}
|
||||
}
|
||||
} // End new interface detection
|
||||
|
||||
|
||||
echo "\n";
|
||||
// Loop ports in the DB and update where necessary
|
||||
foreach ($ports as $port) {
|
||||
echo 'Port '.$port['ifDescr'].'('.$port['ifIndex'].') ';
|
||||
$port_id = $port['port_id'];
|
||||
|
||||
echo 'Port ' . $port['ifName'] . ': ' . $port['ifDescr'] . '(' . $port['ifIndex'] . ') ';
|
||||
if ($port_stats[$port['ifIndex']] && $port['disabled'] != '1') {
|
||||
// Check to make sure Port data is cached.
|
||||
$this_port = &$port_stats[$port['ifIndex']];
|
||||
@@ -477,7 +518,7 @@ foreach ($ports as $port) {
|
||||
}
|
||||
|
||||
// Update RRDs
|
||||
$rrdfile = $host_rrd.'/port-'.safename($port['ifIndex'].'.rrd');
|
||||
$rrdfile = get_port_rrdfile_path ($device['hostname'], $port_id);
|
||||
if (!is_file($rrdfile)) {
|
||||
rrdtool_create(
|
||||
$rrdfile,
|
||||
@@ -576,9 +617,9 @@ foreach ($ports as $port) {
|
||||
|
||||
// Update Database
|
||||
if (count($port['update'])) {
|
||||
$updated = dbUpdate($port['update'], 'ports', '`port_id` = ?', array($port['port_id']));
|
||||
$updated = dbUpdate($port['update'], 'ports', '`port_id` = ?', array($port_id));
|
||||
// do we want to do something else with this?
|
||||
$updated += dbUpdate($port['update_extended'], 'ports_statistics', '`port_id` = ?', array($port['port_id']));
|
||||
$updated += dbUpdate($port['update_extended'], 'ports_statistics', '`port_id` = ?', array($port_id));
|
||||
d_echo("$updated updated");
|
||||
}
|
||||
|
||||
@@ -588,7 +629,7 @@ foreach ($ports as $port) {
|
||||
echo 'Port Deleted';
|
||||
// Port missing from SNMP cache.
|
||||
if ($port['deleted'] != '1') {
|
||||
dbUpdate(array('deleted' => '1'), 'ports', '`device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $port['ifIndex']));
|
||||
dbUpdate(array('deleted' => '1'), 'ports', '`device_id` = ? AND `port_id` = ?', array($device['device_id'], $port_id));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -599,7 +640,7 @@ foreach ($ports as $port) {
|
||||
|
||||
// Clear Per-Port Variables Here
|
||||
unset($this_port);
|
||||
}//end foreach
|
||||
} //end port update
|
||||
|
||||
// Clear Variables Here
|
||||
unset($port_stats);
|
||||
|
||||
Reference in New Issue
Block a user