Graylog 是一个简单易用、功能较全面的日志管理工具,相比 ELK 组合, 优点:
当然, 拓展性上比 ELK 差很多。
整套依赖:
Graylog
提供 graylog 对外接口, CPU 密集Elasticsearch
日志文件的持久化存储和检索, IO 密集MongoDB
只是存储一些 Graylog 的配置Graylog 最简单的架构:
Graylog 集群架构:
环境:centos 7.0 64位
Graylog 官方提供了 docker 镜像:
docker pull mongo:3
docker pull elasticsearch:2
docker pull graylog2/server:2.1.2-1
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"
docker-compose -f graylog.yml up -d
浏览器访问 http://x.x.x.x:9000
, 默认账户名和密码均为 admin
, 可得:
Graylog 配置文件默认装在容器内的 /usr/share/graylog/data/config/graylog.conf
, 可以通过 volume 挂载自己的配置。
其他如 log4j2
, journal
, plugin
也都在 /usr/share/graylog/data/config/
目录。
例如修改时区,只需要修改配置文件:
# Default is UTC
root_timezone = Asia/Shanghai
首先在 webui 创建 input:
以 rsyslog 为例:
/etc/rsyslog.d/graylog.conf
:
*.* @@x.x.x.x:514;RSYSLOG_SyslogProtocol23Format
service rsyslog restart
即可查看该 input 的 message:
GELF (Graylog Extended Log Format) 可以接收结构化的事件, 支持压缩(GZIP’d or ZLIB’d)和分块。
GELF message:
string
string
string
string
number
number
string
number
string
string
or number
, 通过 _
前缀添加自定义的字段新建一个 GELF HTTP
input:
推送日志:
curl -XPOST http://106.75.62.142:12201/gelf -p0 -d '{"message":"这是一条消息", "host":"172.3.3.3", "facility":"test", "topic": "meme"}'
查看推送的日志:
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');
});
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"
)
}
新建 input, 监听 515 端口,这里我们体验一下 graylog 的 Extractor,给改 input 添加一个 Extractor:
我们加了一个抓取器,来提取 node
, topic
两个字段。
在 webui 查看该 input 的 message:
Graylog 内置的告警条件:
内置告警方式:
体验一下 HTTP 回调。
新建一个 Stream, 进入 manager alerts
, 新建一个告警条件:
创建一个 HTTP 回调:
告警以 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": [
"[email protected]"
],
"users": [
"[email protected]"
]
},
"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
}
}
查看告警历史:
更多更好用的功能等待发现…
原文