Skip to content

Feature: Init scripts #96

@manios

Description

@manios

Issue

This docker image approach is to be lightweight in size and provide only the basic Nagios features. However in everyday use, we always need to add custom plugins or install extra software which suits on our needs based on the infrastructure that we want to monitor.

Up until now the only way that we could achieve this would be by extending this Docker image and provide a new image which uses manios/docker-nagios as its base.

FROM manios/nagios

# Add your own Docker commands
# ...

This is useful but needs everytime to build a new custom image when a new release is out.

Many issues have shown that we need to improve it:

  1. problems running custom plugins (check_nrpe) #15
  2. Custom Plugin Error, Python Error #20
  3. Consider including Synology MIBS #46
  4. have you considered adding nagvis and nconfig? #47
  5. [Enhancement] include two optional perl modules in build #70

Feature: Init scripts

After investigating a lot of different ways, we found out a simple and effective solution: "Initialisation scripts!"

This solution is used by the Oracle XE database Docker image provided by gvenzl/oci-oracle-xe.

The docker image is able to run custom init scripts at the first time the container runs. This is useful if you want to install extra software and plugins, customise the container or execute any initialisation script of your choice.

This feature has the following characteristics in detail:

  • You can run one or more init scripts which have to be present in the /container-entrypoint-init-scripts container directory.
  • The custom init script(s) run to completion at the first container run.
  • Nagios process starts after all init scripts complete successfully.
  • If any init script throws an error, the container is restarted.
  • When all init scripts run to completion (exit code 0), a special file is written to the container filesystem, in the path ${NAGIOS_HOME}/container_first_run. This file acts as a flag which verifies that the init scripts will be executed only on the first run.
  • If you want your init scripts to run in a specific order, then make sure to name them by using a numeric prefix such as:
    1. 0001-install-mongodb.sh
    2. 0002-install-jq.sh
    3. 0003-configure-custom-plugins.sh

You can also use a volume mount for your init scripts which can survive container deletions:

docker run -d --name nagios \
    -p 8080:80 \
    -v "$(pwd)/customscripts:/container-entrypoint-init-scripts" \
    manios/nagios:latest

Example

Let us say that we need to install yq, jq and mzupan/nagios-plugin-mongodb. We can create 3 scripts in a directory called customscripts:

0001-add-jq.sh:

#!/bin/sh

apk update
apk add jq

echo "SUCCESS! Installed jq !"

0002-add-yq.sh:

#!/bin/sh

apk update
apk add yq

echo "SUCCESS! Installed yq!"

0003-add-mongodb-plugin.sh:

#!/bin/sh

# Install dependencies
apk update
apk add python3 git


# Clone 
git clone https://github.com/mzupan/nagios-plugin-mongodb.git

venvPath="/nagios-plugin-mongodb"

# Create Python virtual environment
cd nagios-plugin-mongodb/
python3 -m venv "${venvPath}"
. "${venvPath}"/bin/activate

# install Python dependencies
pip install --upgrade pip
pip install -r requirements

echo "SUCCESS! Nagios MongoDB plugin has been installed!"

We want the 3 scripts will run in order, hence the name prefixes 0001, 0002, 0003. We run

docker run -d --name nagios \
    -p 8080:80 \
    -v "$(pwd)/customscripts:/container-entrypoint-init-scripts" \
    manios/nagios:latest

and we can see in the logs (only on the first container run):

CONTAINER: Executing user defined scripts...

CONTAINER: sourcing /container-entrypoint-init-scripts/0001-add-jq.sh ...
# -- logs truncated for this example
(1/2) Installing oniguruma (6.9.10-r0)
(2/2) Installing jq (1.8.1-r0)
Executing busybox-1.37.0-r19.trigger
OK: 206 MiB in 219 packages
SUCCESS! Installed jq !


CONTAINER: sourcing /container-entrypoint-init-scripts/0003-add-yq.sh ...
# -- logs truncated for this example
(1/1) Installing yq-go (4.47.2-r1)
Executing busybox-1.37.0-r19.trigger
OK: 218 MiB in 220 packages
SUCCESS! Installed yq!

CONTAINER: sourcing /container-entrypoint-init-scripts/0003-add-mongodb-plugin.sh ...
# -- logs truncated for this example
SUCCESS! Nagios MongoDB plugin has been installed!

CONTAINER: DONE: Executing user defined scripts.

## -------------------------------
## Nagios process starts afterwards
## -------------------------------

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions