Newer Older
119 lines | 3.546kb
ajout scripts utilisés
Sébastien authored on 2023-04-24
1
#!/bin/bash
2

            
3
basename=$(basename $0)
4
monitoring_status="/dev/shm/${basename}${USER:+.$USER}.status"
5
monitoring_disabled="/dev/shm/${basename}${USER:+.$USER}.disabled"
6
monitoring_turns="/dev/shm/${basename}${USER:+.$USER}.turns"
7
touch $monitoring_status $monitoring_disabled $monitoring_turns
8
now=$(date +%s)
9

            
10
function _err () {
11
    test $# -eq 2 || return
12
    err[${#err[@]}]="$1"
13
    err[${#err[@]}]="$2%0a"
14
}
15

            
16
function _load () {
17
    local conf=
18
    local l=
19
    local d=
20
    local l_ok=
21
    local count_loaded=
22
    local msg=
23
    local directories=(
24
        "$HOME/.config/$basename"
25
        "${0}.d"
26
        "/usr/local/share/${basename}"
27
    )
28
    for conf in $*; do
29
        count_loaded=0
30
        if grep -q "^@$conf$" $monitoring_disabled; then
31
            disabled[${#disabled[@]}]=$conf
32
        else
33
            for dir in ${directories[@]}; do
34
                test -r "$dir/$conf" && source "$dir/$conf" $dir && let count_loaded++
35
            done
36
            test $count_loaded -gt 0 && loaded[${#loaded[@]}]=$conf
37
        fi
38
    done
39
    if test ${#loaded[@]} -ne $#; then
40
        for conf in $*; do
41
            l_ok=0
42
            for l in ${loaded[@]}; do
43
                test $conf == $l && l_ok=1 && break
44
            done
45
            if test $l_ok -eq 0; then
46
                for d in ${disabled[@]}; do
47
                    test $conf == $d && msg+="$conf (disabled)%0a" && l_ok=1 && break
48
                done
49
            fi
50
            test $l_ok -eq 0 && msg+="$conf%0a"
51
        done
52
        _err "_load" "config not loaded:%0a${msg/%%0a}"
53
    fi
54
}
55

            
56
function _turn () {
57
    test -n "$1" && [[ $1 =~ [0-9]+ ]] || return 1
58
    echo ${FUNCNAME[1]} >> $monitoring_turns
59
    test $(( $(grep -c ${FUNCNAME[1]} $monitoring_turns) % $1 )) -eq 0 && sed -i "/^${FUNCNAME[1]}$/d" $monitoring_turns
60
}
61

            
62
function _do_tests () {
63
    local t=
64
    for t in $(declare -f | sed -rn '/^[[:alpha:]]/s/^([[:alnum:]_]+)\s*\(\)\s*$/\1/p'); do
65
        local msg=
66
        if grep -q "^${t}$" $monitoring_disabled; then
67
            echo supervision by $t disabled
68
        else
69
            msg=$( $t | sed 's/$/%0a/g' )
70
            if test -n "$msg"; then
71
                echo "problem: ${t}"
72
                err[${#err[@]}]=$t
73
                err[${#err[@]}]="${msg}"
74
            else
75
                if ! grep --quiet --word-regexp $t $monitoring_turns; then
76
                    sed -ri "/^$t=.+$/d" $monitoring_status
77
                fi
78
            fi
79
        fi
80
    done
81
}
82

            
83
function _do_manage_errors () {
84
    source $monitoring_status
85
    local msg=
86
    local t=
87
    local errors=
88
    local notification=0
89

            
90
    if test ${#err[@]} -gt 0; then
91
        errors="Supervision $HOSTNAME (${loaded[@]}):%0a"
92
        for ((i=0; i<${#err[@]}; i++)); do
93
            t=${err[i]}
94
            msg=${err[++i]}
95
            errors="${errors}"'*'" [${t}] ${msg}"
96
            if test -n "${!t}"; then
97
                if test ${!t} -lt $(( $now - 86400 )); then
98
                    sed -ri "s/^(${t}=).+$/\1$now/" $monitoring_status
99
                    let notification++
100
                fi
101
            else
102
                echo "${t}=$now" >> $monitoring_status
103
                let notification++
104
            fi
105
        done
106
        errors=$(tr -d '\n' <<< $errors)
107
        if test "$SHLVL" -eq 1; then
108
            if test $notification -gt 0; then
supprime donnée personnelle
Sébastien authored on 2023-04-25
109
                $(dirname "$0")/smsapi "${error_msg_prefix}$errors"
ajout scripts utilisés
Sébastien authored on 2023-04-24
110
            fi
111
        else
112
            echo -e ${errors//\%0a/\\n}
113
        fi
114
    fi
115
}
116

            
117
_load ${*:-common-tests $HOSTNAME}
118
_do_tests
119
_do_manage_errors