简体   繁体   English

Python-Postfix日志的最后一小时

[英]Python - last hour of postfix log

I am looking for an effective way of getting the past hour of the maillog log file created by postfix. 我正在寻找一种获取由postfix创建的过去一个小时的日志日志文件的有效方法。 I want to do that in python or in bash. 我想在python或bash中做到这一点。

So far I have extracted the month and the day that is saved in the postfix log: 到目前为止,我已经提取了保存在postfix日志中的月份和日期:

now_m = today.ctime().split()[1]
now_d = int(today.ctime().split()[2])

but am stuck over here and need some fresh ideas. 但被困在这里,需要一些新鲜的想法。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Code example: 代码示例:

Apr  2 11:53:15 server01 postfix/bounce[9177]: 62A347FB99: sender non-delivery notification: 6F4B67FB97
Apr  2 11:53:15 server01 postfix/qmgr[8140]: 5E9B07FB95: removed
Apr  2 11:53:15 server01 postfix/qmgr[8140]: 62A347FB99: removed
Apr  2 11:53:15 server01 postfix/qmgr[8140]: 6F5837FB98: from=<>, size=4054, nrcpt=1 (queue active)
Apr  2 11:53:15 server01 postfix/bounce[9182]: 652D67FB9D: sender non-delivery notification: 6F5837FB98
Apr  2 11:53:15 server01 postfix/qmgr[8140]: 652D67FB9D: removed
Apr  2 11:53:15 server01 postfix/qmgr[8140]: 6EE717FB92: from=<>, size=4926, nrcpt=1 (queue active)
Apr  2 11:53:15 server01 postfix/qmgr[8140]: 6F4B67FB97: from=<>, size=3448, nrcpt=1 (queue active)
Apr  2 11:53:15 server01 postfix/smtpd[9163]: disconnect from unknown[10.0.0.4]

and another example: 再举一个例子:

Aug 30 09:00:56 server01 postfix/qmgr[2321]: 1654A7FB86: removed
Aug 30 09:01:57 server01 postfix/smtpd[4320]: connect from unknown[10.0.0.0]
Aug 30 09:01:57 server01 postfix/smtpd[4320]: disconnect from unknown[10.0.0.0]
Aug 30 09:02:16 server01 postfix/smtpd[4320]: connect from unknown[10.0.0.0]
Aug 30 09:02:16 server01 postfix/smtpd[4320]: 21F077FB86: client=unknown[10.0.0.0]

It's simple: 这很简单:

NOW=`date +%s`; THEN=`expr $NOW - 3600`; until (("$THEN" > "$NOW")); do DATE=`date -d @\$THEN +'%b %e %k:%M:%S'`; grep -e "^$DATE" /var/log/maillog && sed -n -e "/^$DATE/,\$p" /var/log/maillog && break; THEN=`expr $THEN + 1`; done

In other words: 换一种说法:

1) get current time $NOW 1)获取当前时间$ NOW

2) get time one hour ago $THEN 2)1小时前获得时间$ THEN

3) if there is a line beginning with $THEN, print the line and everything after it (and break the cycle) 3)如果有以$ THEN开头的行,则打印该行及其后的所有内容(并中断循环)

4) increment $THEN by one second and repeat until $NOW (or until you find something) 4)将$ THEN增加一秒钟,然后重复直到$ NOW(或者直到找到东西)

It's not very efficient and not very fast but will probably serve you well. 它不是很有效,也不是很快,但是可能会为您提供很好的服务。

The basic track is to read each line, and parse it's timestamp into a datetime.datetime object. 基本轨道是读取每一行,并将其时间戳解析为datetime.datetime对象。 There are several options to do so, eg using its .fromtimestamp (static) method. 有几种方法可以这样做,例如,使用其.fromtimestamp (静态)方法。 Or the more general parsers (see utile) 或更一般的解析器(请参阅utile)

Then use datetime.datetime.now() and subtract datetime.timedelta(mins=60) ; 然后使用datetime.datetime.now()减去datetime.timedelta(mins=60) ; the result can be compared to the parsed timestamp. 可以将结果与已解析的时间戳进行比较。

It as simple as that! 就这么简单! No need to do any manual, work. 无需做任何手册,工作。

PS add an example of the log file/line next time. PS下次添加日志文件/行的示例。 The we can give the concrete example code 我们可以给出具体的示例代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM