kasus:
Dalam server produksi menjalankan service secara otomatis menggunakan crond, masalah muncul bila crond mati atau gagal menjalan kan service.
maka saya buat plugins untuk check crond service berdasarkan log file, mengikuti referensi
http://rubypond.com/blog/bash-script-to-check-timestamp-on-logfile
saya rubah sesuai kebutuhan.
di dalam crond file, buat agar service menghasilkan dua file, log file dan error log file
Dalam server produksi menjalankan service secara otomatis menggunakan crond, masalah muncul bila crond mati atau gagal menjalan kan service.
maka saya buat plugins untuk check crond service berdasarkan log file, mengikuti referensi
http://rubypond.com/blog/bash-script-to-check-timestamp-on-logfile
saya rubah sesuai kebutuhan.
di dalam crond file, buat agar service menghasilkan dua file, log file dan error log file
/path/to/myscript > /var/log/myscript 2>&1; echo $? > /var/log/myscript-error-code
#!/bin/bash # Standard Nagios return codes #OK = 0 #WARNING = 1 #CRITICAL = 2 #UNKNOWN = 3 usage() { echo "usage: check_crond [-w warning_value (in minutes)] [-c critical_value (in minutes)] [-d log_path] [ -l log_file] [-e error_log_file]" echo echo "check_crond -w 0 -c 5 -d /var/log -l my_log_file -e my_error_log_file" echo echo "warning value must less than critical value" exit 3 } while getopts w:c:d:l:e: OPTNAME; do case "$OPTNAME" in w) WARNVAL="$OPTARG" ;; c) CRITVAL="$OPTARG" ;; d) LOGDIR="$OPTARG" ;; l) LOGFILE="$OPTARG" ;; e) LOGERROR="$OPTARG" ;; *) usage ;; esac done #check value if [ "$WARNVAL" != "" -a "$CRITVAL" != "" ]; then if [ $CRITVAL -le $WARNVAL ] ; then echo "Error: critical_value must be greater than warning_value" exit 3 fi fi # Compare if [[ $(find $LOGDIR -name $LOGERROR) ]] ; then # Get last change time stamp from log and error file RUN=`stat -c %y $LOGDIR/$LOGFILE | cut -f1 -d '.'` ERROR=`stat -c %y $LOGDIR/$LOGERROR | cut -f1 -d '.'` # Convert date into second MIN_RUN=`date -d "$RUN" +%s` MIN_ERROR=`date -d "$ERROR" +%s` if [ $MIN_ERROR -gt $MIN_RUN ] ; then MINUTES_SINCE=$(((`date +%s` - `date -d "$ERROR" +%s`)/60)) if [ $MINUTES_SINCE -ge $WARNVAL ] ; then echo "** WARNING **" echo "last error at $MINUTES_SINCE minutes ago" exit 1; fi if [ $MINUTES_SINCE -gt $CRITVAL ] ; then echo "** CRITICAL **" echo "last error at $MINUTES_SINCE minutes ago" exit 2; fi else echo "** OK **"; echo "crond run at $RUN at `hostname`" fi else echo "Error: file not found, please put the right path file" exit 3 fi exit 0
Comments
Post a Comment