|
#!/bin/bash |
|
# Author: diyfr |
|
CONFIG_FILE=/home/user/supervision/supervision.cfg |
|
|
|
# check container : sample check_container proxy_1 |
|
# Return container status, IP |
|
# OK - running |
|
# WARNING - restarting |
|
# CRITICAL - stopped |
|
# UNKNOWN - does not exist |
|
|
|
check_container(){ |
|
CONTAINER=$1 |
|
|
|
if [ "x${CONTAINER}" == "x" ]; then |
|
echo "UNKNOWN" # - Container ID or Friendly Name Required" |
|
exit 3 |
|
fi |
|
if [ "x$(which docker)" == "x" ]; then |
|
echo "UNKNOWN" # - Missing docker binary" |
|
exit 3 |
|
fi |
|
docker info > /dev/null 2>&1 |
|
if [ $? -ne 0 ]; then |
|
echo "UNKNOWN" # - Unable to talk to the docker daemon" |
|
exit 3 |
|
fi |
|
RUNNING=$(docker inspect --format="{{.State.Running}}" $CONTAINER 2> /dev/null) |
|
if [ $? -eq 1 ]; then |
|
echo "UNKNOWN" # - $CONTAINER does not exist." |
|
exit 3 |
|
fi |
|
if [ "$RUNNING" == "false" ]; then |
|
echo "CRITICAL" # - $CONTAINER is not running." |
|
exit 2 |
|
fi |
|
RESTARTING=$(docker inspect --format="{{.State.Restarting}}" $CONTAINER) |
|
if [ "$RESTARTING" == "true" ]; then |
|
echo "WARNING" # - $CONTAINER state is restarting." |
|
exit 1 |
|
fi |
|
STARTED=$(docker inspect --format="{{.State.StartedAt}}" $CONTAINER) |
|
echo "OK - started at: ${STARTED:0:19}" |
|
} |
|
listContainers() |
|
{ |
|
found=false |
|
while read line |
|
do |
|
if [[ $found == false && "${line:0:1}" != '[' ]] |
|
then |
|
continue |
|
fi |
|
if [[ $found == true && "${line:0:1}" = '[' ]] |
|
then |
|
if [[ -v container ]] |
|
then |
|
printf " - %-.25s %s\n" "$name .......................... " ": `check_container $container`" |
|
fi |
|
unset name |
|
unset container |
|
continue |
|
fi |
|
found=true |
|
if [[ $found == true && "${line%=*}" = "name" ]] |
|
then |
|
name=${line#*=} |
|
fi |
|
if [[ $found == true && "${line%=*}" = "container" ]] |
|
then |
|
container=${line#*=} |
|
fi |
|
done |
|
if [ -v container ] |
|
then |
|
printf " - %-.25s %s\n" "$name .......................... " ": `check_container $container`" |
|
fi |
|
} |
|
|
|
|
|
# Basic info |
|
HOSTNAME=`uname -n` |
|
# Last reboot |
|
BOOT_EVENT=`last reboot | head -1` |
|
#ROOT=`df -Ph | grep xvda1 | awk '{print $4}' | tr -d '\n'` |
|
VOLUMES=`df -hk /home/docker/vol | tail -1 | awk '{print $5}'` |
|
DOCKER=`df -hk /home/docker/lib | tail -1 | awk '{print $5}'` |
|
diskusage=($(df -H | grep -vE '^Filesystem|tmpfs|cdrom|mmcblk0p1' | awk '{ print $5 " " $1 }'| cut -f1 -d '%')) |
|
tolerance=(85) |
|
CRITICAL_VOLUMES="" |
|
for i in "${diskusage[@]}" |
|
do |
|
if [[ "$i" -gt "$tolerance" ]] |
|
then |
|
line=$(df -hl |grep $i%) |
|
CRITICAL_VOLUMES="${CRITICAL_VOLUMES} |
|
${line}" |
|
fi |
|
done |
|
|
|
# System load |
|
MEMORY1=`free -t -m | grep Total | awk '{print $3" MB";}'` |
|
MEMORY2=`free -t -m | grep "Mem" | awk '{print $2" MB";}'` |
|
LOAD1=`cat /proc/loadavg | awk {'print $1'}` |
|
LOAD5=`cat /proc/loadavg | awk {'print $2'}` |
|
LOAD15=`cat /proc/loadavg | awk {'print $3'}` |
|
echo -e " |
|
=============================================== |
|
|
|
- Hostname..............: $HOSTNAME |
|
- Evenement boot .......: $BOOT_EVENT |
|
- 'volumes' disk usage..: $VOLUMES |
|
- 'docker' disk usage...: $DOCKER |
|
- Volumes critiques ....: $CRITICAL_VOLUMES |
|
|
|
=============================================== |
|
|
|
- CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min) |
|
- Memory used.........: $MEMORY1 / $MEMORY2 |
|
- Swap in use.........: `free -m | tail -n 1 | awk '{print $3}'` MB |
|
|
|
=============================================== |
|
|
|
- Docker containers running |
|
|
|
`(listContainers < $CONFIG_FILE)` |
|
|
|
=============================================== |
|
" |