我需要脚本从cron运行并监控我的网站。需要的功能是在发生一些错误时发送电子邮件(超时,服务不可用,未找到,......)。所以我想分享我的解决方案;)
答案 0 :(得分:1)
答案 1 :(得分:1)
<强>更新强>
我更新了脚本,现在你可以设置:
您可以在此处找到工作代码 - http://pastebin.com/Cf9GyVJB
<?php
function checkURL($url) {
//array of emails to send warning
$adminEmails=array("admin1@t-zones.sk","admin2@vodafonemail.cz");
//email of sender
$senderEmail="monitoring@domain.tld";
//array of valid http codes
$validStatus=array(200,301,302);
//minimum filesize in bytes
$minFileSize=500;
if(!function_exists('curl_init')) die("Curl PHP package not installed!");
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response=curl_exec($ch);
$info=curl_getinfo($ch);
$statusCode=intval($info['http_code']);
$filesize=$info['size_download'];
if(!in_array($statusCode,$validStatus) || $filesize<$minFileSize) {
$message = "Web ERROR ($url) - Status Code: $statusCode, Filesize: $filesize\r\n";
foreach($adminEmails as $email) {
mail($email, "Web Monitoring ERROR", $message, "From: $senderEmail\r\nReply-To: $senderEmail\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\n");
}
}
}
checkURL("http://google.com/");
?>
答案 2 :(得分:0)
看一下麻雀 - http://blogs.perl.org/users/melezhik/2015/11/easy-nginx-monitoring-with-sparrow.html,这是一个perl web测试,监控框架可以很容易地用于任何类型的Web应用程序。麻雀消耗所谓的麻雀插件 - 可重复使用的测试套件,有一些已经写过,但你可以轻松创建自己的。完整的文档可以在这里找到 - https://github.com/melezhik/sparrow。
问候,麻雀的作者。