Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Tidy up sensor discovery #5550

Merged
merged 17 commits into from Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions includes/discovery/functions.inc.php
Expand Up @@ -953,3 +953,35 @@ function ignore_storage($descr)

return $deny;
}

/**
* @param $types
* @param $device
* @param array $pre_cache
*/
function sensors($types, $device, $pre_cache = array())
{
global $config;
if (is_array($types)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid the is_array check by casting $types to array foreach ((array)$types as $type)

foreach ($types as $type) {
echo ucfirst($type) . ': ';

$dir = $config['install_dir'] . '/includes/discovery/sensors/' . $type .'/';

if (is_file($dir . $device['os_group'] . '.inc.php')) {
include_once $dir . $device['os_group'] . '.inc.php';
}
if (is_file($dir . $device['os'] . '.inc.php')) {
include_once $dir . $device['os'] . '.inc.php';
}
if (isset($config['modules_compat']['rfc1628'][$device['os']]) && $config['modules_compat']['rfc1628'][$device['os']]) {
if (is_file($dir . '/rfc1628.inc.php')) {
include_once $dir . '/rfc1628.inc.php';
}
}
d_echo($valid['sensor'][$type]);
check_valid_sensors($device, $type, $valid['sensor']);
echo "\n";
}
}
}
39 changes: 25 additions & 14 deletions includes/discovery/sensors.inc.php
Expand Up @@ -5,6 +5,7 @@
// Pre-cache data for later use
require 'includes/discovery/sensors/pre-cache.inc.php';

// Run custom sensors
require 'includes/discovery/sensors/cisco-entity-sensor.inc.php';
require 'includes/discovery/sensors/entity-sensor.inc.php';
require 'includes/discovery/sensors/ipmi.inc.php';
Expand All @@ -17,17 +18,27 @@
include 'includes/discovery/sensors/openbsd.inc.php';
}

require 'includes/discovery/sensors/temperatures.inc.php';
require 'includes/discovery/sensors/humidity.inc.php';
require 'includes/discovery/sensors/voltages.inc.php';
require 'includes/discovery/sensors/frequencies.inc.php';
require 'includes/discovery/sensors/runtime.inc.php';
require 'includes/discovery/sensors/current.inc.php';
require 'includes/discovery/sensors/power.inc.php';
require 'includes/discovery/sensors/fanspeeds.inc.php';
require 'includes/discovery/sensors/charge.inc.php';
require 'includes/discovery/sensors/load.inc.php';
require 'includes/discovery/sensors/states.inc.php';
require 'includes/discovery/sensors/dbm.inc.php';
require 'includes/discovery/sensors/signal.inc.php';
require 'includes/discovery/sensors/airflow.inc.php';
if (strstr($device['hardware'], 'Dell')) {
include 'includes/discovery/sensors/fanspeeds/dell.inc.php';
include 'includes/discovery/sensors/states/dell.inc.php';
include 'includes/discovery/sensors/temperatures/dell.inc.php';
}

$run_sensors = array(
'airflow',
'current',
'charge',
'dbm',
'fanspeeds',
'frequencies',
'humidity',
'load',
'power',
'runtime',
'signal',
'states',
'temperatures',
'voltages',
);
sensors($run_sensors, $device, $pre_cache);
unset($pre_cache);
36 changes: 0 additions & 36 deletions includes/discovery/sensors/airflow.inc.php

This file was deleted.

2 changes: 1 addition & 1 deletion includes/discovery/sensors/airflow/apc.inc.php
Expand Up @@ -23,7 +23,7 @@
* @author Neil Lathwood <neil@lathwood.co.uk>
*/

foreach ($cooling_unit_analog as $index => $data) {
foreach ($pre_cache['cooling_unit_analog'] as $index => $data) {
$cur_oid = '.1.3.6.1.4.1.318.1.1.27.1.4.1.2.1.3.' . $index;
$descr = $data['coolingUnitStatusAnalogDescription'];
$scale = $data['coolingUnitStatusAnalogScale'];
Expand Down
13 changes: 0 additions & 13 deletions includes/discovery/sensors/charge.inc.php

This file was deleted.

58 changes: 28 additions & 30 deletions includes/discovery/sensors/charge/apc.inc.php
@@ -1,47 +1,45 @@
<?php

if ($device['os'] == 'apc') {
$oids = snmp_get($device, '.1.3.6.1.4.1.318.1.1.1.2.3.1.0', '-OsqnU', '');
$oids = snmp_get($device, '.1.3.6.1.4.1.318.1.1.1.2.3.1.0', '-OsqnU');
d_echo($oids."\n");

// Try High-Precision First
if (!empty($oids)) {
echo 'APC UPS Battery Charge High Precision';
$type = 'apc';
list($oid,$current) = explode(' ', $oids);

$precision = 10;
$sensorType = 'apc';
$current_oid = '.1.3.6.1.4.1.318.1.1.1.2.3.1.0';
$index = 0;
$current_val = ($current / $precision);
$limit = 100;
$lowlimit = 0;
$warnlimit = 10;
$descr = 'Battery Charge';

discover_sensor($valid['sensor'], 'charge', $device, $current_oid, $index, $sensorType, $descr, $precision, '1', $lowlimit, $warnlimit, null, $limit, $current_val);
} else {
// Try to just get capacity
$oids = snmp_get($device, '.1.3.6.1.4.1.318.1.1.1.2.2.1.0', '-OsqnU');
d_echo($oids."\n");

// Try High-Precision First
if (!empty($oids)) {
echo 'APC UPS Battery Charge High Precision';
echo 'APC UPS Battery Charge';
$type = 'apc';
list($oid,$current) = explode(' ', $oids);

$precision = 10;
$precision = 1;
$sensorType = 'apc';
$current_oid = '.1.3.6.1.4.1.318.1.1.1.2.3.1.0';
$current_oid = '.1.3.6.1.4.1.318.1.1.1.2.2.1.0';
$index = 0;
$current_val = ($current / $precision);
$current_val = $current;
$limit = 100;
$lowlimit = 0;
$warnlimit = 10;
$descr = 'Battery Charge';

discover_sensor($valid['sensor'], 'charge', $device, $current_oid, $index, $sensorType, $descr, $precision, '1', $lowlimit, $warnlimit, null, $limit, $current_val);
} else {
// Try to just get capacity
$oids = snmp_get($device, '.1.3.6.1.4.1.318.1.1.1.2.2.1.0', '-OsqnU', '');
d_echo($oids."\n");

if (!empty($oids)) {
echo 'APC UPS Battery Charge';
$type = 'apc';
list($oid,$current) = explode(' ', $oids);

$precision = 1;
$sensorType = 'apc';
$current_oid = '.1.3.6.1.4.1.318.1.1.1.2.2.1.0';
$index = 0;
$current_val = $current;
$limit = 100;
$lowlimit = 0;
$warnlimit = 10;
$descr = 'Battery Charge';

discover_sensor($valid['sensor'], 'charge', $device, $current_oid, $index, $sensorType, $descr, $precision, '1', $lowlimit, $warnlimit, null, $limit, $current_val);
}
}//end if
}
}//end if
29 changes: 13 additions & 16 deletions includes/discovery/sensors/charge/dsm.inc.php
@@ -1,9 +1,8 @@
<?php

/*
* LibreNMS
*
* Copyright (c) 2015 Steve Calv�rio <https://github.com/Calvario/>
* Copyright (c) 2015 Steve Calv�rio <https://github.com/Calvario/>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand All @@ -12,21 +11,19 @@
* the source code distribution for details.
*/

if ($device['os'] == 'dsm') {
echo 'DSM UPS Charge';
echo 'DSM UPS Charge';

// UPS Device Manufacturer, example return : SNMPv2-SMI::enterprises.6574.4.1.2.0 = STRING: "American Power Conversion"
$ups_device_manufacturer_oid = '.1.3.6.1.4.1.6574.4.1.2.0';
$ups_device_manufacturer = str_replace('"', '', snmp_get($device, $ups_device_manufacturer_oid, '-Oqv'));
// UPS Device Model, example return : SNMPv2-SMI::enterprises.6574.4.1.1.0 = STRING: "Back-UPS RS 900G"
$ups_device_model_oid = '.1.3.6.1.4.1.6574.4.1.1.0';
$ups_device_model = str_replace('"', '', snmp_get($device, $ups_device_model_oid, '-Oqv'));
// UPS Device Manufacturer, example return : SNMPv2-SMI::enterprises.6574.4.1.2.0 = STRING: "American Power Conversion"
$ups_device_manufacturer_oid = '.1.3.6.1.4.1.6574.4.1.2.0';
$ups_device_manufacturer = str_replace('"', '', snmp_get($device, $ups_device_manufacturer_oid, '-Oqv'));
// UPS Device Model, example return : SNMPv2-SMI::enterprises.6574.4.1.1.0 = STRING: "Back-UPS RS 900G"
$ups_device_model_oid = '.1.3.6.1.4.1.6574.4.1.1.0';
$ups_device_model = str_replace('"', '', snmp_get($device, $ups_device_model_oid, '-Oqv'));


// UPS Battery Charge Value, example return : SNMPv2-SMI::enterprises.6574.4.3.1.1.0 = Opaque: Float: 100.000000
$ups_charge_oid = '.1.3.6.1.4.1.6574.4.3.1.1.0';
$ups_charge = snmp_get($device, $ups_charge_oid, '-Oqv');
if (is_numeric($ups_charge)) {
discover_sensor($valid['sensor'], 'charge', $device, $ups_charge_oid, 'UPSChargeValue', $ups_device_manufacturer.' '.$ups_device_model, 'UPS Charge Value', '1', '1', 0, 10, null, 100, $ups_charge);
}
// UPS Battery Charge Value, example return : SNMPv2-SMI::enterprises.6574.4.3.1.1.0 = Opaque: Float: 100.000000
$ups_charge_oid = '.1.3.6.1.4.1.6574.4.3.1.1.0';
$ups_charge = snmp_get($device, $ups_charge_oid, '-Oqv');
if (is_numeric($ups_charge)) {
discover_sensor($valid['sensor'], 'charge', $device, $ups_charge_oid, 'UPSChargeValue', $ups_device_manufacturer.' '.$ups_device_model, 'UPS Charge Value', '1', '1', 0, 10, null, 100, $ups_charge);
}
54 changes: 26 additions & 28 deletions includes/discovery/sensors/charge/netagent2.inc.php
Expand Up @@ -23,34 +23,32 @@
* @author Tony Murray <murraytony@gmail.com>
*/

if ($device['os'] == 'netagent2') {
$charge_oid = '.1.3.6.1.4.1.935.1.1.1.2.2.1.0';
$charge = snmp_get($device, $charge_oid, '-Osqnv');
$charge_oid = '.1.3.6.1.4.1.935.1.1.1.2.2.1.0';
$charge = snmp_get($device, $charge_oid, '-Osqnv');

if (!empty($charge)) {
$type = 'netagent2';
$index = 0;
$limit = 100;
$lowlimit = 0;
$lowwarnlimit = 10;
$divisor = 1;
$descr = 'Battery Charge';
if (!empty($charge)) {
$type = 'netagent2';
$index = 0;
$limit = 100;
$lowlimit = 0;
$lowwarnlimit = 10;
$divisor = 1;
$descr = 'Battery Charge';

discover_sensor(
$valid['sensor'],
'charge',
$device,
$charge_oid,
$index,
$type,
$descr,
$divisor,
1,
$lowlimit,
$lowwarnlimit,
null,
$limit,
$charge
);
}
discover_sensor(
$valid['sensor'],
'charge',
$device,
$charge_oid,
$index,
$type,
$descr,
$divisor,
1,
$lowlimit,
$lowwarnlimit,
null,
$limit,
$charge
);
}
36 changes: 17 additions & 19 deletions includes/discovery/sensors/charge/rfc1628.inc.php
@@ -1,26 +1,24 @@
<?php

// RFC1628 UPS
if (isset($config['modules_compat']['rfc1628'][$device['os']]) && $config['modules_compat']['rfc1628'][$device['os']]) {
echo 'RFC1628 ';
echo 'RFC1628 ';

$oids = snmp_walk($device, '.1.3.6.1.2.1.33.1.2.4', '-Osqn', 'UPS-MIB');
d_echo($oids."\n");
$oids = snmp_walk($device, '.1.3.6.1.2.1.33.1.2.4', '-Osqn', 'UPS-MIB');
d_echo($oids."\n");

$oids = trim($oids);
foreach (explode("\n", $oids) as $data) {
$data = trim($data);
if ($data) {
list($oid,$descr) = explode(' ', $data, 2);
$split_oid = explode('.', $oid);
$current_id = $split_oid[(count($split_oid) - 1)];
$current_oid = ".1.3.6.1.2.1.33.1.2.4.$current_id";
$current = snmp_get($device, $current_oid, '-O vq');
$descr = 'Battery charge remaining'.(count(explode("\n", $oids)) == 1 ? '' : ' '.($current_id + 1));
$type = 'rfc1628';
$index = (500 + $current_id);
$oids = trim($oids);
foreach (explode("\n", $oids) as $data) {
$data = trim($data);
if ($data) {
list($oid,$descr) = explode(' ', $data, 2);
$split_oid = explode('.', $oid);
$current_id = $split_oid[(count($split_oid) - 1)];
$current_oid = ".1.3.6.1.2.1.33.1.2.4.$current_id";
$current = snmp_get($device, $current_oid, '-O vq');
$descr = 'Battery charge remaining'.(count(explode("\n", $oids)) == 1 ? '' : ' '.($current_id + 1));
$type = 'rfc1628';
$index = (500 + $current_id);

discover_sensor($valid['sensor'], 'charge', $device, $current_oid, $index, $type, $descr, '1', '1', null, null, null, null, $current);
}
discover_sensor($valid['sensor'], 'charge', $device, $current_oid, $index, $type, $descr, '1', '1', null, null, null, null, $current);
}
}//end if
}