[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!

9 Comments

Filed under Tech

OpenELEC XBMC 12.0 “Frodo” with Raspberry Pi Support

XMBC-238x158

XBMC has released today the long waited XBMC 12 “Frodo”. It has support for Linux, Windows, Mac OS X, Android, Raspberry Pi, Apple TV and Apple iOS.I’ve already compiled it from Raspberry Pi (NOT OFFICIAL links below).

Continue reading

2 Comments

Filed under Tech

Quick year review – 2012.

Thank you all, for making this possible!
I hope 2013 provide me enough free time to more new and exciting adventures.

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 40,000 views in 2012. If each view were a film, this blog would power 9 Film Festivals

Click here to see the complete report.

Leave a comment

Filed under Fun

SAPO Codebits 2012

codebits2012

I’m proud to say that i’ve been selected to another awesome edition of SAPO Codebits! I’ve participated on Codebits before and let me tell you it’s awesome! Three days that look like one. Forget sleeping, just enjoy! I’m also excited for the fact that Raspberry Pi guys will be there and i’ll have a great opportunity to “feel” the project even closer😉

Let the *fun* begin!😀

 

Leave a comment

Filed under Fun, Tech

Raspberry Pi OpenELEC latest PVR support (DVB-T)

Raspberry Pi Logo

OpenELEC build r12431 has already a nice media_build support, so you don’t need to add media_build support anymore. I’ve already updated my guide, so you won’t get all confused.

Download here the latest OpenELEC build to the moment (r13118).

Leave a comment

Filed under Tech