make sysinfo go ajax

This commit is contained in:
dg9vh 2016-10-12 16:54:11 +00:00
parent 4e5e8ecb7d
commit 4204ac33bd
4 changed files with 207 additions and 2 deletions

174
ajax.php
View file

@ -46,4 +46,178 @@ if ($_GET['section'] == "localTx") {
}
echo '{"data": '.json_encode($lastHeard)."}";
}
if ($_GET['section'] == "sysinfo") {
$cputemp = NULL;
$cpufreq = NULL;
if (file_exists ("/sys/class/thermal/thermal_zone0/temp")) {
exec("cat /sys/class/thermal/thermal_zone0/temp", $cputemp);
$cputemp = $cputemp[0] / 1000;
}
showLapTime("cputemp");
if (file_exists ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")) {
exec("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", $cpufreq);
$cpufreq = $cpufreq[0] / 1000;
}
showLapTime("cpufreq");
if (defined("TEMPERATUREALERT") && $cputemp > TEMPERATUREHIGHLEVEL && $cputemp !== NULL) {
?>
<script>
function deleteLayer(id) {
if (document.getElementById && document.getElementById(id)) {
var theNode = document.getElementById(id);
theNode.parentNode.removeChild(theNode);
}
else if (document.all && document.all[id]) {
document.all[id].innerHTML='';
document.all[id].outerHTML='';
}
// OBSOLETE CODE FOR NETSCAPE 4
else if (document.layers && document.layers[id]) {
document.layers[id].visibility='hide';
delete document.layers[id];
}
}
function makeLayer(id,L,T,W,H,bgColor,visible,zIndex) {
if (document.getElementById) {
if (document.getElementById(id)) {
alert ('Layer with this ID already exists!');
return;
}
var ST = 'position:absolute; text-align:center;padding-top:20px;'
+'; left:'+L+'px'
+'; top:'+T+'px'
+'; width:'+W+'px'
+'; height:'+H+'px'
+'; clip:rect(0,'+W+','+H+',0)'
+'; visibility:'
+(null==visible || 1==visible ? 'visible':'hidden')
+(null==zIndex ? '' : '; z-index:'+zIndex)
+(null==bgColor ? '' : '; background-color:'+bgColor);
var LR = '<DIV id='+id+' style="'+ST+'">CPU-Temperature is very high!<br><input type="button" value="Close" onclick="deleteLayer(\'LYR1\')"></DIV>';
if (document.body) {
if (document.body.insertAdjacentHTML)
document.body.insertAdjacentHTML("BeforeEnd",LR);
else if (document.createElement && document.body.appendChild) {
var newNode = document.createElement('div');
newNode.setAttribute('id',id);
newNode.setAttribute('style',ST);
document.body.appendChild(newNode);
}
}
}
}
var audio = new Audio('sounds/alert.mp3');
audio.play();
var x = window.innerWidth/2-100;
var y = window.innerHeight/2-50;
makeLayer('LYR1',x,y,200,100,'red',1,1);
</script>
<?php
}
$output = shell_exec('cat /proc/loadavg');
$sysload = substr($output,0,strpos($output," "))*100;
showLapTime("sysload");
$stat1 = file('/proc/stat');
sleep(1);
$stat2 = file('/proc/stat');
$info1 = explode(" ", preg_replace("!cpu +!", "", $stat1[0]));
$info2 = explode(" ", preg_replace("!cpu +!", "", $stat2[0]));
$dif = array();
$dif['user'] = $info2[0] - $info1[0];
$dif['nice'] = $info2[1] - $info1[1];
$dif['sys'] = $info2[2] - $info1[2];
$dif['idle'] = $info2[3] - $info1[3];
$total = array_sum($dif);
$cpu = array();
foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 1);
$cpuusage = round($cpu['user'] + $cpu['sys'], 2);
showLapTime("cpuusage");
$output = shell_exec('grep -c processor /proc/cpuinfo');
$cpucores = $output;
$output = shell_exec('cat /proc/uptime');
$uptime = format_time(substr($output,0,strpos($output," ")));
$idletime = format_time((substr($output,strpos($output," ")))/$cpucores);
showLapTime("idletime");
if (defined("SHOWPOWERSTATE")) {
$pinStatus = trim(shell_exec("gpio -g read ".POWERONLINEPIN)); // Pin 18
}
//returns 0 = low; 1 = high
?>
<tbody>
<tr>
<?php
if (defined("SHOWPOWERSTATE")) {
?>
<th>Power</th>
<?php
}
if ($cputemp !== NULL) {
?>
<th>CPU-Temperature</th>
<?php
}
if ($cpufreq !== NULL) {
?>
<th>CPU-Frequency</th>
<?php
}
?>
<th>System-Load</th>
<th>CPU-Usage</th>
<th>Uptime</th>
<th>Idle</th>
</tr>
<tr class="gatewayinfo">
<?php
if (defined("SHOWPOWERSTATE")) {
?>
<td><?php if ($pinStatus == POWERONLINESTATE ) {echo "online";} else {echo "on battery";} ?></td>
<?php
}
if ($cputemp !== NULL) {
?>
<td><?php echo $cputemp; ?> &deg;C</td>
<?php
}
if ($cpufreq !== NULL) {
?>
<td><?php echo $cpufreq; ?> MHz</td>
<?php
}
?>
<td><?php echo $sysload; ?> %</td>
<td>
<?php
if (defined("SHOWPROGRESSBARS")) {
?>
<div class="progress"><div class="progress-bar <?php
if ($cpuusage < 30)
echo "progress-bar-success";
if ($cpuusage >= 30 and $cpuusage < 60)
echo "progress-bar-warning";
if ($cpuusage >= 60)
echo "progress-bar-danger";
?>" role="progressbar" aria-valuenow="<?php echo $cpuusage; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $cpuusage; ?>%;"><?php echo $cpuusage; ?>%</div></div>
<?php
} else {
echo $cpuusage." %";
}
?>
</td>
<td><?php echo $uptime; ?></td>
<td><?php echo $idletime; ?></td>
</tr>
</tbody>
<?php
}
?>

31
include/sysinfo_ajax.php Normal file
View file

@ -0,0 +1,31 @@
<div class="panel panel-default">
<!-- Standard-Panel-Inhalt -->
<div class="panel-heading">System Info</div>
<!-- Tabelle -->
<div class="table-responsive">
<table id="sysinfo" class="table table-condensed">
</table>
</div>
</div>
</div>
<script>
function loadXMLDocSysinfo() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("sysinfo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax.php?section=sysinfo",true);
xmlhttp.send();
var timeout = window.setTimeout("loadXMLDocSysinfo()", 20000);
refreshMode();
}
loadXMLDocSysinfo();
</script>

View file

@ -68,7 +68,7 @@ checkSetup();
// Here you can feel free to disable info-sections by commenting out with // before include
include "include/txinfo.php";
showLapTime("txinfo");
include "include/sysinfo.php";
include "include/sysinfo_ajax.php";
showLapTime("sysinfo");
include "include/disk.php";
showLapTime("disk");

View file

@ -1,3 +1,3 @@
<?php
define("VERSION", "20161012-1");
define("VERSION", "20161012-2");
?>