Last active
December 14, 2015 20:58
-
-
Save vekexasia/5147372 to your computer and use it in GitHub Desktop.
Nagios plugin that let you search within a logfile for a pattern ( could be empty ) and have a critical/warning value when the number of lines matching the pattern exceeds critical/warning within the last N minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## This program is free software: you can redistribute it and/or modify | |
## it under the terms of the GNU General Public License as published by | |
## the Free Software Foundation, either version 3 of the License, or | |
## (at your option) any later version. | |
## | |
## This program is distributed in the hope that it will be useful, | |
## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
## GNU General Public License for more details. | |
## | |
# =============== | |
# check_last_logs_lines - plugin to check the number of matching lines in a temporal timeframe | |
# =============== | |
# * Author: Andrea Baccega | |
# version 1.0 (12.Mar.2013) | |
# plugin return codes: | |
# 0 OK | |
# 1 Warning | |
# 2 Critical | |
# 3 Unknown | |
while getopts "hw:c:m:l:r:" opt | |
do | |
case $opt in | |
h) | |
showhelp=1 | |
break | |
;; | |
w) | |
declare -r warning="$OPTARG" | |
;; | |
c) | |
declare -r critical="$OPTARG" | |
;; | |
m) | |
declare -r minutes="$OPTARG" | |
;; | |
l) | |
declare -r logfile="$OPTARG" | |
;; | |
r) | |
declare -r regexp="$OPTARG" | |
;; | |
esac | |
done | |
printUsage() { | |
echo "Usage: $0 [-h] -l <logfile> -m <minutes> -r <regexp> -w <warning> -c <critical> " | |
echo "" | |
echo "Example: $0 -l /var/log/apache_errors.log -r 'HTTP/1\\.1\" 404' -m 5 -w 10 -c 30 " | |
} | |
printHelp() { | |
printUsage | |
echo "" | |
echo "This plugin checks the number of messages processed by the logfile you specify in the last N minutes. matching an optional regexp" | |
echo "" | |
echo "For more details, see inside the script ;)" | |
echo "" | |
exit 3 | |
} | |
getnrMesgs () { | |
CURTIME=`date +%s` | |
TIMEEGREP="(" | |
TIMEEGREP=$TIMEEGREP$(date +%R) | |
for ((i=1;i<=$minutes; i++)); do | |
TMP=$(date --date="@$(($CURTIME - $i * 60))" +%R) | |
TIMEEGREP="$TIMEEGREP|$TMP" | |
done; | |
TIMEEGREP="$TIMEEGREP)" | |
egrep "${regexp}" $logfile | tail -$(($critical + 1)) | egrep "${TIMEEGREP}" | wc -l | |
} | |
if [ "$showhelp" = "1" ]; then | |
printHelp | |
exit 3 | |
fi | |
if [ ! "$warning" ] || [ ! "$critical" ] || [ ! "$minutes" ] || [ ! "$logfile" ]; then | |
printUsage | |
exit 3 | |
fi | |
if [ ! $minutes -ge "0" ] || [ ! $minutes -le "180" ]; then | |
printUsage | |
exit 3 | |
fi | |
if [ ! "$logfile" ]; then | |
echo "Could not find the specified logfile!" | |
exit 3 | |
fi | |
if [ $warning -ge $critical ]; then | |
echo "<warning> has to be smaller than <critical>!" | |
exit 3 | |
fi | |
nrmsgs=`getnrMesgs` | |
echo "Matched log entries in the last $minutes minutes: $nrmsgs" | |
if [ "$nrmsgs" -ge "$critical" ]; then | |
exit 2 | |
elif [ "$nrmsgs" -ge "$warning" ]; then | |
exit 1 | |
else | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment