grok 正则解析日志例子<1>

前端之家收集整理的这篇文章主要介绍了grok 正则解析日志例子<1>前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <pre name="code" class="html">下面是日志的样子
  2. 55.3.244.1 GET /index.html 15824 0.043
  3.  
  4. 正则的例子
  5. %{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}
  6.  
  7. 配置文件里是怎么写得?
  8.  
  9. input {
  10. file {
  11. path => “/var/log/http.log”
  12. }
  13. }
  14. filter {
  15. grok {
  16. match => [ "message","%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" ]
  17. }
  18. }
  19.  
  20. 解析后,是个什么样子?
  21.  
  22. client: 55.3.244.1
  23. method: GET
  24. request: /index.html
  25. bytes: 15824
  26. duration: 0.043
  27.  
  28. /*********1
  29.  
  30. zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat log01.conf
  31. input {
  32. file {
  33. path => "/var/log/http.log"
  34. }
  35. }
  36.  
  37.  
  38. output {
  39. stdout {
  40. codec=>rubydebug{}
  41. }
  42. }
  43. 此时的输出
  44. Pipeline main started
  45. {
  46. "message" => "55.3.244.1 GET /index.html 15824 0.043","@version" => "1","@timestamp" => "2016-08-27T15:03:23.554Z","path" => "/var/log/http.log","host" => "0.0.0.0"
  47. }
  48.  
  49.  
  50. /***换成json呢?
  51.  
  52. zjtest7-frontend:/usr/local/logstash-2.3.4/config# ../bin/logstash -f log01.conf
  53. Settings: Default pipeline workers: 1
  54. Pipeline main started
  55. {"message":"55.3.244.1 GET /index.html 15824 0.043","@version":"1","@timestamp":"2016-08-27T15:05:07.945Z","path":"/var/log/http.log","host":"0.0.0.0"}
  56.  
  57.  
  58. /***分别发送到elasticsearch看下:
  59.  
  60.  
  61. zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat log01.conf
  62. input {
  63. file {
  64. path => "/var/log/http.log"
  65. }
  66. }
  67.  
  68.  
  69. output {
  70. elasticsearch {
  71. hosts => "192.168.32.80:9200"
  72. index => "logstash-zjzc-test"
  73. }
  74. stdout {
  75. codec => rubydebug
  76. }
  77. }
  78.  
  79. 输出
  80. Settings: Default pipeline workers: 1
  81. Pipeline main started
  82. {
  83. "message" => "55.3.244.1 GET /index.html 15824 0.043","@timestamp" => "2016-08-27T15:08:00.336Z","host" => "0.0.0.0"
  84. }
  85.  
  86. elasticsearch:
  87. {
  88.  
  89. "_index": "logstash-zjzc-test","_type": "logs","_id": "AVbMiuMLEY-onx06xWo-","_version": 1,"_score": 1,"_source": {
  90. "message": "55.3.244.1 GET /index.html 15824 0.043","@version": "1","@timestamp": "2016-08-27T15:08:00.336Z","path": "/var/log/http.log","host": "0.0.0.0"
  91. }
  92.  
  93. }
  94.  
  95.  
  96. /*******使用grok 正则解析日志
  97. zjtest7-frontend:/usr/local/logstash-2.3.4/config# cat log01.conf
  98. input {
  99. file {
  100. path => "/var/log/http.log"
  101. }
  102. }
  103. filter {
  104. grok {
  105. match => [ "message","%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" ]
  106. }
  107. }
  108.  
  109.  
  110. output {
  111. elasticsearch {
  112. hosts => "192.168.32.80:9200"
  113. index => "logstash-zjzc-test"
  114. }
  115. stdout {
  116. codec => rubydebug
  117. }
  118. }
  119.  
  120.  
  121. 输出:
  122. zjtest7-frontend:/usr/local/logstash-2.3.4/config# ../bin/logstash -f log01.conf
  123. Settings: Default pipeline workers: 1
  124. Pipeline main started
  125. {
  126. "message" => "55.3.244.1 GET /index.html 15824 0.043","@timestamp" => "2016-08-27T15:09:59.173Z","host" => "0.0.0.0","client" => "55.3.244.1","method" => "GET","request" => "/index.html","bytes" => "15824","duration" => "0.043"
  127. }
  128.  
  129. elasticsearch:
  130. {
  131.  
  132. "_index": "logstash-zjzc-test","_id": "AVbMjLJeEY-onx06xWpC","@timestamp": "2016-08-27T15:09:59.173Z","host": "0.0.0.0","client": "55.3.244.1","method": "GET","request": "/index.html","bytes": "15824","duration": "0.043"
  133. }
  134.  
  135. }

猜你在找的正则表达式相关文章