scripts / monitoring /
Newer Older
113 lines | 3.185kb
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=
passage du répertoire de sou...
Sébastien MARQUE authored on 2017-02-24
22
    local directories=(
23
        "$HOME/.config/$basename"
24
        "${0}.d"
25
        "/usr/local/share/${basename}"
26
    )
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
27
    for conf in $*; do
28
        count_loaded=0
29
        if grep -q "^@$conf$" $supervision_disabled; then
30
            disabled[${#disabled[@]}]=$conf
31
        else
passage du répertoire de sou...
Sébastien MARQUE authored on 2017-02-24
32
            for dir in ${directories[@]}; do
33
                test -r "$dir/$conf" && source "$dir/$conf" $dir && let count_loaded++
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
34
            done
35
            test $count_loaded -gt 0 && loaded[${#loaded[@]}]=$conf
36
        fi
37
    done
38
    if test ${#loaded[@]} -ne $#; then
39
        for conf in $*; do
40
            l_ok=0
41
            for l in ${loaded[@]}; do
42
                test $conf == $l && l_ok=1 && break
43
            done
44
            if test $l_ok -eq 0; then
45
                for d in ${disabled[@]}; do
46
                    test $conf == $d && msg+="$conf (disabled)%0a" && l_ok=1 && break
47
                done
48
            fi
49
            test $l_ok -eq 0 && msg+="$conf%0a"
50
        done
51
        _err "_load" "config not loaded:%0a${msg/%%0a}"
52
    fi
53
}
54

            
55

            
56
function _do_tests () {
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
57
    local t=
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
58
    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
59
        local msg=
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
60
        if grep -q "^${t}$" $supervision_disabled; then
61
            echo supervision by $t disabled
62
        else
63
            msg=$( $t | sed 's/$/%0a/g' )
64
            if test -n "$msg"; then
65
                echo "problem: ${t}"
66
                err[${#err[@]}]=$t
67
                err[${#err[@]}]="${msg}"
68
            else
69
                if test -n "${!t}"; then
70
                    sed -ri "/^$t=.+$/d" $supervision_status
71
                fi
72
            fi
73
        fi
74
    done
75
}
76

            
77
function _do_manage_errors () {
78
    source $supervision_status
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
79
    local msg=
80
    local t=
81
    local errors=
82
    local notification=0
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
83

            
84
    if test ${#err[@]} -gt 0; then
85
        errors="Supervision $HOSTNAME (${loaded[@]}):%0a"
86
        for ((i=0; i<${#err[@]}; i++)); do
87
            t=${err[i]}
88
            msg=${err[++i]}
89
            errors="${errors}"'*'" [${t}] ${msg}"
90
            if test -n "${!t}"; then
91
                if test ${!t} -lt $(( $now - 86400 )); then
92
                    sed -ri "s/^(${t}=).+$/\1$now/" $supervision_status
93
                    let notification++
94
                fi
95
            else
96
                echo "${t}=$now" >> $supervision_status
97
                let notification++
98
            fi
99
        done
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
100
        errors=$(tr -d '\n' <<< $errors)
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
101
        if test "$SHLVL" -eq 1; then
102
            if test $notification -gt 0; then
103
                $(dirname "$0")/smsapi "$errors"
104
            fi
105
        else
106
            echo -e ${errors//\%0a/\\n}
107
        fi
108
    fi
109
}
110

            
111
_load ${*:-common-tests $HOSTNAME}
112
_do_tests
113
_do_manage_errors