Skip to content

Instantly share code, notes, and snippets.

@moorer2k
Last active April 7, 2020 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moorer2k/d822b9ff0e0fe067cf95e3e54f873be5 to your computer and use it in GitHub Desktop.
Save moorer2k/d822b9ff0e0fe067cf95e3e54f873be5 to your computer and use it in GitHub Desktop.
Use Symfony to execute lm_sensors and return a json response.
<?php
namespace App;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class GetCPUTemps
{
public function getTemps(){
$process = new Process('sensors');
$process->run();
if (!$process->isSuccessful()) {
return 'fail';
}
$sensors_out = $process->getOutput();
preg_match_all("/Core\s(?<Core>\N):\s+\+(?<Current>\N+)\s(?<Format>\w)\s+\(high\s=\s\+(?<Warn>\N+)\s\w,\scrit\s=\s\+(?<Crit>\N{5})\s\w/", $sensors_out, $matches);
for ($i = 0; $i < count($matches['Core']); $i++) {
$arrOut[$i] = ['core_'.$i => $matches['Current'][$i] ] ;
}
return json_encode((['Warn' => $matches['Warn'][0],
'Crit' => $matches['Crit'][0],
'Temps' => $arrOut,
]));
}
}
@moorer2k
Copy link
Author

Example output:

{ Warn: "89.0", Crit: "105.0", Temps: [ { core_0: "35.0" }, { core_1: "28.0" } ] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment