dev/provisioning/modules/puppi/files/mailpuppicheck
author ymh <ymh.work@gmail.com>
Tue, 20 Mar 2018 15:02:40 +0100
changeset 573 25f3d28f51b2
parent 28 b0b56e0f8c7f
permissions -rwxr-xr-x
Added tag 0.0.25 for changeset 190ae1dee68d

#!/bin/bash
# File Managed by Puppet
# Possible Usage:
# Run this command at the end of each Puppet run to be 
# immediately notified if a Puppet Runs has broken some service
# 
# Options:
# -m destination_email
# -i <interval> (in seconds) before retrying a Puppi check
# -r <number_of_retries> how many attempts to retry if there are failures
#
# In puppet.conf add something like:
# postrun_command = "/usr/bin/mailpuppicheck -m roots@example.com"
#
retries=1
interval=2
workdir="/tmp"
counter=0
while [ $# -gt 0 ]; do
  case "$1" in
    -m)
      mail=$2
      shift 2 ;;
    -i)
      internal=$2
      shift 2 ;;
    -r)
      retries=$2
      shift 2 ;;
  esac
done

if [ ! $mail ] ; then
    echo "Provide at least an email addess"
    exit 1
fi

# randfile="$(mktemp)"
lastrunfile=$workdir/puppicheck_lastrun
savedfile=$workdir/puppicheck_saved

while [ $counter -lt $retries ] ; do
  puppi check | grep FAILED > $lastrunfile
  if [ "x$?" == "x0" ] ; then
    errors="yes"
    sleep $interval
  else
    errors="no"
    echo "Run $counter - Errors $errors"
    echo > $savedfile
    exit 0
  fi
  echo "Run $counter - Errors $errors"
  let counter=$counter+1
done

diff $lastrunfile $savedfile
if [ "x$?" == "x0" ] ; then
  echo "No changes detected"
else
  echo "Changes detected"
  notify="yes"
fi

cp $lastrunfile $savedfile
if [ "x$notify" == "xyes" ] ; then 
  # Yes, it's ugly
  cat -v $lastrunfile  | sed -e 's:\^\[\[60G\[\^\[\[0;31m: :g' | sed -e 's:\^\[\[0;39m\]\^M: :g' | mail -s "[puppet] Errors after Puppet run on $(hostname -f)" $mail
  echo "Sent notification"
fi

exit 0