scripts / supervision.d / common-tests /
Sébastien MARQUE ajout de la supervision
ad4691f 7 years ago
1 contributor
38 lines | 1.347kb
# vim: ft=sh

function _systemctl () {
    test -n "$1" || return
    eval $(systemctl show $1 | egrep '^(ExecMainStatus|Result|ActiveState|SubState|Type)=')
    if test -n "$Type"; then
           test $ExecMainStatus -eq 0 \
        && test $Result = 'success' \
        && test $ActiveState = 'active' \
        && [[ ( $Type =~ ^(simple|forking|notify)$ && $SubState = 'running' ) || ( $Type = 'oneshot' && $SubState = 'exited' ) ]] \
        || echo "état du service anormal"
    else
        echo "service $1 inexistant"
    fi
}

function sys_state () {
    local failed_units=
    eval $(systemctl show | egrep '^(SystemState|NFailedUnits)=')
    if test $SystemState != "running" -o $NFailedUnits -ne 0; then
        msg="$SystemState mode ($NFailedUnits units failed):%0a"
        msg+=$(systemctl --failed --no-legend | awk '
            {
                split($1, units, ".")
                failed[units[2]][units[1]]=1
                numfailed[units[2]]++
            }
            END {
                for (ut in failed) { 
                    printf("%s%%0a", ut)
                    for (un in failed[ut]) {
                        last = --numfailed[ut] == 0 ? 1 : 0
                        printf("%s %s%s", last ? "└─" : "├─", un, last ? "" : "%0a")
                    }
                }
            }')
    fi
}