網頁監控turnkey 讀取log發生的錯誤

2021-07-19
作家
Jacky Wu
關鍵字
Turnkey
PHP
Linux
log
Log
活動攝影

網頁監控turnkey log 用PHP

故事:

有一天發現伺服器的LOG長的奇妙的快,短短的一天竟然有21GB的LOG,

經過根本原因的查找,才知道是監控turnkey的LOG,因為讀不到這個檔案造成的

錯誤,而Web伺服器也因為磁碟空間已滿,造成Web 伺服器無法打開。

 

寫一個index.php (內容如下)

--------程式碼開始----------

PHP Open and Read lordroach-turnkey Log File

 

header("Content-type:text/html; charset=utf-8");

// Use fopen function to open a file 要加IF 判斷

if(($file = fopen("/var/nas/Turnkey_log/Turnkey_monitor_log.log", "r"))){

}

// Read the file line by line until the end

while (!feof($file)) {

$value = fgets($file);

print  $value . "
";

}

 

// Close the file that no longer in use

 

fclose($file);

 

?>

 

 

 

 

 

---------------------程式碼結束

錯誤內容LOG如下

[php7:warn] [pid 85303] [client 208.115.199.18:33516] PHP Warning:  feof() expects parameter 1 to be resource, bool given in /var/www/html/lordroach-turnkey-check/index.php on line 10, referer: http://lordroach-turnkey-check.luckywave.com.tw

 

網路解法

fopen失敗並返回false。
false不是資源,因此是警告。
在將$fp作為類似資源的引數注入之前,最好先測試它:

if(($fp = fopen($file, "r"))) {
    [...]
}