<?php
include('../includes/common.inc.php');
checkReferrer() or die();

// this code is part of servermonitor project engine - https://www.phpservermonitor.org/
if (!($load_tmp = shell_exec('cat /proc/loadavg | awk \'{print $1","$2","$3}\''))) {
    $load = array(0, 0, 0);
}else{
    if (!($num_cores = shell_exec('/bin/grep -c ^processor /proc/cpuinfo'))){
	    if (!($num_cores = trim(shell_exec('/usr/bin/nproc'))))  {
	        $num_cores = 1;
	    }
	}

	if ((int)$num_cores <= 0) $num_cores = 1;
    $cores = (int)$num_cores;
    $load_exp = explode(',', $load_tmp);
    $load = array_map(
        function ($value, $cores) {
            $v = (int)($value * 100 / $cores);
            if ($v > 100)
                $v = 100;
            return $v;
        }, 
        $load_exp,
        array_fill(0, 3, $cores)
    );
}
$datas = $load;
echo json_encode($datas);
?>