2013年10月27日 星期日

php based CPU&RAM檢視

一個以php + GD為基礎的CPU&RAM使用量檢視
只支援Linux作業系統(因為是用"/proc/stat"跟"/proc/meminfo"來算的)



程式碼:
<?php
//CPU使用率觀察器
//版本: 1.2
//作者: cs8425
//e-mail: cs8425@gamil.com
//授權: 創用 CC 姓名標示-相同方式分享 3.0 台灣 授權條款
//
//注意: 為了安全起見,最好對來源IP進行限制!!

//error_reporting(E_ALL);
//ini_set("display_errors", 1); //for Debug

$refresh 800;    //更新時間(ms)
session_start();

if(isset(
$_GET['mode'])){
switch (
$_GET['mode']) {
    case 
1:
        
//$cpu_count = exec('cat /proc/cpuinfo |grep "processor"|wc -l');
        
$im = @imagecreate(650300)

            or die(
"Cannot Initialize new GD image stream");

        
$background_color imagecolorallocate($im0xFF0xFF0xFF);    //背景色
        
$text_color imagecolorallocate($im0x000xF50x3D);
        for(
$i=0;$i<10;$i++){
            for(
$j=0;$j<500;$j+=10){
                
//imagedashedline($im, $j, $i*30, $j+5, $i*30, $text_color);
                
imageline($im$j$i*30$j+5$i*30$text_color);
            }
        }
        
imageline($im0299500299$text_color);
        
imagealphablending($imtrue);
        
imagesavealpha($imtrue);

        
//exec('cat /proc/stat | grep "cpu"', $CPULOG);
        
if($fh fopen("/proc/stat","r")){
            while(!
feof($fh)){
                
$LOG=stream_get_line($fh,65535,"\n");
                if(
stristr($LOG,"cpu")){
                    
$CPULOG[] = $LOG;
                }
            }
            
fclose($fh);
        }
        
$cpu_count count($CPULOG);
        
$_SESSION['i'] = $_SESSION['i'] + 1;
        if(
$_SESSION['i'] < 100){
            
$k=$_SESSION['i'];
        }else{
            
$k=100;
        }
        for(
$i=$k;$i>0;$i--){
            for(
$j=0;$j<$cpu_count;$j++){
                
$_SESSION['cpu'][$i][$j] = $_SESSION['cpu'][$i-1][$j];
            };
        }
        for(
$i=0;$i<$cpu_count;$i++){
            
sscanf($CPULOG[$i], "%s %f %f %f %f %f %f %f"$u$a$b$c$d$e$f$g);
            
$SYS_IDLE_2[$i]=$d;
            
$Total_2[$i]=($a $b $c $d $e $f $g);
            
$time=time();
            if((
$time-$_SESSION['time'][$i]) > 20){
                
$_SESSION['cpu'] = null;
                
$_SESSION['i'] = 0;
                
$_SESSION['SYS_IDLE'][$i] = $SYS_IDLE_2[$i];
                
$_SESSION['Total'][$i] = $Total_2[$i];
                
$_SESSION['time'][$i] = $time;
            };
            
$SYS_IDLE[$i]=$SYS_IDLE_2[$i] - $_SESSION['SYS_IDLE'][$i];
            
$Total[$i]=$Total_2[$i] - $_SESSION['Total'][$i];
            if(
$Total[$i] == 0){
                if(
$SYS_IDLE[$i] == 0){
                    
$SYS_USAGE[$i] = 100;
                }else{
                    
$SYS_USAGE[$i] = 0;
                }
            }else{
                
$SYS_USAGE[$i] = $SYS_IDLE[$i] / $Total[$i] * 100;
            };
            
$_SESSION['cpu'][0][$i] = 100 $SYS_USAGE[$i];
            
$_SESSION['SYS_IDLE'][$i] = $SYS_IDLE_2[$i];
            
$_SESSION['Total'][$i] = $Total_2[$i];
            
$_SESSION['time'][$i] = $time;
        };

        
$F 0xFF;
        
$E 0x00;
        
$K 0x600/$cpu_count;
        for(
$i=0;$i<$cpu_count;$i++){
            
$T = ($i*$K)%0x600;
            
$u $T%0x100;
            
$d $F-$u;
            if(
$T<0x100){$A=$F$B=$u$C=$E;}else{
            if(
$T<0x200){$A=$d$B=$F$C=$E;}else{
            if(
$T<0x300){$A=$E$B=$F$C=$u;}else{
            if(
$T<0x400){$A=$E$B=$d$C=$F;}else{
            if(
$T<0x500){$A=$u$B=$E$C=$F;}else{
            if(
$T<0x600){$A=$F$B=$E$C=$d;}}}}}}
            
$cpu_color[$i] = imagecolorallocate($im$A$B$C);
            
//echo $A.", ".$B.", ".$C."<br>\n";
        
}

        for(
$i=0;$i<$k;$i++){
            for(
$j=0;$j<$cpu_count;$j++){
                
imageline($im, (99-$i)*5, (100-$_SESSION['cpu'][$i+1][$j])*3, (100-$i)*5, (100-$_SESSION['cpu'][$i][$j])*3,$cpu_color[$j]);
            };

        }
        
imagestring($im25100sprintf("-cpu: %0.3f"$_SESSION['cpu'][0][0]), $cpu_color[0]);
        for(
$i=1;$i<$cpu_count;$i++){
            
imagestring($im2510$i*20,  sprintf("-cpu%d: %0.3f"$i$_SESSION['cpu'][0][$i]), $cpu_color[$i]);
        }

        
header("Content-type: image/png");
        
imagepng($im);
        
//var_dump($_SESSION['cpu'][0]);
        
imagedestroy($im);
        break;

    case 
2:
        
$data str_replace(":","",file_get_contents("/proc/meminfo"));
        
$convert explode("\n"$data);
        
$convert preg_replace("~([a-zA-Z0-9_\(\)]*)[ ]*([0-9a-zA-Z]*)~i""$1 $2"$convert);
        for (
$i=0;$i<count($convert);$i++){
            
sscanf($convert[$i], "%s %f %s\n"$n$a$b);
            
$OK[$i] = $a;
        }

        
$im imagecreate(650300);
        
$background_color imagecolorallocate($im0xFF0xFF0xFF);    //背景色
        
$text_color imagecolorallocate($im0x000xF50x3D);
        for(
$i=0;$i<10;$i++){
            for(
$j=0;$j<500;$j+=10){
                
//imagedashedline($im, $j, $i*30, $j+5, $i*30, $text_color);
                
imageline($im$j$i*30$j+5$i*30$text_color);
            }
        }
        
imageline($im0299500299$text_color);
        
imagealphablending($imtrue);
        
imagesavealpha($imtrue);
        
$color[0] = imagecolorallocate($im0x140xBE0xE3);
        
$color[1] = imagecolorallocate($im0x000x000x00);
        
$color[2] = imagecolorallocate($im0xEE0xFE0x00);
        
$color[3] = imagecolorallocate($im0xFE0x250x1A);

        
$time=time();
        if((
$time-$_SESSION['ram']['time']) > 200){
            
//session_destroy();
            
$_SESSION['j'] = 0;
            
$_SESSION['ram'] = null;
        };
        
$_SESSION['ram']['time'] = $time;
        
$_SESSION['j'] = $_SESSION['j'] + 1;
        if(
$_SESSION['j'] < 100){
            
$k $_SESSION['j'];
        }else{
            
$k 100;
        }
        if(
$k 1){
            for(
$i=$k-1;$i>0;$i--){
                for(
$j=0;$j<5;$j++){
                    
$_SESSION['ram'][$i][$j] = $_SESSION['ram'][$i-1][$j];
                };
            };
        }
        
$_SESSION['ram'][0] = array(($OK[0] - $OK[1] - $OK[2] - $OK[3]), $OK[1], $OK[2], $OK[3], $OK[0]);
        for(
$i=0;$i<$k;$i++){
            
$cache $_SESSION['ram'][$i][3]/$_SESSION['ram'][$i][4]*100;
            
$buffer $_SESSION['ram'][$i][2]/$_SESSION['ram'][$i][4]*100;
            
$used $_SESSION['ram'][$i][0]/$_SESSION['ram'][$i][4]*100;
            
//Free
            //$free = $_SESSION['ram'][$i][$j]/$_SESSION['ram'][$i][4]*100;
            //Cached
            
imagefilledrectangle($im, (99-$i)*5, (100-$cache-$buffer-$used)*3, (100-$i)*5300,$color[3]);
            
//Buffers
            
imagefilledrectangle($im, (99-$i)*5, (100-$buffer-$used)*3, (100-$i)*5300,$color[2]);
            
//Uesd
            
imagefilledrectangle($im, (99-$i)*5, (100-$used)*3, (100-$i)*5300,$color[0]);
        }
        for(
$j=0;$j<4;$j++){
            
$A $_SESSION['ram'][0][$j]/1024;
            if(
$A>=0.7){
                
$list[$j][0] = $A;
                
$list[$j][1] = "MB";
            }
            
$A $_SESSION['ram'][0][$j]/1048576;
            if(
$A>=0.7){
                
$list[$j][0] = $A;
                
$list[$j][1] = "GB";
            }
            if(
$list[$j][1] == ""){
                
$list[$j][0] = $_SESSION['ram'][0][$j];
                
$list[$j][1] = "KB";
            }
            
$list[$j][2] = $_SESSION['ram'][0][$j]/$_SESSION['ram'][0][4]*100;
        };
        
imagestring($im251020,  sprintf("Used: %0.2f%s(%0.1f%%)"$list[0][0], $list[0][1], $list[0][2]), $color[0]);
        
imagestring($im251040,  sprintf("Free: %0.2f%s(%0.1f%%)"$list[1][0], $list[1][1], $list[1][2]), $color[1]);
        
imagestring($im251060,  sprintf("Buffer: %0.2f%s(%0.1f%%)"$list[2][0], $list[2][1], $list[2][2]), imagecolorallocate($im0xF00xA60x19));
        
imagestring($im251080,  sprintf("Cache: %0.2f%s(%0.1f%%)"$list[3][0], $list[3][1], $list[3][2]), $color[3]);
        
/*var_dump($k);
        var_dump($_SESSION['ram']);*/
        
header("Content-type: image/png");
        
imagepng($im);
        
imagedestroy($im);
        break;

    default:
        
header("Location: ".$_SERVER["SCRIPT_NAME"]);
        break;
}
}else{ 
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<meta http-equiv="Pragma" content="no-cache">
<meta name="KEYWords" content="CPU使用率,CPU,cs8425,php">
<meta name="Author" content="cs8425">
<meta name="Robots" content="all">
<script type="text/javascript">
function refreshImage(){
    img = document.getElementById("i");
    img.src = '<?=$_SERVER["SCRIPT_NAME"]?>?mode=1&'+new Date().getTime();
    img2 = document.getElementById("i2");
    img2.src = '<?=$_SERVER["SCRIPT_NAME"]?>?mode=2&'+new Date().getTime();
}
</script>
</head>
<body onload="refreshImage();">
<center><img id="i" width="650" height="300" src='<?=$_SERVER["SCRIPT_NAME"]?>?mode=1' action="graph"/><br>
<img id="i2" width="650" height="300" src='<?=$_SERVER["SCRIPT_NAME"]?>?mode=2' action="graph"/><br><br>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/tw/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/tw/88x31.png" /></a><br>
<DIV STYLE="font-size : 7pt; font-family : Arial">';
&#26412;&#33879;&#20316;&#30001;<span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">cs8425</span>&#35069;&#20316;&#65292;&#20197;<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/tw/">&#21109;&#29992;CC &#22995;&#21517;&#27161;&#31034;-&#30456;&#21516;&#26041;&#24335;&#20998;&#20139; 3.0 &#21488;&#28771; &#25480;&#27402;&#26781;&#27454;</a>&#37323;&#20986;&#12290;<DIV></center>
<script type="text/javascript">
    setInterval(refreshImage,<?=$refresh ?>);
</script>
</body>
</html>
<?php

}
===============end==============

沒有留言:

張貼留言