Hi Readers,
In this article, we will see the Oracle : Shell script for Crontab Monitoring alert
THIS SCRIPT WILL IDENTIFY changes with the crontab file made comparing with original file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[oracle@nposqa1 ~]$ cat /home/oracle/DBA/bin/crontab_compare.bash #!/bin/bash # set -x # Get the current crontab entries # Compare with the expected crontab entries # Notify if any changes found ######################################################################################### # Control file locations separately. # create following variables in the file # ORIGFILE = Original file to compare the current crontab entries # EMAILRECIPIENTS='example@example.com' # EMAILBODY=Location of email body file. VARIABLES=$1 . $VARIABLES HOST=$(hostname) CURRFILE='/tmp/currcron.file' DIFFILE='/tmp/crondif.file' crontab -l > $CURRFILE diff $ORIGFILE $CURRFILE > $DIFFILE #Function to push email to users on alert function pushmail { SUBJ=$1 MAILBODY=$2 RECIPIENTS=$3 mail -s "$SUBJ" $RECIPIENTS < $MAILBODY } if [ -s $DIFFILE ] then echo "" > $EMAILBODY echo "Attention: Found changes in the current crontab entry" >>$EMAILBODY echo "" >> $EMAILBODY echo "---- Below are the changes from DIFF command ----" >> $EMAILBODY echo "" >> $EMAILBODY cat $DIFFILE >> $EMAILBODY pushmail "ALERT: Found changes in crontab entry on server $HOST" $EMAILBODY $EMAILRECIPIENTS fi |
Config file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[oracle@nposqa1 ~]$ cat /home/oracle/DBA/cfg/crontab_compare.cfg # Control file locations separately. # create following variables in the file # ORIGFILE = Original file to compare the current crontab entries # EMAILRECIPIENTS='example@example.com' # EMAILBODY=Location of email body file. export DIR='/home/oracle/DBA' export DBA_BIN=${DIR}/bin export DBA_LOG=${DIR}/log export DBA_CFG=${DIR}/cfg export DBA_SQL=${DIR}/sql export ORIGFILE="${DBA_CFG}/crontab_orig.cron" export EMAILRECIPIENTS="admin@ktexperts.com" export EMAILBODY="${DBA_CFG}/crontab_emailbody.file" |
Create original crontab file :
1 |
cd /home/oracle/DBA/cfg\ |
1 |
crontab -l >crontab_orig.cron |
Add below entry in crontab
1 |
* * * * * /home/oracle/DBA/bin/crontab_compare.bash /home/oracle/DBA/cfg/crontab_compare.cfg 1>/tmp/crontab_compare.txt 2>/tmp/crontab_compare.err |
Email Output :
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform