一、使用logstash收集nginx日志
1.安装nginx
2.配置nginx日志为json格式
[root@logstash ~]
http {
... ...
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}' ;
access_log /var/log/nginx/nginx_json.log json;
... ...
}
3.启动nginx
[root@logstash ~]
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@logstash ~]
4.配置logstash收集nginx日志
[root@logstash ~]
input {
file {
path => "/var/log/nginx/nginx_json.log"
start_position => "beginning"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200" ]
index => "nginx_%{+YYYY-MM-dd}.log"
}
}
5.启动logstash
[root@logstash ~]
6.ES页面查看数据
7.kibana查看数据
二、修改nginx日志为json格式展示
1.配置nginx日志格式为json
1)方法一:解决日志message无法调用问题
[root@logstash ~]
input {
file {
path => "/var/log/nginx/nginx_json.log"
start_position => "beginning"
}
}
filter {
json {
source => "message"
remove_field => ["message" ]
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200" ]
index => "nginx_json_%{+YYYY-MM-dd}.log"
}
}
[root@logstash ~]
2)方式二:
[root@logstash ~]
input {
file {
path => "/var/log/nginx/nginx_json.log"
start_position => "beginning"
codec => "json"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200" ]
index => "nginx_json_%{+YYYY-MM-dd}.log"
}
}
[root@logstash ~]
2.画饼图
三、使用logstash将日志写入redis
1.准备机器
主机
IP
服务
logstash
10.0.0.54
logstash、nginx
db01
10.0.0.51
ES、kibana
db02
10.0.0.52
ES
db03
10.0.0.53
ES、redis(6381)
2.安装redis
[root@db03 ~]
3.配置收集nginx日志到redis
[root@logstash ~]
input {
file {
path => "/var/log/nginx/nginx_json.log"
start_position => "beginning"
codec => "json"
}
}
output {
redis {
host => "172.16.1.53"
port => "6381"
data_type => "list"
key => "nginx_json_log"
db => "0"
}
}
[root@logstash ~]
[root@logstash ~]
[root@logstash ~]
4.访问页面,查看redis中数据
[root@db03 ~]
127.0.0.1:6381> keys *
1) "nginx_json_log"
127.0.0.1:6381> LLEN nginx_json_log
(integer ) 19
127.0.0.1:6381> LRANGE nginx_json_log 0 -1
5.配置将redis中的数据放到ES
[root@logstash ~]
input {
redis {
host => "172.16.1.53"
port => "6381"
db => "0"
data_type => "list"
key => "nginx_json_log"
}
}
output {
elasticsearch {
hosts => ["10.0.0.51:9200" ]
index => "nginx_redis_es_%{+YYYY-MM-dd}"
}
}
[root@logstash ~]
四、通过TCP和UDP收集数据
1.配置收集远端通过tcp发来的消息
[root@logstash ~]
input {
tcp {
port => 1234
type => "tcplog"
mode => "server"
}
}
output {
stdout {
codec => rubydebug
}
}
[root@logstash ~]
2.测试收集数据
1)使用telnet测试
[root@db02 ~]
Trying 10.0.0.54...
Connected to 10.0.0.54.
Escape character is '^]' .
123
456
{
"@version" => "1" ,
"@timestamp" => 2020-07-20T09:40:49.974Z,
"host" => "10.0.0.52" ,
"port" => 50492,
"type" => "tcplog" ,
"message" => "123\r"
}
{
"@version" => "1" ,
"@timestamp" => 2020-07-20T09:41:36.764Z,
"host" => "10.0.0.52" ,
"port" => 50492,
"type" => "tcplog" ,
"message" => "456\r"
}
2)使用nc工具测试
[root@db04 ~]
[root@db02 ~]
{
"@version" => "1" ,
"@timestamp" => 2020-07-20T09:46:57.706Z,
"host" => "10.0.0.52" ,
"port" => 50520,
"type" => "tcplog" ,
"message" => "zls test nc"
}
[root@db02 ~]
[root@db02 ~]
[root@db02 ~]
{
"@version" => "1" ,
"@timestamp" => 2020-07-20T09:48:28.181Z,
"host" => "10.0.0.52" ,
"port" => 50524,
"type" => "tcplog" ,
"message" => "123"
}
{
"@version" => "1" ,
"@timestamp" => 2020-07-20T09:48:29.503Z,
"host" => "10.0.0.52" ,
"port" => 50524,
"type" => "tcplog" ,
"message" => "123"
}
3.通过伪设备的方式发送日志
[root@db02 ~]
{
"@version" => "1" ,
"@timestamp" => 2020-07-20T09:57:25.807Z,
"host" => "10.0.0.52" ,
"port" => 50526,
"type" => "tcplog" ,
"message" => "伪设备 测试"
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现