openresty+filebeat發(fā)送nginx返回日志到kafka

本文主要分為三個(gè)步驟:
1)openresty安裝意敛,openresty包含了nginx+lua呐能;
2)filebeat安裝往堡;
3)lua腳本編寫(xiě)坝疼;

一:openresty安裝

本次安裝的版本是openresty-1.19.3.1.tar.gz,直接在你想安裝的文件夾解壓即可闸英;

在安裝之前防止缺少相應(yīng)的依賴包:yum install pcre-devel openssl-devel gcc curl锯岖;
進(jìn)入剛剛解壓的文件夾,如果沒(méi)有權(quán)限進(jìn)如文件夾可以修改相應(yīng)權(quán)限(sudo chmod 755 /usr/local/openresty -R)甫何,進(jìn)入文件夾后執(zhí)行下列命令:

./configure

make

sudo make install

如果沒(méi)有權(quán)限請(qǐng)用sudo執(zhí)行出吹;

其他問(wèn)題可參考下列鏈接:http://openresty.org/cn/installation.html

image.png

二:filebeat安裝
這次安裝的版本是:filebeat-6.6.0-linux-x86_64.tar.gz;
在你想安裝的頁(yè)面解壓即可辙喂;
添加filebeat_nginx.yml,對(duì)其進(jìn)行修改捶牢,從nginx中拉去日志鸠珠,并將日志推到kafka,

###################### Filebeat Configuration Example #########################

# This file is an example configuration file highlighting only the most common
# options. The filebeat.reference.yml file from the same directory contains all the
# supported options with more comments. You can use it as a reference.
#
# You can find the full configuration reference here:
# [https://www.elastic.co/guide/en/beats/filebeat/index.html](https://www.elastic.co/guide/en/beats/filebeat/index.html)

# For more available modules and options, please see the filebeat.reference.yml sample
# configuration file.

#=========================== Filebeat inputs =============================

filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

# Change to true to enable this input configuration.
enabled: false

# Paths that should be crawled and fetched. Glob based paths.
paths:
- /var/log/*.log
#- c:\programdata\elasticsearch\logs\*

# Exclude lines. A list of regular expressions to match. It drops the lines that are
# matching any regular expression from the list.
#exclude_lines: ['^DBG']

# Include lines. A list of regular expressions to match. It exports the lines that are
# matching any regular expression from the list.
#include_lines: ['^ERR', '^WARN']

# Exclude files. A list of regular expressions to match. Filebeat drops the files that
# are matching any regular expression from the list. By default, no files are dropped.
#exclude_files: ['.gz$']

# Optional additional fields. These fields can be freely picked
# to add additional information to the crawled log files for filtering
#fields:
# level: debug
# review: 1

### Multiline options

# Multiline can be used for log messages spanning multiple lines. This is common
# for Java Stack Traces or C-Line Continuation

# The regexp Pattern that has to be matched. The example pattern matches all lines starting with [
#multiline.pattern: ^\[

# Defines if the pattern set under pattern should be negated or not. Default is false.
#multiline.negate: false

# Match can be set to "after" or "before". It is used to define if lines should be append to a pattern
# that was (not) matched before or after or as long as a pattern is not matched based on negate.
# Note: After is the equivalent to previous and before is the equivalent to to next in Logstash
#multiline.match: after

#============================= Filebeat modules ===============================

filebeat.config.modules:
# Glob pattern for configuration loading
path: ${path.config}/modules.d/*.yml

# Set to true to enable config reloading
reload.enabled: false

# Period on which files under path should be checked for changes
#reload.period: 10s

#==================== Elasticsearch template setting ==========================

setup.template.settings:
index.number_of_shards: 3
#index.codec: best_compression
#_source.enabled: false

#================================ General =====================================

# The name of the shipper that publishes the network data. It can be used to group

# all the transactions sent by a single shipper in the web interface.
#name:

# The tags of the shipper are included in their own field with each
# transaction published.
#tags: ["service-X", "web-tier"]

# Optional fields that you can specify to add additional information to the
# output.
#fields:
# env: staging

#============================== Dashboards =====================================
# These settings control loading the sample dashboards to the Kibana index. Loading
# the dashboards is disabled by default and can be enabled either by setting the
# options here, or by using the `-setup` CLI flag or the `setup` command.
#setup.dashboards.enabled: false

# The URL from where to download the dashboards archive. By default this URL
# has a value which is computed based on the Beat name and version. For released
# versions, this URL points to the dashboard archive on the [artifacts.elastic.co](http://artifacts.elastic.co/)
# website.
#setup.dashboards.url:

#============================== Kibana =====================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

# Kibana Host
# Scheme and port can be left out and will be set to the default (http and 5601)
# In case you specify and additional path, the scheme is required: [http://localhost:5601/path](http://localhost:5601/path)
# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
#host: "localhost:5601"

# Kibana Space ID
# ID of the Kibana Space into which the dashboards should be loaded. By default,
# the Default Space will be used.
#[space.id](http://space.id/):

#============================= Elastic Cloud ==================================

# These settings simplify using filebeat with the Elastic Cloud ([https://cloud.elastic.co/](https://cloud.elastic.co/)).

# The [cloud.id](http://cloud.id/) setting overwrites the `output.elasticsearch.hosts` and
# `setup.kibana.host` options.
# You can find the `[cloud.id](http://cloud.id/)` in the Elastic Cloud web UI.
#[cloud.id](http://cloud.id/):

# The cloud.auth setting overwrites the `output.elasticsearch.username` and
# `output.elasticsearch.password` settings. The format is `<user>:<pass>`.
#cloud.auth:

#================================ Outputs =====================================

# Configure what output to use when sending the data collected by the beat.

#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
# Array of hosts to connect to.
# hosts: ["localhost:9200"]

# Enabled ilm (beta) to use index lifecycle management instead daily indices.
#ilm.enabled: false

# Optional protocol and basic auth credentials.
#protocol: "https"
#username: "elastic"
#password: "changeme"

#----------------------------- Logstash output --------------------------------
#output.logstash:
# The Logstash hosts
#hosts: ["localhost:5044"]

# Optional SSL. By default is off.
# List of root certificates for HTTPS server verifications
#ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

# Certificate for SSL client authentication
#ssl.certificate: "/etc/pki/client/cert.pem"

# Client Certificate Key
#ssl.key: "/etc/pki/client/cert.key"

#================================ Processors =====================================

# Configure processors to enhance or manipulate events generated by the beat.

processors:
- add_host_metadata: ~
- add_cloud_metadata: ~

#================================ Logging =====================================

# Sets log level. The default log level is info.
# Available log levels are: error, warning, info, debug
#logging.level: debug

# At debug level, you can selectively enable logging only for some components.
# To enable all selectors use ["*"]. Examples of other selectors are "beat",
# "publish", "service".
#logging.selectors: ["*"]

#============================== Xpack Monitoring ===============================
# filebeat can export internal metrics to a central Elasticsearch monitoring
# cluster. This requires xpack monitoring to be enabled in Elasticsearch. The
# reporting is disabled by default.

# Set to true to enable the monitoring reporter.
#xpack.monitoring.enabled: false

# Uncomment to send the metrics to Elasticsearch. Most settings from the
# Elasticsearch output are accepted here as well. Any setting that is not set is
# automatically inherited from the Elasticsearch output configuration, so if you
# have the Elasticsearch output configured, you can simply uncomment the
# following line.
#xpack.monitoring.elasticsearch:

#=======================put log to kafka ==============

filebeat.modules:
- module: nginx
access:
enabled: true
var.paths: ["/usr/local/openresty/nginx/logs/access-map.log"]
error:
enabled: true
var.paths: ["/usr/local/openresty/nginx/logs/error.log"]

#----------------------------------Kafka output--------------------------------#
output.kafka:
version: "1.0.1"
enabled: true
hosts: ['ip:9092', 'ip8:9092', 'ip49:9092']
topic: 'topicName'
required_acks: 1 #default
compression: gzip #default
max_message_bytes: 1000000 #default
codec.format:
string: '%{[message]}'

filebeat啟動(dòng)命令:sudo ./filebeat -e -c filebeat_nginx.yml秋麸;

三:nginx.conf配置:

image.png
image.png

nginx輸出輸入:

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_uri" "$request_body" "$request_body" server_name:"$server_name" "$upstream_addr" "$scheme" resp_body:"$resp_body" ';

解決中文亂碼

log_escape_non_ascii off;

lua腳本編寫(xiě):

lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末渐排,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子灸蟆,更是在濱河造成了極大的恐慌驯耻,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炒考,死亡現(xiàn)場(chǎng)離奇詭異可缚,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)斋枢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)帘靡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人杏慰,你說(shuō)我怎么就攤上這事测柠。” “怎么了缘滥?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)谒主。 經(jīng)常有香客問(wèn)我朝扼,道長(zhǎng),這世上最難降的妖魔是什么霎肯? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任擎颖,我火速辦了婚禮,結(jié)果婚禮上观游,老公的妹妹穿的比我還像新娘搂捧。我一直安慰自己,他們只是感情好懂缕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布允跑。 她就那樣靜靜地躺著,像睡著了一般搪柑。 火紅的嫁衣襯著肌膚如雪聋丝。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天工碾,我揣著相機(jī)與錄音弱睦,去河邊找鬼。 笑死渊额,一個(gè)胖子當(dāng)著我的面吹牛况木,可吹牛的內(nèi)容都是我干的垒拢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼火惊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼求类!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起矗晃,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤仑嗅,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后张症,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體仓技,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年俗他,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了脖捻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡兆衅,死狀恐怖地沮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情羡亩,我是刑警寧澤摩疑,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站畏铆,受9級(jí)特大地震影響雷袋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辞居,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一楷怒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瓦灶,春花似錦鸠删、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至每界,卻和暖如春捅僵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背眨层。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工庙楚, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人趴樱。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓馒闷,卻偏偏與公主長(zhǎng)得像酪捡,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子纳账,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容