1 | <?php |
||||||
2 | /** |
||||||
3 | * HostResources.php |
||||||
4 | * |
||||||
5 | * -Description- |
||||||
6 | * |
||||||
7 | * This program is free software: you can redistribute it and/or modify |
||||||
8 | * it under the terms of the GNU General Public License as published by |
||||||
9 | * the Free Software Foundation, either version 3 of the License, or |
||||||
10 | * (at your option) any later version. |
||||||
11 | * |
||||||
12 | * This program is distributed in the hope that it will be useful, |
||||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||||||
15 | * GNU General Public License for more details. |
||||||
16 | * |
||||||
17 | * You should have received a copy of the GNU General Public License |
||||||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||||
19 | * |
||||||
20 | * @link https://www.librenms.org |
||||||
21 | * |
||||||
22 | * @copyright 2018 Tony Murray |
||||||
23 | * @author Tony Murray <[email protected]> |
||||||
24 | */ |
||||||
25 | |||||||
26 | namespace LibreNMS\OS\Traits; |
||||||
27 | |||||||
28 | use App\Models\Mempool; |
||||||
29 | use Closure; |
||||||
30 | use Exception; |
||||||
31 | use Illuminate\Support\Str; |
||||||
32 | use LibreNMS\Device\Processor; |
||||||
33 | use Rrd; |
||||||
34 | |||||||
35 | trait HostResources |
||||||
36 | { |
||||||
37 | private $hrStorage; |
||||||
38 | private $memoryStorageTypes = [ |
||||||
39 | 'hrStorageVirtualMemory', |
||||||
40 | 'hrStorageRam', |
||||||
41 | 'hrStorageOther', |
||||||
42 | ]; |
||||||
43 | private $ignoreMemoryDescr = [ |
||||||
44 | 'MALLOC', |
||||||
45 | 'UMA', |
||||||
46 | 'procfs', |
||||||
47 | '/proc', |
||||||
48 | ]; |
||||||
49 | private $validOtherMemory = [ |
||||||
50 | 'Memory buffers', |
||||||
51 | 'Cached memory', |
||||||
52 | 'Shared memory', |
||||||
53 | ]; |
||||||
54 | private $memoryDescrWarn = [ |
||||||
55 | 'Cached memory' => 0, |
||||||
56 | 'Memory buffers' => 0, |
||||||
57 | 'Physical memory' => 99, |
||||||
58 | 'Real memory' => 90, |
||||||
59 | 'Shared memory' => 0, |
||||||
60 | 'Swap space' => 10, |
||||||
61 | 'Virtual memory' => 95, |
||||||
62 | ]; |
||||||
63 | |||||||
64 | /** |
||||||
65 | * Discover processors. |
||||||
66 | * Returns an array of LibreNMS\Device\Processor objects that have been discovered |
||||||
67 | * |
||||||
68 | * @return array Processors |
||||||
69 | */ |
||||||
70 | public function discoverProcessors() |
||||||
71 | { |
||||||
72 | echo 'Host Resources: '; |
||||||
73 | $processors = []; |
||||||
74 | |||||||
75 | try { |
||||||
76 | $hrProcessorLoad = $this->getCacheByIndex('hrProcessorLoad', 'HOST-RESOURCES-MIB'); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
77 | |||||||
78 | if (empty($hrProcessorLoad)) { |
||||||
79 | // no hr data, return |
||||||
80 | return []; |
||||||
81 | } |
||||||
82 | |||||||
83 | $hrDeviceDescr = $this->getCacheByIndex('hrDeviceDescr', 'HOST-RESOURCES-MIB'); |
||||||
84 | } catch (Exception $e) { |
||||||
85 | return []; |
||||||
86 | } |
||||||
87 | |||||||
88 | foreach ($hrProcessorLoad as $index => $usage) { |
||||||
89 | $usage_oid = '.1.3.6.1.2.1.25.3.3.1.2.' . $index; |
||||||
90 | $descr = $hrDeviceDescr[$index]; |
||||||
91 | |||||||
92 | if (! is_numeric($usage)) { |
||||||
93 | continue; |
||||||
94 | } |
||||||
95 | |||||||
96 | $device = $this->getDeviceArray(); |
||||||
0 ignored issues
–
show
It seems like
getDeviceArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
97 | if ($device['os'] == 'arista-eos' && $index == '1') { |
||||||
98 | continue; |
||||||
99 | } |
||||||
100 | |||||||
101 | if (empty($descr) |
||||||
102 | || $descr == 'Unknown Processor Type' // Windows: Unknown Processor Type |
||||||
103 | || $descr == 'An electronic chip that makes the computer work.' |
||||||
104 | ) { |
||||||
105 | $descr = 'Processor'; |
||||||
106 | } else { |
||||||
107 | // Make the description a bit shorter |
||||||
108 | $remove_strings = [ |
||||||
109 | 'GenuineIntel: ', |
||||||
110 | 'AuthenticAMD: ', |
||||||
111 | 'CPU ', |
||||||
112 | '(TM)', |
||||||
113 | '(R)', |
||||||
114 | ]; |
||||||
115 | $descr = str_replace($remove_strings, '', $descr); |
||||||
116 | $descr = str_replace(' ', ' ', $descr); |
||||||
117 | } |
||||||
118 | |||||||
119 | $old_name = ['hrProcessor', $index]; |
||||||
120 | $new_name = ['processor', 'hr', $index]; |
||||||
121 | Rrd::renameFile($this->getDeviceArray(), $old_name, $new_name); |
||||||
0 ignored issues
–
show
The method
renameFile() does not exist on App\Facades\Rrd . Since you implemented __callStatic , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
122 | |||||||
123 | $processor = Processor::discover( |
||||||
124 | 'hr', |
||||||
125 | $this->getDeviceId(), |
||||||
0 ignored issues
–
show
It seems like
getDeviceId() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
126 | $usage_oid, |
||||||
127 | $index, |
||||||
128 | $descr, |
||||||
129 | 1, |
||||||
130 | $usage, |
||||||
131 | null, |
||||||
132 | null, |
||||||
133 | $index |
||||||
134 | ); |
||||||
135 | |||||||
136 | if ($processor->isValid()) { |
||||||
137 | $processors[] = $processor; |
||||||
138 | } |
||||||
139 | } |
||||||
140 | |||||||
141 | return $processors; |
||||||
142 | } |
||||||
143 | |||||||
144 | public function discoverMempools() |
||||||
145 | { |
||||||
146 | $hr_storage = $this->getCacheTable('hrStorageTable', 'HOST-RESOURCES-MIB:HOST-RESOURCES-TYPES'); |
||||||
0 ignored issues
–
show
It seems like
getCacheTable() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
147 | |||||||
148 | if (! is_array($hr_storage)) { |
||||||
149 | return collect(); |
||||||
150 | } |
||||||
151 | |||||||
152 | $ram_bytes = snmp_get($this->getDeviceArray(), 'hrMemorySize.0', '-OQUv', 'HOST-RESOURCES-MIB') * 1024 |
||||||
153 | ?: (isset($hr_storage[1]['hrStorageSize']) ? $hr_storage[1]['hrStorageSize'] * $hr_storage[1]['hrStorageAllocationUnits'] : 0); |
||||||
154 | |||||||
155 | return collect($hr_storage)->filter(Closure::fromCallable([$this, 'memValid'])) |
||||||
156 | ->map(function ($storage, $index) use ($ram_bytes) { |
||||||
157 | $total = $storage['hrStorageSize']; |
||||||
158 | if (Str::contains($storage['hrStorageDescr'], 'Real Memory Metrics') || ($storage['hrStorageType'] == 'hrStorageOther' && $total != 0)) { |
||||||
159 | // use total RAM for buffers, cached, and shared |
||||||
160 | // bsnmp does not report the same as net-snmp, total RAM is stored in hrMemorySize |
||||||
161 | if ($ram_bytes) { |
||||||
162 | $total = $ram_bytes / $storage['hrStorageAllocationUnits']; // will be calculated with this entries allocation units later |
||||||
163 | } |
||||||
164 | } |
||||||
165 | |||||||
166 | return (new Mempool([ |
||||||
167 | 'mempool_index' => $index, |
||||||
168 | 'mempool_type' => 'hrstorage', |
||||||
169 | 'mempool_precision' => $storage['hrStorageAllocationUnits'], |
||||||
170 | 'mempool_descr' => $storage['hrStorageDescr'], |
||||||
171 | 'mempool_perc_warn' => $this->memoryDescrWarn[$storage['hrStorageDescr']] ?? 90, |
||||||
172 | 'mempool_used_oid' => ".1.3.6.1.2.1.25.2.3.1.6.$index", |
||||||
173 | 'mempool_total_oid' => null, |
||||||
174 | ]))->setClass(null, $storage['hrStorageType'] == 'hrStorageVirtualMemory' ? 'virtual' : 'system') |
||||||
175 | ->fillUsage($storage['hrStorageUsed'], $total); |
||||||
176 | }); |
||||||
177 | } |
||||||
178 | |||||||
179 | protected function memValid($storage) |
||||||
180 | { |
||||||
181 | if (! in_array($storage['hrStorageType'], $this->memoryStorageTypes)) { |
||||||
182 | return false; |
||||||
183 | } |
||||||
184 | |||||||
185 | if ($storage['hrStorageType'] == 'hrStorageOther' && ! in_array($storage['hrStorageDescr'], $this->validOtherMemory)) { |
||||||
186 | return false; |
||||||
187 | } |
||||||
188 | |||||||
189 | return ! Str::contains($storage['hrStorageDescr'], $this->ignoreMemoryDescr); |
||||||
190 | } |
||||||
191 | } |
||||||
192 |