scripts / monitoring /
Newer Older
119 lines | 3.506kb
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"
_turn X || return: ajoute la...
Sébastien MARQUE authored on 2017-02-26
6
supervision_turns="/dev/shm/${basename}${USER:+.$USER}.turns"
7
touch $supervision_status $supervision_disabled $supervision_turns
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
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=
passage du répertoire de sou...
Sébastien MARQUE authored on 2017-02-24
23
    local directories=(
24
        "$HOME/.config/$basename"
25
        "${0}.d"
26
        "/usr/local/share/${basename}"
27
    )
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
28
    for conf in $*; do
29
        count_loaded=0
30
        if grep -q "^@$conf$" $supervision_disabled; then
31
            disabled[${#disabled[@]}]=$conf
32
        else
passage du répertoire de sou...
Sébastien MARQUE authored on 2017-02-24
33
            for dir in ${directories[@]}; do
34
                test -r "$dir/$conf" && source "$dir/$conf" $dir && let count_loaded++
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
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

            
_turn X || return: ajoute la...
Sébastien MARQUE authored on 2017-02-26
56
function _turn () {
57
    test -n "$1" && [[ $1 =~ [0-9]+ ]] || return 1
58
    echo ${FUNCNAME[1]} >> $supervision_turns
59
    test $(( $(grep -c ${FUNCNAME[1]} $supervision_turns) % $1 )) -eq 0 && sed -i "/${FUNCNAME[1]}/d" $supervision_turns
60
}
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
61

            
62
function _do_tests () {
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
63
    local t=
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
64
    for t in $(declare -f | sed -rn '/^[[:alpha:]]/s/^([[:alnum:]_]+)\s*\(\)\s*$/\1/p'); do
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
65
        local msg=
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
66
        if grep -q "^${t}$" $supervision_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 test -n "${!t}"; then
76
                    sed -ri "/^$t=.+$/d" $supervision_status
77
                fi
78
            fi
79
        fi
80
    done
81
}
82

            
83
function _do_manage_errors () {
84
    source $supervision_status
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
85
    local msg=
86
    local t=
87
    local errors=
88
    local notification=0
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
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/" $supervision_status
99
                    let notification++
100
                fi
101
            else
102
                echo "${t}=$now" >> $supervision_status
103
                let notification++
104
            fi
105
        done
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
106
        errors=$(tr -d '\n' <<< $errors)
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
107
        if test "$SHLVL" -eq 1; then
108
            if test $notification -gt 0; then
109
                $(dirname "$0")/smsapi "$errors"
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