使用ELK Stack从日志创建json对象

时间:2017-03-17 18:46:04

标签: python json elasticsearch logging logstash

我有一些由质量中心(HP Alm)和...之间的桥梁生成的日志。詹金斯:这样的事情:

    Building in workspace D:\Tools\Jenkins\workspace\*******-JenkinsWithQC
[Dani-JenkinsWithQC] $ D:\Tools\Jenkins\workspace\*******-JenkinsWithQC\HpToolsLauncher.exe -paramfile props20022014150821066.txt
"Started..."
Timeout is set to: 5
Run mode is set to: RUN_REMOTE
============================================================================
Starting test set execution
Test set name: JenkinsIntegartionTest, Test set id: 2457
"Number of tests in set: "2
Test 1: [1]Login will run on host: si0vm839
Test 2: [1]Logout will run on host: si0vm839
"Scheduler started at:20.02.2014 15:08:28
-------------------------------------------------------------------------------------------------------
20.02.2014 15:08:29 Running: [1]Login
20.02.2014 15:08:29 Running test: [1]Login, Test id: 938, Test instance id: 1412
Test: [1]Login, Id: 1412, Execution status: Running
Test: [1]Login, Id: 1412, Execution status: Error, Message: Access is denied
20.02.2014 15:08:33 Test complete: [1]Login
-------------------------------------------------------------------------------------------------------
20.02.2014 15:08:33 Running: [1]Logout
20.02.2014 15:08:33 Running test: [1]Logout, Test id: 939, Test instance id: 1413
Test: [1]Logout, Id: 1413, Execution status: Running
Test: [1]Logout, Id: 1413, Execution status: Error, Message: Access is denied
==============\nJob timed out!\n==============
================================================
Run status: Job failed, total tests: 2, succeeded: 0, failures: 0, errors: 2
Build step 'Execute HP tests from HP ALM' changed build result to FAILURE
Finished: FAILURE

我想从这些日志中创建一个类似于以下内容的JSON:

{
    'Build': {
         'TestSet': [
             {
                  'name' .   : 'execution',
                  'id'       : 2457,
                  'startedAt': '20.02.2014 15:08:28',
                  'nbOfTests': 2,
                  'tests' .  : [
                      {
                           'name': "[1]Login",
                           'host': "si0vm839"
                      }
                  ]

             } 
         ]
    }
}

我可以使用python脚本逐行处理正则表达式并创建JSON,但我认为它没有优化,因为我是Python新手......

我想知道我是否可以使用Logstash将它们存储在Elasticsearch中并构建看板仪表板。

提前感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

使用Logstash可以做到这一点,包括摄取的多行编解码器和一些真正的 gnarly grok语句。那不是我期待的项目。事实上,我可以通过以下两种方式解决这个问题:

使用ruby过滤器

这允许内联红宝石将大量的绳子按到你想要的事件领域。实际上,您可以通过这种方式编写自己的codec

使用exec输入

这将卸载将其转换为外部脚本的可解析格式的过程,然后外部脚本将其发送到logstash以注入ElasticSearch。由于我不认为您经常运行其中的多个,这可能对您有用。