将Yamaha路由器(RTX810)的日志通过syslog传输,并使用docker + EFK进行查看

总结

由于RTX810本身只能存储3000行日志,因此使用syslog进行转移。

    • RTX810のログをsyslogで飛ばす

 

    • fluentdのin_syslogで受ける

 

    Elasticsearchに転送してKibanaで可視化

在一个主机上使用Docker构建Fluentd、Elasticsearch和Kibana。

文件架构

文件和目录结构如下所示。
syslog日志会发送到Elasticsearch,并同时存储在log目录中。

├── docker-compose.yaml
├── fluentd
│   ├── Dockerfile
│   └── fluent.conf
└── log

docker-compose 是这样的。

version: '2'

services:
  fluentd:
    build: ./fluentd
    environment:
      FLUENTD_CONF: fluent.conf
    restart: always
    volumes:
      - ./fluentd/fluent.conf:/fluentd/etc/fluent.conf
      - ./log:/home/fluent/syslog
    ports:
      - "514:5140/udp"
    depends_on:
      - elasticsearch

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3
    volumes:
      - esdata:/usr/share/elasticsearch/data
      - esconfig:/usr/share/elasticsearch/config
    expose:
      - "9200"
    restart: always
    environment:
      - bootstrap.memory_lock=true
      - xpack.security.enabled=false
      - xpack.monitoring.enabled=false
      - xpack.watcher.enabled=false
      - xpack.graph.enabled=false
      - xpack.ml.enabled=false
      - http.max_content_length=1g
      - thread_pool.index.queue_size=-1
      - thread_pool.bulk.queue_size=-1
      - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    mem_limit: 2g

  kibana:
    image: docker.elastic.co/kibana/kibana:5.6.3
    ports:
      - "5601:5601"
    restart: always
    environment:
      - "ELASTICSEARCH_URL=http://elasticsearch:9200"
      - xpack.graph.enabled=false
      - xpack.security.enabled=false
      - xpack.ml.enabled=false
    depends_on:
      - elasticsearch

volumes:
  esdata:
    driver: local
  esconfig:
    driver: local
FROM fluent/fluentd

RUN gem install fluent-plugin-elasticsearch

由于在fluentd容器中开放514/udp端口时遇到错误,因此使用docker-compose进行了514⇒5140端口的映射设置,并在容器中使用in_syslog插件来在5140端口接收。

由于使用rfc3164或rfc5424指定的message_format无法成功匹配RTX810发送的syslog,因此改为使用整个消息进行匹配。

将接收到的日志通过copy插件输出到Elasticsearch和文件中。
对于文件的输出路径最好是在权限上/home/fluent/以下(例如/var/log/以下无法写入会出现错误)。

使用单一值格式将消息写入文件,并使用gzip对写入的文件进行压缩。

<source>
  @type syslog
  port 5140
  bind 0.0.0.0
  format /^? *(?<message>.*)$/ 
  time_format %Y/%m/%d %h:%M:%s 
  tag syslog
</source>

<match syslog.**>
  @type copy
  <store>
    @type elasticsearch
    include_tag_key true
    tag_key _tag
    host elasticsearch
    port 9200
    logstash_format true
    logstash_prefix logstash

    buffer_type file
    buffer_path /tmp/fluentd*.buffer
#    buffer_chunk_limit 1g
#    buffer_queue_limit 256
#    flush_interval 60s
#    retry_wait 5s
  </store>
  <store>
    @type file
    path /home/fluent/syslog/log
    compress gzip
    format single_value
  </store>
</match>

准备好以上的文件后,启动容器。

docker-compose build
docker-compose up -d

RTX810设置

syslog hostでdockerホストのIP(この例では192.168.100.221)を指定

syslog facilityはお好みで(下記はlocal7)
転送対象とするログをnotice, info, debugから選ぶ

syslog host 192.168.100.221
syslog facility local7
syslog notice on
syslog info on
syslog debug on

Kibana 可视化平台

通过浏览器访问http://192.168.100.221:5601/。
创建索引模式后,日志将欣然呈现。

image.png
广告
将在 10 秒后关闭
bannerAds