Tuesday, December 29, 2020

Script check quemail stuck on server and alert mail to mailadmin for resolve

vim checkquemail.sh

---------------------------------Start---------------------------------------------

#!/bin/bash

#Start check quemail on system

PATHCHECK="/home/user/quemail.txt"

/usr/sbin/postqueue -p |tail -n 1 > $PATHCHECK && /usr/sbin/postqueue -p |grep "^[A-F0-9]" |sort -k5rn -k6n >> $PATHCHECK

#If found quemail stuck on systems and then send mail alert to Systemadmin

#Set the variable which equal to zero

prev_count=0

count=$(grep 'MAILER' $PATHCHECK | wc -l)

#check word 'MAILER' on text file on path $PATHCHECK if more than 0 do next step

if [ "$prev_count" -lt "$count" ] ; then

# Send a mail to given email id when errors found in log

#SUBJECT="WARNING: found $count quemail was stuck on mailgw, pls check now "`date --date='yesterday' '+%b %e'`""

SUBJECT="WARNING: found $count quemail was stuck on mailgw, pls check now."

#This is a temp file, which is created to store the email message.

MESSAGE="/tmp/logs.txt"

FROM="noreply@domain.com"

TO="mailadmin@domain.com"

echo "ATTENTION: found that there are $count mail queues in the system left to be sent for urgent inspection." >> $MESSAGE

echo "Server: `hostname`" >> $MESSAGE

echo "\n" >> $MESSAGE

echo "+------------------------------------------------------------------------------------+" >> $MESSAGE

echo "$count mail queues" >> $MESSAGE

echo "+------------------------------------------------------------------------------------+" >> $MESSAGE

grep -i "`date --date='yesterday' '+%b %e'`" $PATHCHECK | grep 'MAILER' $PATHCHECK >>  $MESSAGE

#list quemail stuck on system

mutt -s "$SUBJECT" -e "my_hdr From: Server Info <noreply@domain.com>" -- "$TO" < $MESSAGE

rm $MESSAGE

fi

---------------------------End Script----------------------------------

Create crontab for run script

---------crontab -e --------

# m h  dom mon dow   command

*/30 * * * * /home/user/./checkquemail.sh

#check every 30 minute

--------


Ref: https://www.2daygeek.com/linux-bash-script-to-monitor-messages-log-warning-error-critical-send-email/


Wednesday, December 2, 2020

Delete old file over 7 day

----------------script--------------------------------------

find /pathfile/* -type f -mtime +7 -exec rm -rf {} \;

--------------------------------------------------------------