Professional Documents
Culture Documents
1
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tính năng
Prometheus sử dụng mã nguồn Go nên có thể dễ dàng build với các file binary, nó lấy
metric của device cực kỳ nhanh và nhẹ. Một số tính năng chính mình xin trích dẫn lại từ
trang chủ của Prometheus:
A multi-dimensional data model with time series data identified by metric name and
key/value pairs
PromQL, a flexible query language to leverage this dimensionality
No reliance on distributed storage; single server nodes are autonomous
Time series collection happens via a pull model over HTTP
Pushing time series is supported via an intermediary gateway
Targets are discovered via service discovery or static configuration
Multiple modes of graphing and dashboarding support
Kiến trúc
2
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Nguồn: https://prometheus.io
Grafana là gì?
Là một nền tảng để xây dựng các analytics và monitoring. Nói nôm na cho mọi người dễ
hình dung là sau khi lấy được metric từ các thiết bị, grafana sẽ sử dụng metric đó để phân
tích và tạo ra dashboard mô tả trực quan các metric cần thiết cho việc monitoring ví dụ
như cpu, ram, dish, network, iops, session.
Việc xây dựng Dashboard nó là một phần quan trọng trong việc monotor của hệ thống.
Grafana support rất nhiều giải pháp monitor khác nhau.
3
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Nguồn: https://grafana.com/
2.2.Chuẩn bị
CentOS 7
4
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
vim /etc/sysconfig/selinux
Change “SELINUX=enforcing” to “SELINUX=disabled”.
Save file lại và reebot OS.
mv prometheus-2.10.0.linux-amd64 /usr/local/prometheus/
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file /usr/local/prometheus/prometheus.yml \
--storage.tsdb.path /usr/local/prometheus/ \
--web.console.templates=/usr/local/prometheus/consoles \
--web.console.libraries=/usr/local/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
6
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
7
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
8
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Nếu các bạn chỉ đơn thuần download các agent này cài đặt vào máy chủ Windows thì
bạn chỉ có thể thu thập metric ở mức cơ bản, như hình ở bên trên. Dưới dây là hướng
dẫn bạn thu thập metric nâng cao cho service Windows. Ví dụ như: AD, DNS, SQL, IIS, …
Khi cài agent qua CMD sẽ giúp các bạn chọn lọc thu thập những loại metric nào cần thu
thập, giảm các loại metric không cần thiết, có thể thu thập nhiều loại metric hơn mặc
định.
Mở port 9182 trên server linux, và windows server.
Bước 2: Mở CMD với quyền Administrator.
Bước 3: Chạy comment msiexec với cú pháp như sau:
msiexec /i C:\wmi_exporter-0.9.0-amd64.msi
ENABLED_COLLECTORS="ad,cpu,cs,logon,memory,logical_disk,os,service,system,proce
ss,tcp,net,textfile,thermalzone"
Bước 5: Kiểm tra metric bạn truy cập như sau: http://ipserver:9182
9
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Bước 6: Tạo job trong prometheus để giám sát server Windows này với nội dung job
sau:
nano /usr/local/prometheus/prometheus.yml
Nội như sau:
- job_name: 'windows'
static_configs:
- targets: ['10.10.10.8:9182']
labels:
hostname: DC01
type: windows
company: LDG
Tại đây mình thêm trường labels vào để dễ dàng filter metric.
10
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tương tự nếu bạn muốn add nhiều server windows trên cùng 1 job, bạn chỉ cần copy
đoạn target và edit lại thông tin.
11
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tại đây bạn có thể chọn New Dashboard để tạo mới (làm họa sĩ vẽ dashboard) hoặc
Import (sử dụng những dashboard có sẳn rồi edit lại cho phù hợp).
12
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Sau khi chọn file cần import bạn cần chú ý các tham số sau:
Name : tên của dashboard.
Folder : thư mục chứa dashboard.
UID : số định danh của dashboard, nếu số này trùng thì khi bạn import dashboard củ sẽ
bị ghi đè.
Đây là kết quả sau khi bạn import dashboard thành công
13
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
14
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tiếp tục giải nén file vừa download và chuyển nó đến đường dẫn /usr/local/go
tar -xvzf go1.12.5.linux-amd64.tar.gz
mv go /usr/local
Thiết lập các biến môi trường GOROOT và GOPATH
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Kiểm tra version GoLang và verify lại cấu hình
go version
go env
Enable GoLang tự khởi chạy sau khi restart, thêm dòng lệnh thiết lập các biến môi
trường vào .bashrc
vi .bashrc
Bỏ đoạn export vào cuối file và save lại.
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Bước 02: Build SNMP Exporter Config Generator
SNMP Exeporter information links: https://github.com/prometheus/snmp_exporter
Cài đặt các package cần thiết.
sudo yum install git zip unzip gcc gcc-g++ make net-snmp net-snmp-utils net-snmp-libs
net-snmp-devel # RHEL-based distros
Sử dụng go get để download generator về
go get github.com/prometheus/snmp_exporter/tree/master/generator
Thực hiện build mibs từ tool generator, đối với những loại mibs ko có sẵn trong này,
chúng ta phải download từ hãng về và bỏ vào tool.
cd ${GOPATH-$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
go build
15
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
make mibs
Mibs của những thiết bị sẽ được download và copy tới đường dẫn sau ${GOPATH-
$HOME/go}/src/github.com/prometheus/snmp_exporter/generator
16
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Mibs thì các bạn download như sau: Login vào Fortigate, System, SNMP
Ngoài ra cũng có thể tìm trên mạng với những mib của các thiết bị khác.
Bước 04: Enable snmp trên Fortigate. Để enable snmp chúng ta login vào Fortigate,
SNMP, sau đó create snmp v2 (mình ko sử dụng snmp v3) như hình sau:
Community name :là tên mà sẽ khai báo trong file gernagator.yml
IP address: là IP của prometheus server.
17
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
18
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Bước 05: Tạo edit file generator.yml. Mặc định file generator.yml có rất nhiều thông tin
trong này, chúng ta khuyến khị chỉ sử dụng những thông tin liên quan đến thiết bị cần
monitor.
edit file vi generator.yml
modules:
fortigate_snmp:
walk:
- ifXTable
- fgVpn
- fgSystem
- fgIntf
version: 2
max_repetitions: 25
retries: 3
timeout: 10s
auth:
community: Communityname
19
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
wget
https://github.com/prometheus/snmp_exporter/releases/download/v0.15.0/snmp_e
xporter-0.15.0.linux-amd64.tar.gz
Giải nén và copy source đến thư mục /usr/local/snmp_exporter
tar -xvzf snmp_exporter-0.15.0.linux-amd64.tar.gz
mv snmp_exporter* /usr/local/snmp_exporter
Tạo service trong systemd cho snmp_exporter
vi /etc/systemd/system/snmp_exporter.service
Nôi dung file snmp_exporter.service như sau:
[Unit]
Description=Snmp_exporter
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/snmp_exporter/snmp_exporter \
--config.file=/usr/local/snmp_exporter/snmp.yml
[Install]
WantedBy=multi-user.target
Bước 07: Copy file snmp.yml đến thư mục source của snmp_exporter
cp -R /root/go/src/github.com/prometheus/snmp_exporter/generator/snmp.yml
/usr/local/snmp_exporter/
Restart và enable service
systemctl restart snmp_exporter.service
20
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
21
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
device: fortigate
company: LDG
scrape_interval: 3m
scrape_timeout : 3m
metrics_path: /snmp
params:
module: [fortigate_snmp]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 10.10.10.26:9116 # SNMP exporter.
Restart prometheus service
service prometheus restart
Kiểm tra lại kết quả:
22
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tương tự với giám sát Fortigate, chúng ta sẽ tiến hành cấu hình
snmp_exporter giám sát thiết bị Cisco.
Bước 01. Tương tự chúng ta sẽ add thêm config vào file generator.yml
cisco:
# walk: [sysUpTime, interfaces, ifXTable]
walk:
- 1.3.6.1.2.1.2.2.1.1
- 1.3.6.1.2.1.2.2.1.2
- 1.3.6.1.2.1.2.2.1.10
- 1.3.6.1.2.1.2.2.1.13
- 1.3.6.1.2.1.2.2.1.14
- 1.3.6.1.2.1.2.2.1.16
- 1.3.6.1.2.1.2.2.1.19
- 1.3.6.1.2.1.2.2.1.2
- 1.3.6.1.2.1.2.2.1.20
- 1.3.6.1.2.1.2.2.1.5
- 1.3.6.1.2.1.2.2.1.7
23
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
- 1.3.6.1.2.1.2.2.1.8
- 1.3.6.1.2.1.31.1.1.1.1
- 1.3.6.1.2.1.31.1.1.1.18
- 1.3.6.1.4.1.9.9.48.1.1.1.5
- 1.3.6.1.4.1.9.9.48.1.1.1.6
- 1.3.6.1.4.1.9.2.1
- 1.3.6.1.2.1.1.5
lookups:
- source_indexes: [ifIndex]
lookup: ifAlias
- source_indexes: [ifIndex]
lookup: ifDescr
- source_indexes: [ifIndex]
# Use OID to avoid conflict with Netscaler NS-ROOT-MIB.
lookup: 1.3.6.1.2.1.31.1.1.1.1 # ifName
overrides:
ifAlias:
ignore: true # Lookup metric
ifDescr:
ignore: true # Lookup metric
ifName:
ignore: true # Lookup metric
ifType:
type: EnumAsInfo
version: 2
max_repetitions: 25
retries: 3
24
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
timeout: 10s
auth:
community: ldgsnmpmonitor
Trong đó các dãy số này là :
- 1.3.6.1.2.1.2.2.1.1
- 1.3.6.1.2.1.2.2.1.2
OID. Thông tin về OID kiểm tra tại đây .
https://cric.grenoble.cnrs.fr/Administrateurs/Outils/MIBS/
version: 2
Tương ứng với snmp v2
Bước 02: Sau đó export mibs và generate ra file snmp.yml
Bước 03: Copy file snmp.yml đến thư mục source của snmp_exporter
Bước 04: Restart và enable service
systemctl restart snmp_exporter.service
systemctl status snmp_exporter.service
systemctl enable snmp_exporter.service
Bước 05: Truy cập vào https:ipprometheus:9116 và điền các thông tin sau:
25
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
26
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
scrape_timeout : 3m
metrics_path: /snmp
params:
module: [cisco]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 10.10.10.26:9116 # SNMP exporter.
Bước 06: Restart service và kiểm tra lại kết quả:
27
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
28
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
vi /usr/local/etc/rc.d/node_exporter
Tìm đến các tham số sau và chỉnh lại như bên dưới:
: ${node_exporter_enable:="YES"}
: ${node_exporter_user:="root"}
: ${node_exporter_group:="root"}
Bước 04: Enable và start node_exporter
/usr/local/etc/rc.d/node_exporter enabled
/usr/local/etc/rc.d/node_exporter start
Kết quả sau khi cài đặt và cấu hình.
Bước 05: Tạo job trong prometheus để giám sát Pfsense với nội dung job sau:
- job_name: 'pfsense'
static_configs:
- targets: ['10.10.10.54:9100']
labels:
hostname: LDG-VPN-01
type: pfsense
company: LDG
Bước 06: Restart service và kiểm tra lại kết quả
Nhớ phải mở port 9100 cho node_exporter nhé.
29
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
30
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
[Service]
User=root
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=default.target
Bước 04: Restart và enable service
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
Bước 05: Truy cập vào node_exporter để kiểm tra
Trước khi truy cập bạn cần phải mở port 9100.
31
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Bước 06: Tạo job trong prometheus để giám sát server Linux này với nội dung job sau:
- job_name: 'linux'
static_configs:
- targets: ['172.16.111.35:9100']
labels:
hostname: Jitsi-Meet
type: linux
company: LDG
Bước 07: Restart lại prometheus service và kiểm tra kết quả
32
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
33
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Sau khi cài đặt xong, đường dẫn lưu trử tại đây
/usr/local/lib/python3.6/site-packages/vmware_exporter
Bước 03: Tạo user read-only dùng để monitor trên vcenter hoặc ESXi host.
34
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
vsphere_user: 'monitor'
vsphere_password: 'password'
ignore_ssl: True
specs_size: 5000
collect_only:
vms: True
vmguests: False
datastores: True
hosts: True
snapshots: True
Nếu bạn có nhiều vcenter hoặc esxi host thì bạn có thể add thêm nhiều esxi như
mình highlight ở trên.
Bước 05: Tạo service trong systemd cho vmware_exporter
vi /etc/systemd/system vmware_exporter.service
Nội dung file như sau:
Unit]
Description=Prometheus VMWare Exporter
After=network.target
[Service]
User=root
Group=root
ExecStart=/usr/bin/python3 /usr/local/bin/vmware_exporter -c
/usr/local/lib/python3.6/site-packages/vmware_exporter/config.yml
Type=simple
[Install]
35
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
WantedBy=multi-user.target
- job_name: 'ESXI02'
36
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
metrics_path: '/metrics'
static_configs:
- targets:
- 10.10.10.41
labels:
hostname: ESXI02
device: VMWARE
company: XXX
params:
section: [esxi02]
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 10.10.10.26:9272
Bước 08: Restart prometheus service và kiểm tra lại kết quả
Tại đây bạn phải nhớ mở port 9272 nhé.
37
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
38
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
39
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml
[Install]
WantedBy=multi-user.target
Enable và start service
systemctl daemon-reload
systemctl enable alertmanager
systemctl restart alertmanager
Bước 04: Cấu hình alert trong prometheus
vi /usr/local/prometheus/prometheus.yml
Tại targets: nhập thông tin ip, port , tại đây chính là ip của prometheus server và port
9093 (alertmanager dùng port này).
Bước 05: Tạo các rule alert trong prometheus. Để tạo alert trong prometheus, bạn cần
phải định nghĩa ra các rule. Tạo các file rule trong thư mục promethues với format sau:
rulename.yml
40
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
41
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
################ ServiceStatus
42
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
- alert: ServiceStatus
expr: wmi_service_status{status="ok"} != 1
for: 1m
labels:
severity: error
annotations:
summary: "Service Status (instance {{ $labels.instance }})"
description: "Windows Service state is not OK\n VALUE = {{ $value }}\n LABELS: {{
$labels }}"
################ CollectorError
- alert: CollectorError
expr: wmi_exporter_collector_success == 0
for: 1m
labels:
severity: error
annotations:
summary: "Collector Error (instance {{ $labels.instance }})"
description: "Collector {{ $labels.collector }} was not successful\n VALUE = {{ $value
}}\n LABELS: {{ $labels }}"
Bước 06: Tiếp tục chúng ta sẽ khai báo tên của rule đó vào cấu hình của prometheus
theo như hình bên dưới.
vi /usr/local/prometheus/prometheus.yml
43
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Bước 07: Kiểm tra lại rule đã tạo bằng lệnh sau
./promtool check config prometheus.yml
Bước 08: Restart prometheus service và kiểm tra lại kết quả:
44
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tương tự bạn sẽ tạo nhiều rule cho nhiều nhóm thiết bị khác nhau.
Bước 09: Ở bước này mình sẽ tạo 1 con telegram_bot sau đó gửi alert qua telegram.
Download source code và build file binary
cd ${GOPATH-$HOME/go}/src/github.com
go get github.com/inCaller/prometheus_bot
cd ${GOPATH-$HOME/go}/src/github.com/inCaller/prometheus_bot
make clean
make
Copy bỏ vào thư mục /usr/local để dễ dàng quản lý.
mv /root/go/src/github.com/inCaller/prometheus_bot /usr/local
Đăng nhập vào telegram để tạo telegram bot và group nhận alert.
Cách tạo telegram bot tham khảo tại đây
https://www.teleme.io/articles/create_your_own_telegram_bot?hl=vi
Khi tạo telegram bot mình sẽ ghi lại token của con bot
45
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Tiếp tục tạo group sau đó add bot vừa tạo vào, sử dụng telegram qua web bạn sẽ lấy dc
tham số -chat-id
Chat-id chính là : g364942581
46
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
vi /usr/local/prometheus_bot/config.yaml
Nội dung file config.yaml như sau:
telegram_token: "997872129:AAEPKYz3nPwmFsgq6ao-MdPsC5fy5z376GQ"
# ONLY IF YOU USING TEMPLATE required for test
template_path: "template.tmpl"
time_zone: "Asia/Ho_Chi_Minh"
split_token: "|"
# ONLY IF YOU USING DATA FORMATTING FUNCTION, NOTE for developer: important or
test fail
time_outdata: "02/01/2006 15:04:05"
split_msg_byte: 4000
Tiếp tục cấu hình alert manager
vi /usr/local/alertmanager/alertmanager.yml
thay đổi tham số url như sau: http://ipalertmanager:9087/alert/-chat_id
47
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
48
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Copy templet ra ngoài thư mục prometheus_bot và đổi tên thành templet.tmpl
cp production_example.tmpl /usr/local/prometheus_bot/template.tmpl
Tiếp tục edit lại tên của template trong config.yaml của prometheus_bot
49
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
Kết quả
50
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG
51