Last active
April 7, 2020 23:21
-
-
Save moorer2k/d822b9ff0e0fe067cf95e3e54f873be5 to your computer and use it in GitHub Desktop.
Use Symfony to execute lm_sensors and return a json response.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output:
{ Warn: "89.0", Crit: "105.0", Temps: [ { core_0: "35.0" }, { core_1: "28.0" } ] }