PHP Simple Implementation Timing Monitoring nginx Log File Function Example
- 2021-10-24 19:03:19
- OfStack
In this paper, an example is given to describe the simple implementation of PHP to monitor nginx log files regularly. Share it for your reference, as follows:
This function is to realize, regularly monitor the log data generated by nginx, and submit the newly added data to an interface (such as the interface of big data, let big data analyze)
define("MAX_SHOW", 8192*5); // Add data submission threshold
define("LOG_NAME", ""); // Log file read
define("LOG_SIZE", ""); // Keep the last read position
define("LOG_URL", ""); // Log submission address
// Runtime log Original file size
$log_size = get_filesize();
$file_size = filesize(LOG_NAME);
if(empty($log_size)){// The last position was not recorded , Start at the current position
$file_size = $file_size;
}else if($log_size > $file_size){ // The explanation is the first 2 Day log file, pointer to file header
$file_size = 0;
}else{ // Start at the last recorded position
$file_size = $log_size;
}
$file_size_new = 0;
$add_size = 0;
$ignore_size = 0;
$fp = fopen(LOG_NAME, "r");
while(1){
clearstatcache();
$read_num = 0;
$file_size_new = filesize(LOG_NAME);
$add_size = $file_size_new - $file_size;
$add_data = array();
$add_log = '';
if($add_size > 0){
// Greater than 1 Threshold submission data
if($add_size > MAX_SHOW){
fseek($fp, $file_size);
// When the increase exceeds 8192 Pagination read increment required
$page = ceil($add_size/8192);
for($i=1; $i<=$page; $i++){
if($i == $page){// Finally 1 Page
$end_add = $add_size - ($page -1) * 8192;
$add_log .= fread($fp, $end_add);
}else{
$add_log .= fread($fp, 8192);
$file_size_step = $file_size + $i * 8192;
fseek($fp, $file_size_step);
}
}
$add_data['add_log'] = $add_log;
$add_data['add_log'] = base64_encode($add_data['add_log']);
http_post(LOG_URL, $add_data);
$file_size = $file_size_new;
// Record the current position
save_filesize($file_size);
}
}else if($add_size < 0){ // No. 1 2 The sky begins at the head
$file_size = 0;
}
sleep(2);
}
fclose($fp);
/**
* Get the last open file location every time you start
*/
function get_filesize(){
$size = file_get_contents(LOG_SIZE);
return $size;
}
/**
* Save the location of the read file after each submission
*/
function save_filesize($size){
return file_put_contents(LOG_SIZE, $size);
}
/**
* http Request
* @param array $data
* @return boolean
*/
function http_post($url = '', $data = array())
{
if(empty($url)){
return FALSE;
}
if($data){
$data = http_build_query($data);
}
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec ( $ch );
curl_close ( $ch );
return $return;
}
Remarks: Because the log file will be cut after dawn, it is necessary to make 1 judgment, and the judgment is that the log of the second day needs to be read from the head of the log file
Need to optimize the logic: When the intermediate process hangs up, stop for a period of time, restart, from the last position to read again, the submitted data will be relatively large, may exceed the limit of the submitted data size
For more readers interested in PHP related contents, please check the topics of this site: "Summary of PHP Log Operation Skills", "Summary of php File Operation Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.