新聞中心
需求:收集 ubuntu 系統(tǒng)日志,發(fā)送給 logstash,再由 logstash 把數(shù)據(jù)傳遞給 elasticsearch,最后通過 kibana 展示日志數(shù)據(jù)。

創(chuàng)新互聯(lián)建站專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、通河網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為通河等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
Filebeat System Module
Filebeat Modules 可以簡化常見日志格式的收集、解析和可視化。一個(gè)典型的模塊(例如,對(duì)于 Nginx 日志)由一個(gè)或多個(gè)日志文件(fileset)組成(對(duì)于 Nginx 來說,默認(rèn)是 access.log 和 error.log)。這里我們可以使用 Filebeat 的 System Module 完成 ubuntu 的系統(tǒng)日志。
下面介紹配置 System Module 的步驟(假如你已經(jīng)安裝好了 Filebeat)。
啟用 System Module
Filebeat 支持的模塊默認(rèn)都是未啟用的,我們可以通過下面的方式啟用模塊。找到 filebeat 程序,執(zhí)行 moudles enable 命令:
$ sudo ./filebeat modules enable system
上面的命令啟用了 system 模塊,用下面的命令可以查看當(dāng)前已經(jīng)啟用的模塊有哪些:
$ sudo ./filebeat modules list
把數(shù)據(jù)發(fā)送給 logstash
配置 Filebeat 將日志行發(fā)送到 Logstash。要做到這一點(diǎn),在配置文件 filebeat.yml 中禁用 Elasticsearch 輸出,并啟用 Logstash 輸出:
#output.elasticsearch: #hosts: ["xxx.xxx.xxx.xxx:9200"] output.logstash: hosts: ["xxx.xxx.xxx.xxx:5044"]
重啟 filebeat 服務(wù)
$ sudo systemctl restart filebeat.service
配置 Logstash 處理數(shù)據(jù)
要讓 logstash 接受 Filebeat System Module 發(fā)送來的數(shù)據(jù)還是有些難度的,至少我們需要一個(gè)看上去有點(diǎn)復(fù)雜的配置:
input {
beats {
port => 5064
host => "0.0.0.0"
}
}
filter {
if [fileset][module] == "system" {
if [fileset][name] == "auth" {
grok {
match => { "message" => ["%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:\[%{POSINT:[system][auth][pid]}\])?: %{DATA:[system][auth][ssh][event]} %{DATA:[system][auth][ssh][method]} for (invalid user )?%{DATA:[system][auth][user]} from %{IPORHOST:[system][auth][ssh][ip]} port %{NUMBER:[system][auth][ssh][port]} ssh2(: %{GREEDYDATA:[system][auth][ssh][signature]})?",
"%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:\[%{POSINT:[system][auth][pid]}\])?: %{DATA:[system][auth][ssh][event]} user %{DATA:[system][auth][user]} from %{IPORHOST:[system][auth][ssh][ip]}",
"%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sshd(?:\[%{POSINT:[system][auth][pid]}\])?: Did not receive identification string from %{IPORHOST:[system][auth][ssh][dropped_ip]}",
"%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} sudo(?:\[%{POSINT:[system][auth][pid]}\])?: \s*%{DATA:[system][auth][user]} :( %{DATA:[system][auth][sudo][error]} ;)? TTY=%{DATA:[system][auth][sudo][tty]} ; PWD=%{DATA:[system][auth][sudo][pwd]} ; USER=%{DATA:[system][auth][sudo][user]} ; COMMAND=%{GREEDYDATA:[system][auth][sudo][command]}",
"%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} groupadd(?:\[%{POSINT:[system][auth][pid]}\])?: new group: name=%{DATA:system.auth.groupadd.name}, GID=%{NUMBER:system.auth.groupadd.gid}",
"%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} useradd(?:\[%{POSINT:[system][auth][pid]}\])?: new user: name=%{DATA:[system][auth][user][add][name]}, UID=%{NUMBER:[system][auth][user][add][uid]}, GID=%{NUMBER:[system][auth][user][add][gid]}, home=%{DATA:[system][auth][user][add][home]}, shell=%{DATA:[system][auth][user][add][shell]}$",
"%{SYSLOGTIMESTAMP:[system][auth][timestamp]} %{SYSLOGHOST:[system][auth][hostname]} %{DATA:[system][auth][program]}(?:\[%{POSINT:[system][auth][pid]}\])?: %{GREEDYMULTILINE:[system][auth][message]}"] }
pattern_definitions => {
"GREEDYMULTILINE"=> "(.|\n)*"
}
remove_field => "message"
}
date {
match => [ "[system][auth][timestamp]", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
geoip {
source => "[system][auth][ssh][ip]"
target => "[system][auth][ssh][geoip]"
}
}
else if [fileset][name] == "syslog" {
grok {
match => { "message" => ["%{SYSLOGTIMESTAMP:[system][syslog][timestamp]} %{SYSLOGHOST:[system][syslog][hostname]} %{DATA:[system][syslog][program]}(?:\[%{POSINT:[system][syslog][pid]}\])?: %{GREEDYMULTILINE:[system][syslog][message]}"] }
pattern_definitions => { "GREEDYMULTILINE" => "(.|\n)*" }
remove_field => "message"
}
date {
match => [ "[system][syslog][timestamp]", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
}
}
}
output {
elasticsearch {
hosts => xxx.xxx.xxx.xxx
manage_template => false
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
}
}
處理時(shí)區(qū)問題
看到這段配置我多么希望它能夠直接工作??!不幸的是它并不能很好的工作,至少在我的 ubuntu 18.04 上不行。問題的核心是無論 auth.log 還是 syslog,記錄的都是本地時(shí)區(qū)的區(qū)時(shí):
而上面的配置中把這些時(shí)間都當(dāng)成 UTC 時(shí)間來處理了。搞清楚了原因,糾正起來就很容易了,在 date 插件中添加本地的時(shí)區(qū)信息就可以了。比如筆者所在時(shí)區(qū)為東八區(qū),那么就分別在兩個(gè) date 的配置中添加下面的信息:
timezone => "Asia/Chongqing"
讓獨(dú)立的 pipeline 處理該數(shù)據(jù)流
下面創(chuàng)建一個(gè)新的目錄 /etc/logstash/myconf.d,并在 /etc/logstash/myconf.d 目錄下創(chuàng)建 Logstash 配置文件 krtest.conf。然后在 /etc/logstash/pipelines.yml 文件中添加新的 pipeline 配置:
- pipeline.id: main
path.config: "/etc/logstash/conf.d/*.conf"
- pipeline.id: krtest
path.config: "/etc/logstash/myconf.d/krtest.conf"
其中 pipeline.id 為 main 的管道是默認(rèn)的配置,我們新添加了 id 為 krtest 的管道并指定了對(duì)應(yīng)的配置文件路徑。把上面的配置寫入到 /etc/logstash/myconf.d/krtest.conf 文件中。然后重啟 logstash 服務(wù):
$ sudo systemctl restart logstash.service
在 Kibana 中查看日志
最后在 kibana 中添加 filebeat 開頭的 index pattern,就可以通過圖形界面查看 ubuntu 的系統(tǒng)日志了:
網(wǎng)頁標(biāo)題:通過Filebeat收集Ubuntu系統(tǒng)日志
文章地址:http://m.fisionsoft.com.cn/article/dhjocih.html


咨詢
建站咨詢
