Total Complexity | 60 |
Total Lines | 622 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Sensor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sensor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Sensor implements DiscoveryModule, PollerModule |
||
35 | { |
||
36 | protected static $name = 'Sensor'; |
||
37 | protected static $table = 'sensors'; |
||
38 | protected static $data_name = 'sensor'; |
||
39 | protected static $translation_prefix = 'sensors'; |
||
40 | |||
41 | private $valid = true; |
||
42 | |||
43 | private $sensor_id; |
||
44 | |||
45 | private $type; |
||
46 | private $device_id; |
||
47 | private $oids; |
||
48 | private $subtype; |
||
49 | private $index; |
||
50 | private $description; |
||
51 | private $current; |
||
52 | private $multiplier; |
||
53 | private $divisor; |
||
54 | private $aggregator; |
||
55 | private $high_limit; |
||
56 | private $low_limit; |
||
57 | private $high_warn; |
||
58 | private $low_warn; |
||
59 | private $entPhysicalIndex; |
||
60 | private $entPhysicalMeasured; |
||
61 | |||
62 | /** |
||
63 | * Sensor constructor. Create a new sensor to be discovered. |
||
64 | * |
||
65 | * @param string $type Class of this sensor, must be a supported class |
||
66 | * @param int $device_id the device_id of the device that owns this sensor |
||
67 | * @param array|string $oids an array or single oid that contains the data for this sensor |
||
68 | * @param string $subtype the type of sensor an additional identifier to separate out sensors of the same class, generally this is the os name |
||
69 | * @param int|string $index the index of this sensor, must be stable, generally the index of the oid |
||
70 | * @param string $description A user visible description of this sensor, may be truncated in some places (like graphs) |
||
71 | * @param int|float $current The current value of this sensor, will seed the db and may be used to guess limits |
||
72 | * @param int $multiplier a number to multiply the value(s) by |
||
73 | * @param int $divisor a number to divide the value(s) by |
||
74 | * @param string $aggregator an operation to combine multiple numbers. Supported: sum, avg |
||
75 | * @param int|float $high_limit Alerting: Maximum value |
||
76 | * @param int|float $low_limit Alerting: Minimum value |
||
77 | * @param int|float $high_warn Alerting: High warning value |
||
78 | * @param int|float $low_warn Alerting: Low warning value |
||
79 | * @param int|float $entPhysicalIndex The entPhysicalIndex this sensor is associated, often a port |
||
80 | * @param int|float $entPhysicalMeasured the table to look for the entPhysicalIndex, for example 'ports' (maybe unused) |
||
81 | */ |
||
82 | public function __construct( |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Save this sensor to the database. |
||
140 | * |
||
141 | * @return int the sensor_id of this sensor in the database |
||
142 | */ |
||
143 | final public function save() |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Fetch the sensor from the database. |
||
180 | * If it doesn't exist, returns null. |
||
181 | * |
||
182 | * @return array|null |
||
183 | */ |
||
184 | private function fetch() |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get the table for this sensor |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getTable() |
||
210 | { |
||
211 | return static::$table; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get an array of this sensor with fields that line up with the database. |
||
216 | * Excludes sensor_id and current |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | protected function toArray() |
||
221 | { |
||
222 | return [ |
||
223 | 'sensor_class' => $this->type, |
||
224 | 'device_id' => $this->device_id, |
||
225 | 'sensor_oids' => json_encode($this->oids), |
||
226 | 'sensor_index' => $this->index, |
||
227 | 'sensor_type' => $this->subtype, |
||
228 | 'sensor_descr' => $this->description, |
||
229 | 'sensor_divisor' => $this->divisor, |
||
230 | 'sensor_multiplier' => $this->multiplier, |
||
231 | 'sensor_aggregator' => $this->aggregator, |
||
232 | 'sensor_limit' => $this->high_limit, |
||
233 | 'sensor_limit_warn' => $this->high_warn, |
||
234 | 'sensor_limit_low' => $this->low_limit, |
||
235 | 'sensor_limit_low_warn' => $this->low_warn, |
||
236 | 'sensor_current' => $this->current, |
||
237 | 'entPhysicalIndex' => $this->entPhysicalIndex, |
||
238 | 'entPhysicalIndex_measured' => $this->entPhysicalMeasured, |
||
239 | ]; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Escape null values so dbFacile doesn't mess them up |
||
244 | * honestly, this should be the default, but could break shit |
||
245 | * |
||
246 | * @param array $array |
||
247 | * @return array |
||
248 | */ |
||
249 | private function escapeNull($array) |
||
250 | { |
||
251 | return array_map(function ($value) { |
||
252 | return is_null($value) ? ['NULL'] : $value; |
||
253 | }, $array); |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Run Sensors discovery for the supplied OS (device) |
||
258 | * |
||
259 | * @param OS $os |
||
260 | */ |
||
261 | public static function runDiscovery(OS $os) |
||
262 | { |
||
263 | // Add discovery types here |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Poll sensors for the supplied OS (device) |
||
268 | * |
||
269 | * @param OS $os |
||
270 | */ |
||
271 | public static function poll(OS $os) |
||
272 | { |
||
273 | $table = static::$table; |
||
274 | |||
275 | $query = "SELECT * FROM `$table` WHERE `device_id` = ?"; |
||
276 | $params = [$os->getDeviceId()]; |
||
277 | |||
278 | $submodules = Config::get('poller_submodules.wireless', []); |
||
279 | if (! empty($submodules)) { |
||
280 | $query .= ' AND `sensor_class` IN ' . dbGenPlaceholders(count($submodules)); |
||
281 | $params = array_merge($params, $submodules); |
||
282 | } |
||
283 | |||
284 | // fetch and group sensors, decode oids |
||
285 | $sensors = array_reduce( |
||
286 | dbFetchRows($query, $params), |
||
287 | function ($carry, $sensor) { |
||
288 | $sensor['sensor_oids'] = json_decode($sensor['sensor_oids']); |
||
289 | $carry[$sensor['sensor_class']][] = $sensor; |
||
290 | |||
291 | return $carry; |
||
292 | }, |
||
293 | [] |
||
294 | ); |
||
295 | |||
296 | foreach ($sensors as $type => $type_sensors) { |
||
297 | // check for custom polling |
||
298 | $typeInterface = static::getPollingInterface($type); |
||
299 | if (! interface_exists($typeInterface)) { |
||
300 | echo "ERROR: Polling Interface doesn't exist! $typeInterface\n"; |
||
301 | } |
||
302 | |||
303 | // fetch custom data |
||
304 | if ($os instanceof $typeInterface) { |
||
305 | unset($sensors[$type]); // remove from sensors array to prevent double polling |
||
306 | static::pollSensorType($os, $type, $type_sensors); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | // pre-fetch all standard sensors |
||
311 | $standard_sensors = collect($sensors)->flatten(1)->all(); |
||
312 | $pre_fetch = self::fetchSnmpData($os->getDeviceArray(), $standard_sensors); |
||
313 | |||
314 | // poll standard sensors |
||
315 | foreach ($sensors as $type => $type_sensors) { |
||
316 | static::pollSensorType($os, $type, $type_sensors, $pre_fetch); |
||
317 | } |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Poll all sensors of a specific class |
||
322 | * |
||
323 | * @param OS $os |
||
324 | * @param string $type |
||
325 | * @param array $sensors |
||
326 | * @param array $prefetch |
||
327 | */ |
||
328 | protected static function pollSensorType($os, $type, $sensors, $prefetch = []) |
||
329 | { |
||
330 | echo "$type:\n"; |
||
331 | |||
332 | // process data or run custom polling |
||
333 | $typeInterface = static::getPollingInterface($type); |
||
334 | if ($os instanceof $typeInterface) { |
||
335 | d_echo("Using OS polling for $type\n"); |
||
336 | $function = static::getPollingMethod($type); |
||
337 | $data = $os->$function($sensors); |
||
338 | } else { |
||
339 | $data = static::processSensorData($sensors, $prefetch); |
||
340 | } |
||
341 | |||
342 | d_echo($data); |
||
343 | |||
344 | self::recordSensorData($os, $sensors, $data); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Fetch snmp data from the device |
||
349 | * Return an array keyed by oid |
||
350 | * |
||
351 | * @param array $device |
||
352 | * @param array $sensors |
||
353 | * @return array |
||
354 | */ |
||
355 | private static function fetchSnmpData($device, $sensors) |
||
356 | { |
||
357 | $oids = self::getOidsFromSensors($sensors, get_device_oid_limit($device)); |
||
358 | |||
359 | $snmp_data = []; |
||
360 | foreach ($oids as $oid_chunk) { |
||
361 | $multi_data = snmp_get_multi_oid($device, $oid_chunk, '-OUQnt'); |
||
362 | $snmp_data = array_merge($snmp_data, $multi_data); |
||
363 | } |
||
364 | |||
365 | // deal with string values that may be surrounded by quotes, scientific number format and remove non numerical characters |
||
366 | array_walk($snmp_data, function (&$oid) { |
||
367 | preg_match('/-?\d+(\.\d+)?(e-?\d+)?/i', $oid, $matches); |
||
368 | if (isset($matches[0])) { |
||
369 | $oid = cast_number($matches[0]); |
||
370 | } else { |
||
371 | $oid = trim('"', $oid); // allow string only values |
||
372 | } |
||
373 | }); |
||
374 | |||
375 | return $snmp_data; |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Process the snmp data for the specified sensors |
||
380 | * Returns an array sensor_id => value |
||
381 | * |
||
382 | * @param array $sensors |
||
383 | * @param array $prefetch |
||
384 | * @return array |
||
385 | * |
||
386 | * @internal param $device |
||
387 | */ |
||
388 | protected static function processSensorData($sensors, $prefetch) |
||
389 | { |
||
390 | $sensor_data = []; |
||
391 | foreach ($sensors as $sensor) { |
||
392 | // pull out the data for this sensor |
||
393 | $requested_oids = array_flip($sensor['sensor_oids']); |
||
394 | $data = array_intersect_key($prefetch, $requested_oids); |
||
395 | |||
396 | // if no data set null and continue to the next sensor |
||
397 | if (empty($data)) { |
||
398 | $data[$sensor['sensor_id']] = null; |
||
399 | continue; |
||
400 | } |
||
401 | |||
402 | if (count($data) > 1) { |
||
403 | // aggregate data |
||
404 | if ($sensor['sensor_aggregator'] == 'avg') { |
||
405 | $sensor_value = array_sum($data) / count($data); |
||
406 | } else { |
||
407 | // sum |
||
408 | $sensor_value = array_sum($data); |
||
409 | } |
||
410 | } else { |
||
411 | $sensor_value = current($data); |
||
412 | } |
||
413 | |||
414 | if ($sensor['sensor_divisor'] && $sensor_value !== 0) { |
||
415 | $sensor_value = ($sensor_value / $sensor['sensor_divisor']); |
||
416 | } |
||
417 | |||
418 | if ($sensor['sensor_multiplier']) { |
||
419 | $sensor_value = ($sensor_value * $sensor['sensor_multiplier']); |
||
420 | } |
||
421 | |||
422 | $sensor_data[$sensor['sensor_id']] = $sensor_value; |
||
423 | } |
||
424 | |||
425 | return $sensor_data; |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * Get a list of unique oids from an array of sensors and break it into chunks. |
||
430 | * |
||
431 | * @param array $sensors |
||
432 | * @param int $chunk How many oids per chunk. Default 10. |
||
433 | * @return array |
||
434 | */ |
||
435 | private static function getOidsFromSensors($sensors, $chunk = 10) |
||
436 | { |
||
437 | // Sort the incoming oids and sensors |
||
438 | $oids = array_reduce($sensors, function ($carry, $sensor) { |
||
439 | return array_merge($carry, $sensor['sensor_oids']); |
||
440 | }, []); |
||
441 | |||
442 | // only unique oids and chunk |
||
443 | $oids = array_chunk(array_keys(array_flip($oids)), $chunk); |
||
444 | |||
445 | return $oids; |
||
446 | } |
||
447 | |||
448 | protected static function discoverType(OS $os, $type) |
||
449 | { |
||
450 | $typeInterface = static::getDiscoveryInterface($type); |
||
451 | if (! interface_exists($typeInterface)) { |
||
452 | echo "ERROR: Discovery Interface doesn't exist! $typeInterface\n"; |
||
453 | } |
||
454 | |||
455 | $have_discovery = $os instanceof $typeInterface; |
||
456 | if ($have_discovery) { |
||
457 | echo "$type: "; |
||
458 | $function = static::getDiscoveryMethod($type); |
||
459 | $sensors = $os->$function(); |
||
460 | if (! is_array($sensors)) { |
||
461 | c_echo("%RERROR:%n $function did not return an array! Skipping discovery."); |
||
462 | $sensors = []; |
||
463 | } |
||
464 | } else { |
||
465 | $sensors = []; // delete non existent sensors |
||
466 | } |
||
467 | |||
468 | self::checkForDuplicateSensors($sensors); |
||
469 | |||
470 | self::sync($os->getDeviceId(), $type, $sensors); |
||
471 | |||
472 | if ($have_discovery) { |
||
473 | echo PHP_EOL; |
||
474 | } |
||
475 | } |
||
476 | |||
477 | private static function checkForDuplicateSensors($sensors) |
||
478 | { |
||
479 | $duplicate_check = []; |
||
480 | $dup = false; |
||
481 | |||
482 | foreach ($sensors as $sensor) { |
||
483 | /** @var Sensor $sensor */ |
||
484 | $key = $sensor->getUniqueId(); |
||
485 | if (isset($duplicate_check[$key])) { |
||
486 | c_echo("%R ERROR:%n A sensor already exists at this index $key "); |
||
487 | $dup = true; |
||
488 | } |
||
489 | $duplicate_check[$key] = 1; |
||
490 | } |
||
491 | |||
492 | return $dup; |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Returns a string that must be unique for each sensor |
||
497 | * type (class), subtype (type), index (index) |
||
498 | * |
||
499 | * @return string |
||
500 | */ |
||
501 | private function getUniqueId() |
||
504 | } |
||
505 | |||
506 | protected static function getDiscoveryInterface($type) |
||
507 | { |
||
508 | return str_to_class($type, 'LibreNMS\\Interfaces\\Discovery\\Sensors\\') . 'Discovery'; |
||
509 | } |
||
510 | |||
511 | protected static function getDiscoveryMethod($type) |
||
512 | { |
||
513 | return 'discover' . str_to_class($type); |
||
514 | } |
||
515 | |||
516 | protected static function getPollingInterface($type) |
||
517 | { |
||
518 | return str_to_class($type, 'LibreNMS\\Interfaces\\Polling\\Sensors\\') . 'Polling'; |
||
519 | } |
||
520 | |||
521 | protected static function getPollingMethod($type) |
||
522 | { |
||
523 | return 'poll' . str_to_class($type); |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Is this sensor valid? |
||
528 | * If not, it should not be added to or in the database |
||
529 | * |
||
530 | * @return bool |
||
531 | */ |
||
532 | public function isValid() |
||
533 | { |
||
534 | return $this->valid; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Save sensors and remove invalid sensors |
||
539 | * This the sensors array should contain all the sensors of a specific class |
||
540 | * It may contain sensors from multiple tables and devices, but that isn't the primary use |
||
541 | * |
||
542 | * @param int $device_id |
||
543 | * @param string $type |
||
544 | * @param array $sensors |
||
545 | */ |
||
546 | final public static function sync($device_id, $type, array $sensors) |
||
547 | { |
||
548 | // save and collect valid ids |
||
549 | $valid_sensor_ids = []; |
||
550 | foreach ($sensors as $sensor) { |
||
551 | /** @var $this $sensor */ |
||
552 | if ($sensor->isValid()) { |
||
553 | $valid_sensor_ids[] = $sensor->save(); |
||
554 | } |
||
555 | } |
||
556 | |||
557 | // delete invalid sensors |
||
558 | self::clean($device_id, $type, $valid_sensor_ids); |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Remove invalid sensors. Passing an empty array will remove all sensors of that class |
||
563 | * |
||
564 | * @param int $device_id |
||
565 | * @param string $type |
||
566 | * @param array $sensor_ids valid sensor ids |
||
567 | */ |
||
568 | private static function clean($device_id, $type, $sensor_ids) |
||
569 | { |
||
570 | $table = static::$table; |
||
571 | $params = [$device_id, $type]; |
||
572 | $where = '`device_id`=? AND `sensor_class`=?'; |
||
573 | |||
574 | if (! empty($sensor_ids)) { |
||
575 | $where .= ' AND `sensor_id` NOT IN ' . dbGenPlaceholders(count($sensor_ids)); |
||
576 | $params = array_merge($params, $sensor_ids); |
||
577 | } |
||
578 | |||
579 | $delete = dbFetchRows("SELECT * FROM `$table` WHERE $where", $params); |
||
580 | foreach ($delete as $sensor) { |
||
581 | echo '-'; |
||
582 | |||
583 | $message = static::$name; |
||
584 | $message .= " Deleted: $type {$sensor['sensor_type']} {$sensor['sensor_index']} {$sensor['sensor_descr']}"; |
||
585 | log_event($message, $device_id, static::$table, 3, $sensor['sensor_id']); |
||
586 | } |
||
587 | if (! empty($delete)) { |
||
588 | dbDelete($table, $where, $params); |
||
589 | } |
||
590 | } |
||
591 | |||
592 | /** |
||
593 | * Return a list of valid types with metadata about each type |
||
594 | * $class => array( |
||
595 | * 'short' - short text for this class |
||
596 | * 'long' - long text for this class |
||
597 | * 'unit' - units used by this class 'dBm' for example |
||
598 | * 'icon' - font awesome icon used by this class |
||
599 | * ) |
||
600 | * |
||
601 | * @param bool $valid filter this list by valid types in the database |
||
602 | * @param int $device_id when filtering, only return types valid for this device_id |
||
603 | * @return array |
||
604 | */ |
||
605 | public static function getTypes($valid = false, $device_id = null) |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * Record sensor data in the database and data stores |
||
612 | * |
||
613 | * @param OS $os |
||
614 | * @param array $sensors |
||
615 | * @param array $data |
||
616 | */ |
||
617 | protected static function recordSensorData(OS $os, $sensors, $data) |
||
656 | } |
||
657 | } |
||
658 | } |
||
659 |