scripts / monitoring.d / raspberrypi /
Sébastien MARQUE refresh swap if possible
c26f0eb 3 years ago
1 contributor
123 lines | 4.654kb
# vim: ft=sh
dir=$1

function nginx () {
    nc -zw2 127.0.0.1 80 || echo "injoignable sur port 80 local"
    nc -zw2 sebmarque.hd.free.fr 80 || echo "injoignable sur port 80 distant"
    nc -zw2 sebmarque.hd.free.fr 443 || echo "injoignable sur port 443 distant"
}

function searx () {
    _turn 5 || return
    nc -zw2 127.0.0.1 8888 || echo "injoignable sur port 8888"
}

function gitprep () {
    _turn 5 || return
    nc -zw2 127.0.0.1 10020 || echo "injoignable sur port 10020"
}

function webapps () {
    _turn 3 || return # test toutes les 3 heures
    awk -v sq="'" '
        /^[^#]+#\s*monitoring\s/ {
            if ($1 == "listen") {
                port = ":" gensub(";", "", "1", $2)
                proto = gensub(";", "", "1", $3) == "ssl" ? "https" : "http"
                pname = gensub(/^\s*listen\s.+#\s*monitoring\s+/, "1", "1")
                if (proto == "https" && port == ":443") port = ""
            }
            if ($1 == "server_name") {
                server_name = gensub(";", "", "1", $2)
                sname = gensub(/^\s*server_name\s.+#\s*monitoring\s+/, "1", "1")
            }
            if ($1 == "location" && sname != "" && pname != "" && sname == pname) {
                l=gensub(/^\s*location\s[^\/]*(\/.*)\{\s*#\s*monitoring\s+(.+)$/,"\\1,\\2", "1")
                split(l, location, /\s*,\s*/)
                if (location[2] == sname) {
                    monitoring[location[2]][location[1]]["monitoring"] = location[3]
                    monitoring[location[2]][location[1]]["response_code"] = location[4]
                    monitoring[location[2]][location[1]]["url"] = proto "://" server_name port location[1] location[5]
                    if (location[6] != "")
                        monitoring[location[2]][location[1]]["user-agent"] = "--user-agent " sq location[6] sq
                    else
                        monitoring[location[2]][location[1]]["user-agent"] = "--user-agent " sq "Mozilla/5 (monitoring)" sq
                }
            }
        }
        END {
            for (mid in monitoring) {
                for (locid in monitoring[mid]) {
                    curl = sprintf("curl --silent --max-time 2 --output /dev/null --write-out %{response_code} --output /dev/null %s %s",
                          monitoring[mid][locid]["user-agent"],
                          monitoring[mid][locid]["url"])
                    curl | getline response_code
                    close(curl)
                    if (response_code != monitoring[mid][locid]["response_code"] && response_code != "000")
                        printf("%s (%s)\n", monitoring[mid][locid]["monitoring"], response_code)
                }
            }
        }
    ' /etc/nginx/sites-enabled/*
}

function section () {
    nc -zw5 cloud.paris12.pcf.fr 443 || echo "cloud inaccessible"
    nc -zw5 cloud.paris12.pcf.fr 2200 || echo "section inaccessible"
}

function core_temp () {
    local min=60
    local level=("warning" "severe" "danger!")
    local step=$(( $(</sys/class/thermal/thermal_zone0/temp) - (min * 1000) ))
    if test $step -gt 1; then
#    if test $step -gt 5000; then # en été il fait plus chaud, c'est normal que ça monte à 60° régulièrement
        step=$((step / 10000))
        test $step -le 2 || step=2
        echo "${level[step]}: température > $((min + step * 10))°"
    fi
}

function core_alim () {
    eval $(/opt/vc/bin/vcgencmd measure_volts core)
    volt_fmt=${volt/\.}
    volt_fmt=${volt_fmt/V}
    test $volt_fmt -lt 12000 -o $volt_fmt -gt 15000 && echo "$volt"
}

function clean_swap () {
    _turn 2 || return
    if free -m | awk '
        /^Mem:/ {
            free = $4
        }
        /^Swap:/ {
            used = $3
        }
        END {
            exit !(used > 31 && free - used > 0)
        }
    '; then
        systemctl stop dphys-swapfile \
        && systemctl stop zram        \
        && systemctl start zram       \
        && systemctl start dphys-swapfile \
        || echo 'erreur'
    fi
}

function mem_pressure () {
    local meminfo=($(</proc/meminfo))
    local level=(danger severe warning)
    local margin=30000
    for ((i=0; i<${#meminfo[@]}; i+=3)); do
        test ${meminfo[i]} == 'MemAvailable:' && break
    done
    meminfo[i+2]=$(( ( ${meminfo[i+1]} - $margin ) / 100000 ))
    test ${meminfo[i+2]} -ge ${#range[@]} || echo "${level[${meminfo[i+2]}]}: available ${meminfo[i+1]}kB"
}

function piwigo_private () {
    _turn 20 || return
    test "$(curl --silent 'http://192.168.0.71/photos/ws.php?format=json&method=pwg.categories.getList' | sum)" = "00569     1" || echo "albums visibles publiquement"
}