stix
stix

Reputation: 822

Web monitoring - check url

I need script to run from cron and monitoring my sites. Needed function is send me email when some error hapeened (timeout, service unavailable, not found,....). So I want to share my solution ;)

Upvotes: 1

Views: 2587

Answers (3)

Alexey Melezhik
Alexey Melezhik

Reputation: 971

Take a look at sparrow - http://blogs.perl.org/users/melezhik/2015/11/easy-nginx-monitoring-with-sparrow.html , this is a perl web test, monitoring framework could be easily used for any kinds of web applications. Sparrow consumes so called sparrow plugins - reusable tests suites, there are some already written, but you can easy create your own one. Full documentation could be found here - https://github.com/melezhik/sparrow.

Regards, the author of sparrow.

Upvotes: 0

stix
stix

Reputation: 822

UPDATE

I update script, now you can set:

  • array of emails to send warning (I recommend sms email for free sms warning)
  • email of warning sender
  • array of valid http status codes
  • minimum filesize (updated version check filesize too)

You can find working code here - http://pastebin.com/Cf9GyVJB

<?php
function checkURL($url) {

//array of emails to send warning
$adminEmails=array("[email protected]","[email protected]");
//email of sender
$senderEmail="[email protected]";
//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/");
?>

Upvotes: 1

Dmitry Igoshin
Dmitry Igoshin

Reputation: 81

I would rather use some of the hosted services like Pingdom. However, if you really want to have it in house, take a look at Zabbix or Nagios.

Upvotes: 1

Related Questions