Tag Archives: daemon

Monitor Your Elk-M1, Log to MySQL, and Daemonize it!

I received a request a couple of days ago from someone who saw my posts on the Elk–Control perl library that I took over responsibility for. I had mentioned that I logged every Elk event into a MySQL database.

I am happy to report that I have this script running as a daemon, logging every ElkM1 event into my database. And it runs for months without issue.

I will keep these instructions brief. If you have detailed questions, please ask in the comments and I’ll help the best I can.

Download

You can download all the scirpts, just click here.

Notables

  1. If you don’t know a least a little perl and some MySQL basics, you may want to look elsewhere.
  2. The script and accompanying files are designed to work in concert as a daemonized process.
  3. Everything is logged into ‘elk_monitor.log’ in the same directory (or directory of your choosing) as the script. If you have a lot of passive infra-red motion detectors, you will want to monitor the log size.

Basic File Structure

file structure

elk_monitor.pl

This is the main script, it runs in a loop checking for new messages every second. You will need edit the %zonelist, %outputs, %areas near the top. Also, you will need to set some paths, look near the top of the file.

Required: ElkM1::Control, Log4perl

elk_monitor.sh

This is the shell script that controls elk_monitor.pl as a daemon. Setting up daemons can be complex as each system is different, so google around for some help.

elkm1.sql

A MySQL script to setup the tables and fields that the elk_monitor.pl script uses. You will need to add a database named ‘elkm1’. After that you can execute this script to create everything else for you.

lib/elkm1_db.pl

The database setting and basic sql commands are abstracted into this library. This is where you will need to set MySQL’s host, name, password, etc.

lib/file_tickler.pl

I use a custom ruby script to monitor the daemon. This library just touches a file every minute to let us know the elk_monitor.pl script is still running. Why? Well, I have had the script ‘freeze’ without exiting and most monitoring applications do not detect this. Obviously, this part is optional.

Conclusion

If you get all the settings right, she should run all day/week/month for you.

I and not a perl expert. Far from it. So I’m sure everything could use some tidying. If you do, please let me know and I’ll incorporate your changes.

Posted in Home Automation, perl. Tagged with , , .