ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP脚本使用cron作业检查Web服务器状态

2019-11-01 07:31:48  阅读:261  来源: 互联网

标签:cron-task http-headers httpresponse php


我正在寻找可以在我的Web主机上作为cron作业运行的PHP脚本.它需要遍历网站列表,并检查以确保每个网站都返回Http响应200 OK.如果网站未返回该响应或不可用,则需要向网站管理员发送电子邮件.

解决方法:

此后,我已经对该脚本进行了细化,以检查您的网站/网络服务器是否仍在正常运行.我对错误处理进行了一些改进,并添加了一封舒适邮件,以通知您脚本已成功运行.

舒适性电子邮件依赖于另一个名为healthcheck.txt的文件来存储一些值,直到下次运行该脚本为止.如果未自动创建,则只需创建一个0字节的文本文件,然后将其上传并为其设置正确的文件权限(读/写).

<?php
// set email server parameters
ini_set('sendmail_from', 'server.status@host.example.com' );
ini_set('SMTP', '127.0.0.1' );
ini_set('smtp_port', '25' );

ini_set('allow_url_fopen', true); //enable fopen

// define list of webservers to check
$webservers = array('www.example.com', 'www.example2.com');

function sendemail($subject,$message) // email function using standard php mail
{
$wrapmessage = wordwrap($message,70,"\n",true); // mail function can't support a message more than 70 characters per line
$to = 'you@example.com'; // who to send the emails to
// Headers ensure a properly formatted email
$headers = 'From: server.status@host.example.com' . "\r\n" .
    'Reply-To: server.status@host.example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

return mail($to, $subject, $wrapmessage, $headers); //send the email
}

function getresponse($url) //queries a url and provides the header returned and header response
{
$ch = curl_init(); // create cURL handle (ch)
if (!$ch) { // send an email if curl can't initialise
    $subject = "Web Server Checking Script Error";
    $message = "The web server checking script issued an error when it tried to process ".$url.". Curl did not initialise correctly and issued the error - ".curl_error($ch)." The script has died and not completed any more tasks.";
    sendemail($subject,$message);
    die();
}
// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL, "http://".$url."/");
$ret = curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);   
$ret = curl_setopt($ch, CURLOPT_HEADER, true);
$ret = curl_setopt($ch, CURLOPT_NOBODY, true);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 30);

// execute
$ret = curl_exec($ch);

if (empty($ret)) {
    // some kind of an error happened
    $subject = "Web Server Checking Script Error";
    $message = "The web server checking script issued an error when it tried to process ".$url.". Curl was trying to execute and issued the error - ".curl_error($ch)." Further URLs will be tried.";
    sendemail($subject,$message);
    curl_close($ch); // close cURL handler
    } else {
        $info = curl_getinfo($ch); //get header info - output is an array
        curl_close($ch); // close cURL handler

        if (empty($info['http_code'])) {
                $subject = "Web Server Checking Script Error";
                $message = "The web server checking script issued an error when it tried to process ".$url."\r\nNo HTTP code was returned";
                sendemail($subject,$message);
        } else {
            // load the HTTP code descriptions
            $http_codes = parse_ini_file("/server/path/to/http-response-codes.ini");

            // results - code number and description
            $result = $info['http_code'] . " " . $http_codes[$info['http_code']];
        return $result; // $result contained a code, so return it
        }
    return None; //$info was empty so return nothing
    }
return None; // $ret was empty so return nothing
}

// this bit of code initiates the checking of the web server
foreach ($webservers as $webserver) { //loop through the array of webservers
    $status = getresponse($webserver); //get the status of the webserver
        if (empty($status)) {
        // nothing happens here because if $status is empty, the function returned nothing and an email was already sent.
        } else {
            if (strstr($status, "200")) { //search for the error code that means everything is ok
            // If found, don't do anything, just process the next one
            } else {
                $timestamp = date("m/d/Y H:i:s a", time()); //get the current date and time
                $error = $webserver." - ".$status." status error detected"; //set error message with server and response code
                $message = "At - ".$timestamp." - a http response error was detected on ".$webserver.".\r\nInstead of a 200 OK response, the server returned ".$status."\r\nThis requires immediate attention!"; //At what time was an error detected on which server and what was the error message
                sendemail($error,$message); //trigger the sendemail function
            }
        }
}

// Health Check. Comfort email twice a day to show script is actually running.
$healthfile = "/server/path/to/healthcheck.txt"; // path with the name of the file to store array data
$hfsize = filesize($healthfile); // filesize of healthcheck file
$notify = "16:00"; // specify the earliest time in the day to send the email - cron job settings dictate how close you'll get to this
$datenow = date("d-m-Y"); //what is current date as of now

if (file_exists($healthfile) && $hfsize !== 0) { //read contents of array from file if it exists and has data, otherwise create array with some defaults
    $valuestor = unserialize(file_get_contents($healthfile));
    } else { // file doesn't exist so we'll create an array with some defaults
    $valuestor = array("email_sent"=>0, "sent_date"=>$datenow, "iterations"=>0);
}
$i = $valuestor['iterations']; //get the iterations number from the valuestor array
$curdate = strtotime($datenow); //convert current date to seconds for comparison
$stordate = strtotime($valuestor['sent_date']); //convert stored date to seconds
if ($valuestor['email_sent'] == 1) { // has the email already been sent today
    if ($curdate == $stordate) { // if it has, is the current date equal to the stored date
        $i++; // yes it is, just increment the iterations
    } else { // it's a new day, reset the array
        $timestamp = date("m/d/Y H:i:s a", time()); //get the current date and time
        $subject = "Web Server Checking Script Health Status"; //set email subject line
        $message = "Message created: ".$timestamp."\r\nThe Web Server Checking script ran successfully for ".$i." time(s) on the ".$valuestor['sent_date']; //email message
        sendemail($subject,$message); //trigger the sendemail function
        $valuestor['email_sent'] = 0; // set email sent to false
        $valuestor['sent_date'] = $datenow; // set email send date to today
        $i = 1; // this is the first time the script has run today, so reset i to 1. It gets written to the array later.
        // echo $message;
    }
} else { // email has not been sent today
    $checktime = strtotime($notify); //convert $notify time (for current date) into seconds since the epoch
    if (time() >= $checktime) { // are we at or have we gone past checktime
        $i++; // increase the number of script iterations by 1
        $timestamp = date("m/d/Y H:i:s a", time()); //get the current date and time
        $subject = "Web Server Checking Script Health Status"; //set email subject line
        $message = "Message created: ".$timestamp."\r\nThe Web Server Checking script has successfully run and completed ".$i." time(s) today."; //email message
        sendemail($subject,$message); //trigger the sendemail function
        $valuestor['email_sent'] = 1; // set array to show that email has gone
        // echo $message;
    } else { // we haven't reached the check time yet
    $i++; // just increment the iterations
    }
}
$valuestor['iterations'] = $i; // update the array with the iterations number

// save the array to the file again
$fp = fopen($healthfile, 'w+'); // open or create the file, clear its contents and write to it
if (!$fp) { // handle the error with an email if the file won't open
$subject = "Web Server Checking Script Error";
$message = "The web server checking script issued an error when trying to open or create the file ".$healthfile." The script was ended without any new information being stored.";
sendemail($subject,$message);
} else {
fwrite($fp, serialize($valuestor)); // write to the file and serialise the array valuestor
fclose($fp); // close the file connection
} 
die(); // make sure that script dies and cron job terminates
?>

标签:cron-task,http-headers,httpresponse,php
来源: https://codeday.me/bug/20191101/1981949.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有