first commit

This commit is contained in:
xoy 2023-04-21 02:43:40 +02:00
commit a94b10ce8d
2 changed files with 68 additions and 0 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# LM-Sensors-JSON
Ein PHP Script, welches aus den Sensordaten des Linux Paketes LM-Sensors eine JSON Datei ausgibt.

65
index.php Normal file
View File

@ -0,0 +1,65 @@
<?php
$sensor_data = explode("\n", shell_exec('sensors'));
function get_lines($data) {
$lines = array();
$new_line = '';
$first = TRUE;
foreach ($data as $line) {
if($line != '' && $line[0] == " ")
$new_line .= $line;
else {
if(!$first) {
$lines[] = explode('(', str_replace(' ', '', $new_line))[0];
} else {
$first = FALSE;
}
$new_line = $line;
}
}
return $lines;
}
function get_blocks($lines) {
$blocks = array();
$new_block = array();
foreach ($lines as $line) {
if($line == '')
$blocks[] = $new_block;
else {
if(str_contains($line, ':'))
$new_block[] = explode(':', $line);
else
$new_block[] = $line;
}
}
return $blocks;
}
function generate_json($blocks) {
$json_array = array();
foreach ($blocks as $block) {
$block_json_array = array();
$block_title = '';
foreach($block as $item) {
if(gettype($item) == 'array') {
$block_json_array = array_merge($block_json_array, array($item[0] => $item[1]));
}
else
$block_title = $item;
}
$json_array = array_merge($json_array, array($block_title => $block_json_array));
}
return json_encode($json_array);
}
header('Content-Type: application/json');
echo generate_json(get_blocks(get_lines($sensor_data)));