Skip to content

Instantly share code, notes, and snippets.

@unique1984
Last active November 8, 2017 22:41
Show Gist options
  • Save unique1984/7c048dbc2f38abc2e52f7aa6f8ff0221 to your computer and use it in GitHub Desktop.
Save unique1984/7c048dbc2f38abc2e52f7aa6f8ff0221 to your computer and use it in GitHub Desktop.
Linux System Resources PHP implementation
#!/usr/bin/php
<?php
$sleep_militime=150000;
class top_info{
private static function top_command(){
exec("/usr/bin/top -b -n1",$out);
return $out;
}
private static function debug_time(){
return microtime(true);
}
private static function convert_from_kbyte($byteSize) {
$bytes = (float)str_replace(".",",",$byteSize);
$sizes = array('KB', 'MB', 'GB', 'TB', 'PB');
$factor = floor((strlen($bytes) - 1) / 3);
$size = sprintf('%.2f', $bytes / pow(1024, $factor)) ." ".$sizes[$factor];
return $size;
}
private static function gorev_say($line){
preg_match('/Tasks:([\s+]\d+)/i',$line,$gorev);
return $gorev[1];
}
private static function ram_info($line){
preg_match('/KiB Mem:.\s+(\d+)\s+total,\s+(\d+)\s+used,\s+(\d+)\s+free/i',$line,$hafiza);
return $hafiza;
}
private static function ram_total($total){
return self::convert_from_kbyte($total);
}
private static function ram_used($total){
exec("ps aux | awk '{sum +=$4}; END {print sum}'",$out);
return self::convert_from_kbyte($out[0]*$total/100);
}
private static function ram_free($total){
exec("ps aux | awk '{sum +=$4}; END {print sum}'",$out);
return self::convert_from_kbyte((100-$out[0])*$total/100);
//~ return self::convert_from_kbyte($free*$total/100);
}
private static function top_ten_app(){
exec("ps -A --sort -rss -o comm,pmem,rss | head -n 21",$out);
return $out;
}
private function cpu_stat(){
exec("lscpu | grep -i 'CPU(s):'",$core);
preg_match("/\d/",$core[0],$match);
$core=implode("",$match);
for($i=0;$i<$core;$i++){
//~ echo "cpu$i\n";
exec("grep 'cpu".$i."' /proc/stat | awk '{usertime=($2-$10)} {nicetime=($3-$11)} {idlealltime=($5+$6)} {systemalltime=($4+$7+$8)} {steal=($9)} {virtalltime=($10+$11)} {using=(usertime+nicetime+systemalltime+steal+virtalltime)} {totaltime=(using+idlealltime)} END {print using,idlealltime,totaltime }'",$cpu);
$cpu_usage=explode(" ",$cpu[0]);
$return[$i]=array();
$return[$i][]=$cpu_usage[0]; // using
$return[$i][]=$cpu_usage[1]; // idle all time
$return[$i][]=$cpu_usage[2]; // total time
}
return $return;
//~ print_r($return);
//~ die;
}
private static function usage_info($sleep){
$return=array();
$top_out=self::top_command();
//~ Görev Sayısı
$return[]=self::gorev_say($top_out[1]);
//~ Ram Kullanımı
$ram_info=self::ram_info($top_out[3]);
$return[]=self::ram_total($ram_info[1]);
//~ $return[]=self::ram_using($ram_info[2]);
$return[]=self::ram_used($ram_info[1]);
$return[]=self::ram_free($ram_info[1]);
//~ CPU Kullanımı
exec("lscpu | grep -i 'CPU(s):'",$core);
preg_match("/\d/",$core[0],$match);
$core=implode("",$match);
for($i=0;$i<$core;$i++){
$cpu_first=self::cpu_stat();
usleep($sleep);
$cpu_second=self::cpu_stat();
$idled[$i]=$cpu_second[$i][1] - $cpu_first[$i][1];
$totald[$i]=$cpu_second[$i][2] - $cpu_first[$i][2];
$cpu[$i]=round(($totald[$i]-$idled[$i])*100/$totald[$i],4);
}
$return[]=$cpu;
$return[]=self::top_ten_app();
return $return;
}
public static function show_usage($sleep){
$begin=self::debug_time();
$usage=self::usage_info($sleep); // Bu fonksiyonun oluşturulma amacı istenildiğinde parçalı okuma gerçekleştirmek, yoksa usage_info() tüm işi yapmakta.
$end=self::debug_time();
echo "Hesaplama Süresi : ".round($end-$begin,4)."\n";
echo "Hesaplama Süresi 1 den Büyükse scriptin başındaki \$sleep_militime değerini küçültün !\n";
$return=array();
$return["Toplam Çalışan Görev"]=$usage[0];
$return["Toplam Hafıza"]=$usage[1];
$return["Kullanılan Hafıza"]=$usage[2];
$return["Boştaki Hafıza"]=$usage[3];
$return["Cpu Kullanımı"]="\n\t\t\t% ".implode("\n\t\t\t% ",$usage[4]);
//~ unset($usage[5][0]);
$return["Top 10 Uygulama"]=$usage[5];
return $return;
}
}
//~ function izle(){
//~ global $sleep_militime;
//~ $usage=top_info::show_usage($sleep_militime);
//~ print_r($usage);
//~ file_put_contents("/home/yasin/sh/top.txt",print_r($usage,1));
//~ usleep(200000);
//~ izle();
//~ }
//~ izle();
$usage=top_info::show_usage($sleep_militime);
print_r($usage);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment