mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Sensors move can skip and output into singleton (#16392)
* Sensors move can skip and output into singleton * style * rrd_type --------- Co-authored-by: PipoCanaja <38363551+PipoCanaja@users.noreply.github.com>
This commit is contained in:
@@ -27,6 +27,8 @@ namespace App\Discovery;
|
|||||||
|
|
||||||
use App\Models\Device;
|
use App\Models\Device;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use LibreNMS\Config;
|
||||||
use LibreNMS\DB\SyncsModels;
|
use LibreNMS\DB\SyncsModels;
|
||||||
|
|
||||||
class Sensor
|
class Sensor
|
||||||
@@ -47,10 +49,19 @@ class Sensor
|
|||||||
|
|
||||||
public function discover(\App\Models\Sensor $sensor): static
|
public function discover(\App\Models\Sensor $sensor): static
|
||||||
{
|
{
|
||||||
|
if ($this->canSkip($sensor)) {
|
||||||
|
Log::info('~');
|
||||||
|
Log::debug("Skipped Sensor: $sensor");
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
$sensor->device_id ??= \DeviceCache::getPrimary()->device_id;
|
$sensor->device_id ??= \DeviceCache::getPrimary()->device_id;
|
||||||
$this->models->push($sensor);
|
$this->models->push($sensor);
|
||||||
$this->discovered[$sensor->syncGroup()] = false;
|
$this->discovered[$sensor->syncGroup()] = false;
|
||||||
|
|
||||||
|
Log::debug("Discovered Sensor: $sensor");
|
||||||
|
Log::info("$sensor->sensor_descr: Cur $sensor->sensor_current, Low: $sensor->sensor_limit_low, Low Warn: $sensor->sensor_limit_low_warn, Warn: $sensor->sensor_limit_warn, High: $sensor->sensor_limit");
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,4 +88,23 @@ class Sensor
|
|||||||
{
|
{
|
||||||
return $this->models;
|
return $this->models;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function canSkip(\App\Models\Sensor $sensor): bool
|
||||||
|
{
|
||||||
|
if (! empty($sensor->sensor_class) && (Config::getOsSetting($this->device->os, "disabled_sensors.$sensor->sensor_class") || Config::get("disabled_sensors.$sensor->sensor_class"))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
foreach (Config::getCombined($this->device->os, 'disabled_sensors_regex') as $skipRegex) {
|
||||||
|
if (! empty($sensor->sensor_descr) && preg_match($skipRegex, $sensor->sensor_descr)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (Config::getCombined($this->device->os, "disabled_sensors_regex.$sensor->sensor_class") as $skipRegex) {
|
||||||
|
if (! empty($sensor->sensor_descr) && preg_match($skipRegex, $sensor->sensor_descr)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -143,4 +143,23 @@ class Sensor extends DeviceRelatedModel implements Keyable
|
|||||||
{
|
{
|
||||||
return "$this->sensor_class-$this->poller_type";
|
return "$this->sensor_class-$this->poller_type";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$data = $this->only([
|
||||||
|
'sensor_oid',
|
||||||
|
'sensor_index',
|
||||||
|
'sensor_type',
|
||||||
|
'sensor_descr',
|
||||||
|
'poller_type',
|
||||||
|
'sensor_divisor',
|
||||||
|
'sensor_multiplier',
|
||||||
|
'entPhysicalIndex',
|
||||||
|
'sensor_current',
|
||||||
|
]);
|
||||||
|
$data[] = "(limits: LL: $this->sensor_limit_low, LW: $this->sensor_limit_low_warn, W: $this->sensor_limit_warn, H: $this->sensor_limit)";
|
||||||
|
$data[] = "rrd_type = $this->rrd_type";
|
||||||
|
|
||||||
|
return implode(', ', $data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -208,11 +208,6 @@ function discover_sensor($unused, $class, $device, $oid, $index, $type, $descr,
|
|||||||
if (! is_numeric($divisor)) {
|
if (! is_numeric($divisor)) {
|
||||||
$divisor = 1;
|
$divisor = 1;
|
||||||
}
|
}
|
||||||
if (can_skip_sensor($device, $class, $descr)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
app('sensor-discovery')->discover(new \App\Models\Sensor([
|
app('sensor-discovery')->discover(new \App\Models\Sensor([
|
||||||
'poller_type' => $poller_type,
|
'poller_type' => $poller_type,
|
||||||
@@ -635,7 +630,7 @@ function discovery_process($os, $sensor_class, $pre_cache)
|
|||||||
$discovery = $os->getDiscovery('sensors');
|
$discovery = $os->getDiscovery('sensors');
|
||||||
$device = $os->getDeviceArray();
|
$device = $os->getDeviceArray();
|
||||||
|
|
||||||
if (! empty($discovery[$sensor_class]) && ! can_skip_sensor($device, $sensor_class, '')) {
|
if (! empty($discovery[$sensor_class]) && ! app('sensor-discovery')->canSkip(new \App\Models\Sensor(['sensor_class' => $sensor_class]))) {
|
||||||
$sensor_options = [];
|
$sensor_options = [];
|
||||||
if (isset($discovery[$sensor_class]['options'])) {
|
if (isset($discovery[$sensor_class]['options'])) {
|
||||||
$sensor_options = $discovery[$sensor_class]['options'];
|
$sensor_options = $discovery[$sensor_class]['options'];
|
||||||
@@ -747,7 +742,6 @@ function discovery_process($os, $sensor_class, $pre_cache)
|
|||||||
$value = ($value / $divisor) * $multiplier;
|
$value = ($value / $divisor) * $multiplier;
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "$descr: Cur $value, Low: $low_limit, Low Warn: $low_warn_limit, Warn: $warn_limit, High: $high_limit" . PHP_EOL;
|
|
||||||
$entPhysicalIndex = YamlDiscovery::replaceValues('entPhysicalIndex', $index, null, $data, $pre_cache) ?: null;
|
$entPhysicalIndex = YamlDiscovery::replaceValues('entPhysicalIndex', $index, null, $data, $pre_cache) ?: null;
|
||||||
$entPhysicalIndex_measured = isset($data['entPhysicalIndex_measured']) ? $data['entPhysicalIndex_measured'] : null;
|
$entPhysicalIndex_measured = isset($data['entPhysicalIndex_measured']) ? $data['entPhysicalIndex_measured'] : null;
|
||||||
|
|
||||||
@@ -974,33 +968,6 @@ function add_cbgp_peer($device, $peer, $afi, $safi)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* check if we should skip this sensor from discovery
|
|
||||||
*
|
|
||||||
* @param $device
|
|
||||||
* @param string $sensor_class
|
|
||||||
* @param string $sensor_descr
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
function can_skip_sensor($device, $sensor_class = '', $sensor_descr = '')
|
|
||||||
{
|
|
||||||
if (! empty($sensor_class) && (Config::getOsSetting($device['os'], "disabled_sensors.$sensor_class") || Config::get("disabled_sensors.$sensor_class"))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
foreach (Config::getCombined($device['os'], 'disabled_sensors_regex') as $skipRegex) {
|
|
||||||
if (! empty($sensor_descr) && preg_match($skipRegex, $sensor_descr)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach (Config::getCombined($device['os'], "disabled_sensors_regex.$sensor_class") as $skipRegex) {
|
|
||||||
if (! empty($sensor_descr) && preg_match($skipRegex, $sensor_descr)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if we should skip this device from discovery
|
* check if we should skip this device from discovery
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user