#!/bin/bash # Nagios plugin # check if days from last update linux exceed # return a Nagios exit code depending on the result # 0 = OK # 1 = WARNING # 2 = CRITICAL # 3 = UNKNOWN # for help printing print_help() { echo "This Nagios plugin check last linux update exceeding a given elapsed time" echo "Usage : $0 -p -w -c " echo " -p parameter : name of the monitoring file apt. For granularity, quote commands with spaces." echo " -w parameter : minimal elapsed time for status WARNING on NAGIOS, in days." echo " -c parameter : minimal elapsed time for status CRITICAL on NAGIOS, in days." echo "returned performance data : number of process; oldest time in minutes; warning time in minutes; critical time in minutes; 0;" exit 3 } # check if there is at least one argument if [ -z $1 ] then echo "Missing arguments" echo "try \'$0 --help\' for help" exit 3 fi # print help if [[ ( $1 = "--help" || $1 = "-h" ) ]] then print_help exit 3 fi # assign value to arguments # print an error in case of unkown argument while getopts ":w:c:p:" options do case $options in w ) warning=$OPTARG ;; c ) critical=$OPTARG ;; p ) proc=$OPTARG ;; * ) echo "Unknown argument" echo "try \'$0 --help\' for help" exit 3 ;; esac done # check if all arguments are present if [[ ( -z $warning || -z $critical || -z $proc ) ]] then echo "Missing argument" echo "try \'$0 --help\' for help" exit 3 fi if [ -f "$proc" ]; then # echo "$proc exists." # cerco se c'è la stringa End-Date e, se c'è estraggo l'ultima X=( $(cat $proc | grep 'End-Date' | tail -1)) # X[1] riporta la data #echo $now #echo ${X[0]} #echo ${X[1]} #echo ${X[2]} #echo ${X[3]} #echo ${X[4]} #echo ${X[5]} # devo gestire le eccezioni cioè se il file esiste # e se cat /var/log/apt/history.log | grep 'End-Date' | tail -1 ritorna qualcosa # se non è così devo ritornare critical con messaggio ultimo update non disponibile if [ -n "${X[1]}" ]; then # Data di oggi convertita nel formato utile per la trasformazione in secondi currentDate=$(date +'%m/%d/%Y') #echo $currentDate # Data ultimo update se esiste convertita nel formato utile per la trasformazione in secondi lastupdDate=$(date -d ${X[1]} +'%m/%d/%Y') #echo $lastupdDate # Data ultimo update nel formato italiano ultimoupdate=$(date -d ${X[1]} +'%d/%m/%Y') # converto le date da confrontare in secondi currentseconds=$(date -d $currentDate +'%s') lastupdDateseconds=$(date -d $lastupdDate +'%s') #echo $currentseconds #echo $lastupdDateseconds # calcolo i giorni dall'ultimo update ((giorni=(currentseconds-lastupdDateseconds)/60/60/24)) msg="$giorni giorni dall'ultimo aggiornamento" if [ "$giorni" -gt "$critical" ] then echo "CRITICAL: $msg ($ultimoupdate)" exit 2 elif [ "$giorni" -gt "$warning" ] then echo "WARNING: $msg ($ultimoupdate)" exit 1 else echo "OK: $msg ($ultimoupdate)" exit 0 fi else echo "CRITICAL: Update mai eseguito!!!" exit 2 fi else echo "UNKNOWN: file $proc non esiste" exit 3 fi