Graylog 是一個簡單易用、功能較全面的日誌管理工具,相比 ELK 組合, 優勢:node
固然, 拓展性上比 ELK 差不少。git
整套依賴:github
Graylog
提供 graylog 對外接口, CPU 密集Elasticsearch
日誌文件的持久化存儲和檢索, IO 密集MongoDB
只是存儲一些 Graylog 的配置Graylog 最簡單的架構:web
Graylog 集羣架構:docker
環境:centos 7.0 64位express
Graylog 官方提供了 docker 鏡像:json
docker pull mongo:3 docker pull elasticsearch:2 docker pull graylog2/server:2.1.2-1
docker-compose 拉起服務:centos
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
api
瀏覽器訪問 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": [ "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 } }
查看告警歷史:
更多更好用的功能等待發現...