Bu örneğimizde Fluentd kullanarak JSON tipindeki logları bir log dosyasından okuyup diğer bir dosyaya yazacağız.


Mantık


  1. Uygulama logları logs/application/registration.log* dosyasına yazar.

  2. Fluentd logs/application/registration.log* dosyasını takip eder.

  3. Fluentd yeni okunan logları logs/fluentd/registration.log* dosyasına yazar.

Yukarıda'da gördüğümüz gibi log rotasyon özelliğini, * simgesini logs/application/registration.log dosyasının sonuna ekleyerek aktif hale getirmiş olduk.


Fluentd


Aşağıdaki işlemlerin neden yapıldığını anlamak için Before Installing Fluentd sayfasını okumanız faydalı olacaktır.


NTP kurulumu


Şu anki zaman Wed Mar 28 19:25:10 UTC 2018 ama sunucudaki zaman ise aşağıda'da gördüğümüz gibi yalnış görünüyor.


ubuntu@linux:~$ date
Sat Mar 24 22:48:10 UTC 2018

Sunucunun zamanını yenileyelim.


ubuntu@linux:~$ sudo timedatectl set-timezone Europe/London

ubuntu@linux:~$ sudo apt-get install ntp

ubuntu@linux:~$ sudo nano /etc/ntp.conf # This is whole new content
driftfile /var/lib/ntp/drift
# Specify UK NTP servers.
server 0.uk.pool.ntp.org
server 1.uk.pool.ntp.org
server 2.uk.pool.ntp.org
server 3.uk.pool.ntp.org
# Use Ubuntu's NTP server as a fallback.
server ntp.ubuntu.com
# Local users may obtain data from NTP servers.
restrict 127.0.0.1
restrict ::1

ubuntu@linux:~$ sudo service ntp restart

Aşağıda'da gördüğümüz gibi zaman doğrulanmış durumda.


ubuntu@linux:~$ date
Wed Mar 28 19:29:38 BST 2018

Max # of File Descriptors değerinin arttırımı


Sistemin $ ulimit -n değeri varsayılan olarak 1024 olarak ayarlıdır ama bunu arttırmak için aşağıdaki adımları uygulayıp sistemi yeniden başlatın. Sistemin yeniden başlatılmasından sonra değer 65536 olacaktır.


ubuntu@linux:~$ sudo nano /etc/security/limits.conf # Add these lines to the file
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536

Network Kernel Parametrelerinin iyileştirilmesi


Aşağıdaki satırları konfigürasyon dosyasına ekleyip $ sudo sysctl -p komutunu çalıştırın.


ubuntu@linux:~$ sudo nano /etc/sysctl.conf
net.core.somaxconn = 1024
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_wmem = 4096 12582912 16777216
net.ipv4.tcp_rmem = 4096 12582912 16777216
net.ipv4.tcp_max_syn_backlog = 8096
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535

Fluentd td-agent kurulumu


Aşağıdaki komutları kullanıp td-agent'i kurup yeniden başlatın. Konfigürasyon dosyası /etc/td-agent/td-agent.conf yolundadır. Daha fazla bilgi için Installing Fluentd Using deb Package sayfasını okuyun.


ubuntu@linux:~$ curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-xenial-td-agent2.sh | sh

ubuntu@linux:~$ sudo /etc/init.d/td-agent status/start/stop/restart

Test


Varsayılan /var/log/td-agent/td-agent.log log dosyasına mesaj yazıp yazamayacağımızı test edelim. Aşağıdaki komutu çalıştırdıktan sonra "td-agent.log" dosyasının sonunda 2018-03-28 20:04:44 +0100 debug.test: {"json":"hello world"} satırını göreceğiz.


ubuntu@linux:~$ curl -X POST -d 'json={"json":"hello world"}' http://localhost:8888/debug.test

Hazırlık


Fluentd logları belirli bir klasör içindeki dosyaya yazacağı için td-agent kullanıcısının klasörün sahibi olması lazım. Bu klasör ayrıca "pozisyon" veritabanı dosyasına da ev sahipliği de yapacaktır. Bu dosya en son hangi dosyanın hangi satırında kaldığını tutacağı için, td-agent daha önceden okuduğu logları ikilemeyecektir.


ubuntu@linux:~$ mkdir logs

ubuntu@linux:~$ mkdir logs/application

ubuntu@linux:~$ mkdir logs/fluentd

ubuntu@linux:~$ sudo chown td-agent -R logs/fluentd

ubuntu@linux:~$ ls -l
drwxrwxr-x 4 ubuntu ubuntu 4096 Apr 6 20:32 logs

ubuntu@linux:~$ ls -l logs/
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr 6 20:32 application
drwxrwxr-x 2 td-agent ubuntu 4096 Apr 6 20:32 fluentd

Örnek bir tane log dosyası yaratıp onu loglarımızı yazmak için kullanalım ki, td-agent bu dosyayı takip etsin.


ubuntu@linux:~$ touch logs/application/registration.log.1

ubuntu@linux:~$ ls -l logs/application/
-rw-rw-r-- 1 ubuntu ubuntu 0 Apr 6 20:39 registration.log.1

Konfigürasyon


Aşağıdaki konfigürasyon girdisini td-agent konfigürasyon dosyasına ekledikten sonra kaydedip çıkın.


ubuntu@linux:~$ sudo nano /etc/td-agent/td-agent.conf

# Directive determines the input sources
# Watches source and triggers an event with a tag attached to it
<source>
@type tail # Uses tail plugin to read logs from
format json # Assumes that the log file is in "json" format
read_from_head true # Start to read the logs from the head of file, not bottom
tag api.user.registration # Tag triggered event with "api.user.registration"
path /home/ubuntu/logs/application/registration.log* # Paths to the files which will be tailed
pos_file /home/ubuntu/logs/fluentd/registration.log.pos # Path to the "position" database file
</source>

# Directive determines the output destinations
# Catches an event with a specific tag attached to it
<match api.user.registration>
@type file # Uses file plugin to write logs to
path /home/ubuntu/logs/fluentd/registration.log # Path to the log file which logs will be written to
</match>

Aşağıdaki komut ile td-agent'i yeniden başlatalım ki td-agent'in yeni konfigürasyondan haberi olsun.


ubuntu@linux:~$ sudo /etc/init.d/td-agent restart
[ ok ] Restarting td-agent (via systemctl): td-agent.service.

Yeniden başlatma işleminden hemen sonra td-agent "pozisyon" veritabanı dosyasını yaratır.


ubuntu@linux:~$ ls -l logs/fluentd/
total 4
-rw-r--r-- 1 td-agent td-agent 83 Apr 6 20:56 registration.log.pos

Test


ubuntu@linux:~$ echo '{"user":"1"}' >> logs/application/registration.log.1 
ubuntu@linux:~$ echo '{"user":"2"}' >> logs/application/registration.log.1
ubuntu@linux:~$ echo '{"user":"3"}' >> logs/application/registration.log.1

ubuntu@linux:~$ ls -l logs/fluentd/

-rw-r--r-- 1 td-agent td-agent 61 Apr 6 21:02 registration.log.20180406.b56933893cd87b6b8
-rw-r--r-- 1 td-agent td-agent 83 Apr 6 21:02 registration.log.pos

ubuntu@linux:~$ cat logs/fluentd/registration.log.20180406.b56933893cd87b6b8

2018-04-06T21:02:30+01:00 api.user.registration {"user":"1"}
2018-04-06T21:02:49+01:00 api.user.registration {"user":"2"}
2018-04-06T21:02:55+01:00 api.user.registration {"user":"3"}

ubuntu@linux:~$ touch logs/application/registration.log.2

ubuntu@linux:~$ echo '{"admin":"1"}' >> logs/application/registration.log.2
ubuntu@linux:~$ echo '{"admin":"2"}' >> logs/application/registration.log.2
ubuntu@linux:~$ echo '{"admin":"3"}' >> logs/application/registration.log.2

ubuntu@linux:~$ cat logs/fluentd/registration.log.20180406.b56933893cd87b6b8

2018-04-06T21:02:30+01:00 api.user.registration {"user":"1"}
2018-04-06T21:02:49+01:00 api.user.registration {"user":"2"}
2018-04-06T21:02:55+01:00 api.user.registration {"user":"3"}
2018-04-06T21:07:37+01:00 api.user.registration {"admin":"1"}
2018-04-06T21:07:37+01:00 api.user.registration {"admin":"2"}
2018-04-06T21:07:38+01:00 api.user.registration {"admin":"3"}

Referanslar