1、收集访问日志

1)、首先是要在nginx里面配置日志格式化输出

    log_format  main  "$http_x_forwarded_for | $time_local | $request | $status | $body_bytes_sent | $request_body | $content_length | $http_referer | $http_user_agent |"
"$http_cookie | $remote_addr | $hostname | $upstream_addr | $upstream_response_time | $request_time" ; access_log /var/log/nginx/access.log main;

2)、接下来开始在logstash创建处理nginx的配置文件

input {
file {
path => ["/var/log/nginx/access.log"]
}
} filter {
ruby {
init => "@kname =['http_x_forwarded_for','time_local','request','status','body_bytes_sent','request_body','content_length','http_referer','http_user_agent','http_cookie','remote_addr','hostname','upstream_addr','upstream_response_time','request_time']"
code => "new_event = LogStash::Event.new(Hash[@kname.zip(event.get('message').split('|'))])
new_event.remove('@timestamp')
event.append(new_event)
"
} if [request] {
ruby {
init => "@kname = ['method','uri','verb']"
code => "
new_event = LogStash::Event.new(Hash[@kname.zip(event.get('request').split(' '))])
new_event.remove('@timestamp')
event.append(new_event)
"
}
}
if [uri] {
ruby{
init => "@kname = ['url_path','url_args']"
code => "
new_event = LogStash::Event.new(Hash[@kname.zip(event.get('uri').split('?'))])
new_event.remove('@timestamp')
event.append(new_event)
"
}
}
kv {
prefix =>"url_"
source =>"url_args"
field_split =>"&"
include_keys => ["uid","cip"]
remove_field => ["url_args","uri","request"]
}
mutate {
convert => [
"body_bytes_sent","integer",
"content_length","integer",
"upstream_response_time","float",
"request_time","float"
]
}
date {
match => [ "time_local","dd/MMM/yyyy:hh:mm:ss Z" ]
locale => "en"
}
}
output{stdout{}}

此处的例子借鉴ELKstack权威指南里面的例子,不过书中的例子有错,我这里修改好了,可以参考书籍39页和66页

github:https://github.com/weixinqing/Logstash-example/blob/master/initnginx.conf

3)、最后允许一下看一下效果所示:

{
"url_path" => "/",
"body_bytes_sent" => ,
"@version" => "",
"message" => "- | 05/Mar/2019:16:21:40 +0800 | GET / HTTP/1.1 | 304 | 0 | - | - | - | Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0 |- | 172.16.0.10 | elk-chaofeng07 | - | - | 0.000",
"host" => "ELK-chaofeng07",
"http_cookie" => "- ",
"upstream_addr" => " - ",
"upstream_response_time" => 0.0,
"@timestamp" => --05T08::.352Z,
"uri" => "/",
"request" => " GET / HTTP/1.1 ",
"path" => "/var/log/nginx/access.log",
"url_args" => nil,
"hostname" => " elk-chaofeng07 ",
"verb" => "HTTP/1.1",
"http_user_agent" => " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0 ",
"time_local" => " 05/Mar/2019:16:21:40 +0800 ",
"request_body" => " - ",
"remote_addr" => " 172.16.0.10 ",
"status" => " 304 ",
"request_time" => 0.0,
"method" => "GET",
"http_referer" => " - ",
"tags" => [
[] "_dateparsefailure"
],
"content_length" => ,
"http_x_forwarded_for" => "- "
}

唯一不足的就是中间报了个错误,可以自行解决一下。

2、收集错误日志

定义logstash处理的配置文件

input{
file {
path => ["/var/log/nginx/error.log"]
}
}
filter{
grok {
match => {"message" => "(?<datetime>\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d) \[(?<errortype>\w+)\] \S+: \*\d+ (?<errormsg>[^,]+), \w+: %{IP:remotehost}, \w+: \w+, \w+: (?<request>[^,]+), \w+: \"%{IP:localhost}\""}
}
mutate {
remove_field => ["message"]
}
if [request] {
ruby {
init => "@kname = ['method','uri','verb']"
code => "
new_event = LogStash::Event.new(Hash[@kname.zip(event.get('request').split(' '))])
new_event.remove('@timestamp')
event.append(new_event)
"
}
} }
output{stdout{}}

查看一下效果:

{
"@version" => "",
"path" => "/var/log/nginx/error.log",
"remotehost" => "172.16.0.10",
"request" => "\"GET /8 HTTP/1.1\"",
"verb" => "HTTP/1.1\"",
"uri" => "/8",
"host" => "ELK-chaofeng07",
"localhost" => "172.16.0.57",
"method" => "\"GET",
"@timestamp" => --05T10::.377Z,
"datetime" => "2019/03/05 18:43:53",
"errormsg" => "open() \"/usr/share/nginx/html/8\" failed (2: No such file or directory)",
"errortype" => "error"
}

最新文章

  1. App解读
  2. Kooboo CMS - Html.FrontHtml.Position 详解
  3. Java语言程序设计(基础篇)第一章
  4. I/O阻塞非阻塞,同步异步
  5. 获取wifi信息
  6. SVO实时全局光照优化(里程碑MK0):Sparse Voxel Octree based Global Illumination (SVO GI)
  7. 承香墨影 Android--Matrix图片变换处理
  8. 【HAPPY FOREST】用Unreal Engine4绘制实时CG影像
  9. css修改,类似elememt.style样式修改
  10. POJ - 1159 Palindrome(dp-回文变形)
  11. 利用google浏览器开发者工具调试网页(详)
  12. Extjs grid 组件
  13. shell编程之文件包含
  14. Shiro第三篇【授权、自定义reaml授权】
  15. Spring Boot + Freemarker多语言国际化的实现
  16. UOJ #449. 【集训队作业2018】喂鸽子
  17. python模块 - pywinauto(windows自动化安装软件)
  18. cas单点登陆。就这一篇就够了!!!!!
  19. HDU 3333 Turing Tree(树状数组/主席树)
  20. .net DLL程序集中打包另一个DLL

热门文章

  1. Migrate from ASP.NET Core 2.0 to 2.1
  2. postgresql 获取修改列的值
  3. mysql 开发进阶篇系列 31 工具篇(mysql连接工具与MyISAM表压缩工具)
  4. Kotlin 最佳实践
  5. CentOS 7 个性化配置指南
  6. MapReduce-深度剖析
  7. Python中property的使用
  8. transformer 源码
  9. Freemarker教程1(基本使用)
  10. nginx配置指南