Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active November 29, 2016 08:03
Show Gist options
  • Save elvisgiv/3e8dcc420790b7015ae34d6a4e87a17f to your computer and use it in GitHub Desktop.
Save elvisgiv/3e8dcc420790b7015ae34d6a4e87a17f to your computer and use it in GitHub Desktop.

для мастер-машины

(на которую будут приходить логи с нод-машин)

перед этим устанавливаем elasticsearch and kibana

https://gist.github.com/elvisgiv/cf83570c79da80bec326d2bdd454980d

https://gist.github.com/elvisgiv/e32d1c043df4e38594c6bbd10044548d

Установка Fluentd

(from: http://itc-life.ru/fluentd-bystryj-poisk-logov-s-elasticsearch-kibana-and-fluentd) (from: http://www.fluentd.org/guides/recipes/elasticsearch-and-s3)

To set up Fluentd (on Ubuntu Precise), run the following command.

$ `curl -L http://toolbelt.treasuredata.com/sh/install-ubuntu-precise-td-agent2.sh | sh`

Next, install the Elasticsearch plugin (to store data into Elasticsearch) and the secure-forward plugin (for secure communication with the node server)

$ `sudo /usr/sbin/td-agent-gem install fluent-plugin-secure-forward`
$ `sudo /usr/sbin/td-agent-gem install fluent-plugin-elasticsearch`

Finally, configure Fluentd by editing sudo nano /etc/td-agent/td-agent.conf as follows.

$ `sudo nano /etc/td-agent/td-agent.conf`

```ruby
	# Listen to incoming data over SSL
	<source>
	  type forward
	  shared_key 911
	  self_hostname 51.1.0.15
	  cert_auto_generate yes
	</source>

	# Store Data in Elasticsearch
	<match *.**>
	  type copy
	  <store>
	    type elasticsearch
	    host localhost
	    port 9200
	    include_tag_key true
	    tag_key @log_name
	    logstash_format true
	    flush_interval 10s
	  </store>
	</match>

```

Finally, restart Fluentd to enable the new configuration.

$ `sudo service td-agent restart`

Проверка статуса fluentd

$ `systemctl status td-agent.service`
просмотр журналов
$ `journalctl -xe`

NODE MACHINE

Установка — настраиваем отправители логов.

Не забываем открыть порты( с них мы будем слушать логи, которые нам посылаю серверы )

$ `sudo apt-get install iptables-persistent -y`

$ `sudo /sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 42185 -j ACCEPT`
$ `sudo /sbin/iptables -A INPUT -i eth0 -p udp --destination-port 42185 -j ACCEPT`

!!! внимание !!!

`eth0` в коде выше заменяем на `enp3s0` (в моем случае) - это `network_interface`

для того, чтобы понять на что заменять надо ввести команду

$ `ifconfig`

вывод: ```ruby

enp3s0  Link encap:Ethernet  HWaddr fc:aa:14:0d:9f:d6  
	...
	RX bytes:837639863 (837.6 MB)  TX bytes:79243706 (79.2 MB)

```

либо: $ route |grep default |awk ' {print $8} '

вывод: enp3s0

либо мы можем писать команды так:

$ `sudo /sbin/iptables -A INPUT -i $(route |grep default |awk  ' {print $8} ') -p tcp --destination-port 42185 -j ACCEPT`
$ `sudo /sbin/iptables -A INPUT -i $(route |grep default |awk  ' {print $8} ') -p udp --destination-port 42185 -j ACCEPT`

Сохраним их

для этого заходим под суперюзером

$ `su -` или $ `sudo su -`

далее:

$ `iptables-save > /etc/iptables/rules.v4`

Для начала установим rsyslog, если его нет в системе:

$ `sudo apt-get install rsyslog  curl -y`

Настроим rsyslog.

$ `sudo nano /etc/rsyslog.conf`

Добавим строку, она означает, что rsyslog будет отправлять данные на порт 42185, его будет слушать td-agent(fluentd)

`*.* @127.0.0.1:42185`

Сохраняемся.

$ `sudo systemctl unmask rsyslog.service`

Перезапустим Rsyslog сервис.

$ `sudo service rsyslog restart`

install fluentd

$ `curl -L http://toolbelt.treasuredata.com/sh/install-ubuntu-precise-td-agent2.sh | sh`

Finally, configure Fluentd by editing sudo nano /etc/td-agent/td-agent.conf as follows.

$ `sudo nano /etc/td-agent/td-agent.conf`


```ruby
	# Listen to Syslog

	<source>
	 type syslog
	 port 42185
	 tag system
	</source>


	# Ship data to the aggregator

	<match *.**>
	  type forward
	  shared_key 911
	  self_hostname 51.0.15.1 (ip нода)
	  <server>
	    host 51.1.0.15
	  </server>
	</match>

```

Finally, restart Fluentd to enable the new configuration.

$ `sudo service td-agent restart`

Рестартим все сервисы для MAIN MASHINE

$ `sudo service td-agent restart`
$ `sudo service elasticsearch restart`
$ `sudo service kibana restart`

заходим на

`http://localhost:5601`

и выбираем там во вкладке Settings -> Indices index name of pattern logstash-* или что-то другое, я еще не разобрался с индексами, передаваемыми в elasticsearch

sudo nano /etc/td-agent/td-agent.conf for node

            # Listen to Syslog
            <source>
             type syslog
             port 42185
             tag system
            # @type tail
            # path /vagrant/temp/log/development.log
            # pos_file /vagrant/temp/log/development.log.pos
            # tag elvis
            # format multiline
            # format1 /^Started/ format1 /Started (?<method>[^ ]+) "(?<path>[^"]+)" for (?<host>[^ ]+) at (?<time>[^ ]+ [^ ]+ [^ ]+)\n/
            # format2 /Processing by (?<controller>[^\u0023]+)\u0023(?<controller_method>[^ ]+) as (?<format>[^ ]+?)\n/
            # format3 /( Parameters: (?<parameters>[^ ]+)\n)?/
            # format4 / Rendered (?<template>[^ ]+) within (?<layout>.+) \([\d\.]+ms\)\n/
            # format5 /Completed (?<code>[^ ]+) [^ ]+ in (?<runtime>[\d\.]+)ms \(Views: (?<view_runtime>[\d\.]+)ms \| ActiveRecord: (?<ar_runtime>[\d\.]+)ms\)/
            </source>


            # Ship data to the aggregator

            <match *.**>
              type forward
              shared_key 911
              self_hostname 51.0.15.1
              <server>
                host 51.1.0.15
              </server>
             #@type elasticsearch
             #logstash_format true
             #host 51.1.0.15 #(optional; default="localhost")
             #port 9200 #(optional; default=9200)
             #index_name elvis_log #(optional; default=fluentd)
             #type_name elvis_tag #(optional; default=fluentd)
            </match>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment