Bagaimana bila kita ingin memeriksa seluruh file dalam sebuah folder, lalu bila kita menemukan file yang time stamp nya lebih dari 30 menit/ dari waktu yang kita tentukan, nagios mengirimkan peringatan.
#!/bin/bash # Standard Nagios return codes #OK = 0 #WARNING = 1 #CRITICAL = 2 #UNKNOWN = 3 usage() { echo "usage: check_amq [-c critical_value (in minutes)] [-d amq_path]" echo echo "check_amq -c 30 -d /path/to/amq_journal" exit 3 } while getopts c:d: OPTNAME; do case "$OPTNAME" in c) CRITVAL="$OPTARG" ;; d) DIRPATH="$OPTARG" ;; *) usage ;; esac done #check value if [ "$CRITVAL" = "" ]; then echo "Error: critical_value must not empty" usage exit 3 fi #check directory path if [ "$DIRPATH" = "" ]; then echo "Error: directory path must not empty" usage exit 3 fi #set status number to 0 stat=0 # check if file are exist on that directory CHECKFILE=$(($(ls -la $DIRPATH | grep -v '^d'| wc -l)-1)) if [ $CHECKFILE -gt 0 ]; then for file in $(find $DIRPATH -type f -print) do CHECK=`stat -c %y $file | cut -f1 -d '.'` MINUTES_SINCE=$(((`date +%s` - `date -d "$CHECK" +%s`)/60)) if [ $MINUTES_SINCE -gt $CRITVAL ] ; then echo "$file are $MINUTES_SINCE minutes" let stat+=1 fi done if [ $stat -gt 0 ]; then echo "*** CRITICAL ***" echo "$stat files older than $CRITVAL minutes" exit 2; else echo "OK - all file age lower than $CRITVAL minutes" exit 0; fi fi echo "No file at $DIRPATH"hasil nya:
[fazries@fazries.com ~]$ ./check_amq.sh -c 100 -d /home/fazries/123 /home/fazries/123/1.txt are 103 minutes *** CRITICAL *** 1 files older than 100 minutes [fazries@fazries.com ~]$ ./check_amq.sh -c 150 -d /home/fazries/123 OK - all file age lower than 150 minutes
Comments
Post a Comment