Created
August 25, 2012 03:39
-
-
Save shiumachi/3460244 to your computer and use it in GitHub Desktop.
simple NN log parser (pyfes LT 2012.08 sample code)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pyfes LT 2012.08 用ソースコード | |
# namenode のログをパースして ログレベルをカウントするだけのコード | |
# ソースコードに日本語でコメント入れるのは多分初めて | |
# | |
import sys | |
# 1行毎のパース処理はここ | |
def parse(line): | |
arr = line.strip().split() | |
try: | |
log_level = arr[2] | |
except: | |
log_level = '_NULL' | |
return log_level | |
# ファイルをパースしてログレベルを集計して表示する | |
def main(file): | |
log_levels = ['INFO', 'WARN', 'DEBUG', 'ERROR', 'FATAL'] | |
levels = {} | |
for line in file: | |
log_level = parse(line) | |
if log_level not in log_levels: continue | |
levels.setdefault(log_level, 0) | |
levels[log_level] += 1 | |
res = [] | |
for k,v in levels.items(): | |
res.append("{0}: {1}".format(k,v)) | |
return ', '.join(res) | |
if __name__=='__main__': | |
print(main(sys.stdin)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pyfes LT 2012.08 用テストコード | |
# namenode のログをパースして ログレベルをカウントするだけのコード | |
# | |
from nose.tools import * | |
from tempfile import * | |
from nn_parse import * | |
def test_parse(): | |
input = "2012-06-04 13:31:07,065 INFO org.apache.hadoop.net.NetworkTopology: Adding a new node: /default-rack/127.0.0.1:50010" | |
input2 = "2012-06-04 13:31:05,466 WARN org.apache.hadoop.util.PluginDispatcher: Unable to load dfs.namenode.plugins plugins" | |
input3 = "/************************************************************" | |
expected = 'INFO' | |
expected2 = 'WARN' | |
expected3 = '_NULL' | |
eq_(expected, parse(input)) | |
eq_(expected2, parse(input2)) | |
eq_(expected3, parse(input3)) | |
# 実際のコマンド利用時は標準入力を使うが、ここでは TemporaryFile をテスト入力として使用 | |
def test_main(): | |
fp = TemporaryFile(mode='w+') | |
fp.write("2012-06-04 13:31:07,065 INFO org.apache.hadoop.net.NetworkTopology: Adding a new node: /default-rack/127.0.0.1:50010\n") | |
fp.write("2012-06-04 13:31:05,466 WARN org.apache.hadoop.util.PluginDispatcher: Unable to load dfs.namenode.plugins plugins\n") | |
fp.write("/************************************************************\n") | |
fp.write("STARTUP_MSG: args = []") | |
fp.seek(0) | |
res = main(fp) | |
fp.close() | |
expected = "INFO: 1, WARN: 1" | |
eq_(expected, res) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment