Tag: python

Nagios Monitoring of Directories

I had a recent occurrence at work that caused me to look around for a tool to monitor a directory for any changes made. Since there didn’t seem to be anything out there, I created a check called dirchanged. It looks at all the files in a directory and creates an sha256 has of the names and contents of the files. That hash is compared to a known value to determine if there have been any changes made.

There are a couple of issues with this check specifically that it doesn’t look into subdirectories and that the hash for comparison is passed on the command line from within the Nagios configuration files. I think the first issue will be fixed soon enough w/ a flag to indicate if the directory tree is to be traversed. The second issue is more cumbersome in that the hash value has to be stored somewhere. I’m not yet certain that putting it in the Nagios configuration files is better than putting it somewhere on the target file system. From the security standpoint, having the check not stored on the target file system is better, much less chance of it being changed by bad guys.

I’ll let it run for a while and see how it behaves and if changes are warranted.

Tags : ,

Python Meets the Arduino

I gave a talk at PyCon 2012 on using Python to control external devices through an Arduino.

Seems I was just a wee bit nervous giving the talk. Perhaps some more practice next time…


Tags : , , ,

Twisted RFID

It’s been a while since I worked with Twisted in Python and I forgot how easy and fun it is to create a robust network server with it.

I’m working on a prototype rfid card reader for use at work. We need to track various types of people who come into the facility – volunteers, faculty, youth, Create members, etc and giving them an id card that they can just swipe when they walk in the building would be a boon to help know who’s here and if they should be doing whatever it is that they’re doing.

The folks over at SparkFun have a simple USB based rfid reader that looks like it’ll do just what’s needed. Plug it in to a USB port and the card looks like an old fashion serial port. On my Mac, it comes up as /dev/tty.usbserial-A900UCVB.

The code does two things. First it reads the rfid, verifies that it’s a valid number and then drops it into a holding tank. Second is that it serves up the rfid from the holding tank (along with some supporting data) as json data through an HTTP server. And because its Twisted asynchronous network code, it can basically do both at the same time.

The next step will be to munge this into the existing timecard system and then we can start beta testing it. That’s going to be a bit more challenging. For now, the prototype code is available at https://github.com/pkropf/rfidcard. Have a look and let me know what you think.

Tags : , ,

Kinect, Python and Pushing Through

There’s tangent project that I’m working on that involves robotics, arduinos, kinect and fire. It’s a small robot called Exuro that has a pair of stainless steel eyes that are meant to track a person coming up to a donation box and when they make a donation, set off a small poofer. The idea is to track people using a kinect and have the eyes move as if they’re watching the closest person to the donation box. Working with an arduino to control external systems is pretty straight forward for me, it’s something that I’ve done before. But pulling sensor data from something like a kinect and interpreting the data is something I’ve never done. It’s rather intimidating. Processing video data at something like 30 frames per second, not something I’m used to do. But it sounds like fun!

There’s an open source driver to access the kinect called libfreenect that’s available from openkinect.org. Included are wrappers for using the library from Python which most definitely my preferred programming language. That works.

Getting libfreenect to build on a Ubuntu 10.10 system was pretty straight forward. Just follow the instructions in the README.asciidoc file. Getting the Python wrappers to work took a bit more effort. cython is used to create the bindings between libfreenect and Python. Unfortunately, the version that’s currently included with Ubuntu 10.10 isn’t up to the task. Once I removed the Ubuntu and installed from the latest source, the Python bindings built and worked as just fine. I’m sure the fine folks maintaining Ubuntu will make a newer version available at some point, I’m just not willing to put this project on hold till they do 😉

There’s a few demo files that are included with the wrapper so you can start to play with the interface, library and the kinect data. Two of them, demo_cv_sync.py and demo_cv_thresh_sweep.py, make for demo. The first opens two windows and shows a live video feed of the rgb camera in one and the depth camera in the other. The other demo shows a video of the depth camera but sweeps through the data showing what’s seen at different depths. These are really interesting demos to help wrap your head around what’s available from the kinect.

I got to wondering about the depth data and if there wasn’t a way to combine the two demos to be able to slide through the depth manually to see what’s there. The result is demo_cv_threshold.py. It allows you to slide along at any depth to see what’s there and then to contract or expand to see what’s around that depth. Here’s a sample video showing my hand pushing through a virtual wall:

The depth slider sets the focal point for what data to display and the threshold provides a +/- tolerance for how much data to display. A depth of 535 and a threshold of 0 would show just the data at 535 while a depth of 535 and a threshold of 6 would show the data from 529 thru 541.

It’s an interesting application to play with the gain a basic understanding of the data being returned and possible ways to use it. I’ve submitted a pull request on github to the maintainers of libfreenect to see if they’re willing to include it in the next release. Here’s hoping that they will.

There’s a lot more work I need to do for this project. The next steps will be to find the closest person in the data stream and calculate their location in the real world in reference to the location of the kinect. And I have almost no idea how to go about doing that. Time to read up on numpy and opencv

Tags : , , ,

OS X and the mystery bits

Sometimes I have to learn a lesson over and over again before I get a glimmer of what’s going on. I spent some time again today trying to get the MySQL client library module for Python working. I need it to be able to talk w/ a MySQL database for an online store. I’d rather use PostgreSQL but in this case, I don’t have a choice.

I downloaded MySQL OS X package and ran the installation program. It did what it was supposed to – install the software under /usr/local. The MySQL client runs and all seems right with the world. I download and install the MySQL Python client module and it builds as expected. But when I try to run Django, I get this error:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dynamic module does not define init function (init_mysql)

Grumble, grumble, grumble.

I try to load the MySQL Python module directly:

peter@drag:~> python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _mysql
Traceback (most recent call last):
  File "", line 1, in
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)

I checked the build output. Checked the install output. Everything looks fine. This should work. Searching the web doesn’t help much since the references I find are from 2007 and 2008. The patches that they describe have already been incorporated into the MySQL Python module.

About this time, I realize that I’ve solved this problem before. It has to do with bits. I run:

peter@drag:~> python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.architecture()
('64bit', '')

I look at the original MySQL disk image that I downloaded and see that its a 32bit application. Mixing a 32bit dynamic library with a 64bit executable doesn’t work.

After downloading the 64bit MySQL disk image, installing it and rebuilding the MySQL Python module, everything works. Now its on to writing code for the online store. Yea.

Tags : , ,