{"id":16,"date":"2015-09-06T06:12:42","date_gmt":"2015-09-06T00:42:42","guid":{"rendered":"http:\/\/madhurendra.com\/?p=16"},"modified":"2015-09-06T06:12:42","modified_gmt":"2015-09-06T00:42:42","slug":"monitoring-website-for-downtime","status":"publish","type":"post","link":"https:\/\/madhurendra.com\/monitoring-website-for-downtime\/","title":{"rendered":"Monitoring Website for Downtime"},"content":{"rendered":"

Sometimes our in house server go down, But at production level we cannot take risk of 1min downtime, so we were supposed to monitor our server for downtime.
\nAlthough there are many cheap services available but i decided to do it my own way, I can use this script in my RPI to monitor even my internal services.<\/p>\n

Below is python code, Which sends email if a server goes down and when it comes back online. Obviously you can add code to get more statistics like Logging to file, average response time, Monitoring page changes, Downtime etc.<\/p>\n

import httplib\r\nimport time\r\nfrom smtplib import SMTP\r\n\r\ndef send_email(to,subject,body,SERVER=\"smtp.gmail.com\",PORT=587,USERNAME=\"something@gmail.com\",PASSWORD=\"YourPassword\"):\r\n\tsmtp = SMTP(SERVER, PORT)\r\n\tsmtp.ehlo()\r\n\tsmtp.starttls()\r\n\tsmtp.login(USERNAME, PASSWORD)\r\n\theaders = \"\\r\\n\".join([\"from: \" + USERNAME,\r\n                       \"subject: \" + subject,\r\n                       \"to: \" + to,\r\n                       \"mime-version: 1.0\",\r\n                       \"content-type: text\/html\"])\r\n                    \r\n\tcontent = headers + \"\\r\\n\\r\\n\" + body\r\n\tsmtp.sendmail(USERNAME, to, content)\r\n\r\n\r\n\r\ndef get_status_code(host,path=\"\/\"):\r\n\ttry:\r\n\t\tconn = httplib.HTTPConnection(host,timeout=10)\r\n\t\tconn.request(\"HEAD\",path)\r\n\t\treturn conn.getresponse().status\r\n\texcept StandardError:\r\n\t\treturn 0\r\n\r\n\r\ndef get_site_status(host):\r\n\tstart = time.time()\r\n\tstatus = False\r\n\tcode = get_status_code(host)\r\n\t#if found, temp redirect, permanent redirect.\r\n\tif code in (200,302,301):\r\n\t\tstatus = True\r\n\treturn status, time.time()-start,code\r\n\r\n\r\ndef check_internet():\r\n\treturn get_site_status(\"google.com\")\r\n\r\ndef check_status():\r\n\ttoMonitor = [\"tikaj.com\"]\r\n\tdown = []\r\n\temail = \"\"\r\n\twhile True: \r\n\t\tfor value in toMonitor:\r\n\t\t\tst = \"Checking %s...\" %value\r\n\r\n\t\t\t#give enough space so that status code can be idented.\r\n\t\t\twhile(len(st)<40):\r\n\t\t\t\tst += \" \"\r\n\t\t\tprint st,\r\n\r\n\t\t\tstatus,rtime,code = get_site_status(value)\r\n\t\t\tprint \"[%s]\" %code,\r\n\r\n\t\t\tif status==False :\r\n\t\t\t\tif check_internet()[0] == True :\r\n\t\t\t\t\tprint \"[Server down]\"\r\n\t\t\t\t\t\r\n\t\t\t\t\t#if not present in Server down list. Add for email and add to down list.\r\n\t\t\t\t\tif value not in down:\r\n\t\t\t\t\t\tdown.append(value)\r\n\t\t\t\t\t\temail += st + \"<b>[SERVER_DOWN]<\/b><br\/>\"\r\n\t\t\t\telse:\r\n\t\t\t\t\tprint \"[Internet Fail]\"\r\n\t\t\telse: \r\n\t\t\t\t# if in down list compose mail that server is up.\r\n\t\t\t\tif value in down:\r\n\t\t\t\t\temail += st + \"<b>[%fs]<\/b>\" %rtime\r\n\t\t\t\t\tdown.remove(value)\r\n\t\t\t\tprint \"[%fs]\" %rtime\r\n\r\n\r\n\t\tif(len(email)>10):\r\n\r\n\t\t\tprint \"Sending alert email...\"\r\n\t\t\tsend_email(\"myemail@gmail.com\",\"ServerDown!\",\"<pre>%s<\/pre>\" %email)\r\n\t\t\temail = \"\"\r\n\r\n\t\ttime.sleep(60) #sleep for 60s\r\n\r\nif __name__ == '__main__':\r\n\tcheck_status()\r\n<\/pre>\n

After saving code to server use cron job to start run script on every boot.<\/p>\n

Edit crontab using `sudo crontab -e` and then add following line to file. <\/p>\n

`@reboot screen -dmS monitor python \/root\/monitor.py &> \/dev\/null`<\/p>\n

Above command starts a `screen` session named monitor and forwards error of screen to null.
\nIncase you want to see output of monitor.py use `screen -r monitor`.
\nYou can also add it simply without screen like
\n`@reboot python \/root\/monitor.py &`<\/p>\n

If you have some suggestions comment.<\/p>\n","protected":false},"excerpt":{"rendered":"

Sometimes our in house server go down, But at production level we cannot take risk of 1min downtime, so we were supposed to monitor our server for downtime. Although there are many cheap services available but i decided to do it my own way, I can use this script in my RPI to monitor even … <\/p>\n

Continue reading<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[3,4,6],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-linux","category-python","category-server","item-wrap"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pciGs2-g","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":2,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/posts\/16\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/madhurendra.com\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}