{"id":330,"date":"2006-10-31T23:09:44","date_gmt":"2006-11-01T03:09:44","guid":{"rendered":"http:\/\/mattclare.ca\/wordpress\/2006\/10\/31\/php-script-to-monitor-server-uptime\/"},"modified":"2009-05-11T14:50:59","modified_gmt":"2009-05-11T18:50:59","slug":"php-script-to-monitor-server-uptime","status":"publish","type":"post","link":"https:\/\/mattclare.ca\/blog\/2006\/10\/31\/php-script-to-monitor-server-uptime\/","title":{"rendered":"PHP Script to monitor server uptime"},"content":{"rendered":"<p>Recently at work I needed to create a script to check a server&#8217;s response time.  This was for the purposes of tracking and graphing the response time (or lack of) of a server that was acting up when most people had better things to do. I also wanted to send out an E-Mail to my cell phone when the server went down.<\/p>\n<div style=\"text-align: center\"><img id=\"image331\" alt=\"responcegraph.png\" src=\"http:\/\/mattclare.ca\/wordpress\/wp-content\/uploads\/2006\/10\/responcegraph.png\" \/><\/div>\n<p>A cursory look around the net indicated there was nothing I could directly grab and deploy, so I created my own script in PHP.  This script can be run from the web or from the command line.<\/p>\n<p>To use this script create another file and include this file with the absolute file name (absolute names are important on the command line).<br \/>\nInclude example:<br \/>\ninclude(&#8216;\/var\/www\/html\/monitor\/server_monitor.php&#8217;);<\/p>\n<p>You can then call the timer_check function.<br \/>\nThis function takes following peramitors, you must set a URL, all other peramitors are optional.<\/p>\n<ul>\n<li>url: ie &#8216;http:\/\/news.google.ca&#8217;<\/li>\n<li>time limit: default is 10 seconds<\/li>\n<li>E-Mail Address: If it takes longer than the time limit who do we call?  This should be an array.<\/li>\n<li>Should we print anything to the screen, or just send E-Mail if ther&#8217;s a problem? true\/false<\/li>\n<li>Maximum attempts:  By default this script makes 10 attemps in a row and averages the results, you can set it to one 1 or even 100<\/li>\n<\/ul>\n<p>Example of a script that might be called google.php:<\/p>\n<pre><code>\u00c3\u00a2\u00c5\u2019\u00c2\u00a9?PHP\r\ninclude('\/var\/www\/html\/monitor\/server_monitor.php');\r\n$url = 'http:\/\/google.ca\/';\r\n$email = array('nobody@localhost','mymobilephone@mycellphone.provider);\r\ntimer_check ($url,10,$email); ?><\/code><\/pre>\n<p>If you wanted to make this a cron job (scheduled task under Linux\/OSX) here&#8217;s two examples that run every 10 minutes.<br \/>\nThese examples keep a log file called server_uptime that can be imported into something like MS Excel and graphed:<\/p>\n<p><code>*\/10 * * * * cd \/var\/www\/html\/monitor; php -f google.php >> server_uptime<\/code>This example has to be run as root, but the negative nice value makes sure your evaluating the connetion to the remote<br \/>\nserver, not your own server&#8217;s load.<\/p>\n<p><code>*\/10 * * * * cd \/var\/www\/html\/monitor; nice -n -2 php -f google.php >> server_uptime<\/code><\/p>\n<p>You can get the source code at: <a href=\"http:\/\/ctlet.brocku.ca\/monitor\/server_monitor.phps\">http:\/\/ctlet.brocku.ca\/monitor\/server_monitor.phps<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently at work I needed to create a script to check a server&#8217;s response time. This was for the purposes of tracking and graphing the response time (or lack of) of a server that was acting up when most people had better things to do. I also wanted to send out an E-Mail to my&hellip; <a class=\"continue\" href=\"https:\/\/mattclare.ca\/blog\/2006\/10\/31\/php-script-to-monitor-server-uptime\/\">Continue Reading<span> PHP Script to monitor server uptime<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,1,3],"tags":[],"_links":{"self":[{"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/posts\/330"}],"collection":[{"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":1,"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":663,"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/posts\/330\/revisions\/663"}],"wp:attachment":[{"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattclare.ca\/blog\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}