本篇全文出自:Jamyy's Weblog
函數應用備忘
* 利用 fsockopen() 探查主機特定埠口是否開啟
* 以 preg_match() 配合正規表示式將字串切割成陣列
程式碼
改寫自 "Creating Your Own Server Stat Script in PHP" by Joni Carlton
<?php
header("refresh: 60; url=" . $_SERVER['PHP_SELF']);
$site = "localhost";
$http = fsockopen($site, 80);
$ftp = fsockopen($site, 21);
$smtp = fsockopen($site, 25);
$pop3 = fsockopen($site, 110);
$mysql = fsockopen($site, 3306);
echo("<b>HTTP:</b> " . fn_chksock($http) . "<br>");
echo("<b>FTP:</b> " . fn_chksock($ftp) . "<br>");
echo("<b>SMTP:</b> " . fn_chksock($smtp) . "<br>");
echo("<b>POP3:</b> " . fn_chksock($pop3) . "<br>");
echo("<b>MySQL:</b> " . fn_chksock($mysql) . "<br>");
echo("<hr>");
$uptime = trim(exec("uptime"));
if ($uptime) {
if (preg_match("/up ([0-9.]+ [a-z]*, [0-9.]+:[0-9.]+), ([0-9.]+) user(.*) load average: (.*), (.*), (.*)/", $uptime, $matches)) {
/* for debug
echo("<pre>");
print_r($matches);
echo("</pre>");
*/
echo("<b>Uptime:</b> $matches[1]<br>");
echo("<b>Current Users:</b> $matches[2]<br>");
echo("<b>Current Load:</b> $matches[4]<br>");
echo("<b>Load 5 mins ago:</b> $matches[5]<br>");
echo("<b>Load 15 mins ago:</b> $matches[6]<br>");
} else {
echo($uptime);
}
} else {
echo("cannot run command: 'uptime'");
}
echo("<hr>");
echo("<b>System:</b> " . exec("uname -s -r"));
function fn_chksock($check) {
if ($check) {
return "Working";
} else {
return "<font color='red'>Not Working</font>";
}
}
?>
執行實例
轉貼內容結束
留言列表