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 ;)

Answers


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.


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("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/");
?>

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.


Need Your Help

Write a value with OPC UA Java (OPC Foundation)

java simulation opc scada opc-ua

I am trying to connect to a remote environment using the OPC UA java stack from the OPC Foundation. Reading values is working correct, for that I am using the following code: