Table of Contents

docker-swarm.sh - Docker

Author Niels Haedecke
Compatibility Xymon 4.3
Requirements Bash, Docker >= 1.12
Download https://github.com/ufud-org/docker-swarm.sh
Last Update 2017-05-30

Description

This Xymon script provides a status monitor for your docker containers running in docker swarm mode (requires docker >= 1.12). It requires that a xymon client is installed on your docker swarm manager node.

Installation

Client side

1.) copy docker-swarm.sh to the xymon client 'ext' directory on your docker swarm manager node (e.g. /usr/lib/xymon/client/ext on Debian based Linux distributions).

2.) copy docker-swarm.cfg to the xymon client 'etc' directoy on your docker swarm manager node (e.g. /usr/lib/xymon/client/etc on Debian based Linux distributions).

3.) after copying, edit docker-swarm.cfg to your needs by adding/modifying the service names you want the script to check on the cluster, one service name per line. As an example, preconfigured docker-swarm.cfg already contains some service names (mariadb, git, etc.).

4.) add the following lines to your clientlaunch.cfg on your docker swarm manager node (on Debian based distributions you can find it at /usr/lib/xymon/client/etc/clientlaunch.cfg):

[swarm]
      ENVFILE $XYMONCLIENTHOME/etc/xymonclient.cfg
      CMD $XYMONCLIENTHOME/ext/docker-swarm.sh
      LOGFILE $XYMONCLIENTHOME/logs/hobbitclient.log
      INTERVAL 1m

5.) add the xymon user to the docker group (example for Debian based distributions):

usermod -aG docker xymon

6.) restart the xymon client on your swarm manager node.

Server side

Nothing

Source

docker-swarm.sh

Show Code ⇲

Hide Code ⇱

#!/bin/bash
# docker-swarm.sh
# Xymon add on check for monitoring docker containers in swarm mode.
# (c) 2017 by N. Haedecke (webmaster -at- ufud -dot- org)

AUTHOR="N. Haedecke"
VERSION="<p><center><h5>`basename $0`, $AUTHOR </h5></center>"

# Script internal variables. Change only if needed.
LOGFILE="${BBTMP}/docker-swarm.log"
TMPFILE="${BBTMP}/docker-swarm.tmp"
MSGFILE="${BBTMP}/docker-swarm.msg"
CFGFILE="${XYMONCLIENTHOME}/etc/docker-swarm.cfg"

# status constants
GREEN='green'
YELLOW='yellow'
RED='red'
TEST='swarm'
STATUS='green'

if [ -f "$TMPFILE" ]; then
   rm $TMPFILE
fi

if [ -f "$MSGFILE" ]; then
   rm $MSGFILE
fi

echo $VERSION >> "$MSGFILE"

search_max=$search_index
search_index=0

result_index=0
for i in $(cat "$CFGFILE") 
do 
  docker stack ps $i --no-trunc --format "{{.Name}} {{.Node}} {{.CurrentState}} {{.Error}}" >> $TMPFILE
  LINE=$(grep "$i" "$TMPFILE")
  !  echo "$LINE" | grep "Nothing found in stack"
  MYRESULTSTAT[$result_index]=$?
  MYRESULTARRA[$result_index]=$LINE
  MYRESULTSRVC[$result_index]=$i
  MYRESULTCNTR[$result_index]=$(grep "$i" "$TMPFILE" | wc -l )
  result_index=$((result_index+1))
done

result_max=$result_index

printf 'Docker swarm services:\n\n' >> "$MSGFILE"

for y in $(seq 0 $((result_max-1)) )
do
   if [ "${MYRESULTSTAT[y]}" -eq 1 ]
   then
      printf '&red service failed: %-20s\n\n' "${MYRESULTSRVC[y]}" >> "$MSGFILE"
      STATUS=red
   else
      printf '&green %-s (%-s)\n' "${MYRESULTSRVC[y]}" "${MYRESULTCNTR[y]}" >> "$MSGFILE"
      printf '%-s\n\n' "${MYRESULTARRA[y]}" >> "$MSGFILE"
   fi
done

# Send status message
"$BB" "$BBDISP" "status "$MACHINE"."$TEST" "$STATUS" $("$DATE")
$("$CAT" $(echo "$MSGFILE"))"

docker-swarm.cfg

Show Code ⇲

Hide Code ⇱

mariadb
openldap
nextcloud
git

Known Bugs and Issues

To Do

Credits

Changelog