It is currently Fri May 06, 2016 1:22 am

incomming mail counting ....

All times are UTC - 6 hours

Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Sep 03, 2009 7:46 am   

Joined: Thu Sep 03, 2009 7:40 am
Posts: 2
Hi all,

I have tried a few different things to do this but non of the ways are flawless, i need a script that counts incomming mail to a unique mailaccount, and out put total mail recived every day. I gonna run the script once every hour.

i want it to output something like this:

1 Sep: 45
2 Sep: 198
3 Sep: 90
...and so on.
The thing is that i can do this with cat/grep and NL, but the mail.log files rotate so i want it to keep track of the changes. In some cases there are two dates entries in the same logfile etc. Is this doable in bash ?

 PostPosted: Fri Sep 04, 2009 4:40 am   
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 264
Location: London - UK
It certainly is doable, as no information is provided about the format of the data or how the data is a problem it's difficult to suggest a solution.
Your example output seems to present data at a daily level but you say you intend the script to run once per hour which seems a little odd. If you only need daily data then run it once per day just after midnight and have the grep commands you run operate on both log files that may contain data from 'yesterday'.

If you can pin down the requirements more I'll try to provide more useful info :)
If you post the actual commands also we'll likely be able to make them more efficient, I say this based on your statement that you used cat/grep, which almost always includes the useless use of cat :)

 PostPosted: Sat Sep 05, 2009 6:03 am   

Joined: Thu Sep 03, 2009 7:40 am
Posts: 2
Ahh sorry, here is what i want to look for.

The file i will monitor is postfix's mail.log and i will grep out "to=<> status=sent", it should take out the start dates as well, since the mail.log file can contain 2 or even more dates in a single file, thats what i find hard about this.
I prefer a script that runs like a daemon or something similar, use tail -f or something. i dont wanna run through the scripts once a day i need it in realtime =D

Ty for help =)

 PostPosted: Mon Sep 07, 2009 2:04 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Reading files in realtime is tricky things in bash.
It's not built to be doing things simultaineous, as in reading a file and doing things with it.

You're better of using perl or some language that is built for this.

You can however mimic this behaviour by doing something like this. (This is not stream reading a file!)
while [ true ]; do
   len=$(wc -l /var/log/mail | awk {'print $1'})
   if [ $len -ge $old_len ]; then
      content=$(egrep "to=\<\(?:[^\>]\+\)\>\s\+status=sent" /var/log/mail)
      # ... process content of $content here, most likely a regexp solution (using sed) or awk can be used.
      # But since I don't have a mail log file available atm I can't do this part :)
   sleep 3600

There's a possible performance issue using this thou, when the file is very large (as in 30mb+ in size) it can take a very long time to process.
This will work even if the file rotates, which is not the case if the file is read constantly... it will only be delayed for up to "sleep" seconds :)

Best regards
Fredrik eriksson

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group