mirror of
https://github.com/manios/docker-nagios.git
synced 2026-01-23 02:14:07 +00:00
Add capability to use init scripts. (#96)
This commit is contained in:
parent
d098e3782a
commit
a98132f871
4 changed files with 147 additions and 32 deletions
|
|
@ -177,7 +177,11 @@ RUN sed -i 's,/bin/mail,/usr/bin/mail,' ${NAGIOS_HOME}/etc/objects/commands.cfg
|
|||
sed -i 's/^.*command_line.*Host Alert.*$//g' /opt/nagios/etc/objects/commands.cfg && \
|
||||
sed -i 's/^.*command_line.*Service Alert.*$//g' /opt/nagios/etc/objects/commands.cfg && \
|
||||
sed -i '/notify-host-by-email/a command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$\\n\\n***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\nHost: $HOSTNAME$\\nState: $HOSTSTATE$\\nAddress: $HOSTADDRESS$\\nInfo: $HOSTOUTPUT$\\n\\nDate/Time: $LONGDATETIME$\\n" | /usr/sbin/sendmail -v $CONTACTEMAIL$' ${NAGIOS_HOME}/etc/objects/commands.cfg && \
|
||||
sed -i '/notify-service-by-email/a command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\\n\\n***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\n\\nService: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\n\\nDate/Time: $LONGDATETIME$\\n\\nAdditional Info:\\n\\n$SERVICEOUTPUT$\\n" | /usr/sbin/sendmail -v $CONTACTEMAIL$' ${NAGIOS_HOME}/etc/objects/commands.cfg
|
||||
sed -i '/notify-service-by-email/a command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\\n\\n***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\n\\nService: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\n\\nDate/Time: $LONGDATETIME$\\n\\nAdditional Info:\\n\\n$SERVICEOUTPUT$\\n" | /usr/sbin/sendmail -v $CONTACTEMAIL$' ${NAGIOS_HOME}/etc/objects/commands.cfg && \
|
||||
\
|
||||
: '# Modify etc/resource.cfg in order to include custom plugins directory' && \
|
||||
sed -i 's/^\(.*\)\(Sets .USER2. to be the path to\) event handlers.*$/\1\2 custom plugins directory/g' "${NAGIOS_HOME}"/etc/resource.cfg && \
|
||||
sed -i 's;^#\([$]USER2[$]=\).*$;\1/opt/Custom-Nagios-Plugins;g' "${NAGIOS_HOME}"/etc/resource.cfg
|
||||
|
||||
RUN echo "use_timezone=${NAGIOS_TIMEZONE}" >> ${NAGIOS_HOME}/etc/nagios.cfg && \
|
||||
sed -i 's/date_format=us/date_format=iso8601/g' ${NAGIOS_HOME}/etc/nagios.cfg
|
||||
|
|
|
|||
25
README.md
25
README.md
|
|
@ -73,6 +73,31 @@ docker run -e "TZ=Europe/Athens" manios/nagios:latest
|
|||
|
||||
This will configure and use globally `"Europe/Athens"` in both container and Nagios process.
|
||||
|
||||
### Init scripts
|
||||
|
||||
Since tag `build-31` 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`
|
||||
1. `0002-install-jq.sh`
|
||||
1. `0003-configure-custom-plugins.sh`
|
||||
|
||||
You can also use a volume mount for your init scripts which can survive container deletions:
|
||||
|
||||
```sh
|
||||
docker run -d --name nagios \
|
||||
-p 8080:80 \
|
||||
-v "$(pwd)/customscripts:/container-entrypoint-init-scripts" \
|
||||
manios/nagios:latest
|
||||
```
|
||||
|
||||
## Flavours
|
||||
|
||||
This Docker image is designed with optimising resources usage in mind and is build for multiple hardware architectures. The following matrix can be used to determine if your hardware architecture is represented in a docker image tag:
|
||||
|
|
|
|||
|
|
@ -177,7 +177,11 @@ RUN sed -i 's,/bin/mail,/usr/bin/mail,' ${NAGIOS_HOME}/etc/objects/commands.cfg
|
|||
sed -i 's/^.*command_line.*Host Alert.*$//g' /opt/nagios/etc/objects/commands.cfg && \
|
||||
sed -i 's/^.*command_line.*Service Alert.*$//g' /opt/nagios/etc/objects/commands.cfg && \
|
||||
sed -i '/notify-host-by-email/a command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$\\n\\n***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\nHost: $HOSTNAME$\\nState: $HOSTSTATE$\\nAddress: $HOSTADDRESS$\\nInfo: $HOSTOUTPUT$\\n\\nDate/Time: $LONGDATETIME$\\n" | /usr/sbin/sendmail -v $CONTACTEMAIL$' ${NAGIOS_HOME}/etc/objects/commands.cfg && \
|
||||
sed -i '/notify-service-by-email/a command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\\n\\n***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\n\\nService: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\n\\nDate/Time: $LONGDATETIME$\\n\\nAdditional Info:\\n\\n$SERVICEOUTPUT$\\n" | /usr/sbin/sendmail -v $CONTACTEMAIL$' ${NAGIOS_HOME}/etc/objects/commands.cfg
|
||||
sed -i '/notify-service-by-email/a command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\\n\\n***** Nagios *****\\n\\nNotification Type: $NOTIFICATIONTYPE$\\n\\nService: $SERVICEDESC$\\nHost: $HOSTALIAS$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\n\\nDate/Time: $LONGDATETIME$\\n\\nAdditional Info:\\n\\n$SERVICEOUTPUT$\\n" | /usr/sbin/sendmail -v $CONTACTEMAIL$' ${NAGIOS_HOME}/etc/objects/commands.cfg && \
|
||||
\
|
||||
: '# Modify etc/resource.cfg in order to include custom plugins directory' && \
|
||||
sed -i 's/^\(.*\)\(Sets .USER2. to be the path to\) event handlers.*$/\1\2 custom plugins directory/g' "${NAGIOS_HOME}"/etc/resource.cfg && \
|
||||
sed -i 's;^#\([$]USER2[$]=\).*$;\1/opt/Custom-Nagios-Plugins;g' "${NAGIOS_HOME}"/etc/resource.cfg
|
||||
|
||||
RUN echo "use_timezone=${NAGIOS_TIMEZONE}" >> ${NAGIOS_HOME}/etc/nagios.cfg && \
|
||||
sed -i 's/date_format=us/date_format=iso8601/g' ${NAGIOS_HOME}/etc/nagios.cfg
|
||||
|
|
|
|||
|
|
@ -6,42 +6,70 @@ set -eu
|
|||
# this script becomes PID 1 inside the container, catches termination signals, and stops
|
||||
# processes managed by runit
|
||||
|
||||
if [ -z "$(ls -A ${NAGIOS_HOME}/etc)" ]; then
|
||||
echo "Started with empty ETC, copying example data in-place"
|
||||
cp -Rp /orig/etc/* ${NAGIOS_HOME}/etc/
|
||||
fi
|
||||
# Prints a line by prepending the date in ISO-8601 format
|
||||
# usage: logDate MESSAGE
|
||||
# ie: logDate "Spongebob"
|
||||
logDate() {
|
||||
mydate=`date +"%Y-%m-%dT%H:%M:%S%z"`
|
||||
|
||||
if [ -z "$(ls -A ${NAGIOS_HOME}/var)" ]; then
|
||||
echo "Started with empty VAR, copying example data in-place"
|
||||
cp -Rp /orig/var/* ${NAGIOS_HOME}/var/
|
||||
fi
|
||||
echo "${mydate} $@"
|
||||
}
|
||||
|
||||
if [ ! -f "${NAGIOS_HOME}/etc/htpasswd.users" ] ; then
|
||||
htpasswd -c -b -s "${NAGIOS_HOME}/etc/htpasswd.users" "${NAGIOSADMIN_USER}" "${NAGIOSADMIN_PASS}"
|
||||
chown -R ${NAGIOS_USER}:${NAGIOS_GROUP} "${NAGIOS_HOME}/etc/htpasswd.users"
|
||||
fi
|
||||
# Run custom scripts provided by the user
|
||||
# usage: run_custom_scripts PATH
|
||||
# ie: run_custom_scripts /container-entrypoint-init-scripts
|
||||
# This runs *.sh files
|
||||
# Adapted from: https://github.com/gvenzl/oci-oracle-xe/blob/0cedd27ab04771789f1425639434d33940935f6c/container-entrypoint.sh
|
||||
run_custom_scripts() {
|
||||
|
||||
# Set current $NAGIOSADMIN_USER to the configuration of $NAGIOS_HOME/etc/cgi.cfg
|
||||
if [ "${NAGIOSADMIN_USER}" != "nagiosadmin" ]; then
|
||||
echo "Modifying ${NAGIOS_HOME}/etc/cgi.cfg in order to use ${NAGIOSADMIN_USER} as its authorised user"
|
||||
sed -i "s|\(authorized_for_system_information=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_configuration_information=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_system_commands=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_services=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_hosts=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_service_commands=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_host_commands=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
fi
|
||||
SCRIPTS_ROOT="${1}";
|
||||
|
||||
# Remove pid files
|
||||
# Check whether parameter has been passed on
|
||||
if [ -z "${SCRIPTS_ROOT}" ]; then
|
||||
echo "No SCRIPTS_ROOT passed on, no scripts will be run.";
|
||||
return;
|
||||
fi;
|
||||
|
||||
echo -n "Removing Apache HTTPD PID file before starting it ..."
|
||||
(rm -f /var/run/apache2/httpd.pid || true)
|
||||
echo "OK"
|
||||
# Execute custom provided files (only if directory exists and has files in it)
|
||||
if [ -d "${SCRIPTS_ROOT}" ] && [ -n "$(ls -A "${SCRIPTS_ROOT}")" ]; then
|
||||
|
||||
echo -n "Removing rsyslogd PID file before starting it ..."
|
||||
(rm -f /var/run/rsyslogd.pid || true)
|
||||
echo "OK"
|
||||
echo -e "\nCONTAINER: Executing user defined scripts..."
|
||||
|
||||
run_custom_scripts_recursive ${SCRIPTS_ROOT}
|
||||
|
||||
echo -e "CONTAINER: DONE: Executing user defined scripts.\n"
|
||||
|
||||
fi;
|
||||
}
|
||||
|
||||
# This recursive function traverses through sub directories by calling itself with them
|
||||
# usage: run_custom_scripts_recursive PATH
|
||||
# ie: run_custom_scripts_recursive /container-entrypoint-init-scripts/001_subdir
|
||||
# This runs *.sh files and traveres in sub directories
|
||||
# Adapted from: https://github.com/gvenzl/oci-oracle-xe/blob/0cedd27ab04771789f1425639434d33940935f6c/container-entrypoint.sh
|
||||
run_custom_scripts_recursive() {
|
||||
local f
|
||||
for f in "${1}"/*; do
|
||||
case "${f}" in
|
||||
*.sh)
|
||||
if [ -x "${f}" ]; then
|
||||
echo -e "\nCONTAINER: running ${f} ..."; "${f}"; echo "CONTAINER: DONE: running ${f}"
|
||||
else
|
||||
echo -e "\nCONTAINER: sourcing ${f} ..."; . "${f}" echo "CONTAINER: DONE: sourcing ${f}"
|
||||
fi;
|
||||
;;
|
||||
|
||||
*)
|
||||
if [ -d "${f}" ]; then
|
||||
echo -e "\nCONTAINER: descending into ${f} ..."; run_custom_scripts_recursive "${f}"; echo "CONTAINER: DONE: descending into ${f}"
|
||||
else
|
||||
echo -e "\nCONTAINER: ignoring ${f}"
|
||||
fi;
|
||||
;;
|
||||
esac
|
||||
echo "";
|
||||
done
|
||||
}
|
||||
|
||||
# This function sets the timezone to Nagios and Apache configuration files
|
||||
setTimezone() {
|
||||
|
|
@ -102,6 +130,60 @@ shutdown() {
|
|||
exit
|
||||
}
|
||||
|
||||
### =========== ###
|
||||
### MAIN METHOD ###
|
||||
### =========== ###
|
||||
|
||||
# If it is the first container startup / initialization,
|
||||
# execute any custom user scripts
|
||||
if [ ! -f "${NAGIOS_HOME}/container_first_run" ]; then
|
||||
run_custom_scripts /container-entrypoint-init-scripts
|
||||
|
||||
# After scripts complete, create ${NAGIOS_HOME}/container_first_run
|
||||
# file to mark the end of first startup
|
||||
touch "${NAGIOS_HOME}/container_first_run"
|
||||
fi
|
||||
|
||||
# If the $NAGIOS_HOME/etc directory is empty, copy example configuration
|
||||
if [ -z "$(ls -A ${NAGIOS_HOME}/etc)" ]; then
|
||||
echo "Started with empty ETC, copying example data in-place"
|
||||
cp -Rp /orig/etc/* ${NAGIOS_HOME}/etc/
|
||||
fi
|
||||
|
||||
# If the $NAGIOS_HOME/var directory is empty, copy example data
|
||||
if [ -z "$(ls -A ${NAGIOS_HOME}/var)" ]; then
|
||||
echo "Started with empty VAR, copying example data in-place"
|
||||
cp -Rp /orig/var/* ${NAGIOS_HOME}/var/
|
||||
fi
|
||||
|
||||
# If the htpasswd.users file does not exist, create a new one and place NAGIOSADMINUSER as
|
||||
# its first user
|
||||
if [ ! -f "${NAGIOS_HOME}/etc/htpasswd.users" ] ; then
|
||||
htpasswd -c -b -s "${NAGIOS_HOME}/etc/htpasswd.users" "${NAGIOSADMIN_USER}" "${NAGIOSADMIN_PASS}"
|
||||
chown -R ${NAGIOS_USER}:${NAGIOS_GROUP} "${NAGIOS_HOME}/etc/htpasswd.users"
|
||||
fi
|
||||
|
||||
# Set current $NAGIOSADMIN_USER to the configuration of $NAGIOS_HOME/etc/cgi.cfg
|
||||
if [ "${NAGIOSADMIN_USER}" != "nagiosadmin" ]; then
|
||||
echo "Modifying ${NAGIOS_HOME}/etc/cgi.cfg in order to use ${NAGIOSADMIN_USER} as its authorised user"
|
||||
sed -i "s|\(authorized_for_system_information=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_configuration_information=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_system_commands=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_services=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_hosts=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_service_commands=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
sed -i "s|\(authorized_for_all_host_commands=\).*|\1${NAGIOSADMIN_USER}|g" /opt/nagios/etc/cgi.cfg
|
||||
fi
|
||||
|
||||
# Remove pid files
|
||||
echo -n "Removing Apache HTTPD PID file before starting it ..."
|
||||
(rm -f /var/run/apache2/httpd.pid || true)
|
||||
echo "OK"
|
||||
|
||||
echo -n "Removing rsyslogd PID file before starting it ..."
|
||||
(rm -f /var/run/rsyslogd.pid || true)
|
||||
echo "OK"
|
||||
|
||||
# Set timezones before start
|
||||
setTimezone
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue