Monitor Your PC & the PC to Server Connection

by W6LSN on June 18, 2009

How do I know if the GPSCON PC is still running?

I’ve had a problem off an on where the PC that runs the GPSCON software will crash. Since it is located under the operating table, it is difficult to see. The z3801a is visible, but I have to make an effort to go look at it. I thought it would be easier if I could receive a text message or e-mail alerting me to a problem.

So, Recently I sat down and taught myself enough php coding to be dangerous :)

The PHP file below will do just that, send an e-mail and text message when the GPSCON PC has crashed. I’ve made it simple enough that you can customize certain aspects of it to, hopefully, meet your needs too. Maybe you want a message every time the program runs or you don’t want a text message, etc.

You’ll find the program at the end of this post

1. Edit the file

a. the filename needs to be the complete name of the file as described by your server. [gpsstat1.png/gpsstat2.png/gpsstat.txt] are the three files that are routinely updated by GPSCON depending on how you configured it.
b. The variable $AllowStale is the time you consider OK (in minutes). I recommend a value of 2x your upload frequency unless that is a very long time. I upload the file a little less frequently than every 10 minutes. I’ve set the staleness to 30 minutes because I occasionally see time delays of around 20 minutes which is a 2x miss. I’ve never seen a 30 minute delay without the GPSCON computer having crashed.
c. $mailTo is the email address for notifications be sure to include the quotes
d. $textTo is the text messaging address for Failures Only. (include the quotes)
e. $mailFm is the address to use in the from line of the message. I use webmaster@w6lsn.com Your server setup may require specific things about this address. e.g. it may have to match the domain name it’s loaded on.
f. $mailGood set it to “yes” if you want to receive an e-mail every time the program runs. I’ve set this to no since my server is setup to send me a file with the output of the program every time it runs. I don’t need or want a second e-mail.
g. $mailBad set it to “no” if you do NOT want an e-mail when the GPSCON to server connection is lost.
h. $textBad set it to “no” if you do NOT want a text message when the GPSCON to server connection is lost.

2. FTP the file to your site.

If you have used a fully qualified file name with the path all the way to your root designation then you can load it into any directory on your server. I recommend loading it to a web accessible directory for testing and once you verify that it functions properly, move it to another non-web accessible directory. For testing I put my file in the same directory that GPSCON loads the files to. Once done, I moved it to a “non-web” accessible directory.

3. Set up “CRON” job(s) on your server

Set them to run at an interval that you’d like to check the functioning of your PC. Mine with a 10 minute update rate from GPSCON to the server has been set to run every 15 minutes. This means that if the file is 30 minutes old just after the CRON job runs it will be 45 minutes old when I’m notified.

The beauty of this php program is that it is configurable. You decide whether or not you want an e-mail every time the program runs. You decide if you want an e-mail, text message or both if GPSCON fails to upload to your webserver. All of the changes that you need to make are in the top section.

<?php
// full filename (including path) to be checked for staleness
$filename = '/dir/subdir/gpsstat1.png';
$AllowStale = 30; //minutes  should be greater than upload interval
$mailTo = "myname@mydomainname.com";
$textTo = "4081234567@txtmessaging.provder.com";
$mailFm = "webmaster@mydomainname.com";
$mailGood = "no";              // "yes" = e-mail for each "GOOD" program run
$mailBad = "yes";
$textBad = "yes";
////////////////////////////////////////////////////////////////////////////////////
$delta = 60 * $AllowStale;    //Allowed staleness.  60 seconds * X minutes
$mailFm = "-f".$mailFm;       //adds the required formating for the php mail function
///////////////////////////////////////////////////////////////////////////////////

$fTime1 = filectime($filename);
$now= time();
$fAge = $now - $fTime1;

echo $delta/60 ."       allowed staleness (mins)\n\n";
echo date("Y-m-d H:i:s", filectime($filename))." filetime\n\n";
echo date("Y-m-d H:i:s", $now)." now\n\n";
echo $fAge." actual staleness in seconds\n";
echo $fAge/60 ." mins staleness\n\n";

// Next section section sends e-mail if good when run
//////////////////////////////////////////////////////////////////////////////////
if ($fAge < $delta){
    echo "still good \n";

    if ($mailGood == "yes"){
       mail($mailTo,  'GPS OK', 'OK, file age is '.$fAge/60 .' mins', null, $mailFm);}
}
// Next few lines are used to alert you if the files are stale.
/////////////////////////////////////////////////////////////////////////////
else {
    echo "file is stale \n";

    if ($mailBad == "yes"){
       mail($mailTo,  'GPS Stale', 'Stale'.$fAge/60 .' mins', null, $mailFm);}

    if ($textBad == "yes"){
       mail($textTo,  'GPS Stale: ', 'Stale'.$fAge/60 .' mins', null, $mailFm);}
}
?>

If you are having difficulty determining your fully qualified path name, I have a php script that will help you figure it out. So What is that directory? Simply save it with a filename ending in php. FTP it to your site. Run the script.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled

Previous post:

Next post: