Logstash将解析的日志插入MySQL数据库

发布时间:2020-08-08 04:12:22 作者:rong341233
来源:网络 阅读:546

1.首先Logstash需要安装logstash-output-jdbc插件(以下方式基于docker)
Dockerfile如下:

FROM logstash:7.4.0
RUN sed -i 's#https://rubygems.org#https://gems.ruby-china.com#g' Gemfile && logstash-plugin install logstash-output-jdbc

2.解析nginx日志

        date {
                match => ["timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]   #解析时间
        }
        urldecode {
                field => "Http_Request"     #请求日志转为中文
        }
        geoip {     #获取IP插件
                database => "/usr/share/logstash/geodb/GeoLite2-City.mmdb"
                source => "Client_IP"
                target => "geoip"
                fields => ["country_name","region_name", "city_name", "ip", "longitude", "latitude", "location"]
                add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
                add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
        } 
        useragent {   #解析用户浏览器特征
                source => "User_Agent"
                target => "ua"
        }
        mutate {     #日志必要处理
                convert => [ "[geoip][coordinates]", "float" ]
                convert => [ "Http_Status_Code", "integer" ]
                convert => [ "Http_Bytes", "integer" ]
                convert => [ "Request_Time", "float" ]
                convert => [ "Upstream_Response_Time", "float" ]
                remove_field => [ "beat", "@version", "auth", "prospector", "source", "offset"]
                gsub =>["timestamp","\ \+0800",""]    #将时间的+0800进行删除
                gsub =>["User_Agent","\"",""]  #将浏览器的双引号去除

        }
output {
        jdbc {
                connection_string => "jdbc:mysql://mysql/elk?user=root&password=123456&useUnicode=true&characterEncoding=UTF8"
                driver_jar_path => "/usr/share/logstash/geodb/mysql-connector-java-8.0.18.jar"    #主意加载mysql连接器
                statement => ["INSERT INTO nginx_access_log(Client_IP,X_Forwarded_For,time,App_Version,Device,Device_Name,System_Version,Http_Method,Site,Http_Request,Http_Status_Code,Http_Referrer,Usercode,NetType,Openudid,Request_Time,Upstream_Response_Time,User_agent,city,province) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)","Client_IP","X_Forwarded_For","timestamp","App_Version","Device","Device_Name","System_Version","Http_Method","Site","Http_Request","Http_Status_Code","Http_Referrer","Usercode","NetType","Openudid","Request_Time","Upstream_Response_Time","User_Agent","[geoip][city_name]","[geoip][region_name]"]
#此处注意,比如需要插入城市信息[geoip][city_name]
        }
}               
推荐阅读:
  1. 日志分析logstash插件介绍
  2. Logstash语法常用案例解析(二)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

logstash mysql og st

上一篇:JS中加载cssText延时

下一篇:vue的class和style

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》