Проект

Общее

Профиль

Logstash » История » Версия 5

Константин Пильник, 2022-11-29 18:33

1 1 Константин Пильник
h1. Logstash
2
3
h2. разбираем лог syslog
4
5 5 Константин Пильник
h4. rsyslog
6
7
<pre><code class="bash">
8
~# cat /etc/rsyslog.d/remote.conf 
9
# udp
10
*.* @127.0.0.1:500
11
</code></pre>
12
13 1 Константин Пильник
{{collapse(logstash.conf)
14 4 Константин Пильник
<pre><code class="php">
15 1 Константин Пильник
input {
16
  file {
17
    type => "message"
18
    path => [ "/var/log/messages" ]
19
    start_position => "end"
20
    stat_interval => 1
21
    discover_interval => 3
22
  }
23 5 Константин Пильник
# production
24
#  udp {
25
#    port => 500
26
#    type => syslog
27
#  }
28 1 Константин Пильник
}
29
30
filter {
31
  if [type] == "message" {
32
    grok {
33
      match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?:\s+%{GREEDYDATA:syslog_message}" }
34
    }
35
    date {
36
      match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
37
    }
38
  }
39
}
40
41
output {
42
  stdout {}
43 5 Константин Пильник
# production
44
# elasticsearch {
45
#   hosts    => ["elasticsearch:9200"]
46
#   index    => "%{[type]}-%{+YYYY.MM.dd}"
47
#   user     => "${ELASTIC_USERNAME}"
48
#   password => "${ELASTIC_PASSWORD}"
49
# }
50 1 Константин Пильник
}
51 3 Константин Пильник
</code></pre>
52 1 Константин Пильник
}}
53
54
h2. разбираем лог nginx из файловой системы
55
56
{{collapse(nginx.conf)
57
<pre>
58
log_format json_combined escape=json
59
  '{'
60
    '"time_local":"$time_local",'
61
    '"remote_addr":"$remote_addr",'
62
    '"remote_user":"$remote_user",'
63
    '"request":"$request",'
64
    '"status": "$status",'
65
    '"body_bytes_sent":"$body_bytes_sent",'
66
    '"request_time":"$request_time",'
67
    '"http_referrer":"$http_referer",'
68
    '"http_user_agent":"$http_user_agent"'
69
  '}';
70
access_log /var/log/nginx/access.log json_combined;
71
</pre>
72
}}
73
{{collapse(logstash.conf)
74 4 Константин Пильник
<pre><code class="php">
75 1 Константин Пильник
input {
76
  file {
77
    type => "nginx"
78
    path => [ "/var/log/nginx/access.log" ]
79
    start_position => "end"
80
  }
81
}
82
83
filter {
84
  if [type] == "nginx" {
85
    json {
86
      source => "message"
87
    }
88
  }
89
}
90
91
output {
92
  stdout {}
93
}
94
</code></pre>
95
}}
96
97
h2. разбираем лог nginx из syslog
98
99
{{collapse(nginx.conf)
100
<pre>
101
log_format json_combined escape=json
102
  '{'
103
    '"time_local":"$time_local",'
104
    '"remote_addr":"$remote_addr",'
105
    '"remote_user":"$remote_user",'
106
    '"request":"$request",'
107
    '"status": "$status",'
108
    '"body_bytes_sent":"$body_bytes_sent",'
109
    '"request_time":"$request_time",'
110
    '"http_referrer":"$http_referer",'
111
    '"http_user_agent":"$http_user_agent"'
112
  '}';
113
access_log syslog:server=unix:/dev/log,tag=nginx,severity=error,nohostname json_combined;
114
</pre>
115
}}
116
{{collapse(logstash.conf)
117 4 Константин Пильник
<pre><code class="php">
118 1 Константин Пильник
input {
119
  file {
120
    type => "syslog"
121
    path => [ "/var/log/syslog" ]
122
    start_position => "end"
123
  }
124
}
125
126
filter {
127
  if [type] == "syslog" {
128
    grok {
129
      match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?:\s+%{GREEDYDATA:syslog_message}" }
130
    }
131
    json {
132
      source => "[syslog_message]"
133
      target => "[nginx_message]"
134
    }
135
  }
136
}
137
138
output {
139
  stdout {}
140
}
141 3 Константин Пильник
</code></pre>
142 1 Константин Пильник
}}
143
144
145 2 Константин Пильник
h3. ссылки
146
147 1 Константин Пильник
https://www.elastic.co/guide/en/logstash/current/input-plugins.html
148
https://www.elastic.co/guide/en/logstash/current/filter-plugins.html
149
https://www.elastic.co/guide/en/logstash/current/output-plugins.html