Monthly Archives: August 2014

[Docker] How to monitor your server(s) with Nagios


Lately I’ve been working with Docker¬†experimenting development setups with JBoss Wildfly, ActiveMQ, Jenkins, Postfix, Nagios, etc. For those who are not familiar with:

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications.

You can easily create a container with your ideal setup and deploy your app, in a matter of minutes! You can have fun with Docker, here.

Nagios is a very powerful monitoring tool that can help you to monitor your infrastructure, detect security breaches, spot problems before they occur, etc.
I’ve forked Brian Goff Nagios image¬†and added the possibility to monitor HTTPS by enabling SSL (here). But then, how do I create my configuration files and add them to Nagios?


Based on cpuguy83 Nagios or my image, I’ve created the following Dockerfile:

# Nagios docker modified image

# use tpires/nagios base image (SSL ON)
FROM tpires/nagios

# Install/remove dependencies
ENV DEBIAN_FRONTEND noninteractive

# I've had some troubles trying to configure postfix inside nagios, so let's remove it and later create a postfix image to also be used by other containers.
RUN apt-get remove -y --purge postfix
RUN apt-get install -y msmtp heirloom-mailx
RUN apt-get autoremove -y
RUN apt-get autoclean

RUN chmod +x /

CMD ["/bin/bash", "-c", "/"]

(I’m going to use an external postfix image to send emails. You can, of course, keep and configure postfix inside this image.)
We’re adding an init script, to let us copy configuration file(s) to our Nagios image and edit some of the existing ones. But where will those configuration file(s) come from? Later, we’ll create a nagios configuration image containing them. Let’s have a look at first.


# If $NAGIOS HOME isn't set, set it!
if [ -z $NAGIOS_HOME ];

# Create, if not already, servers configuration directory
if [ ! -d "$NAGIOS_HOME/etc/servers" ];
 mkdir -p $NAGIOS_HOME/etc/servers

# Copy all existing configuration file(s) to Nagios servers directory and uncomment that line in nagios.cfg.
if [ ! -z $CONF_PATH ]; then
 cp -f $CONF_PATH/*.cfg $NAGIOS_HOME/etc/servers/
 sed -i "s|#cfg_dir=$NAGIOS_HOME/etc/servers|cfg_dir=$NAGIOS_HOME/etc/servers|g" $NAGIOS_HOME/etc/nagios.cfg

# mail configuration
# We're setting MSMTP client to connect to our postfix and send emails.
# If you're using an external postfix image don't forget to set an environment variable MAIL.
if [ ! -z /etc/msmtprc ];
 echo 'account postfix' > /etc/msmtprc
 echo 'host postfix' >> /etc/msmtprc
 echo 'port 25' >> /etc/msmtprc
 echo 'from '$POSTFIX_ENV_MAIL >> /etc/msmtprc
 echo 'protocol smtp' >> /etc/msmtprc
 echo 'account default : postfix' >> /etc/msmtprc

 chmod 600 /etc/msmtprc

 cp /etc/msmtprc $NAGIOS_HOME/.msmtprc
 chown nagios:nagios $NAGIOS_HOME/.msmtprc

 # remove postfix from the startup script
 rm -rf /etc/sv/postfix

# Modify system default client to msmtp.
if [ ! -z /etc/mailrc ];
 echo 'set sendmail=/usr/bin/msmtp' > /etc/mailrc
 echo 'set sendmail=/usr/bin/msmtp' >> /etc/nail.rc
 echo 'set smtp=postfix:25' >> /etc/nail.rc  ## i'm using postfix as host due to --link name when starting nagios.
 echo 'set from='$POSTFIX_ENV_MAIL >> /etc/nail.rc

 cp /etc/mailrc $NAGIOS_HOME/.mailrc
 chown nagios:nagios $NAGIOS_HOME/.mailrc
 chown nagios:nagios /etc/nail.rc

# remove localhost configuration (by default is enabled)
rm -f $NAGIOS_HOME/etc/objects/localhost.cfg
sed -i "s|cfg_file=/opt/nagios/etc/objects/localhost.cfg|#cfg_file=/opt/nagios/etc/objects/localhost.cfg|g" $NAGIOS_HOME/etc/nagios.cfg

# add timezone check
sed -i 's|use_timezone=|#use_timezone=|g' $NAGIOS_HOME/etc/nagios.cfg

# start nagios

Our Nagios image is ready! Now let’s create our configuration image with this Dockerfile:

# Docker Nagios Conf


# scratch is a minimal docker image, ideal to hold configuration files.
FROM scratch

ADD configuration-file.cfg /conf/

VOLUME [ "/conf" ]

This image holds our configuration file(s) and now we can start it and import its volume!

Let’s first create our images.

$ cd nagios
$ docker build -t myrepo/nagios .
$ cd conf
$ docker build -t myrepo/nagios-conf .

Now let’s put it to work, by first running our configuration image and then import its volume:

$ docker run -d --name nagios-conf myrepo/nagios-conf /dev
$ docker run -d --name nagios -e CONF_PATH=/conf -e NAGIOSADMIN_PASS=myownpassword -e HOME=/root --volumes-from nagios-conf -v /etc/localtime:/etc/localtime -p 8080:80 myrepo/nagios

# if you've an external postfix image add: --link postfix:postfix

We’re starting our Nagios container setting its configuration path (CONF_PATH), modifying the default password (NAGIOSADMIN_PASS), setting HOME directory, importing configuration files, synchronizing with system localtime and setting 8080 as our external port.

Let’s browse to http://localhost:8080 and Nagios should be up and running!


Filed under Tech