Skip to content

Commit ea363c6

Browse files
authored
Remove echo from ports polling module (librenms#17075)
* remove echo from ports polling module * Apply fixes from StyleCI --------- Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
1 parent 09d49c5 commit ea363c6

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

includes/polling/ports.inc.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
}
210210
}
211211

212-
echo 'Caching Oids: ';
212+
$fetched_data_string = 'Fetched data ';
213213
$port_stats = [];
214214

215215
if ($device['os'] === 'f5' && (version_compare($device['version'], '11.2.0', '>=') && version_compare($device['version'], '11.7', '<'))) {
@@ -219,7 +219,7 @@
219219
} else {
220220
$selected_attrib = DeviceCache::get($device['device_id'] ?? null)->getAttrib('selected_ports');
221221
if ($selected_attrib !== null ? $selected_attrib == 'true' : Config::getOsSetting($device['os'], 'polling.selected_ports')) {
222-
echo 'Selected ports polling ';
222+
$fetched_data_string .= '(Selected ports polling): ';
223223

224224
// remove the deleted and disabled ports and mark them skipped
225225
$polled_ports = array_filter($ports, function ($port) use ($ports) {
@@ -237,7 +237,7 @@
237237
$walk_base = $total_port_count - $polled_port_count < 5 || $polled_port_count / $total_port_count > 0.9;
238238

239239
if ($walk_base) {
240-
echo "Not enough ports for selected port polling, walking base OIDs instead\n";
240+
Log::info('Not enough ports for selected port polling, walking base OIDs instead');
241241
foreach ($table_base_oids as $oid) {
242242
$port_stats = snmpwalk_cache_oid($device, $oid, $port_stats, 'IF-MIB');
243243
}
@@ -288,7 +288,7 @@
288288
}
289289
}
290290
} else {
291-
echo 'Full ports polling ';
291+
$fetched_data_string .= '(Full ports polling): ';
292292
// For devices that are on the bad_ifXentry list, try fetching ifAlias to have nice interface descriptions.
293293

294294
if (! in_array(strtolower($device['hardware'] ?? ''), array_map('strtolower', (array) Config::getOsSetting($device['os'], 'bad_ifXEntry', [])))) {
@@ -308,12 +308,12 @@
308308
} else {
309309
// For devices with ifXentry data, only specific ifEntry keys are fetched to reduce SNMP load
310310
foreach ($ifmib_oids as $oid) {
311-
echo "$oid ";
311+
$fetched_data_string .= "$oid ";
312312
$port_stats = snmpwalk_cache_oid($device, $oid, $port_stats, 'IF-MIB', null, '-OQUst');
313313
}
314314
}
315315
if ($device['os'] != 'asa') {
316-
echo 'dot3StatsDuplexStatus';
316+
$fetched_data_string .= 'dot3StatsDuplexStatus ';
317317
if (Config::get('enable_ports_poe') || Config::get('enable_ports_etherlike')) {
318318
$port_stats = snmpwalk_cache_oid($device, 'dot3StatsIndex', $port_stats, 'EtherLike-MIB');
319319
}
@@ -336,8 +336,8 @@
336336
// Code by OS device
337337

338338
if ($device['os'] == 'ios' || $device['os'] == 'iosxe') {
339-
echo 'cpeExtPsePortEntry';
340-
$port_stats_poe = snmpwalk_cache_oid($device, 'cpeExtPsePortEntry', [], 'CISCO-POWER-ETHERNET-EXT-MIB');
339+
$fetched_data_string .= 'cpeExtPsePortTable ';
340+
$port_stats_poe = snmpwalk_cache_oid($device, 'cpeExtPsePortTable', [], 'CISCO-POWER-ETHERNET-EXT-MIB');
341341
$port_ent_to_if = snmpwalk_cache_oid($device, 'portIfIndex', [], 'CISCO-STACK-MIB');
342342

343343
if (! $port_ent_to_if) {
@@ -363,7 +363,7 @@
363363
}
364364
}
365365
} elseif ($device['os'] == 'vrp') {
366-
echo 'HwPoePortEntry';
366+
$fetched_data_string .= 'HwPoePortTable ';
367367

368368
$vrp_poe_oids = [
369369
'hwPoePortReferencePower',
@@ -377,7 +377,7 @@
377377
$port_stats = snmpwalk_cache_oid($device, $oid, $port_stats, 'HUAWEI-POE-MIB');
378378
}
379379
} elseif ($device['os'] == 'linksys-ss') {
380-
echo 'rlPethPsePort';
380+
$fetched_data_string .= 'rlPethPsePort ';
381381

382382
$linksys_poe_oids = [
383383
'pethPsePortAdminEnable',
@@ -394,8 +394,8 @@
394394
$port_stats[$if_id] = array_merge($port_stats[$if_id], $value);
395395
}
396396
} elseif ($device['os'] == 'jetstream') {
397-
echo 'tpPoePortConfigEntry';
398-
$port_stats_poe = snmpwalk_cache_oid($device, 'tpPoePortConfigEntry', [], 'TPLINK-POWER-OVER-ETHERNET-MIB');
397+
$fetched_data_string .= 'tpPoePortConfigTable ';
398+
$port_stats_poe = snmpwalk_cache_oid($device, 'tpPoePortConfigTable', [], 'TPLINK-POWER-OVER-ETHERNET-MIB');
399399
$ifTable_ifDescr = snmpwalk_cache_oid($device, 'ifDescr', [], 'IF-MIB');
400400

401401
$port_ent_to_if = [];
@@ -444,6 +444,8 @@
444444
}
445445
}
446446

447+
Log::info($fetched_data_string);
448+
447449
$polled = time();
448450

449451
// End Building SNMP Cache Array
@@ -503,7 +505,7 @@
503505
$port_id = dbInsert(['device_id' => $device['device_id'], 'ifIndex' => $ifIndex, 'ifName' => $ifName], 'ports');
504506
dbInsert(['port_id' => $port_id], 'ports_statistics');
505507
$ports[$port_id] = dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', [$port_id]);
506-
echo 'Adding: ' . $ifName . '(' . $ifIndex . ')(' . $port_id . ')';
508+
Log::info('Adding: ' . $ifName . '(' . $ifIndex . ')(' . $port_id . ')');
507509
} elseif ($ports[$port_id]['deleted'] == 1) {
508510
// Port re-discovered after previous deletion?
509511
dbUpdate(['deleted' => '0'], 'ports', '`port_id` = ?', [$port_id]);
@@ -535,8 +537,6 @@
535537
}
536538
} // End new interface detection
537539

538-
echo "\n";
539-
540540
// get last poll time to optimize poll_time, poll_prev and poll_period in table db
541541
$prev_poll_times = array_filter(array_column($ports, 'poll_time'));
542542
$max_poll_time_prev = empty($prev_poll_times) ? null : max($prev_poll_times);
@@ -557,7 +557,7 @@
557557

558558
/* We don't care for disabled ports, go on */
559559
if ($port['disabled'] == 1) {
560-
echo "{$port_info_string}disabled.\n";
560+
Log::info("{$port_info_string}disabled.");
561561
continue;
562562
}
563563

@@ -568,12 +568,11 @@
568568
if (! in_array($port_id, $ports_found)) {
569569
if ($port['deleted'] != '1') {
570570
dbUpdate(['deleted' => '1'], 'ports', '`device_id` = ? AND `port_id` = ?', [$device['device_id'], $port_id]);
571-
echo "{$port_info_string}deleted.\n";
571+
Log::info("{$port_info_string}deleted.");
572572
}
573573
continue;
574574
}
575575

576-
echo $port_info_string;
577576
if ($port_stats[$ifIndex]) {
578577
// Check to make sure Port data is cached.
579578
$this_port = &$port_stats[$ifIndex];
@@ -627,7 +626,7 @@
627626

628627
// Overwrite ifDuplex with dot3StatsDuplexStatus if it exists
629628
if (isset($this_port['dot3StatsDuplexStatus'])) {
630-
echo 'dot3Duplex ';
629+
$port_info_string .= 'dot3Duplex ';
631630
$this_port['ifDuplex'] = $this_port['dot3StatsDuplexStatus'];
632631
}
633632

@@ -663,7 +662,7 @@
663662
}
664663

665664
// FIXME use $q_bridge_mib[$this_port['ifIndex']] to see if it is a trunk (>1 array count)
666-
echo 'VLAN = ' . ($this_port['ifVlan'] ?? '?') . ' ';
665+
$port_info_string .= ' VLAN = ' . ($this_port['ifVlan'] ?? '?') . ' ';
667666

668667
// attempt to fill missing fields
669668
port_fill_missing_and_trim($this_port, $device);
@@ -727,7 +726,7 @@
727726
if (Debug::isEnabled()) {
728727
d_echo($oid . ': ' . $old . ' -> ' . $new . ' ');
729728
} else {
730-
echo $oid . ' ';
729+
$port_info_string .= $oid . ' ';
731730
}
732731
} else {
733732
if (in_array($oid, ['ifOperStatus', 'ifAdminStatus', 'ifSpeed'])) {
@@ -798,7 +797,7 @@
798797
if ($oid_rate < 0) {
799798
$oid_rate = '0';
800799
$oid_diff = '0';
801-
echo "negative $oid";
800+
$port_info_string .= "negative $oid ";
802801
}
803802

804803
$current_port_stats[$oid . '_rate'] = $oid_rate;
@@ -814,7 +813,7 @@
814813
$port_debug = $port['port_id'] . '|' . $polled . '|' . $polled_period . '|' . $this_port['ifHCInOctets'] . '|' . $this_port['ifHCOutOctets'];
815814
$port_debug .= '|' . $current_port_stats['ifInOctets_rate'] . '|' . $current_port_stats['ifOutOctets_rate'] . "\n";
816815
file_put_contents('/tmp/port_debug.txt', $port_debug, FILE_APPEND);
817-
echo 'Wrote port debugging data';
816+
Log::info('debug_port enabled, wrote port debugging data to /tmp/port_debug.txt');
818817
}
819818

820819
$current_port_stats['ifInBits_rate'] = round($current_port_stats['ifInOctets_rate'] * 8);
@@ -826,9 +825,9 @@
826825
$current_port_stats['ifOutBits_perc'] = Number::calculatePercent($current_port_stats['ifOutBits_rate'], $this_port['ifSpeed'], 0);
827826
}
828827

829-
echo 'bps(' . Number::formatSi($current_port_stats['ifInBits_rate'], 2, 3, 'bps') . '/' . Number::formatSi($current_port_stats['ifOutBits_rate'], 2, 0, 'bps') . ')';
830-
echo 'bytes(' . Number::formatBi($current_port_stats['ifInOctets_diff'] ?? 0) . '/' . Number::formatBi($current_port_stats['ifOutOctets_diff'] ?? 0) . ')';
831-
echo 'pkts(' . Number::formatSi($current_port_stats['ifInUcastPkts_rate'] ?? 0, 2, 3, 'pps') . '/' . Number::formatSi($current_port_stats['ifOutUcastPkts_rate'] ?? 0, 2, 0, 'pps') . ')';
828+
$port_info_string .= 'bps(' . Number::formatSi($current_port_stats['ifInBits_rate'], 2, 3, 'bps') . '/' . Number::formatSi($current_port_stats['ifOutBits_rate'], 2, 0, 'bps') . ') ';
829+
$port_info_string .= 'bytes(' . Number::formatBi($current_port_stats['ifInOctets_diff'] ?? 0) . '/' . Number::formatBi($current_port_stats['ifOutOctets_diff'] ?? 0) . ') ';
830+
$port_info_string .= 'pkts(' . Number::formatSi($current_port_stats['ifInUcastPkts_rate'] ?? 0, 2, 3, 'pps') . '/' . Number::formatSi($current_port_stats['ifOutUcastPkts_rate'] ?? 0, 2, 0, 'pps') . ') ';
832831

833832
// Update data stores
834833
$rrd_name = Rrd::portName($port_id, '');
@@ -903,7 +902,7 @@
903902
if ($current_oid != $port[$oid]) {
904903
// If data has changed, build a query
905904
$port['update'][$oid] = $current_oid;
906-
echo 'PAgP ';
905+
$port_info_string .= 'PAgP ';
907906
Eventlog::log("$oid -> " . $current_oid, $device['device_id'], 'interface', Severity::Notice, $port['port_id']);
908907
}
909908
}
@@ -944,7 +943,7 @@
944943
// End Update Database
945944
}
946945

947-
echo "\n";
946+
Log::info($port_info_string);
948947

949948
// Clear Per-Port Variables Here
950949
unset($this_port, $port);

0 commit comments

Comments
 (0)