1. 程式人生 > >使用fluentd實時收到nginx日誌到mysql資料庫

使用fluentd實時收到nginx日誌到mysql資料庫

前言

本篇介紹如何使用fluentd把nginx的log日誌讀取,並且解析成為一個一個MySQL的欄位,最後儲存到mysql的資料庫中。

環境

我用的是aws的ec2,作業系統是amazon定製的Amazon Linux AMI

安裝fluentd

使用root使用者

curl -L https://toolbelt.treasuredata.com/sh/install-redhat-td-agent2.sh | sh

安裝完畢後,在/usr/sbin/下會有td-agent , td-agent-gem ,td-agent-ui三個可執行檔案。其中td-agent-gem用來安裝之外的fluent的外掛。

配置檔案在/etc/td-agent/td-agent.conf

啟動fluentd命令集合

/etc/init.d/td-agent start
/etc/init.d/td-agent stop
/etc/init.d/td-agent restart
/etc/init.d/td-agent status

log可以在/var/log/td-agent/td-agent.log檢視

編輯fluentd配置檔案

定義一個source,讀取nginx的log檔案

<source>
  @type tail
  path /tmp/nginx.log
  pos_file /var/log/td-agent/nginx.log.pos
  tag nginx.access
  format /^(?<remote>[^ ]*) - (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<http_x_forwarded_for>[^\"]*)" "(?<host>[^\"]*)" "(?<country>[^\"]*)" "(?<city>[^\"]*)")?$/
  time_format %d/%b/%Y:%H:%M:%S %z
</source>

注意:format的地方需要根據自己的nginx的log的格式進行相應的調整

接下去定義一個寫入到mysql的match

<match nginx.access>
  @type mysql_bulk
  host your_host
  database your_db
  username your_username
  password your_password
  column_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,log_time
  key_names remote,host,user,method,path,code,size,referer,agent,country,city,http_x_forwarded_for,${time}
  table nginx_access
  flush_interval 10s
</match>

附上mysql的建表語句

CREATE TABLE `nginx_access` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `remote` text,
  `host` text,
  `user` text,
  `method` text,
  `path` text,
  `code` text,
  `size` text,
  `referer` text,
  `agent` text,
  `country` text,
  `city` text,
  `http_x_forwarded_for` text,
  `log_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

安裝mysql_bulk的外掛

從https://github.com/tagomoris/fluent-plugin-mysql克隆下專案。由於我用的fluentd是0.12版本,所以對應的fluent-plugin-mysql的版本是v0.1.5,所以Git checkout v0.1.5。

進行gem build fluent-plugin-mysql.gemspec生成.gem檔案。
然後用/usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem命令來安裝外掛。

如果安裝外掛失敗的話,很大可能是沒有mysql-devel。

mysql client is missing. You may need to 'apt-get install libmysqlclient-dev' or 'yum install mysql-devel', and try again.

所以先執行yum install mysql-devel。

[[email protected] fluent-plugin-mysql]# /usr/sbin/td-agent-gem install fluent-plugin-mysql-0.1.5.gem
Building native extensions.  This could take a while...
Successfully installed mysql2-0.4.9
Fetching: mysql2-cs-bind-0.0.6.gem (100%)
Successfully installed mysql2-cs-bind-0.0.6
Fetching: jsonpath-0.8.7.gem (100%)
Successfully installed jsonpath-0.8.7
Successfully installed fluent-plugin-mysql-0.1.5
Parsing documentation for mysql2-0.4.9
Installing ri documentation for mysql2-0.4.9
Parsing documentation for mysql2-cs-bind-0.0.6
Installing ri documentation for mysql2-cs-bind-0.0.6
Parsing documentation for jsonpath-0.8.7
Installing ri documentation for jsonpath-0.8.7
Parsing documentation for fluent-plugin-mysql-0.1.5
Installing ri documentation for fluent-plugin-mysql-0.1.5
Done installing documentation for mysql2, mysql2-cs-bind, jsonpath, fluent-plugin-mysql after 0 seconds
4 gems installed

結語

可能遇到的坑總結一下:

  • gem命令的時候,需要使用/usr/sbin/td-agent-gem,td-agent使用的是這個環境的gem依賴
  • ruby安裝mysql的外掛的時候,需要mysql-devel,否則會報錯
  • nginx的log format可能根據自己的情況調整
  • source檔案的tail需要該輸入檔案的目錄許可權是755