Kontrola velikosti souborů z Nagiosu

Na jednom serveru potřebujeme hlídat velikost logů zda nerostou moc nad obvyklé meze. Nechci je rotovat po dosažení nějaké velikosti, chci jen vědět, že se děje nějaká neplecha.

 

#!/bin/bash

DIRECTORY=/logy
WARNSIZE=1024
CRITSIZE=2048

STATE_OK=0              # define the exit code if status is OK
STATE_WARNING=1         # define the exit code if status is Warning (not really used)
STATE_CRITICAL=2        # define the exit code if status is Critical
STATE_UNKNOWN=3         # define the exit code if status is Unknown

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin


for cmd in find stat [
do
 if ! `which ${cmd} 1>/dev/null`
 then
  echo "UNKNOWN: ${cmd} does not exist, please check if command exists and PATH is correct"
  exit ${STATE_UNKNOWN}
 fi
done

RESULT=""
ERROR=0
WARNING=0

[ -d $DIRECTORY ] || exit ${STATE_OK}

while read fname; do
        size=$(($(stat --format=%s "${fname}")/1024/1024))
        RESULT="${RESULT}${fname#$DIRECTORY} ${size} MiB, "
        if [ ${size} -gt $CRITSIZE ]; then
                ERROR=1
        elif [ ${size} -gt $WARNSIZE ]; then
                WARNING=1
        fi
done < <(find ${DIRECTORY} -type f -size +${WARNSIZE}M -not -name '*.gz')

if [ $ERROR -gt 0 ]; then
        echo "ERROR: ${RESULT}"
        exit ${STATE_CRITICAL}
elif [ $WARNING -gt 0 ]; then
        echo "WARNING: ${RESULT}"
        exit ${STATE_WARNING}
else
        echo "OK: vsechny logy jsou mensi nez ${WARNSIZE} MiB"
        exit ${STATE_OK}
fi