dev/provisioning/modules/puppi/files/mailpuppicheck
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 #!/bin/bash
       
     2 # File Managed by Puppet
       
     3 # Possible Usage:
       
     4 # Run this command at the end of each Puppet run to be 
       
     5 # immediately notified if a Puppet Runs has broken some service
       
     6 # 
       
     7 # Options:
       
     8 # -m destination_email
       
     9 # -i <interval> (in seconds) before retrying a Puppi check
       
    10 # -r <number_of_retries> how many attempts to retry if there are failures
       
    11 #
       
    12 # In puppet.conf add something like:
       
    13 # postrun_command = "/usr/bin/mailpuppicheck -m roots@example.com"
       
    14 #
       
    15 retries=1
       
    16 interval=2
       
    17 workdir="/tmp"
       
    18 counter=0
       
    19 while [ $# -gt 0 ]; do
       
    20   case "$1" in
       
    21     -m)
       
    22       mail=$2
       
    23       shift 2 ;;
       
    24     -i)
       
    25       internal=$2
       
    26       shift 2 ;;
       
    27     -r)
       
    28       retries=$2
       
    29       shift 2 ;;
       
    30   esac
       
    31 done
       
    32 
       
    33 if [ ! $mail ] ; then
       
    34     echo "Provide at least an email addess"
       
    35     exit 1
       
    36 fi
       
    37 
       
    38 # randfile="$(mktemp)"
       
    39 lastrunfile=$workdir/puppicheck_lastrun
       
    40 savedfile=$workdir/puppicheck_saved
       
    41 
       
    42 while [ $counter -lt $retries ] ; do
       
    43   puppi check | grep FAILED > $lastrunfile
       
    44   if [ "x$?" == "x0" ] ; then
       
    45     errors="yes"
       
    46     sleep $interval
       
    47   else
       
    48     errors="no"
       
    49     echo "Run $counter - Errors $errors"
       
    50     echo > $savedfile
       
    51     exit 0
       
    52   fi
       
    53   echo "Run $counter - Errors $errors"
       
    54   let counter=$counter+1
       
    55 done
       
    56 
       
    57 diff $lastrunfile $savedfile
       
    58 if [ "x$?" == "x0" ] ; then
       
    59   echo "No changes detected"
       
    60 else
       
    61   echo "Changes detected"
       
    62   notify="yes"
       
    63 fi
       
    64 
       
    65 cp $lastrunfile $savedfile
       
    66 if [ "x$notify" == "xyes" ] ; then 
       
    67   # Yes, it's ugly
       
    68   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
       
    69   echo "Sent notification"
       
    70 fi
       
    71 
       
    72 exit 0