mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Sensors remove reliance on global variable (#16344)
* Sensors remove reliance on global variable * Apply fixes from StyleCI * Clear the instance instead of reset. Remove $valid['sensors'] from docs --------- Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
This commit is contained in:
@@ -197,10 +197,8 @@ function discover_device(&$device, $force_module = false)
|
||||
//end discover_device()
|
||||
|
||||
// Discover sensors
|
||||
function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = 1, $multiplier = 1, $low_limit = null, $low_warn_limit = null, $warn_limit = null, $high_limit = null, $current = null, $poller_type = 'snmp', $entPhysicalIndex = null, $entPhysicalIndex_measured = null, $user_func = null, $group = null, $rrd_type = 'GAUGE')
|
||||
function discover_sensor($unused, $class, $device, $oid, $index, $type, $descr, $divisor = 1, $multiplier = 1, $low_limit = null, $low_warn_limit = null, $warn_limit = null, $high_limit = null, $current = null, $poller_type = 'snmp', $entPhysicalIndex = null, $entPhysicalIndex_measured = null, $user_func = null, $group = null, $rrd_type = 'GAUGE'): bool
|
||||
{
|
||||
$guess_limits = Config::get('sensors.guess_limits', true);
|
||||
|
||||
$low_limit = set_null($low_limit);
|
||||
$low_warn_limit = set_null($low_warn_limit);
|
||||
$warn_limit = set_null($warn_limit);
|
||||
@@ -216,257 +214,31 @@ function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr,
|
||||
|
||||
d_echo("Discover sensor: $oid, $index, $type, $descr, $poller_type, $divisor, $multiplier, $entPhysicalIndex, $current, (limits: LL: $low_limit, LW: $low_warn_limit, W: $warn_limit, H: $high_limit), rrd_type = $rrd_type \n");
|
||||
|
||||
if (isset($warn_limit, $low_warn_limit) && $low_warn_limit > $warn_limit) {
|
||||
// Fix high/low thresholds (i.e. on negative numbers)
|
||||
[$warn_limit, $low_warn_limit] = [$low_warn_limit, $warn_limit];
|
||||
}
|
||||
app('sensor-discovery')->discover(new \App\Models\Sensor([
|
||||
'poller_type' => $poller_type,
|
||||
'sensor_class' => $class,
|
||||
'device_id' => $device['device_id'],
|
||||
'sensor_oid' => $oid,
|
||||
'sensor_index' => $index,
|
||||
'sensor_type' => $type,
|
||||
'sensor_descr' => $descr,
|
||||
'sensor_divisor' => $divisor,
|
||||
'sensor_multiplier' => $multiplier,
|
||||
'sensor_limit' => $high_limit,
|
||||
'sensor_limit_warn' => $warn_limit,
|
||||
'sensor_limit_low' => $low_limit,
|
||||
'sensor_limit_low_warn' => $low_warn_limit,
|
||||
'sensor_current' => $current,
|
||||
'entPhysicalIndex' => $entPhysicalIndex,
|
||||
'entPhysicalIndex_measured' => $entPhysicalIndex_measured,
|
||||
'user_func' => $user_func,
|
||||
'group' => $group,
|
||||
'rrd_type' => $rrd_type,
|
||||
]));
|
||||
|
||||
if (dbFetchCell('SELECT COUNT(sensor_id) FROM `sensors` WHERE `poller_type`= ? AND `sensor_class` = ? AND `device_id` = ? AND sensor_type = ? AND `sensor_index` = ?', [$poller_type, $class, $device['device_id'], $type, (string) $index]) == '0') {
|
||||
if ($guess_limits && is_null($high_limit)) {
|
||||
$high_limit = sensor_limit($class, $current);
|
||||
}
|
||||
|
||||
if ($guess_limits && is_null($low_limit)) {
|
||||
$low_limit = sensor_low_limit($class, $current);
|
||||
}
|
||||
|
||||
if (! is_null($high_limit) && $low_limit > $high_limit) {
|
||||
// Fix high/low thresholds (i.e. on negative numbers)
|
||||
[$high_limit, $low_limit] = [$low_limit, $high_limit];
|
||||
}
|
||||
|
||||
$insert = [
|
||||
'poller_type' => $poller_type,
|
||||
'sensor_class' => $class,
|
||||
'device_id' => $device['device_id'],
|
||||
'sensor_oid' => $oid,
|
||||
'sensor_index' => $index,
|
||||
'sensor_type' => $type,
|
||||
'sensor_descr' => $descr,
|
||||
'sensor_divisor' => $divisor,
|
||||
'sensor_multiplier' => $multiplier,
|
||||
'sensor_limit' => $high_limit,
|
||||
'sensor_limit_warn' => $warn_limit,
|
||||
'sensor_limit_low' => $low_limit,
|
||||
'sensor_limit_low_warn' => $low_warn_limit,
|
||||
'sensor_current' => $current,
|
||||
'entPhysicalIndex' => $entPhysicalIndex,
|
||||
'entPhysicalIndex_measured' => $entPhysicalIndex_measured,
|
||||
'user_func' => $user_func,
|
||||
'group' => $group,
|
||||
'rrd_type' => $rrd_type,
|
||||
];
|
||||
|
||||
$inserted = dbInsert($insert, 'sensors');
|
||||
|
||||
d_echo("( $inserted inserted )\n");
|
||||
|
||||
echo '+';
|
||||
log_event('Sensor Added: ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr, $device, 'sensor', 3, $inserted);
|
||||
} else {
|
||||
$sensor_entry = dbFetchRow('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? AND `sensor_type` = ? AND `sensor_index` = ?', [$class, $device['device_id'], $type, (string) $index]);
|
||||
|
||||
if (! isset($high_limit)) {
|
||||
if ($guess_limits && ! $sensor_entry['sensor_limit']) {
|
||||
// Calculate a reasonable limit
|
||||
$high_limit = sensor_limit($class, $current);
|
||||
} else {
|
||||
// Use existing limit
|
||||
$high_limit = $sensor_entry['sensor_limit'];
|
||||
}
|
||||
}
|
||||
|
||||
if (! isset($low_limit)) {
|
||||
if ($guess_limits && ! $sensor_entry['sensor_limit_low']) {
|
||||
// Calculate a reasonable limit
|
||||
$low_limit = sensor_low_limit($class, $current);
|
||||
} else {
|
||||
// Use existing limit
|
||||
$low_limit = $sensor_entry['sensor_limit_low'];
|
||||
}
|
||||
}
|
||||
|
||||
// Fix high/low thresholds (i.e. on negative numbers)
|
||||
if (isset($high_limit) && $low_limit > $high_limit) {
|
||||
[$high_limit, $low_limit] = [$low_limit, $high_limit];
|
||||
}
|
||||
|
||||
if ((string) $high_limit != (string) $sensor_entry['sensor_limit'] && $sensor_entry['sensor_custom'] == 'No') {
|
||||
$update = ['sensor_limit' => $high_limit];
|
||||
$updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', [$sensor_entry['sensor_id']]);
|
||||
d_echo("( $updated updated )\n");
|
||||
|
||||
echo 'H';
|
||||
log_event('Sensor High Limit Updated: ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr . ' (' . $high_limit . ')', $device, 'sensor', 3, $sensor_entry['sensor_id']);
|
||||
}
|
||||
|
||||
if ((string) $sensor_entry['sensor_limit_low'] != (string) $low_limit && $sensor_entry['sensor_custom'] == 'No') {
|
||||
$update = ['sensor_limit_low' => $low_limit];
|
||||
$updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', [$sensor_entry['sensor_id']]);
|
||||
d_echo("( $updated updated )\n");
|
||||
|
||||
echo 'L';
|
||||
log_event('Sensor Low Limit Updated: ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr . ' (' . $low_limit . ')', $device, 'sensor', 3, $sensor_entry['sensor_id']);
|
||||
}
|
||||
|
||||
if ((string) $warn_limit != (string) $sensor_entry['sensor_limit_warn'] && $sensor_entry['sensor_custom'] == 'No') {
|
||||
$update = ['sensor_limit_warn' => $warn_limit];
|
||||
$updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', [$sensor_entry['sensor_id']]);
|
||||
d_echo("( $updated updated )\n");
|
||||
|
||||
echo 'WH';
|
||||
log_event('Sensor Warn High Limit Updated: ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr . ' (' . $warn_limit . ')', $device, 'sensor', 3, $sensor_entry['sensor_id']);
|
||||
}
|
||||
|
||||
if ((string) $sensor_entry['sensor_limit_low_warn'] != (string) $low_warn_limit && $sensor_entry['sensor_custom'] == 'No') {
|
||||
$update = ['sensor_limit_low_warn' => $low_warn_limit];
|
||||
$updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', [$sensor_entry['sensor_id']]);
|
||||
d_echo("( $updated updated )\n");
|
||||
|
||||
echo 'WL';
|
||||
log_event('Sensor Warn Low Limit Updated: ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr . ' (' . $low_warn_limit . ')', $device, 'sensor', 3, $sensor_entry['sensor_id']);
|
||||
}
|
||||
|
||||
if ($oid == $sensor_entry['sensor_oid'] &&
|
||||
$descr == $sensor_entry['sensor_descr'] &&
|
||||
$multiplier == $sensor_entry['sensor_multiplier'] &&
|
||||
$divisor == $sensor_entry['sensor_divisor'] &&
|
||||
$entPhysicalIndex_measured == $sensor_entry['entPhysicalIndex_measured'] &&
|
||||
$entPhysicalIndex == $sensor_entry['entPhysicalIndex'] &&
|
||||
$user_func == $sensor_entry['user_func'] &&
|
||||
$group == $sensor_entry['group']
|
||||
|
||||
) {
|
||||
echo '.';
|
||||
} else {
|
||||
$update = [
|
||||
'sensor_oid' => $oid,
|
||||
'sensor_descr' => $descr,
|
||||
'sensor_multiplier' => $multiplier,
|
||||
'sensor_divisor' => $divisor,
|
||||
'entPhysicalIndex' => $entPhysicalIndex,
|
||||
'entPhysicalIndex_measured' => $entPhysicalIndex_measured,
|
||||
'user_func' => $user_func,
|
||||
'group' => $group,
|
||||
];
|
||||
$updated = dbUpdate($update, 'sensors', '`sensor_id` = ?', [$sensor_entry['sensor_id']]);
|
||||
echo 'U';
|
||||
log_event('Sensor Updated: ' . $class . ' ' . $type . ' ' . $index . ' ' . $descr, $device, 'sensor', 3, $sensor_entry['sensor_id']);
|
||||
d_echo("( $updated updated )\n");
|
||||
}
|
||||
}//end if
|
||||
$valid[$class][$type][$index] = 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
//end discover_sensor()
|
||||
|
||||
function sensor_low_limit($class, $current)
|
||||
{
|
||||
// matching an empty case executes code until a break is reached
|
||||
switch ($class) {
|
||||
case 'temperature':
|
||||
$limit = $current - 10;
|
||||
break;
|
||||
case 'voltage':
|
||||
$limit = $current * 0.85;
|
||||
break;
|
||||
case 'humidity':
|
||||
$limit = 30;
|
||||
break;
|
||||
case 'fanspeed':
|
||||
$limit = $current * 0.80;
|
||||
break;
|
||||
case 'power_factor':
|
||||
$limit = -1;
|
||||
break;
|
||||
case 'signal':
|
||||
$limit = -80;
|
||||
break;
|
||||
case 'airflow':
|
||||
case 'snr':
|
||||
case 'frequency':
|
||||
case 'pressure':
|
||||
case 'cooling':
|
||||
$limit = $current * 0.95;
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}//end switch
|
||||
|
||||
return round($limit, 11);
|
||||
}
|
||||
|
||||
//end sensor_low_limit()
|
||||
|
||||
function sensor_limit($class, $current)
|
||||
{
|
||||
// matching an empty case executes code until a break is reached
|
||||
switch ($class) {
|
||||
case 'temperature':
|
||||
$limit = $current + 20;
|
||||
break;
|
||||
case 'voltage':
|
||||
$limit = $current * 1.15;
|
||||
break;
|
||||
case 'humidity':
|
||||
$limit = 70;
|
||||
break;
|
||||
case 'fanspeed':
|
||||
$limit = $current * 1.80;
|
||||
break;
|
||||
case 'power_factor':
|
||||
$limit = 1;
|
||||
break;
|
||||
case 'signal':
|
||||
$limit = -30;
|
||||
break;
|
||||
case 'load':
|
||||
$limit = 80;
|
||||
break;
|
||||
case 'airflow':
|
||||
case 'snr':
|
||||
case 'frequency':
|
||||
case 'pressure':
|
||||
case 'cooling':
|
||||
$limit = $current * 1.05;
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}//end switch
|
||||
|
||||
return round($limit, 11);
|
||||
}
|
||||
|
||||
//end sensor_limit()
|
||||
|
||||
function check_valid_sensors($device, $class, $valid, $poller_type = 'snmp')
|
||||
{
|
||||
$entries = dbFetchRows('SELECT * FROM sensors AS S, devices AS D WHERE S.sensor_class=? AND S.device_id = D.device_id AND D.device_id = ? AND S.poller_type = ?', [$class, $device['device_id'], $poller_type]);
|
||||
|
||||
if (count($entries)) {
|
||||
foreach ($entries as $entry) {
|
||||
$index = $entry['sensor_index'];
|
||||
$type = $entry['sensor_type'];
|
||||
$class = $entry['sensor_class'];
|
||||
d_echo($index . ' -> ' . $type . "\n");
|
||||
|
||||
if (! $valid[$class][$type][$index]) {
|
||||
echo '-';
|
||||
if ($class == 'state') {
|
||||
dbDelete('sensors_to_state_indexes', '`sensor_id` = ?', [$entry['sensor_id']]);
|
||||
}
|
||||
dbDelete('sensors', '`sensor_id` = ?', [$entry['sensor_id']]);
|
||||
log_event('Sensor Deleted: ' . $entry['sensor_class'] . ' ' . $entry['sensor_type'] . ' ' . $entry['sensor_index'] . ' ' . $entry['sensor_descr'], $device, 'sensor', 3, $entry['sensor_id']);
|
||||
}
|
||||
|
||||
unset($oid);
|
||||
unset($type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//end check_valid_sensors()
|
||||
|
||||
function discover_juniAtmVp(&$valid, $device, $port_id, $vp_id, $vp_descr)
|
||||
{
|
||||
d_echo("Discover Juniper ATM VP: $port_id, $vp_id, $vp_descr\n");
|
||||
@@ -854,12 +626,11 @@ function ignore_storage($os, $descr)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $valid
|
||||
* @param OS $os
|
||||
* @param $sensor_type
|
||||
* @param $pre_cache
|
||||
*/
|
||||
function discovery_process(&$valid, $os, $sensor_class, $pre_cache)
|
||||
function discovery_process($os, $sensor_class, $pre_cache)
|
||||
{
|
||||
$discovery = $os->getDiscovery('sensors');
|
||||
$device = $os->getDeviceArray();
|
||||
@@ -998,7 +769,7 @@ function discovery_process(&$valid, $os, $sensor_class, $pre_cache)
|
||||
}
|
||||
}
|
||||
|
||||
discover_sensor($valid['sensor'], $sensor_class, $device, $oid, $uindex, $sensor_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value, 'snmp', $entPhysicalIndex, $entPhysicalIndex_measured, $user_function, $group, $data['rrd_type'] ?? 'GAUGE');
|
||||
discover_sensor(null, $sensor_class, $device, $oid, $uindex, $sensor_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value, 'snmp', $entPhysicalIndex, $entPhysicalIndex_measured, $user_function, $group, $data['rrd_type'] ?? 'GAUGE');
|
||||
|
||||
if ($sensor_class === 'state') {
|
||||
create_sensor_to_state_index($device, $sensor_name, $uindex);
|
||||
@@ -1014,7 +785,7 @@ function discovery_process(&$valid, $os, $sensor_class, $pre_cache)
|
||||
* @param OS $os
|
||||
* @param array $pre_cache
|
||||
*/
|
||||
function sensors($types, $os, $valid, $pre_cache = [])
|
||||
function sensors($types, $os, $pre_cache = [])
|
||||
{
|
||||
$device = &$os->getDeviceArray();
|
||||
foreach ((array) $types as $sensor_class) {
|
||||
@@ -1032,9 +803,8 @@ function sensors($types, $os, $valid, $pre_cache = [])
|
||||
include $dir . '/rfc1628.inc.php';
|
||||
}
|
||||
}
|
||||
discovery_process($valid, $os, $sensor_class, $pre_cache);
|
||||
d_echo($valid['sensor'][$sensor_class] ?? []);
|
||||
check_valid_sensors($device, $sensor_class, $valid['sensor']);
|
||||
discovery_process($os, $sensor_class, $pre_cache);
|
||||
app('sensor-discovery')->sync(sensor_class: $sensor_class, poller_type: 'snmp');
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user