Using m4 with Nagios: Advanced Ideas

Nagios configuration has been traditionally cumbersome and extensive; there are a lot of things to configure. The addition of templating some time ago helped, but not entirely. A configuration element such as a server or a switch can take up a huge amount of configuration and be quite repetitive, too.

Using m4 can alleviate all of these problems. When combined with GNU Make and Nagios configuration directories, changing the configuration can be done quite simply and easily.

With this beginning of a Makefile in /etc/nagios, all *.cfg files will be converted to *.m4 files as they are included as

%.cfg : %.m4
        $(M4) -I conf.d/includes < $< > $*.cfg

With this default rule in place, and all configuration files in the conf.d directory can be converted with this
Makefile syntax:

FILES=$(wildcard conf.d/*.cfg)
all: $(FILES)

This uses the GNU Make wildcard function to generate a list of files easily. Other directories
can be added with new calls to the wildcard function; it is not recursive and won’t descend
into directories.

Finish off the Makefile with these:

restart: all
        service nagios3 restart
.PHONY: all restart

This makes it possible to put the m4 files into conf.d (with matching cfg file or it won’t activate!) and use
a library of predefined macro files included in conf.d/includes. If make is called with make restart then all
configuration files will be processed by m4 as needed and Nagios will be reloaded.

M4 should be included in the base Linux install – but often isn’t. Load it and use it today!

3 thoughts on “Using m4 with Nagios: Advanced Ideas”

  1. I read this post several times, and i cannot for the life of me figure out how you’d use M4 to help Nagios build config files. Maybe because I don’t use M4, I think the last time I did was tweaking Sendmail configs back in RH6.x, or maybe because I make extensive use (some might consider abusive) of templates, service groups, and inheritance. I’d love to see a follow-up post showing how you use M4 with Nagios.

    1. You can use m4 to build a standard set of commands or services. One common scenario I see is the building of a collection of standard services for a normal server. Using m4 also makes it possible to build macros of macros: a group of SNMP services for instance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: