diff options
| author | Horus3 | 2014-07-20 17:26:58 +0200 |
|---|---|---|
| committer | Horus3 | 2014-07-20 17:26:58 +0200 |
| commit | 363f7b89a7d38ef4fc99f81780b5c2d08fa12203 (patch) | |
| tree | 4eceb7f84884685fb02fb6357df1e6c334686d46 /bin/check.sh | |
| parent | 5b129ba6d3d46f90d6064fad857a2354ab78f5f8 (diff) | |
| download | dotfiles-363f7b89a7d38ef4fc99f81780b5c2d08fa12203.tar.gz | |
cli command which checks for programs and notice you, when there are down
Diffstat (limited to 'bin/check.sh')
| -rwxr-xr-x | bin/check.sh | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/bin/check.sh b/bin/check.sh new file mode 100755 index 0000000..ebbe279 --- /dev/null +++ b/bin/check.sh @@ -0,0 +1,146 @@ +#!/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 "-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 + ;; + -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 |
