#!/bin/bash # enter a mail adress for the report NOTICETO="" # set to 1 to NOT send a message on failure NONOTICE= # add process names to check. escape white spaces CheckList=( nginx mysqld php5-fpm ) # set to 1 to suppress output (cronjob) QUIET= # do not edit under this line # -------------------------- usage(){ echo "Usage: $1" echo "-c --check add a process name to check" echo "-p --print print list of process we check for" echo "-q --quiet surpress output" echo "-v --verbose be verbose" echo "-m --mailto ADRESS send report to ADRESS" echo "-n --nonotice don't send mail" echo "-h --help prints this help" echo "Have a nice day." exit 0 } while true; do case "$1" in -c|--check) if [ "x$2" != "x" ]; then CheckList+=("$2") else echo "$1 requieres a process name as an argument" 1>&2 exit 1 fi shift shift ;; -p|--print) for i in ${CheckList[@]}; do echo $i done exit 0 ;; -q|--quiet) QUIET=1 shift ;; -v|--verbose) QUIET=0 shift ;; -m|--mailto) if [ "x$2" != "x" ]; then NOTICETO="$2" else echo "$1 requieres a mail adress as an argument" 1>&2 exit 1 fi shift shift ;; -n|--nonotice|--nomail) NONOTICE=1 shift ;; -h|--help) usage $0 shift ;; --) shift break ;; -*) echo "Unknown argument '$1'" 1>&2 echo "Try -h for help" exit 1 ;; *) break ;; esac done # hostname HOST=$(hostname) # exit code EXIT=0 if [ "x$NOTICETO" == "x" ]; then NOTICETO="empty" fi # check if NONOTICE was set if [ "x$NONOTICE" == "x" ]; then NONOTICE=0 fi if [ $NOTICETO == "empty" ]; then if [ $NONOTICE -eq 0 ]; then echo "For status reports we need a mail adress." 1>&2 exit 1 fi fi # check if QUIET was set if [ "x$QUIET" == "x" ]; then QUIET=0 fi function do_check() { pgrep "$1" > /dev/null 2>&1 if [ $? -ne 0 ]; then if [ $NONOTICE -eq 0 ]; then echo "This is a automatic message." | mail -s "$HOST: $1 failure" "$NOTICETO" fi return 1 else return 0 fi } for i in ${CheckList[@]}; do if [ $QUIET -eq 0 ]; then echo Checking: $i... fi do_check "$i" RET=$? if [ $RET -ne 0 ]; then EXIT=1 fi if [ $QUIET -eq 0 ]; then if [ $RET -ne 0 ]; then echo -n "$(tput setaf 1)Test for $(tput bold)$i$(tput sgr0) $(tput setaf 1)failed.$(tput sgr0)." if [ $NONOTICE -eq 0 ]; then echo " We have sent a notice." else echo "" fi else echo "$(tput bold)$i$(tput sgr0) $(tput setaf 3)is up!$(tput sgr0)" fi fi done exit $EXIT