Meus primeiros 2 plugins para o Nagios #linux #nagios #debian #shellscript
Montei 2 plugins para o Nagios. Um checa se os IPs informados como parâmetro estão listados em uma série de RBLs e o outro plugin baixa um arquivo do SNDS (SmartNetworkDataServices – serviço do Hotmail), para verificação de bloqueios de IPs naquele provedor (de longe, o mais crítico).
Depois de muita briga, os plugins finalmente ficaram prontos. Ambos em shell script.
Palhinha dos dois:
#!/bin/bash
log_file=”/endereco_do_arquivo_de_log”
pid_file=”/endereco_do_arquivo_de_pid”
rbls=”/endereco_do_arquivo_de_rbls (.rblcheckrc)”
stored_logs=”/endereco_do_arquivo_temporario_de_logs”
# Funcoes de ajuda, mensagens de log, remocao de arquivo de PID e envio de email
help(){
echo “echo “echo “Usage $0 -c \”<ADDRESS>[[/]<NETMASK>][ <ADDRESS>[[/]<NETMASK>]]>\”"
msg=”[`date`] PID $$: $1″
echo $msg >> $log_file
if [ "$DEBUG" -a "$2" != 1 ]; then
echo $msg
fi
}
kill_pid(){
rm -f $pid_file
}
notification(){
echo -e “$1″ “$2″ | mail -e -s “$3″ $4
}
# Valida se os programas necessarios estao instalados
err=”"
ipcalc=$(which ipcalc) || err=”ipcalc”;
rblcheck=$(which rblcheck) || err=”rblcheck”;
sed=$(which sed) || err=”sed”;
cut=$(which cut) || err=”cut”;
cat=$(which cat) || err=”cat”;
rm=$(which rm) || err=”rm”;
mail=$(which mail) || err=”mail”;
rm=$(which rm) || err=”rm”;
if [ ! -z "$err" ]; then
log_msg “Comando nao encontrado: $err”
kill_pid
exit 0
fi
# Valida se arquivo de rbls existe
if [ ! -f $rbls ] ; then
log_msg “Arquivo de rbls $rbls nao encontrado”
kill_pid
exit 0
fi
# Extracao de argumentos na linha de comando
while getopts c:d:e: OPTION ; do
case $OPTION in
c)
CLASS=$OPTARG
;;
d)
DEBUG=1
;;
e)
EMAIL=$OPTARG
;;
esac
done
# Valida parametros obrigatorios
if [ -z "$CLASS" ]; then
help;
kill_pid
exit 0
fi
#Verifica se o plugin ja esta em execucao
if [ -f $pid_file ] ; then
pid=$(cat $pid_file)
if [[ "$pid" =~ [0-9]+ ]] ; then
return=$(ps -p $pid | wc -l)
if [ $return -gt 1 ] ; then
log_msg “Plugin ja esta em execucao com PID $pid”
kill_pid
exit 0
fi
fi
fi
#Armazena o ID do processo no arquivo de PID
echo $$ > $pid_file
# Calcula intervalo de IPs fornecidos
listed=0
cat /dev/null > $stored_logs
for str in $CLASS; do
# Valida IP/classe
if [ ! $str =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\(\/[0-9]+\)?$ ]; then
log_msg “Formato de IP invalido”
kill_pid
exit 0
fi
# Verifica se possui mascara de rede
if [[ $str =~ .*\/.* ]]; then
log_msg “Iniciando execucao do script para $str…”
hostmin=$(ipcalc -b $str|grep HostMin|sed ‘s/[^\.0-9]*//g’)
hostmax=$(ipcalc -b $str|grep HostMax|sed ‘s/[^\.0-9]*//g’)
else
hostmin=$str
hostmax=$str
fi
# Obtem o IP inicial e final para aplicar o loop
from=$(echo $hostmin|cut -d’.’ -f4)
to=$(echo $hostmax|cut -d’.’ -f4)
initial=$(echo $hostmin|sed ‘s/[0-9]*$//g’)
# Consulta cada IP
for i in $(seq $from $to) ; do
log_msg “Consultando IP $initial$i” 1
result=`rblcheck -c $(cat $rbls) $initial$i | grep “$initial$i listed”`
if [ ! -z "$result" ]; then
listed=1
log_msg “$result”
echo “$result” >> $stored_logs
fi
done
done
# Retorno
if [[ $listed == 1 ]] ; then
cat $stored_logs
notification “Resultado da checagem de IPs: \n\n” “`cat $stored_logs`” “RBLCHECK WARNING” $EMAIL
kill_pid
rm -f $stored_logs
exit 0
else
result=”OK. Nenhum IP bloqueado.”
echo “$result” >> $stored_logs
notification “Resultado da checagem de IPs: \n\n” “`cat $stored_logs`” “RBLCHECK IS OK” $EMAIL
kill_pid
rm -f $stored_logs
exit 0
fi
log_msg “$result”
[ $DEBUG ] || echo $result
E SNDSCHECK:
#!/bin/bash
ADDR=”https://endereco_do_arquivo_de_IPs_bloqueados”
DEST=”/endereco_local_do_arquivo_de_IPs_bloqueados”
LOG=”/endereco_do_arquivo_de_logs”
ERR=”/endereco_do_arquivo_de_erros”
STORED_LOGS=”/endereco_do_arquivo_de_logs_temporarios”
PID_FILE=”/endereco_do_arquivo_de_pid”
log_msg(){
msg=”[ `/bin/date` ] $1″
echo “$msg” >> $LOG
}
log_err(){
msg=”[ `/bin/date` ] $1″
echo “$msg” >> $ERR
}
help(){
echo “Usage $0 -e <EMAIL>”
}
kill_pid(){
rm -f $PID_FILE
}
remove_file(){
rm -f $DEST$FILE
}
# Extracao de argumentos na linha de comando
while getopts e: OPTION ; do
case $OPTION in
e)
EMAIL=$OPTARG
;;
esac
done
# Validando dependencias
erro=”"
wget=$(which wget) || err=”wget”;
cat=$(which cat) || err=”cat”;
date=$(which date) || err=”date”;
mail=$(which mail) || err=”mail”;
if [ ! -z $erro ] ; then
log_err “Aplicativo nao encontrado: $erro”
exit $STATE_CRITICAL
fi
if [ ! -d $DEST ] ; then
log_err “Diretorio de destino nao encontrado: $DEST”
exit $STATE_CRITICAL
fi
# Valida parametros do script
if [ ! $EMAIL ] ; then
help;
exit $STATE_CRITICAL
fi
# Valida se processo ja esta em execucao
if [ -f $PID_FILE ] ; then
pid=$(cat $PID_FILE)
if [[ "$PID_FILE" =~ [0-9]+ ]] ; then
retorno=$(ps -e $pid | wc -l)
if [ $retorno -gt 1 ] ; then
log_err “Plugin ja esta em execucao com PID $pid”
echo “Plugin ja esta em execucao com PID $pid”
kill_pid
exit $STATE_WARNING
fi
fi
fi
#Armazena o ID do processo no arquivo de PID
echo $$ > $PID_FILE
# Download do arquivo
if ! wget -q –no-verbose -T 60 –no-check-certificate -P $DEST $ADDR ; then
log_err “Erro ao baixar arquivo.”
echo “Erro ao baixar arquivo.”
exit $STATE_CRITICAL
fi
# Retencao dos IPs
listed=0
FILE=$(ls $DEST | grep ipStatus)
IPS=$(cat $DEST$FILE | cut -d , -f 1,2 | sed -r “s/,/\n/g” | sort | uniq)
if [ ! -z "$IPS" ] ; then
blocked=1
fi
# Retorno para o Nagios
if [[ $blocked == 1 ]] ; then
echo $IPS | sed -r “s/ /\n/g” > $STORED_LOGS
cat $STORED_LOGS
echo -e “Os IPs abaixo estao bloqueados no Hotmail: \n\n” “`echo $IPS | sed -r ‘s/ /\n/g’`” | mail -e -s “SNDSCheck WARNING” $EMAIL
exit $STATE_WARNING
else
IPS=”Nao ha IPs bloqueados no Hotmail.”
echo $IPS
echo -e “$IPS” | mail -e -s “SNDSCheck is OK” $EMAIL
exit $STATE_OK
fi
kill_pid
remove_file