网络监控 - 检查网址

时间:2013-09-13 09:18:39

标签: php curl cron monitoring

我需要脚本从cron运行并监控我的网站。需要的功能是在发生一些错误时发送电子邮件(超时,服务不可用,未找到,......)。所以我想分享我的解决方案;)

3 个答案:

答案 0 :(得分:1)

我宁愿使用某些托管服务,例如Pingdom。但是,如果您真的想将它放在内部,请查看ZabbixNagios

答案 1 :(得分:1)

<强>更新

我更新了脚本,现在你可以设置:

  • 要发送警告的电子邮件数组(我建议短信发送电子邮件免费短信警告)
  • 警告发件人的电子邮件
  • 有效的http状态代码数组
  • 最小文件大小(更新版本检查文件大小)

您可以在此处找到工作代码 - 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

问候,麻雀的作者。