Sending Remote Inotify Events To MediaTomb

MediaTomb is a robust open-source UPnP server that has quickly become an important part of my network. It organizes media in what it calls virtual containers, a fancy but vague term to describe how the media it serves can be organized and displayed according to a hierarchy unrelated to the filesystem on which it resides. This is a great idea, but an abstract hierarchy not defined by the filesystem means that the mediatomb server needs a method for discovering new media and adding it to its database.

Enter Inotify, the new-ish linux kernel feature that allows applications to listen for filesystem events. Mediatomb can be configured to listen for inotify events on interesting directories. This is certainly the only decent way to manage very large media libraries, as the alternative method of periodically rescanning the filesystem for new files is an expensive operation on large or remote libraries.

Using inotify works on simple installations, but it won't work when mediatomb uses NFS or another remote filesystem to access the library. While creating library files on the mediatomb machine itself will go through the kernel VFS layer and generate an inotify event that mediatomb can respond to, changing or creating a file on the NFS server or on any other mounted client will not propagate inotify events to other hosts. Though communicating these remote events appears built in to newer versions of samba, I needed a solution that would work with NFS.

I've put together a program, mtproxy, that will watch configured directories for filesystem events, taking each event and turning it into an HTTP request that the mediatomb http daemon can understand. It will force mediatomb to add or remove a file from its database whenever a new fle is created, deleted or renamed in a watched directory.

Prerequisites
  • The fileserver on which this runs must have python installed. It was developed on python 2.5, though it should work on any 2.x release as old as 2.4
  • The fileserver must have the python-inotify library installed. This is available on every distribution that I've looked at, though most distributions do not install it by default.
  • The mediatomb server must have the web interface enabled (controlled by the ui configuration element) and authentication disabled. This is the default configuration that ships with mediatomb
  • The mediatomb server should have a top-level virtual container named PC Directory. This is controlled by the pc-directory element in the mediatomb configuration and is enabled by default. Not having this enabled will prevent mtproxy from notifying mediatomb when files are removed
Usage
This program turns interesting inotify events into HTTP requests that can trigger a
mediatomb server to add or remove media.

Required Parameters:
    -h, --host          Hostname of mediatomb daemon. Mediatomb should be running with webui enabled and
                        authorization disabled (the default configuration). This parameter is required.
    -w, --watch         A directory to watch. Try to avoid specifying overlapping directories
                        This parameter is required and can be specified multiple times.
Optional Parameters:
    -p, --port          Port to look for mediatomb web service on. Defaults to 49152.
    -t, --translate     A simple regex for translating paths on the file server to a path on the mediatomb machine.
                        If this machine sees a file at /export/video/movie.mp4, and mediatomb sees the same file at
                        /mnt/video/movie.mp4, you'll want to specify a value of 'export,mnt'. Can be specified
                        multiple times.
    -l, --log           Log output to a given file.
    -v, --verbose       Print extra debugging information.
    -V, --version       Print version number and exit.
    --help              Print this message and exit.

Download mtproxy-0.2.1, updated on Feb 25, 2009

Patches and bug reports are more than welcome. Feel free to leave your comments below.