cron-monitor.sh script - Documentation for BMC PATROL for Scripting 2.1
Liam Parker
This script is available at $PATROL_HOME\..\TRO\Conf\Scripts\Samples directory. The script content is as follows:
e#!/bin/sh -f
# This sample script can be used with the KM to set-up monitoring of cron job execution.
# The KM is polling on the /var/log/cron file which contains result of all crontab jobs.
#==================================================================================================
# Notice: access to /var/log/cron is usually limited to root only, and the policy should be defined accordingly.
#==================================================================================================
# the script here how to poll on a file based on a time-stamp.
# i.e. poll on a file and get only the text added to it during the last period (e.g. 60 seconds).
# this is based on calculating difference between:
# $LINE_TIME - The time at which a job was running
# $TIME_STAMP - Now minus given collection cycle (e.g. 2 minutes) - and provided as command line argumment of the script.
# also presented:
# 1. date -d and +%s flags are used to compare dates in EPOCH format
# 2. Usage of variable NEW_JOB_COUNT as counter and how to increment it's value
# the KM then report on the number of jobs executing during the last collection (polling) cycle
TMPFILE=/tmp/`basename $0`.tmp
TIME_STAMP=`date -d "$1 minutes ago" +%s`
NEW_JOB_COUNT=0
function process_cron_line
{ LINE_TIME=`date -d "$1 $2 $3" +%s` if [ $LINE_TIME -ge $TIME_STAMP ]; then NEW_JOB_COUNT=$((NEW_JOB_COUNT + 1)) echo $* >> $TMPFILE fi
}
if [ -r /var/log/cron ]; then for raw_line in $(tail -n 10 /var/log/cron|sed 's/ /\FF/g') do line=`echo $raw_line | sed 's/\FF/ /g'` process_cron_line $line done echo $NEW_JOB_COUNT if [ $NEW_JOB_COUNT -gt 0 ]; then cat $TMPFILE rm $TMPFILE fi
else echo -1 echo "Cannot open /var/log/cron, please a different user or change file permissions."
fi