php_analyzer.type_inference.infeasible_condition.generic
1 | <?php |
||
2 | /* |
||
3 | * LibreNMS module to capture statistics from the AT-NTP-MIB |
||
4 | * |
||
5 | * Copyright (c) 2018 Matt Read <[email protected]> |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify it |
||
8 | * under the terms of the GNU General Public License as published by the |
||
9 | * Free Software Foundation, either version 3 of the License, or (at your |
||
10 | * option) any later version. Please see LICENSE.txt at the top level of |
||
11 | * the source code distribution for details. |
||
12 | */ |
||
13 | |||
14 | $module = 'ntp'; |
||
15 | |||
16 | $component = new LibreNMS\Component(); |
||
17 | $components = $component->getComponents($device['device_id'], ['type'=>$module]); |
||
18 | |||
19 | // We only care about our device id. |
||
20 | $components = $components[$device['device_id']]; |
||
21 | |||
22 | // Begin our master array, all other values will be processed into this array. |
||
23 | $tblComponents = []; |
||
24 | |||
25 | // Let's gather some data.. |
||
26 | // For Reference: |
||
27 | // https://github.com/librenms/librenms/blob/master/mibs/awplus/AT-NTP-MIB |
||
28 | // https://www.alliedtelesis.com/documents/network-time-protocol-ntp-feature-overview-and-configuration-guide |
||
29 | $atNtpAssociationEntry = snmpwalk_group($device, 'atNtpAssociationEntry', 'AT-NTP-MIB'); |
||
30 | |||
31 | /* |
||
32 | * False == no object found - this is not an error, no objects exist |
||
33 | * null == timeout or something else that caused an error, there may be objects but we couldn't get it. |
||
34 | */ |
||
35 | if (is_null($atNtpAssociationEntry)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
36 | // We have to error here or we will end up deleting all our components. |
||
37 | echo "Error\n"; |
||
38 | } else { |
||
39 | // No Error, lets process things. |
||
40 | d_echo("Objects Found:\n"); |
||
41 | |||
42 | // Let's grab the index for each NTP peer |
||
43 | foreach ($atNtpAssociationEntry as $index => $value) { |
||
44 | $result = []; |
||
45 | $result['UID'] = (string) $index; // This is cast as a string so it can be compared with the database value. |
||
46 | $result['peer'] = $atNtpAssociationEntry[$index]['atNtpAssociationPeerAddr']; |
||
47 | $result['port'] = '123'; // awplus only supports default NTP Port. |
||
48 | $result['stratum'] = $atNtpAssociationEntry[$index]['atNtpAssociationStratum']; |
||
49 | $result['peerref'] = $atNtpAssociationEntry[$index]['atNtpAssociationRefClkAddr']; |
||
50 | $result['label'] = $result['peer'] . ':' . $result['port']; |
||
51 | |||
52 | // Set the status, 16 = Bad |
||
53 | if ($result['stratum'] == 16) { |
||
54 | $result['status'] = 2; |
||
55 | $result['error'] = 'NTP is not in sync'; |
||
56 | } else { |
||
57 | $result['status'] = 0; |
||
58 | $result['error'] = ''; |
||
59 | } |
||
60 | |||
61 | d_echo('NTP Peer found: '); |
||
62 | d_echo($result); |
||
63 | $tblComponents[] = $result; |
||
64 | } |
||
65 | |||
66 | /* |
||
67 | * Ok, we have our 2 array's (Components and SNMP) now we need |
||
68 | * to compare and see what needs to be added/updated. |
||
69 | * |
||
70 | * Let's loop over the SNMP data to see if we need to ADD or UPDATE any components. |
||
71 | */ |
||
72 | foreach ($tblComponents as $key => $array) { |
||
73 | $component_key = false; |
||
74 | |||
75 | // Loop over our components to determine if the component exists, or we need to add it. |
||
76 | foreach ($components as $compid => $child) { |
||
77 | if ($child['UID'] === $array['UID']) { |
||
78 | $component_key = $compid; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | if (! $component_key) { |
||
83 | // The component doesn't exist, we need to ADD it - ADD. |
||
84 | $new_component = $component->createComponent($device['device_id'], $module); |
||
85 | $component_key = key($new_component); |
||
86 | $components[$component_key] = array_merge($new_component[$component_key], $array); |
||
87 | echo '+'; |
||
88 | } else { |
||
89 | // The component does exist, merge the details in - UPDATE. |
||
90 | $components[$component_key] = array_merge($components[$component_key], $array); |
||
91 | echo '.'; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /* |
||
96 | * Loop over the Component data to see if we need to DELETE any components. |
||
97 | */ |
||
98 | foreach ($components as $key => $array) { |
||
99 | // Guilty until proven innocent |
||
100 | $found = false; |
||
101 | |||
102 | foreach ($tblComponents as $k => $v) { |
||
103 | if ($array['UID'] == $v['UID']) { |
||
104 | // Yay, we found it... |
||
105 | $found = true; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if ($found === false) { |
||
110 | // The component has not been found. we should delete it. |
||
111 | echo '-'; |
||
112 | $component->deleteComponent($key); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | // Write the Components back to the DB. |
||
117 | $component->setComponentPrefs($device['device_id'], $components); |
||
118 | echo "\n"; |
||
119 | } // End if not error |
||
120 | |||
121 | $module = strtolower($module); |
||
122 | if (count($components) > 0) { |
||
123 | if (dbFetchCell('SELECT COUNT(*) FROM `applications` WHERE `device_id` = ? AND `app_type` = ?', [$device['device_id'], $module]) == '0') { |
||
124 | dbInsert(['device_id' => $device['device_id'], 'app_type' => $module, 'app_status' => '', 'app_instance' => ''], 'applications'); |
||
125 | } |
||
126 | } else { |
||
127 | dbDelete('applications', '`device_id` = ? AND `app_type` = ?', [$device['device_id'], $module]); |
||
128 | } |
||
129 |