<?php

/*
 * cpu temp for raspberry pi
 * requires snmp extend agent script from librenms-agent
 */

use Illuminate\Support\Str;

$sensor_oid = '.1.3.6.1.4.1.8072.1.3.2.4.1.2.9.114.97.115.112.98.101.114.114.121.1';
$value = SnmpQuery::get($sensor_oid)->value();
$value = trim((string) $value, '"');
if (is_numeric($value)) {
    $sensor_type = 'raspberry_temp';
    $descr = 'CPU Temp';
    discover_sensor(null, 'temperature', $device, $sensor_oid, 1, $sensor_type, $descr, 1, 1, null, null, null, null, $value);
}

if (Str::startsWith($device['sysObjectID'], '.1.3.6.1.4.1.232.')) {
    echo 'HP_ILO ';
    $oids = snmp_walk($device, '.1.3.6.1.4.1.232.6.2.6.8.1.2.1', '-Osqn', '');
    $oids = trim((string) $oids);
    foreach (explode("\n", $oids) as $data) {
        $data = trim($data);
        if ($data != '') {
            [$oid] = explode(' ', $data);
            $split_oid = explode('.', $oid);
            $temperature_id = $split_oid[count($split_oid) - 2] . '.' . $split_oid[count($split_oid) - 1];

            $descr_oid = ".1.3.6.1.4.1.232.6.2.6.8.1.3.$temperature_id";
            $descr = SnmpQuery::mibDir('hp')->get($descr_oid)->value();

            $temperature_oid = ".1.3.6.1.4.1.232.6.2.6.8.1.4.$temperature_id";
            $temperature = SnmpQuery::get($temperature_oid)->value();

            $threshold_oid = ".1.3.6.1.4.1.232.6.2.6.8.1.5.$temperature_id";
            $threshold = SnmpQuery::get($threshold_oid)->value();

            if (! empty($temperature)) {
                discover_sensor(null, 'temperature', $device, $temperature_oid, $oid, 'hpilo', $descr, '1', '1', null, null, null, $threshold, $temperature);
            }
        }
    }
}

if (preg_match('/(Linux).+(ntc)/', (string) $device['sysDescr'])) {
    $sensor_type = 'chip_axp209_temperature';
    $oid = '.1.3.6.1.4.1.8072.1.3.2.4.1.2.10.112.111.119.101.114.45.115.116.97.';
    $lowlimit = -40;
    $lowwarnlimit = -35;
    $warnlimit = 120;
    $limit = 130;
    $descr = 'AXP209 Temperature';
    $index = '116.1';
    $value = SnmpQuery::get($oid . $index)->value();
    if (is_numeric($value)) {
        discover_sensor(null, 'temperature', $device, $oid . $index, $index, $sensor_type, $descr, '1', '1', $lowlimit, $lowwarnlimit, $warnlimit, $limit, $value);
    }
}

include base_path('includes/discovery/sensors/temperature/supermicro.inc.php');
