Link to home
Start Free TrialLog in
Avatar of LinuxDuke
LinuxDukeFlag for South Africa

asked on

/var/log file to mail root when http and mrepo logs get updated

Hi there,

I need /var/log/marepo.log and /var/log/http.log to send the loacal root user an email when they are updated. How can I go about doing this on my linux machines.

Thanks
Avatar of Sudhirchauhan3
Sudhirchauhan3
Flag of India image

you just want the root to be notified when these files are updated or want to mail entore file content too?
store the md5sum of those files in /root/marepo.log.md5 and /root/httpd.log.md5
then write a cronscript that will check the md5sum of those files.. say every 5 minutes.. if the md5sum differs then it will update /root/*.md5 and send notification email to the root user.
Avatar of omarfarid
you could use a script that check if those files were updated and then mail you

e.g.

touch /tmp/myfile
sleep 5
find /var/log -newer /tmp/myfile -name /var/log/marepo.log > /tmp/results 2> /dev/null
c=`wc -l < /tmp/results`
if [ $c -gt 0 ]
then
      mailx -s "changed " user@domain < /var/log/marepo.log
      touch /tmp/myfile
fi

run this script as crontab job say every 5 min or as you need
Avatar of LinuxDuke

ASKER

I need the content of the files to be mailed to root@localhost. I will try omarfarid solution and see if it will work. I'm not clued up with scripting so I will do it on bash.Any help will be highly appreciated.
if you take the script given (please put full path names for commands , e.g. /usr/bin/touch , etc ) and put it in a file which you make executable, then you can run it to do what you want. If help is needed, I can customize it for you.
Hi omarfarid,

I have created a file under my home directory called mrepoerror and made this file executable.
-rwxrwxrwx   1 gugu users  296 Aug  7 21:39 mrepoerror

I then add the code to it and modified it, the file has been created under /tmp but its empty and nothing has been sent to my mail.
The file content is as follow:
#!/bin/bash
/bin/touch /tmp/myfile
sleep 5
find /var/log -newer /tmp/myfile -name /var/log/httpd/mrepo-wwwerror_log > /tmp/results 2> /dev/null
c=`wc -l < /tmp/results`
if [ $c -gt 0 ]
then
      mailx -s "mrepoerror" root@localhost < /var/log/httpd/mrepo-wwwerror_log
      touch /tmp/myfile
fi

If I can explian what I want to do is to mail my self the content of the following files which are under /var/log/httpd/mrepo-wwwaccess_log and /var/log/httpd/mrepo-wwwerror_log when ever the mrepo server fetches updates.
Hi Guys,

May I get help in creating a bash script that will check this file /var/log/httpd/mrepo-wwwaccess_log  below everytime at 12:00AM and at 12:00PM and send an e-mail to root@localhost with the details in side the file like below.
cat /var/log/httpd/mrepo-wwwaccess_log | mail -s oak root

Can I also get help in creating a script that will check this file when ever it gets updated and send an email to root with the file content like on the example below.

cat /var/log/httpd/mrepo-wwwerror_log | mail -s oak root
I'm not familiar with scripting ur help is highly appreciated.




Hi LinuxDuke,

what did not work in the script?
Hi Omarfarid,

Yes as I have stated above, I did create script with the instructions but it only creates a blank file called myfile under temp and sends no email after executing it. The is also no errors under logs so I'm not to sure what it's doing. I have created my own script but is not doing what it's suppose to do but I do get mails from this one.

This is the one I created and added it under a cron job to execute hourly
 #!/bin/bash
#mail -s root oak < /var/log/httpd/mrepo-wwwerror_log
cat /var/log/httpd/mrepo-wwwerror_log.1 | mail -s "Mrepo Error log" root
cat /var/log/httpd/mrepo-wwwaccess_log | mail -s "Mrepo Access log" root

This works fine but I needed a script that will only send mail when a machine activates the access log or when an error happens on the mrepo server.
I still need to make this work fine
Thanks
the script I provided will create an empty file in /tmp just to get the time stamp of the system and then use it as a reference to see if the log file get updated after that time. So, if the script run say every 5 min or 1 hour then it will compare the time stamp of both files and mail you the log file in case it was newer than the empty file and then update the time stamp of the empty file.
If you want me to help in including my logic into your script , then I can.
Please help me by including your logic on the script Omarfarid.
Thanks
#!/bin/bash
#
c=`/usr/bin/find /var/log -newer /tmp/myfile 2> /dev/null | /usr/bin/grep -w /var/log/httpd/mrepo-wwwerror_log.1 | /usr/bin/wc -l`
if [ $c -gt 0 ]
then
      /usr/bin/cat /var/log/httpd/mrepo-wwwerror_log.1 | /usr/bin/mailx -s "Mrepo Error log" root
#      /usr/bin/cat /var/log/httpd/mrepo-wwwaccess_log | /usr/bin/mailx -s "Mrepo Access log" root
      /usr/bin/mailx -s "changed " user@domain < /var/log/marepo.log
      /usr/bin/touch /tmp/myfile
fi

You neet to test this script as follows

touch /tmp/myfile

touch /var/log/httpd/mrepo-wwwerror_log.1

run the script  and see if you get email

then run the script again and if the file is not updated then you should not get email

you may apply the same for the 2nd  log file
Hey Omar,

Thanks it looks like it's working because root is getting e-mails, I'm not sure why you included the following line on the script /usr/bin/mailx -s "changed " user@domain < /var/log/marepo.log. Because that is where I get an error after executing the sript on line 8.
./mrepoerrors: line 8: /var/log/marepo.log: No such file or directory
Maybe if you can explain that I will be able to fix the problem and with this logic I will be able to do for my other log.

Well, I just added my part to your script :) if you feel that it is no needed, then you can remove it.
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial