File: //lib64/nagios/plugins/check_md5files
#!/bin/bash
# Name: check_md5files
# Author: Tsvetan Gerov <tsvetan@worldhost.group>
# Version 0.2
#
# Description: Check the md5sum of a multiple files stored in a file
# Usage: To add a file/files to a list, run:
# md5sum /path/to/file >> /usr/lib64/nagios/plugins/md5list
MD5=$(which md5sum)
MD5_LIST="/usr/lib64/nagios/plugins/md5list"
MD5_LIST_CUSTOM="/usr/lib64/nagios/plugins/md5list.custom"
if [ -f "$MD5_LIST_CUSTOM" ]; then
MD5_LIST="$MD5_LIST_CUSTOM"
fi
# Initialize flags and error message
CRITICAL=false
ERROR_MESSAGE=""
# Check for deps
DEPS="$MD5 $MD5_LIST"
for DEP in $DEPS; do
if [ ! -f "$DEP" ]; then
echo "[UNKNOWN] Not found $DEP"
exit 3
fi
done
# Check MD5 sum of files
for FILE in $(cat $MD5_LIST | awk '{print$2}'); do
MD5SUM=$(grep $FILE $MD5_LIST | awk '{print$1}')
if [ ! -f $FILE ]; then
CRITICAL=true
ERROR_MESSAGE+="$FILE does not exists!, "
else
LMD5=$($MD5 $FILE | awk '{print$1}')
if [ $MD5SUM != $LMD5 ]; then
CRITICAL=true
ERROR_MESSAGE+="$FILE has been modified!, "
fi
fi
done
# Return final state
if [ "$CRITICAL" = true ]; then
echo "[CRITICAL] ${ERROR_MESSAGE%, }"
exit 2
else
echo "[OK] All files match their MD5SUM"
exit 0
fi