Instructables

Garage Monitor 3001 Paging System

Step 3: Monitoring Script

Picture of Monitoring Script
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] ";
}

?>

 
Remove these adsRemove these ads by Signing Up