{"id":171,"date":"2011-01-24T17:26:08","date_gmt":"2011-01-24T17:26:08","guid":{"rendered":"http:\/\/www.omniweb.com\/wordpress\/?p=171"},"modified":"2011-01-24T17:28:39","modified_gmt":"2011-01-24T17:28:39","slug":"website-monitoring-send-email-when-down","status":"publish","type":"post","link":"https:\/\/www.omniweb.com\/wordpress\/?p=171","title":{"rendered":"Website Monitoring &#038; send Email when down"},"content":{"rendered":"<p>We use a perl script to monitor our webservers, and if any are down, send an email (SMS) to the right people.<\/p>\n<p>With thanks and compliments to <a href=\"http:\/\/www.linuxhomenetworking.com\/forums\/showthread.php\/18755-Simple-Website-Monitoring-Script-in-Perl\">these guys<\/a><br \/>\nHere is the perl script, monitor-website.pl<br \/>\n<code><br \/>\n#!\/usr\/bin\/perl<\/p>\n<p>    use Getopt::Std;<\/p>\n<p>    # Define the address the alerts should come from<\/p>\n<p>    $monitor_email              = \"Website_Monitor\";<\/p>\n<p>    # Define global variables<br \/>\n    $curl_executable            = \"\/usr\/bin\/curl\";<br \/>\n    $mail_executable            = \"\/usr\/sbin\/sendmail\";<br \/>\n    $OPT_STRING                 = 'hs:e:';<\/p>\n<p>    # Start program<br \/>\n    &main();<\/p>\n<p>sub main {<\/p>\n<p>  # Read in the options<br \/>\n  getopts( \"$OPT_STRING\", \\%opt ) or usage();<\/p>\n<p>  # Set variables<br \/>\n  my $url_header        = \"\";<br \/>\n  my $url_body          = \"\";<br \/>\n  my $email_address     = \"\";<br \/>\n  my $url               = $ARGV[0];<br \/>\n  my $valid_string      = \"<\/html>\";<\/p>\n<p>  # Determine options<br \/>\n  if ($opt{s}) { $valid_string = $opt{s};}<br \/>\n  usage() if $opt{h};<br \/>\n  usage() if !$opt{e};<\/p>\n<p>  # Define more variables<br \/>\n  $email_address = $opt{e};<\/p>\n<p>  # Monitor URL<br \/>\n  $url_header = `$curl_executable -I -s $url`;<\/p>\n<p>  if ($url_header =~ \/200 OK\/){<br \/>\n        $url_body = `$curl_executable -s $url`;<br \/>\n        if ($url_body =~ \/$valid_string\/){<br \/>\n                #print (\"$url_header\\n\");<br \/>\n        }<br \/>\n        else{<br \/>\n                &send_warning($email_address, $url);<br \/>\n        }<br \/>\n  }<br \/>\n  else{<br \/>\n        &send_warning($email_address, $url);<br \/>\n  }<\/p>\n<p>}<\/p>\n<p>sub send_warning {<\/p>\n<p>  my ($email_address,$url) = @_;<\/p>\n<p>  # Create timestamp<br \/>\n  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);<br \/>\n  $year = $year + 1900;<br \/>\n  $mon  = $mon + 1;<br \/>\n  my $time_stamp = $year.\"-\".$mon.\"-\".$mday.\" \".$hour.\":\".$min.\":\".$sec; <\/p>\n<p>  my $email_content = $url .\" failure at \". $time_stamp . \"\\n\";<\/p>\n<p>  unless(open (MAIL, \"| \/usr\/sbin\/sendmail -t\")) {<br \/>\n        print \"error.\\n\";<br \/>\n        warn \"Error starting sendmail: $!\";<br \/>\n  }<br \/>\n  else{<br \/>\n        print MAIL \"From: \" . $monitor_email . \"\\n\";<br \/>\n        print MAIL \"To: \" . $email_address . \"\\n\";<br \/>\n        print MAIL \"Subject: Website URL Monitor Failure\\n\\n\";<br \/>\n        print MAIL $email_content;<br \/>\n        close(MAIL) || warn \"Error closing mail: $!\";<br \/>\n        print (\"$email_content\");<br \/>\n  }<br \/>\n}<\/p>\n<p>sub usage {<\/p>\n<p>print STDERR << \"EOF\";\n\n    This program monitors HTTP URLS\n\n    usage: $0 [-h] [-s \"string\"]\n\n     -h                   : This (help) message\n\n     -e email-address     : The email address that should be alerted.\n\n     -s                   : Unique string found on the web \n                            page that proves it is working correctly\n\n    example: $0 -s -e email-address URL\n\n\nEOF\n    exit;\n}\n<\/code><\/p>\n<p>Now with that file in place, I create a shell to batch this, called monitor.sh<br \/>\n<code><br \/>\nmonitor-website.pl -e my@email.address http:\/\/www.omniweb.com<br \/>\nmonitor-website.pl -e my@email.address http:\/\/newserver.omniweb.com<br \/>\n<\/code><\/p>\n<p>chmod +x both of these and put the monitor.sh in cron to run every 10 minutes (ideally on a remote server, since if your whole server is down this won't run!) and voila, notification when your sites are down.<br \/>\n<code><br \/>\n## Monitor websites every 10 minutes, email when outages<br \/>\n*\/10 * * * *  root  monitor_sites.sh > \/var\/log\/monitor.log<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We use a perl script to monitor our webservers, and if any are down, send an email (SMS) to the right people. With thanks and compliments to these guys Here is the perl script, monitor-website.pl #!\/usr\/bin\/perl use Getopt::Std; # Define &hellip; <a href=\"https:\/\/www.omniweb.com\/wordpress\/?p=171\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/171"}],"collection":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=171"}],"version-history":[{"count":4,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}