Files
librenms-librenms/includes/polling/customoid.inc.php

60 lines
1.9 KiB
PHP
Raw Normal View History

<?php
use LibreNMS\RRD\RrdDefinition;
2020-09-21 15:59:34 +02:00
foreach (dbFetchRows('SELECT * FROM `customoids` WHERE `customoid_passed` = 1 AND `device_id` = ?', [$device['device_id']]) as $customoid) {
d_echo($customoid);
$prev_oid_value = $customoid['customoid_current'];
$rawdata = snmp_get($device, $customoid['customoid_oid'], '-Oqv');
2020-09-21 15:43:38 +02:00
$user_funcs = [
2020-09-21 15:59:34 +02:00
'celsius_to_fahrenheit',
'fahrenheit_to_celsius',
'uw_to_dbm',
2020-09-21 15:43:38 +02:00
];
if (is_numeric($rawdata)) {
$os->enableGraph('customoid');
$oid_value = $rawdata;
} else {
$oid_value = 0;
2020-09-21 15:59:34 +02:00
$error = 'Invalid SNMP reply.';
}
if ($customoid['customoid_divisor'] && $oid_value !== 0) {
$oid_value = ($oid_value / $customoid['customoid_divisor']);
}
if ($customoid['customoid_multiplier']) {
$oid_value = ($oid_value * $customoid['customoid_multiplier']);
}
if (isset($customoid['user_func']) && in_array($customoid['user_func'], $user_funcs)) {
$oid_value = $customoid['user_func']($oid_value);
}
2020-09-21 15:43:38 +02:00
echo 'Custom OID ' . $customoid['customoid_descr'] . ': ';
echo $oid_value . ' ' . $customoid['customoid_unit'] . "\n";
2020-09-21 15:43:38 +02:00
$fields = [
'oid_value' => $oid_value,
2020-09-21 15:43:38 +02:00
];
2020-09-21 15:43:38 +02:00
$rrd_name = ['customoid', $customoid['customoid_descr']];
if ($customoid['customoid_datatype'] == 'COUNTER') {
$datatype = $customoid['customoid_datatype'];
} else {
$datatype = 'GAUGE';
}
$rrd_def = RrdDefinition::make()
->addDataset('oid_value', $datatype);
$tags = compact('rrd_name', 'rrd_def');
data_update($device, 'customoid', $tags, $fields);
2020-09-21 15:43:38 +02:00
dbUpdate(['customoid_current' => $oid_value, 'lastupdate' => ['NOW()'], 'customoid_prev' => $prev_oid_value], 'customoids', '`customoid_id` = ?', [$customoid['customoid_id']]);
}//end foreach
unset($customoid, $prev_oid_value, $rawdata, $user_funcs, $oid_value, $error, $fields, $rrd_def, $rrd_name, $tags);