Alex Liang

使用ELK + Metricbeat收集MongoDB Log

最近研究如何使用ELK(Elasticsearch, Logstash和Kibana)監控MongoDB的效能,之前公司己經使用ELK監控爬蟲,這次試著擴大範圍並加入告警的通知。

安裝ELK

我們使用docker-elk安裝ELK stack

1
git clone [email protected]:deviantony/docker-elk.git

安裝SENTINL Plugin

Kibana需要安裝SENTINL這套plugin才能加上watcher的功能

修改kibana資料夾的Dockerfile,讓docker-compose在build的時候能安裝plugin

1
2
3
4
5
6
7
8
ARG ELK_VERSION

# https://github.com/elastic/kibana-docker
FROM docker.elastic.co/kibana/kibana-oss:${ELK_VERSION}

# Add your kibana plugins setup here
# Example: RUN kibana-plugin install <name|url>
RUN kibana-plugin install https://github.com/sirensolutions/sentinl/releases/download/tag-6.5.0-0/sentinl-v6.5.4.zip

SENTINL的版本建議和ELK一致,這裡可以找到所有版本的release

設定SENTINL

為了讓事件發生時能發出通知,需要設定如email、slack等平台的存取權限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---
## Default Kibana configuration from kibana-docker.
## from https://github.com/elastic/kibana-docker/blob/master/build/kibana/config/kibana.yml
#
server.name: kibana
server.host: "0"
elasticsearch.url: elasticsearch:9200
sentinl:
settings:
email:
active: true
host: "smtp.gmail.com"
user: "[email protected]"
port: 587
password: password
tls: true
timeout: 20000 # mail server connection timeout
slack:
active: true
token: [your token]
1
2
docker-compose build
docker-compose up

這裡以GMAIL和slack做為通知平台,官網有其它範例。

連上管理介面應該能看到左側多了SENTINL的欄位如下:

sentinl

使用Metricbeat收集log

Metricbeat用來收集後端服務的log,舉凡MongoDB, Apache, Docker和Kubernetes都有相關的模組可以使用

  • 安裝Metricbeat,一樣使用docker image
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh
docker stop metricbeat
docker rm -v metricbeat
docker run -d \
--name=metricbeat \
--hostname=
--volume="$(pwd)/metricbeat.yml:/usr/share/metricbeat/metricbeat.yml:ro" \
--volume="/var/run/docker.sock:/var/run/docker.sock:ro" \
--volume="/sys/fs/cgroup:/hostfs/sys/fs/cgroup:ro" \
--volume="/proc:/hostfs/proc:ro" \
--volume="/:/hostfs:ro" \
docker.elastic.co/beats/metricbeat:6.5.4 metricbeat -e -c metricbeat.yml
docker exec -d metricbeat metricbeat setup
  • 修改metricbeat config
    進入docker image環境,根目錄有metricbeat.reference.ymlmetricbeat.yml二個檔案,前者為各種服務的設定參考;後者為乾淨的設定檔。我們在metricbeat.yml加入mongodb和system的設定(你也可以自訂檔名)
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
metricbeat.modules:
#------------ MongoDB Module ------------------------
- module: mongodb
metricsets: ["dbstats", "status", "collstats", "metrics", "replstatus"]
period: 10s
enabled: true

# The hosts must be passed as MongoDB URLs in the format:
# [mongodb://][user:pass@]host[:port].
# The username and password can also be set using the respective configuration
# options. The credentials in the URL take precedence over the username and
# password configuration options.
hosts: ["localhost:27017"]

#============ System ===============================
- module: system
metricsets:
- cpu # CPU usage
- load # CPU load averages
- memory # Memory usage
- network # Network IO
- process # Per process metrics
- process_summary # Process summary
- uptime # System Uptime
- core # Per CPU core usage
- diskio # Disk IO
- filesystem # File system usage for each mountpoint
- fsstat # File system summary metrics
#- raid # Raid
#- socket # Sockets and connection info (linux only)
enabled: true
period: 1m
processes: [".*"]

# Configure the metric types that are included by these metricsets.
cpu.metrics: ["percentages"] # The other available options are normalized_percentages and ticks.
core.metrics: ["percentages"] # The other available option is ticks

這裡節錄module的部分,Kibana和Elasticsearch的host需要根據環境修改

  • 啓動 Metricbeat
1
sh docker_metricbeat.sh

Kibana dashboard

在瀏覽器進入kibana的介面,在Management/Index Patterns確認有metricbeat-*的index,將它設為default index。

Dashboard裡可以找到[Metricbeat System] Overview[Metricbeat MongoDB] Overview這二個dashboard,點進去應該能看到以下的畫面

mongodb

system

到這裡己經能看到系統的狀態,接下來要設定watcher讓狀況發生時能通知管理者。

參考:

Watching/Alerting on Real-Time Data in Elasticsearch Using Kibana and SentiNL

使用Elastic Stack(ELK)來監控MongoDB