1

On the server for everyday there will be a file with name "xxxx_xxx_2016-11-08_0.log" on the lcoation /usr/logs the date changes every day in the file name but format is same, there is one file per day, the file content is written once in every 4 hours. It writes file content at 12 AM, 4AM, 8AM, 12 PM, 4 PM, 8 PM. A monitoring job need to go into the content around 3:30 AM, 730 AM, 1130AM , 330 PM, 730 PM go and check the file and see if it has any error as "maxretry, not synchronized" if this line is found please send an alert and create an email .Also if you see an error like "FCS Bad receipt" do the same as above.I am new to the scripting and Unix.Please help me with above requirement.

  • user crontab to schedule your script at the times mentioned. use grep to search log file for error, trigger mail if grep gets a match. use mail for the purpose – debal Dec 5 '16 at 6:15
  • Yes we can do that but the log file name changes everday with date like "xxxx_xxx_2016-11-08_0.log" so I dont know how to do that part. – Gomsthyshankar Selvakumar Dec 5 '16 at 6:20
  • date +"%Y-%m-%d" – debal Dec 5 '16 at 6:25
  • made some changes to the code.. realized some syntax errors were present – debal Dec 5 '16 at 8:12
0

This is just a sample script that you could use to modify as per your needs.

FILE="xxxx_xxx_`date +"%Y-%m-%d"`_0.log"

grep -E "maxretry|not synchronized|FCS Bad receipt" $FILE > fcs_error.log

if [[ $(wc -l fcs_error.log | awk '{print $1}') -gt 0 ]]; then
    mail -s "error found" mail_id <fcs_error.log
fi

Chek cron on how to schedule jobs

Use -n option of grep for printing line number. See grep for more details.

  • SImpler if [ -s fcs_error.log ]; then mail ...or since grep's status indicates if it found a match just if grep -E "strings" $FILE >fcs_error.log; then mail ... – dave_thompson_085 Dec 5 '16 at 9:34
  • The above mentioned code worked for me.Now I want also display the line number in which the pattern(maxretry not synchronized,FCS Bad receipt) is present. – Gomsthyshankar Selvakumar Dec 5 '16 at 9:51
  • updated answer, please check. linux has a very handy tool, man to learn about new commands. Please read the documentation and you will find solution to most of your queries. – debal Dec 5 '16 at 11:40
  • I had another issue with above script.As I mentioned the file content is written once in every 4 hours. It writes file content at 12 AM, 4AM, 8AM, 12 PM, 4 PM, 8 PM and i have scheduled a cron job to check into the content around 3:30 AM, 730 AM, 1130AM , 330 PM, 730 PM go and check the file and see if it has any error as "maxretry, not synchronized" if this line is found please send an alert and send an email. – Gomsthyshankar Selvakumar Dec 13 '16 at 9:10
  • The issue is that for an max try error occured at 12 am and when my scripts checks the file at 3.30 am I get an email about the error.Now my log is again written at 4 am with no error and my script again checks at 7.30 am and it will again send email about the error which it should not as my script checks the entire file.Ideally it should only check the last update not the entire file.Is it possible to do it. – Gomsthyshankar Selvakumar Dec 13 '16 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.