From a48332b52967631352e0dec8a66402953cb68c10 Mon Sep 17 00:00:00 2001
From: Soulou <leo@unbekandt.eu>
Date: Thu, 30 Apr 2015 22:21:27 +0200
Subject: Version v5, graceful stop, email alert and better doc

---
 Dockerfile     |  5 +++--
 README.md      | 39 +++++++++++++++++++++++++++++++++++++++
 VERSION        |  5 +++++
 nginx-munin    |  2 +-
 start-munin.sh | 36 +++++++++++++++++++++++++++++++-----
 5 files changed, 79 insertions(+), 8 deletions(-)
 create mode 100644 README.md
 create mode 100644 VERSION

diff --git a/Dockerfile b/Dockerfile
index ff21e0b..b07e0b3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -5,7 +5,7 @@ MAINTAINER Leo Unbekandt <leo.unbekandt@appsdeck.eu>
 RUN adduser --system --home /var/lib/munin --shell /bin/false --uid 1103 --group munin
 
 RUN apt-get update -qq && RUNLEVEL=1 DEBIAN_FRONTEND=noninteractive \
-    apt-get install -y -qq cron munin munin-node nginx apache2-utils wget
+    apt-get install -y -qq cron munin munin-node nginx apache2-utils wget heirloom-mailx
 RUN rm /etc/nginx/sites-enabled/default && mkdir -p /var/cache/munin/www && chown munin:munin /var/cache/munin/www && mkdir -p /var/run/munin && chown -R munin:munin /var/run/munin
 
 VOLUME /var/lib/munin
@@ -14,6 +14,7 @@ VOLUME /var/log/munin
 ADD ./munin.conf /etc/munin/munin.conf
 ADD ./nginx-munin /etc/nginx/sites-enabled/munin
 ADD ./start-munin.sh /munin
-EXPOSE 80
+
+EXPOSE 8080
 CMD bash /munin
 
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3902d0b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# Docker image for munin server
+
+## Configuration
+
+All the configuration is done through the environment.
+
+### HTTP Credentials 
+
+* `MUNIN_USER`
+* `MUNIN_PASSWORD`
+
+### SMTP info for alerts
+
+* `SMTP_HOST`
+* `SMTP_PORT`
+* `SMTP_USERNAME`
+* `SMTP_PASSWORD`
+
+### Alert target
+
+* `ALERT_RECIPIENT`
+* `ALERT_SENDER`
+
+### List of the nodes to check
+
+* `NODES` format: `name1:ip1 name2:ip2 …`
+
+## Port
+
+Container is listening on the port 8080
+
+## Volumes
+
+For a bit of persistency
+
+/var/log/munin   -> logs
+/var/lib/munin   -> db
+/var/run/munin   -> lock and pid files
+/var/cache/munin -> file deserved by HTTP
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..b348c1d
--- /dev/null
+++ b/VERSION
@@ -0,0 +1,5 @@
+# v5 - 30 Apr 2015
+
+* Graceful shutdown
+* Do not repeat configuration on restart
+* Email for alerts
diff --git a/nginx-munin b/nginx-munin
index ab67f68..66bf1d1 100644
--- a/nginx-munin
+++ b/nginx-munin
@@ -1,5 +1,5 @@
 server {
-  listen 80 default_server;
+  listen 8080 default_server;
   server_name munin;
   access_log /var/log/nginx/munin-access.log;
   error_log /var/log/nginx/munin-server.log;
diff --git a/start-munin.sh b/start-munin.sh
index b0fb3b0..66a2d4b 100644
--- a/start-munin.sh
+++ b/start-munin.sh
@@ -3,21 +3,41 @@ NODES=${NODES:-}
 MUNIN_USER=${MUNIN_USER:-user}
 MUNIN_PASSWORD=${MUNIN_PASSWORD:-password}
 
-htpasswd -b -c /etc/munin/htpasswd.users "$MUNIN_USER" "$MUNIN_PASSWORD"
+if [ -n "${SMTP_USERNAME}" -a -n "${SMTP_PASSWORD}" -a -n "${SMTP_HOST}" -a -n "${SMTP_PORT}" ] ; then
+  cat > /var/lib/munin/.mailrc <<EOF
+  set smtp-use-starttls
+  set ssl-verify=ignore
+  set smtp=smtp://${SMTP_HOST}:${SMTP_PORT}
+  set smtp-auth=login
+  set smtp-auth-user=${SMTP_USERNAME}
+  set smtp-auth-password=${SMTP_PASSWORD}
+EOF
+fi
+
+grep -q 'contact.mail' /etc/munin/munin.conf; rc=$?
+if  [ $rc -ne 0 -a -n "${ALERT_RECIPIENT}" -a -n "${ALERT_SENDER}" ] ; then
+  echo "Setup alert email from ${ALERT_SENDER} to ${ALERT_RECIPIENT}"
+  echo 'contact.mail.command mail -r '${ALERT_SENDER}' -s "[${var:group};${var:host}] -> ${var:graph_title} -> warnings: ${loop<,>:wfields  ${var:label}=${var:value}} / criticals: ${loop<,>:cfields  ${var:label}=${var:value}}"' ${ALERT_RECIPIENT} >> /etc/munin/munin.conf
+fi
+
+[ -e /etc/munin/htpasswd.users ] || htpasswd -b -c /etc/munin/htpasswd.users "$MUNIN_USER" "$MUNIN_PASSWORD"
 
 # generate node list
 for NODE in $NODES
 do
-    NAME=`echo $NODE | cut -d ":" -f1`
-    HOST=`echo $NODE | cut -d ":" -f2`
+  NAME=`echo $NODE | cut -d ":" -f1`
+  HOST=`echo $NODE | cut -d ":" -f2`
+  if ! grep -q $HOST /etc/munin/munin.conf ; then
     cat << EOF >> /etc/munin/munin.conf
 [$NAME]
     address $HOST
     use_node_name yes
 
 EOF
+    fi
 done
 
+[ -d /var/cache/munin/www ] || mkdir /var/cache/munin/www
 # placeholder html to prevent permission error
 if [ ! -e /var/cache/munin/www/index.html ]; then
 cat << EOF > /var/cache/munin/www/index.html
@@ -30,7 +50,7 @@ Munin has not run yet.  Please try again in a few moments.
 </body>
 </html>
 EOF
-chown munin:munin /var/cache/munin/www/index.html
+chown munin:munin -R /var/cache/munin/www
 chmod g+w /var/cache/munin/www/index.html
 fi
 
@@ -46,4 +66,10 @@ echo $NODES
 /usr/sbin/nginx
 # show logs
 echo "Tailing /var/log/syslog..."
-tail -F /var/log/syslog /var/log/munin/munin-update.log
+tail -F /var/log/syslog /var/log/munin/munin-update.log & pid=$!
+
+sleep 1
+
+trap "kill $pid $(cat /var/run/munin/munin-node.pid) $(cat /var/run/nginx.pid) $(cat /var/run/crond.pid) $(cat /var/run/rsyslogd.pid)" TERM QUIT INT
+
+wait
-- 
cgit v1.2.3