scripts / supervision /
Newer Older
103 lines | 2.994kb
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
1
#!/bin/bash
2

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

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

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

            
50

            
51
function _do_tests () {
52
    for t in $(declare -f | sed -rn '/^[[:alpha:]]/s/^([[:alnum:]_]+)\s*\(\)\s*$/\1/p'); do
53
        msg=
54
        if grep -q "^${t}$" $supervision_disabled; then
55
            echo supervision by $t disabled
56
        else
57
            msg=$( $t | sed 's/$/%0a/g' )
58
            if test -n "$msg"; then
59
                echo "problem: ${t}"
60
                err[${#err[@]}]=$t
61
                err[${#err[@]}]="${msg}"
62
            else
63
                if test -n "${!t}"; then
64
                    sed -ri "/^$t=.+$/d" $supervision_status
65
                fi
66
            fi
67
        fi
68
    done
69
}
70

            
71
function _do_manage_errors () {
72
    source $supervision_status
73

            
74
    if test ${#err[@]} -gt 0; then
75
        errors="Supervision $HOSTNAME (${loaded[@]}):%0a"
76
        notification=0
77
        for ((i=0; i<${#err[@]}; i++)); do
78
            t=${err[i]}
79
            msg=${err[++i]}
80
            errors="${errors}"'*'" [${t}] ${msg}"
81
            if test -n "${!t}"; then
82
                if test ${!t} -lt $(( $now - 86400 )); then
83
                    sed -ri "s/^(${t}=).+$/\1$now/" $supervision_status
84
                    let notification++
85
                fi
86
            else
87
                echo "${t}=$now" >> $supervision_status
88
                let notification++
89
            fi
90
        done
91
        if test "$SHLVL" -eq 1; then
92
            if test $notification -gt 0; then
93
                $(dirname "$0")/smsapi "$errors"
94
            fi
95
        else
96
            echo -e ${errors//\%0a/\\n}
97
        fi
98
    fi
99
}
100

            
101
_load ${*:-common-tests $HOSTNAME}
102
_do_tests
103
_do_manage_errors