Skip to content

Instantly share code, notes, and snippets.

@goulon
Last active July 7, 2017 20:00
Show Gist options
  • Save goulon/0083ed48efd8d0fc5cd229429c7e00d0 to your computer and use it in GitHub Desktop.
Save goulon/0083ed48efd8d0fc5cd229429c7e00d0 to your computer and use it in GitHub Desktop.
#!/bin/bash
ctrl_value=0
exit_control=0
repetitions=${repetitions:=5}
warn=${WARN:=0.8}
crit=${CRIT:=1}
# Define qual vai ser o valor da saída (armazena o maior valor)
set_exit_control () {
# $1 - Saída de erro: 1 - Warning / 2 - Critical
if [ $1 -gt $exit_control ]
then
exit_control=$1
fi
}
# Indica para alertar (exit 1) apos "repetitions" vezes que alerta permanecer
get_control () {
# $1 - Indica se normalizou ou não
value=$ctrl_value
if [ "$value" ]
then
if [ "$1" == "sim" ]
then
let ctrl_value=0
echo 0
else
((value++))
if [ $value -ge $repetitions ]
then
echo 1
else
echo 0
fi
let ctrl_value=$value
fi
else
if [ "$1" == "sim" ]
then
let ctrl_value=0
else
let ctrl_value=1
fi
echo 0
fi
}
# Realiza o calculo do LOAD e gera o alerta (exit) conforme a criticidade
get_load () {
vLoadPerCore=$(echo $(uptime | awk '{ gsub(/,/, ""); print $(NF-2)}') / $(cat /proc/cpuinfo | grep 'processor'| wc -l) | bc -l)
if [ $(echo ${vLoadPerCore}'>='${crit} | bc -l) -eq 1 ]
then
echo "LOAD CRITICAL - $(printf "%.4f" ${vLoadPerCore})"
if [ $(get_control "nao") -eq 1 ]
then
set_exit_control 2
fi
elif [ $(echo ${vLoadPerCore}'>='${warn} | bc -l) -eq 1 ]
then
echo "LOAD WARNING - $(printf "%.4f" ${vLoadPerCore})"
if [ $(get_control "nao") -eq 1 ]
then
set_exit_control 1
fi
else
echo "LOAD OK - $(printf "%.4f" ${vLoadPerCore})"
get_control "sim" > /dev/null
fi
}
# Imprime o help deste script
print_help () {
printf "\n\nOpção\t\t==> Descrição
-------------------------------
%s\t\t==> %s
%s [%s]\t==> %s
%s [%s]\t==> %s
%s [%s]\t==> %s\n\n\n"\
'-h' "Imprime este help;"\
'-w' "número" "Valor limite do LOAD para alertar como warning;"\
'-c' "número" "Valor limite do LOAD para alertar como crítico;"\
'-r' "número" "Define a quantidade de repetições que deverá ocorrer para gerar o alerta, após os limites de warning e critical do LOAD já tiverem ultrapassados."
}
control=0
while getopts 'hw:c:r:' OPT; do
case $OPT in
h) ;;
w) warn="${OPTARG}"; control=1
;;
c) crit="${OPTARG}"; control=1
;;
r) repetitions="${OPTARG}"; control=1
;;
esac
done
if [ $control -eq 0 ]
then
print_help
else
export LANG="en_US.UTF-8"
get_load
fi
exit $exit_control
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment