Oneliner来计算maillog中的所有消息完全大小 [英] Oneliner to calculate complete size of all messages in maillog
问题描述
玉家伙我在一个死胡同我真的在这里,不知道什么尝试...
Ok guys I'm really at a dead end here, don't know what else to try...
我写了一些电子邮件的统计数据的脚本,它需要做的事情之一是计算出的maillog的所有消息的完整大小,这是我写的,到目前为止:
I am writing a script for some e-mail statistics, one of the things it needs to do is calculate the complete size of all messages in the maillog, this is what I wrote so far:
egrep ' HOSTNAME sendmail\[.*.from=.*., size=' maillog | awk '{print $8}' |
tr "," "+" | tr -cd '[:digit:][=+=]' | sed 's/^/(/;s/+$/)\/1048576/' |
bc -ql | awk -F "." '{print $1}'
这是从我的maillog一个样本行:
And here is a sample line from my maillog:
Nov 15 09:08:48 HOSTNAME sendmail[3226]: oAF88gWb003226:
from=<name.lastname@domain.com>, size=40992, class=0, nrcpts=24,
msgid=<E08A679A54DA4913B25ADC48CC31DD7F@domain.com>, proto=ESMTP,
daemon=MTA1, relay=[1.1.1.1]
所以我会尽力解释它一步一步:
So I'll try to explain it step by step:
首先,我通过文件grep来查找包含实际的大小的所有的行,下一个I打印第八场,在这种情况下,大小= 40992,
First I grep through the file to find all the lines containing the actual "size", next i print the 8th field, in this case "size=40992,".
接下来我全部更换一个加号逗号字符。
Next I replace all the comma characters with a plus sign.
然后我删除除位数一切的加号。
Then I delete everything except the digits and the plus sign.
然后我更换行的开头用(和我替换最后额外加上一个标志)后面加上/ 1048576。所以我得到了巨大的前pression看起来像这样:
Then I replace the beginning of the line with a "(", and I replace the last extra plus sign with a ")" followed by "/1048576". So i get a huge expression looking like this:
(1 + 2 + 3 + 4 + 5 ... + n)个/ 1048576
"(1+2+3+4+5...+n)/1048576"
由于我要添加的所有个人信息的大小和分裂,所以我得到的结果以MB为单位。
Because I want to add up all the individual message sizes and divide it so I get the result in MB.
最后一个awk命令是,当我得到一个十进制数,我真的不关心precision所以我只是小数点前打印的部分。
The last awk command is when I get a decimal number I really don't care for precision so i just print the part before the decimal point.
问题是,这并不工作...我可以发誓,它是在一个点的工作,难道是我的前pression太长BC处理?
The problem is, this doesn't work... And I could swear it was working at one point, could it be my expression is too long for bc to handle?
感谢,如果你花时间来通读:)
Thanks if you took the time to read through :)
推荐答案
我觉得一个行 AWK
脚本也可以工作。它匹配您的egrep模式匹配,那么这些行它通过等号(=)分割第八记录,并增加了第二部分(数)的总和变量的任何行。当它看到文件的末尾它打印出的SUM / 1048576值(或Mibibytes的字节数)。
I think a one-line awk
script will work too. It matches any line that your egrep pattern matches, then for those lines it splits the eighth record by the = sign and adds the second part (the number) to the SUM variable. When it sees the END of the file it prints out the value of SUM/1048576 (or the byte count in Mibibytes).
awk '/ HOSTNAME sendmail\[.*.from=.*., size=/{ split($8,a,"=") ; SUM += a[2] } END { print SUM/1048576 }' maillog
这篇关于Oneliner来计算maillog中的所有消息完全大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!