2011-04-20 54 views
1

即時將爲exim4 MTA編寫日誌解析器,我有幾個問題。 (我知道有一個exilog程序)exim4 mta的perl日誌解析器

問: 1.什麼是更好地解析線方式? (其這種線條的abbout的5Gb:d) 香港專業教育學院得到這個$行:

2011-12-24 12:32:12 MeSSag3-Id-Ye <hostname> ([email protected]) <[email protected]> => H=[321.123.321.123] T="Hello this is a test"

,並希望得到這一切領域到變量。 即時使用東西像($var,[var2])=($line =~ /somecoolregexp/);它是快/好還是我應該使用別的東西?

+0

這個問題有太多問題。請一次發佈一個**特定**問題。 – Mat 2011-04-21 05:22:18

+0

好的。虐待編輯這個。 – MealstroM 2011-04-21 06:22:33

回答

1

那麼,這取決於你想要對數據做什麼。

假設你有解決這個大while (<>) { ... },您可以通過只使用分割得到的最簡單的解析:

my @fields = split; 

下一級將增添幾分意義

my ($date, $time, $id, $host, $from, $to, undef, $dest) = split; 

(注,如果你想忽略一個結果,你可以指定爲undef

最後,你可以通過使用正則表達式清理大量的文件裂變。您也可以將上面的拆分與較小的正則表達式組合起來,分別清理每個字段。

my ($datetime, $id, $host, $from, $to, $dest) = 
    /([\d-]+ [\d:]+) \s+  # date and time together 
    (\S+)   \s+  # message id, just a block of non-whitespace 
    <(.*?)>   \s+  # hostname in angle brackets, .*? is non-greedy slurp 
    \((.*?)\)  \s+  # from email in parens 
    <(.*?)>   \s+  # to email in angle brackets 
     \S+   \s+  # separated between to-email and dest 
     (\S+)     # last bit, could be improved to (\w)=\[(.*?)\] 
    /x;      # /x lets us break all of this up, so its a bit readable 

當然,你可以繼續服用這種種愚蠢的,但如果你要開始做這些領域的更具體的分析,我會與最初的拆分之後broken-去out字段解析。例如:

my ($date, $time, ...) = split; 

my ($year, $month, $day) = split(/-/, $date); 
my ($hour, $min, $sec) = split(/:/, $time); 
my ($from_user, $from_host) = ($from =~ /< ([^\@]+) \@ (.*) >/x); 
...etc... 
+0

Tnx。我計劃使用CPAN :: Tail進行監控。我的第一個嘗試是使用split(/ /,$ blabla),但它被T =「bla bla lba」或一些錯誤代碼(如「問題是bla bla」)中的某些行破壞。我計劃將日誌導出到MySQL – MealstroM 2011-04-21 07:55:46