Scripting and mailing
Hi.
I have been "playing" with Linux for a while now, and I have a DNS and Postfix mailserver running on my LAN. The server has no contact with the outside world, which it shouldn't. It's just there for testing purposes. (I use Slackware 9.0) My question is as follows: All mail sent through the system is logged to /var/log/maillog I did: grep to=< /var/log/maillog and this showed me basically what I wanted. Is there a way to get the server to do the following: Every night at 03:00 AM it runs a script or whatever it needs to. This scripts retrieves all mail sent the previous day, from /var/log/maillog It should only retreive the items from the day before. Otherwise it will be a lot of info in the end. I have almost no experience in scripting or schedulig tasks on the server. If anyone can help point me in the right direction, I would really appreciate it. Best regards Geir Andersen |
To start with, you'll need to understand the script scheduler 'cron'.
man cron You'll also need to learn bash scripting. For bash use: http://freeos.com/guides/lsst/ http://www.icon.co.za/~psheer/book/index.html.gz which will also explain cron http://dsl.org/cookbook/cookbook_toc.html another general guide These should get you started. Come back if you have more specific qns. |
Thanks.
I found a tutorial yesterday, that explained cron, and that was ok. After some searching, trying and failing, I got a script partially working. #!/bin/bash cat /var/log/messages | grep "to=<" > /tmp/send.file sendmail -t -oi root@localhost < /tmp/send.file This is very crude, but sends me info about all mail passing through the system. Next I'll have to get bash to get yesterdays date, and filter out only those results as well. Hopefully I' ll get that working today. Thanks for the links. Geir O. |
You probably also have logrotate installed. I think you should check it out. You can configure it to daily rename your /var/log/messages. That way you won't have to filter out yesterdays results any more.
Groetjes, Kees-Jan |
Yes, I have logrotate installed.
Hadn't even thought about that:o . A new log for every day is so much simpler, allthough it will give a lot of files in the end. But, by the time the files are that many, I hope I've learned enough bash-scripting to write a script that deletes or compresses the files older than say, a couple of months. Thanks. -Geir O. |
Don't despair. Logrotate can even do that for you.
On my system, /var/log/messages is rotated weekly, older files are compressed, and it keeps at most 4 files. All you need to do is modify the configuration and, of course, plug in your script that fetches the info from the logfile, just before or after it is rotated. Groetjes, Kees-Jan |
I'll have to look more at the logrotate function then. Looks like it can do everything for me.
This little script also partially works: #!/bin/bash DAY=`date +%d` MONTH=`date +%m` YEAR=`date +%y` YESTERDAY=$(($DAY - 1)) echo "Yesterday's date: " $YESTERDAY"."$MONTH"."$YEAR This will however not work at the first of the month. Instead of returning 30 or 31, it would return 0. I'll sit down some time and see if I can write a script to handle first of the month, and return correctly 30 or 31, whatever the case may be. Thanks again. -Geir O. |
%j gives day of year, so start with that and substract 1 ?
|
Well, that would give: 274 for yesterday, but I want the date: 30.09.(20)04. Not sure how it's written in /var/log/maillog
|
How about getting logrotate to rotate mail logs every day, then yesterday's is always maillog.1 (or similar). Tell it to keep the last N logs eg 30 for 1 mth.
|
All times are GMT -5. The time now is 10:29 PM. |