1 | <?php |
||
2 | /** |
||
3 | * This program is free software: you can redistribute it and/or modify it |
||
4 | * under the terms of the GNU General Public License as published by the |
||
5 | * Free Software Foundation, either version 3 of the License, or (at your |
||
6 | * option) any later version. Please see LICENSE.txt at the top level of |
||
7 | * the source code distribution for details. |
||
8 | * |
||
9 | * LibreNMS module to capture Cisco QFP Statistics |
||
10 | * |
||
11 | * @link https://www.librenms.org |
||
12 | * |
||
13 | * @copyright 2019 LibreNMS |
||
14 | * @author Pavle Obradovic <[email protected]> |
||
15 | */ |
||
16 | if ($device['os_group'] == 'cisco') { |
||
17 | $module = 'cisco-qfp'; |
||
18 | |||
19 | /* |
||
20 | * CISCO-ENTITY-QFP-MIB::ceqfpSystemState values |
||
21 | */ |
||
22 | $system_states = [ |
||
23 | 1 => 'unknown', |
||
24 | 2 => 'reset', |
||
25 | 3 => 'init', |
||
26 | 4 => 'active', |
||
27 | 5 => 'activeSolo', |
||
28 | 6 => 'standby', |
||
29 | 7 => 'hotStandby', |
||
30 | ]; |
||
31 | |||
32 | /* |
||
33 | * CISCO-ENTITY-QFP-MIB::ceqfpSystemTrafficDirection values |
||
34 | */ |
||
35 | $system_traffic_direction = [ |
||
36 | 1 => 'none', |
||
37 | 2 => 'ingress', |
||
38 | 3 => 'egress', |
||
39 | 4 => 'both', |
||
40 | ]; |
||
41 | |||
42 | /* |
||
43 | * Get module's components for a device |
||
44 | */ |
||
45 | $component = new LibreNMS\Component(); |
||
46 | $components = $component->getComponents($device['device_id'], ['type'=>$module]); |
||
47 | $components = $components[$device['device_id']]; |
||
48 | |||
49 | /* |
||
50 | * Walk through CISCO-ENTITY-QFP-MIB::ceqfpSystemTable |
||
51 | */ |
||
52 | $qfp_general_data = snmpwalk_group($device, 'ceqfpSystemTable', 'CISCO-ENTITY-QFP-MIB'); |
||
53 | if ($qfp_general_data) { |
||
0 ignored issues
–
show
|
|||
54 | /* |
||
55 | * Loop through SNMP data and add or update components |
||
56 | */ |
||
57 | foreach ($qfp_general_data as $qfp_index => $data) { |
||
58 | /* |
||
59 | * Get entPhysicalName for QFP |
||
60 | */ |
||
61 | $qfp_name_oid = '.1.3.6.1.2.1.47.1.1.1.1.7.' . $qfp_index; |
||
62 | $qfp_name_data = snmp_get_multi_oid($device, [$qfp_name_oid]); |
||
63 | $qfp_name = $qfp_name_data[$qfp_name_oid]; |
||
64 | |||
65 | /* |
||
66 | * Component data array for `component_prefs` |
||
67 | */ |
||
68 | $component_data = [ |
||
69 | 'label' => 'qfp_' . $qfp_index, |
||
70 | 'entPhysicalIndex' => $qfp_index, |
||
71 | 'name' => $qfp_name, |
||
72 | 'traffic_direction' => $system_traffic_direction[$data['ceqfpSystemTrafficDirection']], |
||
73 | 'system_state' => $system_states[$data['ceqfpSystemState']], |
||
74 | 'system_loads' => $data['ceqfpNumberSystemLoads'], |
||
75 | 'system_last_load' => $data['ceqfpSystemLastLoadTime'], |
||
76 | ]; |
||
77 | |||
78 | /* |
||
79 | * Find existing component ID if QFP is already known |
||
80 | */ |
||
81 | $component_id = false; |
||
82 | foreach ($components as $tmp_component_id => $tmp_component) { |
||
83 | if ($tmp_component['entPhysicalIndex'] == $qfp_index) { |
||
84 | $component_id = $tmp_component_id; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /* |
||
89 | * If $component_id is false QFP Component doesn't exist |
||
90 | * Create new component and add it to $components array |
||
91 | */ |
||
92 | if (! $component_id) { |
||
93 | $new_component = $component->createComponent($device['device_id'], $module); |
||
94 | $component_id = key($new_component); |
||
95 | $components[$component_id] = array_merge($new_component[$component_id], $component_data); |
||
96 | echo '+'; |
||
97 | } else { |
||
98 | $components[$component_id] = array_merge($components[$component_id], $component_data); |
||
99 | echo '.'; |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /* |
||
105 | * Loop trough components, check against SNMP QFP indexes and delete if needed |
||
106 | */ |
||
107 | foreach ($components as $tmp_component_id => $tmp_component) { |
||
108 | $found = in_array($tmp_component['entPhysicalIndex'], array_keys($qfp_general_data)); |
||
109 | if (! $found) { |
||
110 | $component->deleteComponent($tmp_component_id); |
||
111 | echo '-'; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /* |
||
116 | * Save components |
||
117 | */ |
||
118 | $component->setComponentPrefs($device['device_id'], $components); |
||
119 | |||
120 | echo "\n"; |
||
121 | } |
||
122 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.