Now that we have some keys in Consul, we want to get that into a Nagios configuration file using Consul-Template. Consul-Template requires only two files which are used to create a third.

hosts.hcl – template configuration
hosts.ctmpl – template
hosts.cfg – output

I’m using Ubuntu 15.04 and here’s where I put the files;

File Contents

/etc/consul-template.d/config/hosts.hcl

/etc/consul-template.d/templates/hosts.ctmpl

Note: I recommend using a generic-host definition that all hosts inherit from and defines settings like notifications, events, flapping, retention, etc.

Resulting Output

hosts.cfg

All CRUD operations on the key-store will be reflected in the hosts.cfg file. Consul-Template is a daemon and monitors the Consul key-store for updates. When it detects a change it processes templates and outputs the files to disk. Consul-Template is very fast. Updates are usually completed in less than a second.

Hostgroup processing will be covered in a later post.

Tagged on: