分享

Graylog 安装使用

 野崎君noZakiKu 2017-06-08

Graylog 安装使用

简介

Graylog 是一个简单易用、功能较全面的日志管理工具,相比 ELK 组合, 优点:

  • 部署维护简单
  • 查询语法简单易懂(对比ES的语法…)
  • 内置简单的告警
  • 可以将搜索结果导出为 json
  • 提供简单的聚合统计功能
  • UI 比较友好

当然, 拓展性上比 ELK 差很多。

整套依赖:

  • Graylog 提供 graylog 对外接口, CPU 密集
  • Elasticsearch 日志文件的持久化存储和检索, IO 密集
  • MongoDB 只是存储一些 Graylog 的配置

Graylog 最简单的架构

图 11.0.1

Graylog 集群架构:

图 11.0.2

安装

docker 安装

环境:centos 7.0 64位

Graylog 官方提供了 Docker 镜像:

docker pull mongo:3
docker pull elasticsearch:2
docker pull graylog2/server:2.1.2-1
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

docker-compose 拉起服务:

version: '2'
services:
  mongo:
    image: "mongo:3"
    volumes:
        - /data/mongo:/data/db
  elasticsearch:
    image: "elasticsearch:2"
    volumes:
        - /data/elasticsearch:/usr/share/elasticsearch/data
    command: "elasticsearch -Des.cluster.name='graylog'"
  graylog:
    image: graylog2/server:2.1.2-1
    environment:
      GRAYLOG_WEB_ENDPOINT_URI: http://x.x.x.x:9000/api
    depends_on:
      - mongo
      - elasticsearch
    ports:
      - "9000:9000"
      - "514:514"
      - "515:515"                                                 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

docker-compose -f graylog.yml up -d

浏览器访问 http://x.x.x.x:9000, 默认账户名和密码均为 admin, 可得:

图 11.1

修改配置

Graylog 配置文件默认装在容器内的 /usr/share/graylog/data/config/graylog.conf, 可以通过 volume 挂载自己的配置。

其他如 log4j2, journal, plugin 也都在 /usr/share/graylog/data/config/ 目录。

例如修改时区,只需要修改配置文件:

# Default is UTC             
root_timezone = Asia/Shanghai
  • 1
  • 2
  • 1
  • 2

接入 syslog

首先在 webui 创建 input:

图 11.2

以 rsyslog 为例:

/etc/rsyslog.d/graylog.conf:

*.* @@x.x.x.x:514;RSYSLOG_SyslogProtocol23Format
  • 1
  • 1

service rsyslog restart

即可查看该 input 的 message:

图 11.3

GELF (http 为例)

GELF (Graylog Extended Log Format) 可以接收结构化的事件, 支持压缩(GZIP’d or ZLIB’d)和分块。

GELF message:

  • version string
  • host string
  • short_message string
  • full_message string
  • timestamp number
  • level number
  • facility string
  • line number
  • file string
  • _[additional field] string or number, 通过 _ 前缀添加自定义的字段

新建一个 GELF HTTP input:

图 11.6

推送日志:

curl -XPOST http://106.75.62.142:12201/gelf -p0 -d '{"message":"这是一条消息", "host":"172.3.3.3", "facility":"test", "topic": "meme"}'

查看推送的日志:

图 11.7

收集服务日志( nodejs 为例)

log4js, bunyan, winston 等等 nodejs 日志框架都可以, 这里我们以 bunyan 为例, 因为 bunyan 可以将日志以 json 的形式打印。

const express = require('express');
const bodyParser = require('body-parser');
const bunyan = require('bunyan');

const log = bunyan.createLogger({
    name: 'server-bunyan',
    level: 'debug',
    streams: [{
        type: 'rotating-file',
        path: '/data/logs/server-bunyan.log',
        period: '1d',
        count: 3
    }]
});


const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/hello', (req, res) => {
    log.info({                                           
        query: req.query
    }, 'hello');
    res.send('hello world');
});

app.listen(5004, '0.0.0.0', () => {
    log.info('app listening on 5004');
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

rsyslog:

 module(load="imfile" PollingInterval="10")

 # input
 input(type="imfile" File="/data/logs/server.log" Tag="server" ruleset="push_remote")
 input(type="imfile" File="/data/logs/detail.log" Tag="detail" ruleset="push_remote")
 input(type="imfile" File="/data/logs/server-bunyan.log" Tag="bunyan_server" ruleset="push_remote")

 # template
 template(name="mytpl" type="string" string="node1 %programname% %msg%\n" )                          

 # output
 ruleset(name="push_remote") {
   action(
     type="omfwd"
     protocol="tcp"
     target="x.x.x.x"
     port="515"
     template="mytpl"

     action.resumeRetryCount="-1"
     action.resumeInterval="1"

     queue.filename="push-remote"
     queue.size="100000"
     queue.highwatermark="60000"
     queue.lowwatermark="2000"
     queue.maxdiskspace="100g"
     queue.saveonshutdown="on"
     queue.type="LinkedList"
     queue.maxfilesize="128m"
   )
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

新建 input, 监听 515 端口,这里我们体验一下 graylog 的 Extractor,给改 input 添加一个 Extractor:

图 11.5

我们加了一个抓取器,来提取 node, topic 两个字段。

在 webui 查看该 input 的 message:

图 11.4

Alerts

Graylog 内置的告警条件:

  • 消息数量
  • 字段值(number)
  • 字段内容

内置告警方式:

  • Email
  • HTTP 回调

体验一下 HTTP 回调。

新建一个 Stream, 进入 manager alerts, 新建一个告警条件:

图 11.8

创建一个 HTTP 回调:

图 11.9

告警以 post 方式请求回调, 请求的 body 内容:

{
    "check_result": {
        "result_description": "Stream had 0 messages in the last 1 minutes with trigger condition less than 10 messages. (Current grace time: 1 minutes)",
        "triggered_condition": {
            "id": "6bacc1c1-1eac-49f9-9ac8-998ea851f101",
            "type": "message_count",
            "created_at": "2017-01-17T05:25:13.592Z",
            "creator_user_id": "admin",
            "title": "日志一分钟内少于10条",
            "parameters": {
                "grace": 1,
                "threshold_type": "less",
                "threshold": 10,
                "time": 1,
                "backlog": 0
            }
        },
        "triggered_at": "2017-01-17T05:44:11.921Z",
        "triggered": true,
        "matching_messages": []
    },
    "stream": {
        "creator_user_id": "admin",
        "outputs": [],
        "alert_receivers": {
            "emails": [
                "dongsoso@hotmail.com"
            ],
            "users": [
                "dongsoso@hotmail.com"
            ]
        },
        "matching_type": "AND",
        "description": "alert",
        "created_at": "2017-01-17T05:21:58.852Z",
        "disabled": false,
        "rules": [],
        "alert_conditions": [
            {
                "creator_user_id": "admin",
                "created_at": "2017-01-17T05:25:13.592Z",
                "id": "6bacc1c1-1eac-49f9-9ac8-998ea851f101",
                "type": "message_count",
                "title": "日志一分钟内少于10条",
                "parameters": {
                    "grace": 1,
                    "threshold_type": "less",
                    "threshold": 10,
                    "time": 1,
                    "backlog": 0
                }
            }
        ],
        "id": "587da9f62ab79c0001352b7a",
        "title": "test",
        "content_pack": null
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

查看告警历史:

图 11.10

更多更好用的功能等待发现…

原文

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多