我找得到通過後綴創建的maillog的日誌文件的過去一小時的有效途徑的最後一個小時。我想在python或bash中做到這一點。的Python - 後綴日誌
到目前爲止,我已經提取了一個月,並且保存在後綴數天:
now_m = today.ctime().split()[1]
now_d = int(today.ctime().split()[2])
,但我堅持在這裏了,需要一些新鮮的想法。
任何幫助將不勝感激。
代碼例如:
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]
和另一個例子:
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]
如果您不需要確切的最後一小時,你可以只用一天+小時'grep' - 像'grep的「10年9月4日:」 mail.log'。如果你需要確切的小時,你可以編寫一個python腳本來生成一個涵蓋過去一小時的正則表達式(它可能包含不同日期或月份的值) –
如果你包含一個日誌樣本,你可能會得到更多的幫助文件。另外,除非您將此作爲練習,否則您是否在尋找現有工具?像[Postfix Contribs](http://jimsun.linxnet.com/postfix_contrib.html)(你必須喜歡一個有Lynx用戶註釋的頁面)。 –
了'.ctime()'在你的代碼示例來電建議,你應該用最簡單的*工作*解決方案啓動('的ctime()'是不是從Python中的日期以獲得一個月的樣子,日) - 如果你不知道如何; [問](http://stackoverflow.com/questions/ask)。一旦你有一些工作,衡量它有多快,並設定一個目標有多快,你會喜歡它 - [更新你的問題與此信息](http://stackoverflow.com/posts/32401222/edit)。 – jfs