Rss

Multiple Monit accessed through a single URL scheme with Apache mod_rewrite


Just in case you don’t know it, Monit is a really neat tool for UNIX and alike systems, helping to manage and check a whole lot of stuff, including daemons, files and processes.

The best and preferable way to manage many Monit instances is to buy the M/Monit software, made by the same team and allowing to support their great work. But, in some situations you can’t get M/Monit and one might find useful the following quick’n dirty Apache‘s mod_rewrite tip.

Of course, the Monit web server need to be configured and running on the target hosts.

Here is what to put into a VirtualHost:

# Monit instances accessed with /m/<instance-name>/
RewriteEngine on
RewriteMap monit-hosts txt:/etc/apache2/local/map.monits
RewriteRule ^/m/([^/]+)$ https://%{SERVER_NAME}/m/$1/ [R=301,L]
RewriteRule ^/m/([^/]+)/?(.*) http://${monit-hosts:$1}/$2 [P]

And here is what the /etc/apache2/local/map.monits file looks like:

foo    foo.example.com:2812
bar    bar.example.com:2812

And that’s it! Asking for the URL /m/foo/ will lead to displaying the foo.example.com Monit web page and /m/bar/ will do the same for bar.example.com, effectively giving a single URL scheme ^^

Rules explained

The RewriteMap line declares a plain text file to be used as a key/pair database.
The first RewriteRule line adds a trailing slash if omitted at the end of a Monit instance name (eg, if the URL is /m/foo it becomes /m/foo/); this is fairly important in order to avoid troubles.
The last RewriteRule does the magic by using the text database to map a name from the URL to the corresponding host, any subsequent URL (eg, /m/foo/_runtime, without a trailing slash) will work just fine and display the corresponding Monit web page.

Comments are closed.