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

Docker-logo

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?

Setup

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

# Nagios docker modified image
# VERSION 1.0

# use tpires/nagios base image (SSL ON)
FROM tpires/nagios
MAINTAINER Tiago Pires, tandrepires@gmail.com

# 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

ADD launch.sh /launch.sh
RUN chmod +x /launch.sh

EXPOSE 80
CMD ["/bin/bash", "-c", "/launch.sh"]

(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 launch.sh, 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 launch.sh first.

#!/bin/bash

# If $NAGIOS HOME isn't set, set it!
if [ -z $NAGIOS_HOME ];
then
 NAGIOS_HOME=/opt/nagios
fi

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

# 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
fi

# 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 ];
then
 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
fi

# Modify system default client to msmtp.
if [ ! -z /etc/mailrc ];
then
 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
fi

# 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
/usr/local/bin/start_nagios

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

# Docker Nagios Conf
#
# VERSION 1.0

#INSTRUCTION

# scratch is a minimal docker image, ideal to hold configuration files.
FROM scratch
MAINTAINER Tiago Pires, tandrepires@gmail.com

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!

10 Comments

Filed under Tech

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

  1. gof

    I get this error:
    Unable to find image ‘myrepo/nagios-conf’ locally
    Pulling repository myrepo/nagios-conf
    2014/08/15 12:10:46 HTTP code: 404

    • Did you created both Dockerfiles and did docker build to both?

      • gof

        No – I didn’t create both Dockerfiles but pulled : docker pull tpires/nagios !
        Should I build an image from your instructions in this blog?
        Am newbie to docker.

      • Yes, you should follow my post and create both Dockerfile in separate folders and execute docker build.

      • gof

        For creating these images from your blog. Do I use Vi or bash script in Linux or on docker?

      • gof

        Please can you upload this image on Docker repos ,so that I can install & run it.

      • Sorry for the lack of time, but all you’ve to do is to create one directory and one sub-directory (nagios and nagios/conf, for example) and create both Dockerfiles, in both directories (don’t forget launch.sh in nagios). Then run docker build commands and you’ve your setup made.

  2. gof

    When I do : docker run -d –name nagios-conf myrepo/nagios-conf /dev
    I get : Unable to find image ‘myrepo/nagios-conf’ locally
    Pulling repository myrepo/nagios-conf
    2014/08/15 12:10:46 HTTP code: 404

  3. Pingback: Docker y BigData | Otro blog sobre programación

  4. Pingback: Docker Links – Welcome

Leave a reply to gof Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.