Garage Monitor 3001 Paging System

by joe in Workshop > Science

1998 Views, 8 Favorites, 0 Comments

Garage Monitor 3001 Paging System

gm3k1-closed.png
This is an updated paging system for the Garage Monitor 3001( GM3K1).

I was using Nagios before for paging , but after switching to the SheevaPlug I decided to skip installing Nagios and just use a script for this.

Pre Reqs

f12release.png
I am going to assume you are running linux and a db. I use Fedora and Mysql.

And obviously you need to run the Garage Monitor 3001.




Create Table.

logo-mysql-110x57.png
Create a db instance and create a table in it.

CREATE TABLE `status` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `door` enum('overhead','side') DEFAULT NULL,
  `status` enum('Open','Closed') DEFAULT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
)

Monitoring Script

gm3k1-open.png
This is the script I use to monitor the garage, which will grab the page, and insert the status in the database.

#Script to enter garage status in DB
#Joe McManus 2010/01/26


#who mail should go to
$mailto="joe@example.com";

#who mail comes from
$mailfrom="garagemon@example.com";

#set the time
$now=date('Y-m-d H:i');

#web status  url
$garageurl="pink.example.com/garagemon-text.html";

#public url to include in page to check status online
$puburl="http://www.example.com/garage.php";

function connect(&$db){
  $db = mysql_pconnect("localhost", "garage", "monitor");
  if ( mysql_select_db("garagemon", $db)) {
        } else {
        echo "Sorry we are currently experiences technical issues. We'll be back shortly.";
        exit;
        }
}

connect($db);

#check the page
system("curl $garageurl -o /tmp/garage.txt", $retval);
if ($retval == 1 ) {
        echo "Error: Unable to reach web server";
        exit;
}

#Get the door status
$overhead=exec('grep Overhead /tmp/garage.txt | cut -d" " -f2');
$side=exec('grep Side /tmp/garage.txt | cut -d" " -f2');

#insert the side door in to the db
$insert="insert into status(id, date, door, status)";
$insert.="values('', now(), 'side', '$side')";
$result=mysql_query($insert, $db);

#insert the overhead door in to the db
$insert="insert into status(id, date, door, status)";
$insert.="values('', now(), 'overhead', '$overhead')";
$result=mysql_query($insert, $db);

echo "Overhead: $overhead ";
echo "Side: $side ";

#Query for doors being open 10 minutes or more.
$query='select date, door, status from status where status="Open" and date > date_sub(now(), interval 10 minute)';
$result=mysql_query($query, $db);
$rowcount=mysql_num_rows($result);

$mailfile="/tmp/garagemail.txt";
if ($rowcount >= 2) {
        echo "Alert: Door Open ";
        //Message start
        $mailsubject="Alert: Garage Door Open $now";
        $mailmsg="The Garage Monitor 3001 has detected that a door has been open for more than 10 minutes. $now ";
        $mailmsg.="$puburl ";

        if(file_exists($mailfile)) {
                echo "Mail not being sent $mailfile exists ";
        } else {
                mail($mailto, $mailsubject, $mailmsg);
                exec("/bin/touch $mailfile");
        }
} else {
        if(file_exists($mailfile)) {
                echo "Alert: Garage Door Closed $now";
                $mailsubject="Alert: Garage Door Closed";
                $mailmsg="The Garage Monitor 3001 has detected that a door has been closed. $now ";
                $mailmsg.="$puburl ";
                mail($mailto, $mailsubject, $mailmsg);
                exec("/bin/rm $mailfile");
        }
}

#Just a query for debugging
$query='select date, door, status from status where date > date_sub(now(), interval 10 minute)';
$result=mysql_query($query, $db);
echo  "door    | status  | date ";
while($row=mysql_fetch_row($result)) {
        echo "$row[0] | $row[1] | $row[2] ";
}

?>

Add to Cron

crontab.png
Add the script to cron, have it run every 5 minutes.


Alerts

gm3k1-open.png
You will get only get one email if a door is open for 10 minutes or 10 hours, no need to fill up your inbox. You will get one more when it clears.