You are on page 1of 51

CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

Danh mục nội dung


Prometheus là gì? ......................................................................................................................................... 1
Grafana là gì? ................................................................................................................................................ 3
Hướng dẫn cài đặt và sử dụng Prometheus + Grafana................................................................................. 4
2.1. Mô hình triển khai......................................................................................................................... 4
2.2. Chuẩn bị ........................................................................................................................................ 4
2.3. Cài đặt chung ................................................................................................................................ 4
2.4. Cài đặt Promethues....................................................................................................................... 5
2.5. Cài đặt Grafana ............................................................................................................................. 7
2.6. Cài wmi_exporter giám sát tài nguyên và các dịch vụ trên Windows Server ............................... 7
2.7. Cài snmp_exporter để giám sát thiết bị mạng thông qua SNMP................................................ 14
2.8. Cài node_exporter giám sát Linux server ................................................................................... 28
2.8.1. Giám sát FreeBDS (Pfsense firewall) bằng node_exporter ................................................. 28
2.8.2. Giám sát Centos, Ubuntu bằng node_exporter .................................................................. 30
2.9. Cài đặt vmware_exporter giám sát ESXI host. ............................................................................ 33
2.10. Cấu hình Alert trong prometheus ........................................................................................... 39
2.11. . Update soon .......................................................................................................................... 51

Tài Liệu Prometheus + Grafana


Trong tài liệu này mình muốn hướng dẫn các bạn xây dựng một
hệ thống Monitor systems miễn phí trong nội bộ dựa vào Open-
Source.
Prometheus là gì?
Prometheus là một open-source systems monitoring và alerting ban đầu được xây dựng
tại SoundCloud. Vào năm 2012 nhiều công ty, tổ chức đã đứng ra bảo trợ cho Prometheus,
và project này cực kỳ và phát và có rất nhiều người dùng. Hiện tại nó không còn là một
project độc lập mà được phát triển bởi rất nhiều công ty khác nhau. Nó sử dụng mã nguồn
GoLang của google. Hiện tại thì Prometheus 100% là open source và có Apache 2 License
on GitHub.

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/

Hướng dẫn cài đặt và sử dụng Prometheus + Grafana

1. Bài toán doanh nghiệp

2. Hướng dẫn cài đặt Jitsi


2.1.Mô hình triển khai

2.2.Chuẩn bị
CentOS 7

2.3.Cài đặt chung


Update, sync NTP và disables selinux trên Centos 7
Chạy lệnh :
yum install update -y
ntpdate 1.ro.pool.ntp.org

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.

Cài iptables thay thế cho firewalld trên Centos 7


Remove firewalld trên Centos 7
systemctl stop firewalld
systemctl disable firewalld
systemctl mask --now firewalld
Cài đặt Iptables thay thế cho firewalld
yum install iptables-services -y
systemctl start iptables
systemctl enable iptables
Mở các port sau:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3000 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9090 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9116 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9100 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9182 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9093 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 9087 -j ACCEPT
2.4. Cài đặt Promethues
Download here: https://prometheus.io/download/
wget
https://github.com/prometheus/prometheus/releases/download/v2.10.0/prometheu
s-2.10.0.linux-amd64.tar.gz
tar -xvzf prometheus-2.10.0.linux-amd64.tar.gz
5
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

mv prometheus-2.10.0.linux-amd64 /usr/local/prometheus/

Tạo service systemd cho prometheus.


vim /etc/systemd/system/prometheus.service
Đoạn config của file prometheus.service như sau:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[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

Khởi động và enanble prometheus services


systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus

6
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

Truy cập vào web của promethues: http://ip:9090

2.5.Cài đặt Grafana


Link download https://grafana.com/grafana/download
wget https://dl.grafana.com/oss/release/grafana-6.2.1-1.x86_64.rpm
Cài đặt grafana bằng lệnh sau:
sudo yum localinstall grafana-6.2.1-1.x86_64.rpm
Khởi động và enable grafana service
sudo service grafana-server start
sudo /sbin/chkconfig --add grafana-server
systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
sudo systemctl enable grafana-server.service
Truy cập grafana với link sau: http://ip:3000
2.6.Cài wmi_exporter giám sát tài nguyên và các dịch vụ trên Windows Server
Để giảm sát được các server Windows, chúng ta cần phải cài đặt Wmi Exporter (Agent
collectors) để Promethues có thể thu thập được metric từ các server Windows này.

7
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

Bước 1: Download wmi_exporter

8
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

Link download Agent: https://github.com/martinlindhe/wmi_exporter/releases


Có 2 phiên bản Agent:
wmi_exporter.exe (click to run, dành cho các bạn nào chỉ cần các metric được enable
sẵn).
wmi_exporter.msi (dùng để cài đặt thông qua CMD, enable các tính năng thu thập
metric nâng cao).

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"

Trong đó C:\wmi_exporter-0.9.0-amd64.msi là đường dẫn chứa file wmi_exporter.msi.


ENABLED_COLLECTORS=”các loại metric cần thu thập, tên của loại metric trong hình
bên trên”
Tùy theo từng loại windows server mà lựa chọn metric cho phù hợp.
Bước 4: Kết quả sau khi cài đặt xong:

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.

Bước 7: Restart serivce prometheus


systemctl restart prometheus
systemctl status prometheus
Bước 8: Kiểm tra lại host được thêm vào prometheus thành công hay không.
Truy cập vào Prometheus sau đó chọn Status, target để kiểm tra.

Bước 9: Tạo Dashboard monitor Windows server trên Grafana.


Truy cập vào Grafana, sau đó chọn Manage Dashboards như hình:

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).

Tiếp tục chọn vào Upload .json file để import dashboard

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

Các bạn có thể sử dụng dashboard đã edit của mình


https://drive.google.com/drive/folders/1cChzXeqjfCdiUyABYExb0ZYdk6buyfef.
Hoặc download các dashboard tùy ý thích tại đây
https://grafana.com/grafana/dashboards?dataSource=prometheus&direction=asc&ord
erBy=name&search=wmi

Link tham khảo:


https://github.com/martinlindhe/wmi_exporter
2.7.Cài snmp_exporter để giám sát thiết bị mạng thông qua SNMP
Để giám sát các thiết bị mạng, các hệ thống khác sẽ dựa vào snmp để thu thập metric.
Prometheus cũng không ngoại lệ, nó sử dụng snmp_exporter để thu thập metric trên
các thiết bị mạng. Do vậy suy ra những gì có snmp thì promethues sẽ thu thập metric
được.
Để snmp_exporter hoạt động được, chúng ta cần phải file snmp.yml. snmp_exporter
cung cấp cho chúng ta 1 tool để tạo ra file snmp.yml này. Nội dung trong file snmp.yml
này chính là các tham số liên quan đến mibs của thiết bị mà tool snmp_exporter
generator tạo ra.
Bước 01: Chúng ta cần phải install GoLang để build SNMP Exporter Config Generator
Link download tại đây https://golang.org/dl/
Chúng ta sử dụng wget để download golang về
wget https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz

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

Guide Here: https://grafana.com/dashboards/7567


Bước 03: Build file snmp.yml để có thể giám sát thiết bị mạng (tại đây chúng ta giám sát
thiết bị firewall Fortigate)
Tại đây đã download mib của fortigate về và bỏ vào đường dẫn ${GOPATH-
$HOME/go}/src/github.com/prometheus/snmp_exporter/generator/mibs

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

Sau đó export mibs và generate ra file snmp.yml


export MIBDIRS=/usr/share/snmp/mibs
./generator generate
 Như vậy chúng ta đã có file snmp.yml

Bước 06: Cài đặt smnp_exporter lên prometheus server


Link download tại đây: https://github.com/prometheus/snmp_exporter/releases
Sử dụng comment wget để download trực tiếp

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

systemctl status snmp_exporter.service


systemctl enable snmp_exporter.service
Bước 08: Kiểm tra metric
Snmp_exporter chạy trên port 9116 vì vậy chúng ta cần phải mở port này trên
promethues server.
Truy cập vào https:ipprometheus:9116 và điền các thông tin sau:
Target: là ip của fortigate
Module: chính là module mà chúng ta tạo bằng generator cho fortigate

 Nếu thấy có metric là OK.


Bước 09: Tạo job trong prometheus để giám sát Fortigate với nội dung job sau:
vi /usr/local/prometheus/prometheus.yml
####### Fortigate ########
- job_name: 'fortigate'
static_configs:
- targets:
- 172.16.100.1 # fortigate device.
labels:
hostname: LDGGROUP-FG

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ả:

Bước 10: Login vào grafana và import dashboard.

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

Target: là ip của cisco


Module: chính là module mà chúng ta tạo bằng generator cho cisco
 Nếu xuất ra metric là OK.
Bước 06: Tạo job trong prometheus để giám sát Fortigate với nội dung job sau:
vi /usr/local/prometheus/prometheus.yml

Nội dung như sau:


- job_name: 'cisco'
static_configs:
- targets: ['10.10.10.101']
# Access switch.
labels:
hostname: A-SW1
device: cisco
company: XXX
scrape_interval: 3m

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ả:

Bươc 07: Đăng nhập vào Grafana để import dashboard

27
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

2.8. Cài node_exporter giám sát Linux server


2.8.1. Giám sát FreeBDS (Pfsense firewall) bằng node_exporter
Pfsense là firewall mã nguồn mở chạy trên nền tảng FreeBSD. Chính vì thế chúng ta sẽ
cài đặt node_exporter trên Pfsense để có thể giám sát được nó.
Bước 01: SSH vào Pfsense, chọn 8 để vào shell

Bước 02: Cài đặt node_exporter


pkg add http://pkg.freebsd.org/FreeBSD:11:amd64/release_2/All/node_exporter-
0.15.2.txz
Bước 03: Hiệu chỉnh các service trong node_exporter

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

Bước 07: Đăng nhập vào grafana tạo dashboard

2.8.2. Giám sát Centos, Ubuntu bằng node_exporter


Bước 01: Download node_exporter trên Server cần giám sát.
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-
rc.1/node_exporter-1.0.0-rc.1.linux-amd64.tar.gz
Bước 02: Giải nén source code và copy đến đường dẫn sau /usr/local/node_exporter
tar -xvzf node_exporter-1.0.0-rc.1.linux-amd64.tar.gz
mkdir /usr/local/node_exporter
mv node_exporter-1.0.0-rc.1.linux-amd64/node_exporter /usr/local/node_exporter
Bước 03: Tạo service trong systemd cho node_exporter
vim /etc/systemd/system/node_exporter.service
Nội dung trong file như sau:
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

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ả

Bước 08: Đăng nhập vào grafana tạo dashboard

32
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

2.9. Cài đặt vmware_exporter giám sát ESXI host.


Để giám sát các ESXi host hoặc Vcenter chúng ta cần cài vmware_exporter trên
prometheus server.
https://github.com/pryorda/vmware_exporter
vmware_exporter yêu cầu pip3 hoặc python3
Bước 01: Cài đặt python3 và pip3
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
tar -xJf Python-3.6.4.tar.xz
cd Python-3.6.4
./configure
make
make install
pip3 install --upgrade pip
Bước 02: Cài đặt vmware_exporter
pip3 install vmware_exporter

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.

Bước 04: Tạo file cấu hình cho vmware_exporter


cd /usr/local/lib/python3.6/site-packages/vmware_exporter
vi config.yml
Nội dung file config như sau:
default:
vsphere_host: 10.10.10.43
vsphere_user: 'monitor'
vsphere_password: 'password
ignore_ssl: True
specs_size: 5000
collect_only:
vms: True
vmguests: False
datastores: True
hosts: True
snapshots: True
esxi02:
vsphere_host: 10.10.10.41

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

Bước 06: Enable và start service


systemctl enable vmware_exporter.service
systemctl start vmware_exporter.service
Bước 07: Tạo job trong prometheus để giám sát Vmware. Đối với vmware để lấy dc
metric của nhiều host ESXi thì chúng ta phải tạo nhiều job. Bạn nào có cách khác hay
hơn thì chỉ lại mình nhé. Nội dung job như sau:
- job_name: 'ESXI03'
metrics_path: '/metrics'
static_configs:
- targets:
- 10.10.10.43
labels:
hostname: ESXI03
device: VMWARE
company: XXX
params:
section: [default]
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

- 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

Chọn vào 1 job để kiểm tra metric.

Bước 09: Đăng nhập vào grafana tạo dashboard

38
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

2.10. Cấu hình Alert trong prometheus


Bước 01: Download alert_manager về
wget
https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertman
ager-0.20.0.linux-amd64.tar.gz
Bước 02: Giải nén và copy vào thư mục /usr/local để dễ dàng quản lý
tar -xvzf alertmanager-0.20.0.linux-amd64.tar.gz
mv alertmanager-0.20.0.linux-amd64 /usr/local/alertmanager
Bước 03: Tạo service trong systemd cho alertmanager
vi /etc/systemd/system/alertmanager.service
Nội dung file service như sau:
[Unit]
Description=AlertManager
Wants=network-online.target
After=network-online.target

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

Nội dung file rule tùy các bạn định nghĩa.


Tại đây mình tạo 1 rule alert cho windows host: windows-rule.yml
vi /usr/local/prometheus/windows-rules.yml
Nội dung file rule như sau:
############# Define Rule Alert ###############
# my global config
############# Define Rule Alert ###############
groups:
- name: Windows-alert
rules:

################ Memory Usage High


- alert: Memory Usage High
expr: 100*(wmi_os_physical_memory_free_bytes) / wmi_cs_physical_memory_bytes
> 90
for: 1m
labels:
severity: warning
annotations:

41
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

summary: "Memory Usage (instance {{ $labels.instance }})"


description: "Memory Usage is more than 90%\n VALUE = {{ $value }}\n LABELS: {{
$labels }}"

################ CPU Usage High


- alert: Cpu Usage High
expr: 100 - (avg by (instance) (irate(wmi_cpu_time_total{mode="idle"}[2m])) * 100) >
80
for: 1m
labels:
severity: warning
annotations:
summary: "CPU Usage (instance {{ $labels.instance }})"
description: "CPU Usage is more than 80%\n VALUE = {{ $value }}\n LABELS: {{
$labels }}"

################ Disk Usage


- alert: DiskSpaceUsage
expr: 100.0 - 100 * ((wmi_logical_disk_free_bytes{} / 1024 / 1024 ) /
(wmi_logical_disk_size_bytes{} / 1024 / 1024)) > 95
for: 1m
labels:
severity: error
annotations:
summary: "Disk Space Usage (instance {{ $labels.instance }})"
description: "Disk Space on Drive is used more than 95%\n VALUE = {{ $value }}\n
LABELS: {{ $labels }}"

################ 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

Thực hiện test với cú pháp sau:


export TELEGRAM_CHATID="-364942581"
make test
Kiểm tra thấy bot đã gửi được tin nhắn

48
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

Tiếp tục tạo templet gửi alert trong prometheus.


Trong thư mục testdata có nhiều templet cho bạn sử dụng, thường mình dùng templet
production_example.tmpl

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

Restart lại tất cả services đã cấu hình.


service prometheus_bot restart
service alertmanager restart
service prometheus restart

Kết quả

50
CÔNG TY CỔ PHẦN ĐẦU TƯ LDG

2.11. . Update soon

51

You might also like