利用Rsyslog向ElasticSearch推送nginx access日志

1> 重新配置 nginx access 日志格式为 json 格式;

log_format ngx_accss_json '{ "@timestamp": "$time_iso8601", '
                         '"@fields": { '
                         '"remote_addr": "$remote_addr", '
                         '"server_name": "$server_name", '
                         '"server_port": "$server_port", '
                         '"scheme": "$scheme", '
                         '"server_protocol": "$server_protocol", '
                         '"body_bytes_sent": "$body_bytes_sent", '
                         '"request_time": "$request_time", '
                         '"status": "$status", '
                         '"request": "$request", '
                         '"uri": "$uri", '
                         '"request_method": "$request_method", '
                         '"http_referrer": "$http_referer", '
                         '"host": "$host", '
                         '"http_user_agent": "$http_user_agent" } }';


access_log syslog:server=127.0.0.1:514,facility=local7,tag=nginx,severity=info ngx_accss_json;

2> 配置 rsyslog 推送到ElasticSearch, 这里我们启用了 DA 模式;

# 原始消息已为json格式
$template rawmsg,"%msg%"


# ElasticSearch 索引
template(name="nginx-index"
  type="list") {
    constant(value="nginx-")
    property(name="timereported" dateFormat="rfc3339" position.from="1" position.to="4")
    constant(value=".")
    property(name="timereported" dateFormat="rfc3339" position.from="6" position.to="7")
    constant(value=".")
    property(name="timereported" dateFormat="rfc3339" position.from="9" position.to="10")
}


# 
if $syslogfacility-text == "local7" and $syslogtag == "nginx:" then {
    local7.* action(
                type="omelasticsearch"
                template="rawmsg"
                searchIndex="nginx-index"
                dynSearchIndex="on"
                server="127.0.0.1"
                bulkmode="on"
                action.resumeretrycount="-1"
                queue.fileName="nginx_access"
                queue.maxDiskSpace="5g"
                queue.saveOnShutdown="on"
                )
    stop
}

3> 配置 Kibana 



注:

nginx  error_log 无法直接配置格式需修改代码;


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值