Skip to content

Instantly share code, notes, and snippets.

@wwng2333
Last active August 18, 2021 11:58
Show Gist options
  • Save wwng2333/dba795fdb3e197d1daeb9a11bca1f473 to your computer and use it in GitHub Desktop.
Save wwng2333/dba795fdb3e197d1daeb9a11bca1f473 to your computer and use it in GitHub Desktop.
PHP Prober Client
<?php
error_reporting(0);
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
$curl_errno = curl_errno($ch);
curl_close($ch);
if($curl_errno > 0) return false;
return $r;
}
function cpuinfo() {
$cpuinfo = file('/proc/cpuinfo');
foreach ($cpuinfo as $line) {
$templine = str_replace("\t", '', $line);
$templine = str_replace("\n", '', $templine);
$cpuinfo[] = $templine;
}
$cpu = array();
foreach ($cpuinfo as $line) {
$cpu[] = explode(':', $line);
}
$json = str_replace('\t', '', json_encode($cpu));
$json = str_replace('\n', '', $json);
$cpuinfo = json_decode($json, true);
unset($json, $cpu);
$cpu = array();
foreach($cpuinfo as $array) {
$name = $array[0];
if($name == '') continue;
$val = ltrim($array[1]);
$cpu[$name] = $val;
}
return $cpu;
}
function loadavg() {
if(!is_readable('/proc/loadavg')) return false;
$loadavg = explode(' ', file_get_contents('/proc/loadavg'));
return implode(' ', current(array_chunk($loadavg, 4)));
}
function getcores() {
if(!is_readable('/proc/cpuinfo')) return str_replace("\n",'',`nproc`);
$cpuinfo = file('/proc/cpuinfo');
$cores = 0;
for($i=0;$i<count($cpuinfo);$i++) {
$linenow = $cpuinfo[$i];
if(strstr($linenow,'flags')) $cores++;
}
if($cores>0) {
return $cores;
} else {
return str_replace("\n",'',`nproc`);
}
}
function hddusage() {
$df = `df`;
if($df == null) {
$total = disk_total_space('/');
$free = disk_free_space('/');
$used = (int)$total - $free;
$array['percent'] = floor(($used / $total) * 100);
$array['used'] = floor($used / 1024);
$array['total'] = floor($total / 1024);
return $array;
} else {
while(strstr($df,' ')) $df = str_replace(' ',' ',$df);
$df = explode("\n",$df);
$dff = array();
$t_total = 0;
$t_used = 0;
for($i=1;$i<count($df)-1;$i++) {
$linenow = $df[$i];
$temp = explode(' ',$linenow);
$exclude = array('tmpfs','devtmpfs','udev','none','shm');
$Filesystem = $temp[0];
$total = $temp[1];
$used = $temp[2];
for($x=0;$x<count($exclude);$x++) {
$keynow = $exclude[$x];
if($Filesystem == $keynow) continue 2;
}
$t_total = $t_total + $total;
$t_used = $t_used + $used;
}
$array['percent'] = floor(($t_used / $t_total) * 100);
$array['used'] = $t_used;
$array['total'] = $t_total;
return $array;
}
}
function corestat() {
if(!is_readable('/proc/stat')) return false;
preg_match('/^cpu\s*(\d+\s*)*/i', file_get_contents('/proc/stat'), $line);
$corestat = explode(' ', current($line));
$dif = array(
'user' => (int)$corestat[0],
'nice' => (int)$corestat[1],
'system' => (int)$corestat[2],
'idle' => (int)$corestat[3],
'iowait' => (int)$corestat[4],
'irq' => (int)$corestat[5],
'softirq' => (int)$corestat[6],
'stealstolen' => (int)$corestat[7],
'guest' => (int)$corestat[8]
);
$dif['total'] = array_sum($dif);
return $dif;
}
function ramuse() {
if(!is_readable('/proc/meminfo')) return false;
$meminfo = file_get_contents('/proc/meminfo');
$res['MemTotal'] = preg_match('/MemTotal\s*\:\s*(\d+)/i', $meminfo, $MemTotal) ? (int)$MemTotal[1] : 0;
$res['MemFree'] = preg_match('/MemFree\s*\:\s*(\d+)/i', $meminfo, $MemFree) ? (int)$MemFree[1] : 0;
$res['Cached'] = preg_match('/Cached\s*\:\s*(\d+)/i', $meminfo, $Cached) ? (int)$Cached[1] : 0;
$res['Buffers'] = preg_match('/Buffers\s*\:\s*(\d+)/i', $meminfo, $Buffers) ? (int)$Buffers[1] : 0;
$res['SwapTotal'] = preg_match('/SwapTotal\s*\:\s*(\d+)/i', $meminfo, $SwapTotal) ? (int)$SwapTotal[1] : 0;
$res['SwapFree'] = preg_match('/SwapFree\s*\:\s*(\d+)/i', $meminfo, $SwapFree) ? (int)$SwapFree[1] : 0;
return $res;
}
function uptime() {
if(!is_readable('/proc/uptime')) return false;
$cpucores = getcores();
$uptime = str_replace("\n",'',file_get_contents('/proc/uptime'));
$uptimearr = explode(' ',$uptime);
$arr['uptime'] = floor($uptimearr[0]);
$arr['freetime'] = floor($uptimearr[1]) / $cpucores;
$cpucores = getcores();
$arr['freepercent'] = round(($arr['freetime'] / $arr['uptime'] * 100) , 2);
return $arr;
}
function network() {
$net = file("/proc/net/dev");
$netname = array('enp2s0','eth1','eth0','venet0','ens18');
$dev = array();
for($i=2;$i<count($net);$i++) {
$linenow = $net[$i];
$arrnow = explode(':', $linenow);
for($x=0;$x<count($netname);$x++) {
$namenow = $netname[$x];
if(strstr($arrnow[0], $namenow)) {
$strs = $linenow;
} else {
continue;
}
}
}
preg_match_all( "/([^\s]+):[\s]{0,}(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/", $strs, $info );
$return['in'] = $info[2][0];
$return['out'] = $info[10][0];
return $return;
}
function Getipv4() {
$ipipurl = 'http://ip.huomao.com/ip';
$ipipjson = curl($ipipurl);
$ipiparr = json_decode($ipipjson,true);
return $ipiparr['ip'];
}
$json['loadavg'] = loadavg();
$json['cpuinfo'] = cpuinfo();
$json['ramuse'] = ramuse();
$json['uptime'] = uptime();
$json['network'] = network();
$json['network']['v4'] = Getipv4();
$json['hddusage'] = hddusage();
if(!$json['loadavg'] == false) {
echo json_encode($json);
} else {
$cmd = 'php '.$_SERVER['SCRIPT_FILENAME'];
exec($cmd,$result);
if($result == null) {
echo json_encode($json);
} else {
echo implode("\n",$result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment