ELK-Redis架构接入
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/090199dcde563452d03e7da92e831854.png#pic_center)
1.安装redis
10.0.0.161 10.0.0.162 10.0.0.163 安装elasticsearch集群
10.0.0.6 安装nginx和filebeat
10.0.0.151 安装mariadb
10.0.0.7 安装redis
[root@redis ~]
[root@redis ~]
bind 127.0.0.1 10.0.0.7
requirepass 123456
.......(其它配置默认)
[root@redis ~]
[root@redis ~]
[root@web01 ~]
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
tags: ["access"]
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: ["error"]
output.redis:
hosts: ["10.0.0.8:6379"]
password: 123456
timeout: 5
db: 0
keys:
- key: "nginx_access"
when.contains:
tags: "access"
- key: "nginx_error"
when.contains:
tags: "error"
[root@web01 ~]
[root@redis ~]
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> keys *
1) "nginx_error"
2) "nginx_access"
127.0.0.1:6379> TYPE nginx_access
list
127.0.0.1:6379> LLEN nginx_access
(integer) 12
127.0.0.1:6379> LRANG nginx_access 1 2
(error) ERR unknown command 'LRANG'
127.0.0.1:6379> LRANGE nginx_access 1 2
1) "{\"@timestamp\":\"2021-07-19T12:49:45.718Z\",\"@metadata\":{\"beat\":\"filebeat\",\"type\":\"_doc\",\"version\":\"7.4.0\"},\"log\":{\"offset\":1432,\"file\":{\"path\":\"/var/log/nginx/access.log\"}},\"message\":\"124.161.176.119 - - [10/Nov/2019:00:01:52 +0800] \\\"POST /api3/appover HTTP/1.1\\\" 200 103 \\\"www.oldxu.com\\\" \\\"-\\\" code=B157963E-5BDA-4090-A021-A3D46D2E6BA2&secrect=f0fbb455c7aebc69c5cc39d68c7859fe&time=9441×tamp=1478707307012&token=12d0f0cfa1efb81e42c321f027bbe752&uid=4384521 \\\"oldxu/5.0.2 (iPhone;iOS 10.1.1; Scale/2.00)\\\" \\\"-\\\" 10.100.136.65:80 200 0.011 0.011\",\"tags\":[\"access\"],\"input\":{\"type\":\"log\"},\"ecs\":{\"version\":\"1.1.0\"},\"host\":{\"name\":\"web01\"},\"agent\":{\"ephemeral_id\":\"50c0e773-eaf7-4115-8c1c-6c40758d4ccf\",\"hostname\":\"web01\",\"id\":\"c2e57138-56c0-4c2f-9cfc-416d8213b66a\",\"version\":\"7.4.0\",\"type\":\"filebeat\"}}"
2) "{\"@timestamp\":\"2021-07-19T12:49:45.718Z\",\"@metadata\":{\"beat\":\"filebeat\",\"type\":\"_doc\",\"version\":\"7.4.0\"},\"ecs\":{\"version\":\"1.1.0\"},\"host\":{\"name\":\"web01\"},\"agent\":{\"type\":\"filebeat\",\"ephemeral_id\":\"50c0e773-eaf7-4115-8c1c-6c40758d4ccf\",\"hostname\":\"web01\",\"id\":\"c2e57138-56c0-4c2f-9cfc-416d8213b66a\",\"version\":\"7.4.0\"},\"log\":{\"file\":{\"path\":\"/var/log/nginx/access.log\"},\"offset\":1790},\"message\":\"124.161.176.119 - - [10/Nov/2019:00:01:52 +0800] \\\"POST /api3/appover HTTP/1.1\\\" 200 103 \\\"www.oldxu.com\\\" \\\"-\\\" code=B157963E-5BDA-4090-A021-A3D46D2E6BA2&secrect=f0fbb455c7aebc69c5cc39d68c7859fe&time=9441×tamp=1478707307012&token=12d0f0cfa1efb81e42c321f027bbe752&uid=4384521 \\\"oldxu/5.0.2 (iPhone;iOS 10.1.1; Scale/2.00)\\\" \\\"-\\\" 10.100.136.65:80 200 0.011 0.011\",\"tags\":[\"access\"],\"input\":{\"type\":\"log\"}}"
127.0.0.1:6379> LRANGE nginx_error 1 2
1) "{\"@timestamp\":\"2021-07-19T12:46:30.659Z\",\"@metadata\":{\"beat\":\"filebeat\",\"type\":\"_doc\",\"version\":\"7.4.0\"},\"message\":\"2021/07/19 20:42:18 [notice] 7354#7354: nginx/1.20.1\",\"log\":{\"offset\":4267,\"file\":{\"path\":\"/var/log/nginx/error.log\"}},\"tags\":[\"error\"],\"input\":{\"type\":\"log\"},\"host\":{\"name\":\"web01\"},\"agent\":{\"ephemeral_id\":\"50c0e773-eaf7-4115-8c1c-6c40758d4ccf\",\"hostname\":\"web01\",\"id\":\"c2e57138-56c0-4c2f-9cfc-416d8213b66a\",\"version\":\"7.4.0\",\"type\":\"filebeat\"},\"ecs\":{\"version\":\"1.1.0\"}}"
2) "{\"@timestamp\":\"2021-07-19T12:46:30.660Z\",\"@metadata\":{\"beat\":\"filebeat\",\"type\":\"_doc\",\"version\":\"7.4.0\"},\"agent\":{\"type\":\"filebeat\",\"ephemeral_id\":\"50c0e773-eaf7-4115-8c1c-6c40758d4ccf\",\"hostname\":\"web01\",\"id\":\"c2e57138-56c0-4c2f-9cfc-416d8213b66a\",\"version\":\"7.4.0\"},\"log\":{\"file\":{\"path\":\"/var/log/nginx/error.log\"},\"offset\":4320},\"message\":\"2021/07/19 20:42:18 [notice] 7354#7354: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) \",\"tags\":[\"error\"],\"input\":{\"type\":\"log\"},\"ecs\":{\"version\":\"1.1.0\"},\"host\":{\"name\":\"web01\"}}"
127.0.0.1:6379>
[root@logstash-node1 ~]
input {
redis {
host => ["10.0.0.7"]
port => "6379"
password => "123456"
data_type => "list"
key => "nginx_access"
db => "0"
}
redis {
host => ["10.0.0.7"]
port => "6379"
password => "123456"
data_type => "list"
key => "nginx_error"
db => "0"
}
}
filter {
if "access" in [tags][0] {
grok {
match => { "message" => "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] \"%{WORD:method} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} (?:%{NUMBER:bytes}|-) %{QS:hostname} (?:%{QS:referrer}|-) (?:%{NOTSPACE:post_args}|-) %{QS:useragent} (?:%{QS:x_forward_for}|-) (?:%{URIHOST:upstream_host}|-) (?:%{NUMBER:upstream_response_code}|-) (?:%{NUMBER:upstream_response_time}|-) (?:%{NUMBER:response_time}|-)" }
}
useragent {
source => "useragent"
target => "useragent"
}
geoip {
source => "clientip"
}
date {
match => ["timestamp","dd/MMM/yyyy:HH:mm:ss Z"]
target => "@timestamp"
timezone => "Asia/Shanghai"
}
mutate {
convert => ["bytes","integer"]
convert => ["response_time", "float"]
remove_field => ["message","agent","tags"]
add_field => { "target_index" => "logstash-redis-nginx-access-%{+YYYY.MM.dd}" }
}
if [referrer] =~ /^"http/ {
grok {
match => { "referrer" => '%{URIPROTO}://%{URIHOST:referrer_host}' }
}
}
#提取用户请求资源类型以及资源ID编号
if "oldxu.com" in [referrer_host] {
grok {
match => { "referrer" => '%{URIPROTO}://%{URIHOST}/(%{NOTSPACE:oldxu_type}/%{NOTSPACE:oldxu_res_id})?"' }
}
}
}
else if "error" in [tags][0] {
date {
match => ["timestamp","dd/MMM/yyyy:HH:mm:ss Z"]
target => "@timestamp"
timezone => "Asia/Shanghai"
}
mutate {
add_field => { "target_index" => "logstash-redis-nginx-error-%{+YYYY.MM.dd}" }
}
}
}
output {
elasticsearch {
hosts => ["10.0.0.161:9200","10.0.0.162:9200","10.0.0.163:9200"]
index => "%{[target_index]}"
template_overwrite => true
}
stdout {
codec => rubydebug
}
}
[root@logstash-node1 ~]
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/1b220dad14247b839ea4e58d436c5d1e.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/13d3505562f8f8f9cd525a8283aafc5b.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/940215ccca052f3665294c1c4460c1bf.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/fe95091736e0e9c153f1efc4584f51e3.png#pic_center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/c99cf1fe6ed4293f79ded6f83a95c395.png#pic_center)