#!/bin/sh #check.sh : functionnal check functions for various server #Copyright (C) 2016,2023 Weber Yann # #This program is free software; you can redistribute it and/or modify #it under the terms of the GNU General Public License as published by #the Free Software Foundation; either version 3 of the License, or #any later version. # #This program is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #GNU General Public License for more details. # #You should have received a copy of the GNU General Public License #along with this program. If not, see . if [ -z "$color" ] then color=1 fi if [ -z "$verbose" ] then verbose=1 fi if [ -z "$exit_on_fail" ] then exit_on_fail=1 fi alias echo="/bin/echo -e" col_reset() { if [ "$color" -gt 0 ] then tput sgr0 fi } col_set() { if [ -z "$1" ] then return fi if [ "$color" -gt 0 ] then tput setaf "$1" fi } log() { echo "$(col_set "$3")[$(printf "%7s" "$1")]$(col_reset) $2" } datetime() { date -Iseconds } logdate() { echo "$(date -Iseconds) $(col_set "$3")[$(printf "%7s" "$1")]$(col_reset) $2" } fail() { if [ "$verbose" -lt 2 ] then echo "" #for the dot printed with -n fi test_msg="$1" test_status=1 } err() { msg="$(log ERR "$1" 1)" test_msg="$1" test_status=-1 } success() { msg="$(log OK "$1" 2)" test_msg="$1" test_status=0 } # # Tests & testcase functions # tc_name="" tc_infos="" tc_fail=0 tc_run=0 total_fail=0 total_run=0 test_msg="" test_status=0 _test_setup() { test_status=0 test_msg="" } TC_INIT() { tc_name=$1 tc_infos=$2 tc_fail=0 tc_run=0 msg="Running testcase '$tc_name'" if [ -n "$tc_infos" ] then msg="${msg} ($tc_infos)" fi if [ "$verbose" -gt 0 ] then logdate TESTCASE "$msg" 4 fi } TC_END() { if [ "$tc_fail" -gt 0 ] then if [ "$verbose" -gt 1 ] then logdate TESTCASE "-------------$tc_name END------------" 1 fi logdate FAIL "Testcase '$tc_name' tests: ${tc_run} fails: $(col_set 1)${tc_fail}$(col_reset)" 1 if [ "$exit_on_fail" -ne 0 ] then CHECK_REPORT fi elif [ "$verbose" -eq 1 ] then echo "" #for the dot printed with -n logdate TESTCASE "Testcase '$tc_name' tests:${tc_run} fails: $tc_fail" 2 elif [ "$verbose" -gt 1 ] then logdate TESTCASE "-------------$tc_name END------------" 4 fi tc_name="" tc_infos="" tc_fail=0 tc_run=0 } TC_RUN() { _test_setup cmd=$1 shift case $# in 1)$cmd "$1";; 2)$cmd "$1" "$2";; 3)$cmd "$1" "$2" "$3";; 4)$cmd "$1" "$2" "$3" "$4";; *)fail "To many arguments for $cmd" esac tc_run=$(( tc_run + 1)) total_run=$(( total_run + 1)) if [ "$test_status" -ne 0 ] then err_type="FAIL" if [ "$test_status" -lt 0 ] then err_type="ERR" fi tc_fail=$(( tc_fail + 1)) total_fail=$(( total_fail + 1)) logdate $err_type "$tc_name: $test_msg" 1 >&2 elif [ "$verbose" -gt 1 ] then logdate OK "$tc_name: $test_msg" 2 else printf '.' fi } CHECK_START() { logdate STATUS "Starting tests" 3 } CHECK_REPORT() { if [ "$verbose" -eq 0 ] && [ "$tc_fail" -eq "0" ] then echo "" # for dot printed with -n fi if [ "$verbose" -gt 0 ] then logdate STATUS "All tests done" 3 fi if [ "$total_fail" -gt 0 ] then logdate FAIL "Summary : tests: ${total_run} fails: $(col_set 1)${total_fail}$(col_reset)" 1 exit 1 else logdate OK "Summary : tests:${total_run} fails: $total_fail" 2 exit 0 fi } . ./checks/git.sh . ./checks/http.sh . ./checks/mail.sh . ./checks/mpd.sh . ./checks/net.sh . ./checks/ssh.sh . ./checks/webradio.sh . ./checks/xmpp.sh