Last active
April 26, 2018 12:07
-
-
Save rabbitt/d6da491751bbecde3b5f to your computer and use it in GitHub Desktop.
nagios script to check log files
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 | |
PROGRAM="$(/bin/basename "$0")" | |
REVISION="1.0.0" | |
STATE_OK=0 | |
STATE_CRITICAL=2 | |
STATE_UNKNOWN=3 | |
revision_details() { | |
echo "$1 v$2 (nagios-plugins 2.0.3)" | |
echo "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute" | |
echo "copies of the plugins under the terms of the GNU General Public License." | |
echo "For more information about these matters, see the file named COPYING." | |
echo | |
return 0 | |
} | |
usage() { | |
cat <<EOF | |
Usage: | |
${PROGRAM} [options] <regexp>[ <regexp>[ <regexp[ ...]]] | |
Options: | |
-f, --file <FILE> Logfile to search | |
-x, --exitcode <CODE> Code to exit with if match successful | |
-i, --invert Invert match expression (found == fail, not found = success) | |
-s, --start-at <LINE> Line number to start looking from | |
-v, --verbose Verbose output | |
-V, --version Show version and exit | |
-h, --help Show usage information and exit | |
EOF | |
return 0 | |
} | |
show_help() { | |
revision_details "${PROGRAM}" "${REVISION}" | |
usage | |
echo -e "Log file pattern detector plugin for Nagios\n" | |
return 0 | |
} | |
option_fail() { | |
[ $# -gt 0 ] && echo -e "Error: $*\n" | |
show_help | |
exit $STATE_UNKNOWN | |
} | |
exitcode=$STATE_OK #default | |
verbose=0 | |
invert=0 | |
[ $# -lt 1 ] && show_help && exit $STATE_UNKNOWN | |
OPTS=$(getopt --name "${PROGRAM}" -o f:x:s:ivhV -l file:,exitcode:,start-at:invert,verbose,help,version -- "$@") | |
[ $? != 0 ] && exit 1 | |
eval set -- "${OPTS}" | |
while true; do | |
case "$1" in | |
--help|-h) show_help; exit $STATE_OK ;; | |
--version|-V) revision_details "${PROGRAM}" "${REVISION}"; exit $STATE_OK ;; | |
--verbose|-v) verbose=$((verbose + 1));; | |
--invert|-i) invert=1;; | |
--start-at|-s) start_at=$2; shift 1;; | |
--file|-f) | |
logfile="$2"; | |
idxfile="/var/tmp/$(echo "${logfile}" | md5sum | awk '{ print $1 }').idx" | |
shift 1;; | |
--exitcode|-x) | |
found_exitcode=$(($2 + 0)) # this silences certain errors on shellcheck.net | |
shift 1;; | |
--) shift; break;; | |
*) echo "Unknown argument: $1" && usage && exit $STATE_UNKNOWN;; | |
esac | |
shift | |
done | |
[ ${verbose} -gt 0 ] && set -x | |
[ ${verbose} -gt 1 ] && set -v | |
[ -n "${logfile}" ] || option_fail "logfile not specified!" | |
[ -r "${logfile}" ] || option_fail "logfile not readable" | |
[ $# -ne 0 ] || option_fail "no regular expressions specified!" | |
if [ ! -e "${idxfile}" ]; then | |
echo -en 0 > "${idxfile}" | |
fi | |
idx_line=$(cat "${idxfile}") | |
start_at=$((${start_at:-${idx_line}} + 0)) | |
current_line_count=$(wc -l < "${logfile}") | |
# if the number of lines in the file is less than the current | |
# start_at marker, reset start_at to 0 | |
[[ ${current_line_count} -lt ${start_at} ]] && start_at=0 | |
# update the stored line index | |
echo -en "${current_line_count}" > "${idxfile}" | |
# determine total number of lines to check | |
check_line_count=$((current_line_count - start_at - 1)) | |
[[ ${check_line_count} -lt 0 ]] && check_line_count=0 | |
output=() | |
line_matches=0 | |
regexp_matches=0 | |
for regexp in "$@"; do | |
found=$(tail -n+${start_at} "${logfile}" | head -n${check_line_count} | grep -Pc "${regexp}") | |
line_matches=$((line_matches + found)) | |
if [[ ${found} -gt 0 ]]; then | |
regexp_matches=$((regexp_matches + 1)) | |
output+=( "${found} matches for expression '${regexp}'" ) | |
[[ ${invert} -eq 0 ]] && exitcode=${found_exitcode:-$STATE_CRITICAL} | |
else | |
output+=( "no matches found for expression '${regexp}'" ) | |
# with inversion, any expression /not/ found causes termination | |
# with exit code == ( user defined exitcode || state_critical ) | |
[[ ${invert} -eq 1 ]] && exitcode=${found_exitcode:-$STATE_CRITICAL} | |
fi | |
done | |
echo "${line_matches} matches found from ${regexp_matches} expression(s)" | |
for msg in "${output[@]}"; do | |
echo " - $msg" | |
done | |
exit $exitcode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment