scripts / monitoring.d / common-tests /
Newer Older
106 lines | 3.613kb
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
1
# vim: ft=sh
2

            
3
function _systemctl () {
4
    test -n "$1" || return
5
    eval $(systemctl show $1 | egrep '^(ExecMainStatus|Result|ActiveState|SubState|Type)=')
6
    if test -n "$Type"; then
7
           test $ExecMainStatus -eq 0 \
8
        && test $Result = 'success' \
9
        && test $ActiveState = 'active' \
10
        && [[ ( $Type =~ ^(simple|forking|notify)$ && $SubState = 'running' ) || ( $Type = 'oneshot' && $SubState = 'exited' ) ]] \
11
        || echo "état du service anormal"
12
    else
13
        echo "service $1 inexistant"
14
    fi
15
}
16

            
17
function sys_state () {
18
    local failed_units=
19
    eval $(systemctl show | egrep '^(SystemState|NFailedUnits)=')
20
    if test $SystemState != "running" -o $NFailedUnits -ne 0; then
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
21
        echo "$SystemState mode ($NFailedUnits units failed):"
22
        echo "$(systemctl --failed --no-legend | awk '
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
23
            {
24
                split($1, units, ".")
25
                failed[units[2]][units[1]]=1
26
                numfailed[units[2]]++
27
            }
28
            END {
29
                for (ut in failed) { 
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
30
                    print ut
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
31
                    for (un in failed[ut]) {
32
                        last = --numfailed[ut] == 0 ? 1 : 0
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
33
                        print last ? "└─" : "├─", un
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
34
                    }
35
                }
quelques corrections sur la ...
Sébastien MARQUE authored on 2017-02-22
36
            }')"
ajout de la supervision
Sébastien MARQUE authored on 2017-02-22
37
    fi
38
}
ajout d'un test de mise à jo...
Sébastien MARQUE authored on 2017-02-22
39

            
ajout needrestart
Sébastien MARQUE authored on 2019-12-22
40
function restart_needed () {
alerte sur besoin de redémar...
Sébastien MARQUE authored on 2019-12-19
41
    _turn 24 || return
fix need_restart
Sébastien MARQUE authored on 2019-12-24
42
    needrestart -m a -r l -bkl | awk -F': ' '{
alerte sur besoin de redémar...
Sébastien MARQUE authored on 2019-12-19
43
        if ($1 == "NEEDRESTART-SVC")
44
            needrestart[gensub("NEEDRESTART-", "", "1", $1)][gensub(".service", "", "1", $2)]++
45
        else
46
            needrestart[gensub("NEEDRESTART-", "", "1", $1)] = $2
47
        }
48
        END {
améliore détection maj kerne...
seb authored on 2021-01-01
49
            if (needrestart["KSTA"] > 1
50
            && gensub("-v[78]\\+$", "", "1", needrestart["KCUR"]) != gensub("-v[78]\\+$", "", "1", needrestart["KEXP"])) {
alerte sur besoin de redémar...
Sébastien MARQUE authored on 2019-12-19
51
                print "kernel:"
52
                print needrestart["KCUR"] " -> " needrestart["KEXP"]
53
            }
54
            if (length(needrestart["SVC"])) {
55
                print "services:"
56
                for (svc in needrestart["SVC"])
57
                    print "- " svc
58
            }
fix need_restart
Sébastien MARQUE authored on 2019-12-24
59
        }'
alerte sur besoin de redémar...
Sébastien MARQUE authored on 2019-12-19
60
}
61

            
ajout d'une fonction de test
Sébastien MARQUE authored on 2018-12-05
62
function autotest () {
63
    local testfile=/dev/shm/monitoring.autotest
64
    test -e $testfile && echo "it works" && rm -f $testfile
ajout d'un test de mise à jo...
Sébastien MARQUE authored on 2017-02-22
65
}
monitoring des cerificats
Sébastien MARQUE authored on 2017-02-25
66

            
ajout d'une alerte sur l'uti...
Sébastien MARQUE authored on 2018-12-05
67
function swap () {
68
    _turn 24 || return
69
    awk '/^\//{if ($1 !~ "zram") { p=$4/$3*100; if (p > 10) printf("%s (%.1f%%)\n", $1, p)}}' /proc/swaps
70
}
71

            
monitoring des cerificats
Sébastien MARQUE authored on 2017-02-25
72
function certificates () {
vérification des certificats...
Sébastien MARQUE authored on 2017-03-03
73
    _turn 24 || return
exclusion des domaines sans ...
seb authored on 2019-12-28
74
    for domain in /etc/letsencrypt/live/*.*; do
monitoring des cerificats
Sébastien MARQUE authored on 2017-02-25
75
        local notAfter="$(openssl x509 -enddate -noout -in $domain/fullchain.pem)"
76
        local enddate=$(date +%s -d"${notAfter#*=}")
77
        local delay=$(date +%_j -d@$(($enddate - $now)))
78
        if test $delay -lt 10; then
79
            echo "${domain##*/} expire dans $delay jours"
80
        fi
81
    done
82
}
déplacement de l'autit de ch...
Sébastien MARQUE authored on 2017-02-25
83

            
84
function load_avg () {
85
    local procs=$(grep -c "^processor" /proc/cpuinfo)
86
    local load=($(</proc/loadavg))
87
    local avg=(1 5 15)
88
    for ((i=0; i<${#avg[@]}; i++)); do
89
        test ${load[i]%.*} -ge $procs && echo "charge moyenne à ${avg[i]}min = ${load[i]}"
90
    done
91
}
déplacement de fonction pour...
Sébastien MARQUE authored on 2018-12-05
92

            
93
function upgrades () {
94
    local log="/var/log/unattended-upgrades/unattended-upgrades.log"
95
    test -r $log || return
96
    local lastline=$(tail -1 $log)
97
    local lastentry=$(date +%s -d "$(cut -f1,2 -d' ' <<< $lastline)")
98
    test -n "$lastentry" || return
99
    local status=$(cut -f3 -d' ' <<< $lastline)
100
    test $(($lastentry + 86400)) -lt $now && echo "pas de mise à jour depuis le ${lastline%% *}"
101
}
vérif synchro chrony
Sébastien MARQUE authored on 2021-07-07
102

            
103
function chrony () {
104
    local synced=$(chronyc sources | grep -c '^\^\*')
105
    test $synced -ne 1 && echo "NTP pas synchro"
106
}