#!/usr/bin/env php
<?php

/*

  check_temper - Version 1.8
  Written by Robbie Ferguson for NEMS Linux
  -----
  1.8 - March 29, 2024 - Add perfdata to output, remove redundant state from output (exit code provides this)
  1.7 - June 2, 2020 - Correct the output from Humidity Sensor if no TEMPer is detected
  1.6 - June 2, 2020 - Enforce array processing of JSON data
  1.5 - June 1, 2020 - Connect with NEMS SST Calibration for NEMS Linux 1.6
  1.4 - January 31, 2020 - Add humidity sensor
  1.3 - August 4, 2019 - Automatically determine whether ARG is C or F and act accordingly
  1.2 - August 1, 2019 - Add UNKNOWN state if TEMPer is not connected
  1.1 - July 31, 2019 - Add exit codes
  1.0 - July 29, 2019 - Initial release

*/

  if (count($argv) >=5) {
    array_shift($argv);
    $data['crit_low'] = intval(array_shift($argv));
    $data['warn_low'] = intval(array_shift($argv));
    $data['warn_high'] = intval(array_shift($argv));
    $data['crit_high'] = intval(array_shift($argv));
    $data['type'] = (strtolower(array_shift($argv)) ?: 'temp');

    if ($data['warn_high'] > 59) {
      // Over 59 Degrees: we're working in Fahrenheit
      $type = 'F';
    } else {
      $type = 'C';
    }
  } else {
    echo 'NEMS Linux TEMPer Check' . PHP_EOL;
    echo PHP_EOL . 'Usage:                 ' . $argv[0] . ' crit_low warn_low warn_high crit_high [temp|hum]' . PHP_EOL;
    echo 'Example (Temperature): ' . $argv[0] . ' 10 20 39 45 temp' . PHP_EOL;
    echo 'Example (Humidity):    ' . $argv[0] . ' 20 35 65 80 hum' . PHP_EOL;
    echo PHP_EOL . 'For backward compatibility, if type is not specified, temp is assumed.' . PHP_EOL;
    exit(3);
  }
  $tmp = json_decode(shell_exec('/usr/local/bin/nems-info temper'), true);

// sample data
// $tmp = json_decode('{"0":{"vendorid":16701,"productid":8455,"manufacturer":"","product":"","busnum":1,"devnum":5,"devices":["hidraw0","hidraw1"],"firmware":"TEMPerGold_V3.1","hex_firmware":"54454d506572476f6c645f56332e3120","hex_data":"808009274e200000","internal temperature":23.43},"sensors":{"thermal":1},"output":{"temperature":23.43,"humidity":0}}',true);

  if (is_array($tmp) && isset($tmp[0])) {
    $temper = $tmp[0];
    $data['temperature']['C'] = round($tmp['output']['temperature'],1);
    $data['temperature']['F'] = round(($data['temperature']['C'] * 1.8 + 32),1);

    if ($data['type'] == 'hum' && !isset($temper['internal humidity'])) {
      if ($tmp['sensors']['thermal'] == 1) {
        echo 'Your TEMPer device does not have a humidity sensor.';
      } else {
        echo 'TEMPer device disconnected or not present.';
      }
      exit(3);
    } else {
      $data['humidity'] = round($tmp['output']['humidity'],1);
    }

    if ($data['type'] == 'temp') {
      $perfdata = "|Temperature[low]={$data['temperature'][$type]}${type};{$data['warn_low']};{$data['crit_low']};;";
      $perfdata .= " Temperature[high]={$data['temperature'][$type]}${type};{$data['warn_high']};{$data['crit_high']};;";
      if ($data['temperature'][$type] < $data['crit_low']) {
        echo $data['temperature']['C'] . '°C / ' . $data['temperature']['F'] . '°F (LOW)' . $perfdata;
        exit(2);
      } elseif ($data['temperature'][$type] < $data['warn_low']) {
        echo $data['temperature']['C'] . '°C / ' . $data['temperature']['F'] . '°F (LOW)' . $perfdata;
        exit(1);
      } elseif ($data['temperature'][$type] > $data['crit_high']) {
        echo $data['temperature']['C'] . '°C / ' . $data['temperature']['F'] . '°F (HIGH)' . $perfdata;
        exit(2);
      } elseif ($data['temperature'][$type] > $data['warn_high']) {
        echo $data['temperature']['C'] . '°C / ' . $data['temperature']['F'] . '°F (HIGH)' . $perfdata;
        exit(1);
      } else {
        echo $data['temperature']['C'] . '°C / ' . $data['temperature']['F'] . '°F' . $perfdata;
        exit(0);
      }
    } elseif ($data['type'] == 'hum') {
      $perfdata = "|Humidity[low]={$data['humidity']}%;{$data['warn_low']};{$data['crit_low']};;";
      $perfdata .= " Humidity[high]={$data['humidity']}%;{$data['warn_high']};{$data['crit_high']};;";
      if ($data['humidity'] < $data['crit_low']) {
        echo $data['humidity'] . '% Relative Humidity (LOW)' . $perfdata;
        exit(2);
      } elseif ($data['humidity'] < $data['warn_low']) {
        echo $data['humidity'] . '% Relative Humidity (LOW)' . $perfdata;
        exit(1);
      } elseif ($data['humidity'] > $data['crit_high']) {
        echo $data['humidity'] . '% Relative Humidity (HIGH)' . $perfdata;
        exit(2);
      } elseif ($data['humidity'] > $data['warn_high']) {
        echo $data['humidity'] . '% Relative Humidity (HIGH)' . $perfdata;
        exit(1);
      } else {
        echo $data['humidity'] . '% Relative Humidity' . $perfdata;
        exit(0);
      }
    } else {
      echo 'Invalid command type. Options are temp (temperature) or hum (humidity).';
      exit(3);
    }
  } else {
    echo 'TEMPer device disconnected or not present.';
    exit(3);
  }
?>
