Hướng dẫn sử dụng Grafana

1. Cài đặt trên Ubuntu 16.04

2. Một số khái niệm cơ bản trong Grafana

  • Data Source

  • Organization

  • User

  • Panel

  • Query Editor

  • Dashboard

3. Tham khảo

1. Cài đặt Grafana trên Ubuntu 16.04

Phần tiếp theo hướng dẫn cài đặt Grafana trên Ubuntu 16.04. Sử dụng quyền người dùng sudo hoặc người dùng root.

  • Cài đặt Grafana từ kho APT repository :

  • Thêm dòng sau vào file /etc/apt/sources.list:

    deb https://packagecloud.io/grafana/stable/debian/ stretch main

  • Thêm Package Cloud key, cho phép bạn cài đặt các gói đã kí:

    curl https://packagecloud.io/gpg.key | sudo apt-key add -

  • Update repository trên máy và cài đặt Grafana:

    apt-get update -y apt-get install grafana -y

  • Thông tin các gói cài đặt:

    • File bin: /usr/sbin/grafana-server

    • Cài đặt script init.d: /etc/init.d/grafana-server

    • Tạo file biến môi trường mặc định: /etc/default/grafana-server

    • File cấu hình cài đặt: /etc/grafana/grafana.ini

    • Dịch vụ có tên: grafana-server.service

    • Cấu hình mặc định thiết lập ghi log vào file: /var/log/grafana/grafana.log

    • Cấu hình mặc định xác định file sqlite3 db trong file: /var/lib/grafana/grafana.db lưu các thông tin về cấu hình của giao diện của grafana.

    • Cài đặt các file HTML/JS/CSS và các file grafana khác tại thư mục: /usr/share/grafana/

  • Sau khi cài đặt xong, khởi động dịch vụ grafana :

    systemctl daemon-reload systemctl start grafana-server systemctl status grafana-server

  • Chạy lệnh sau để khởi động grafana cùng hệ thống:

    systemctl enable grafana-server.service

  • Kiểm tra trên trình duyệt dịch vụ Grafana trên cổng 3000:

    Hướng dẫn sử dụng Grafana

  • Đăng nhập vào sử dụng username và password mặc định đều là admin.

    Hướng dẫn sử dụng Grafana

2. Một số khái niệm cơ bản trong Grafana:

Data Source

  • Grafana hỗ trợ nhiều storage backend khác nhau cho các dữ liệu thời gian thực. Các DS sau được hỗ trợ chính thức: Graphite, InfluxDB, OpenTSDB, Prometheus, Elasticsearch, CloudWatch.

  • Ngôn ngữ truy vấn và khả năng của mỗi DS là khác nhau. Tuy nhiên, bạn có thể kết hợp dữ liệu từ nhiều DS khác nhau trong cùng một Dashboard, nhưng mỗi Panel thuộc về một DS cụ thể thuộc về một tổ chức xác định

Organization

  • Grafana hỗ trợ nhiều tổ chức để hỗ trợ nhiều loại mô hình triển khai, bao gồm sử dụng một máy Grafana để cung cấp dịch vụ cho các tổ chức có tiềm năng không đáng tin cậy.

  • Trong nhiều trường hợp, Grafana sẽ được triển khai với một tổ chức.

  • Mỗi tổ chức có thể có một hoặc nhiều DS.

  • Tất của các Dashboard được làm chủ bởi một tổ chức xác định.

User

  • User là tên người dùng trong Grafana. Một user có thể thuộc về một hoặc nhiều tổ chức, và có thể được gán với các mức quyenf khác nhau thông qua các role.

Panel

  • Panel là khối trực quan hóa cơ bản trong Grafana. Mỗi Panel cung cấp một Query Editor (phụ thuộc vào DS được lựa chọn trong panel) cho phép bạn trích xuất trực quan hóa hoàn hảo để hiển thị trên Dashboard bằng cách sử dụng Query Editor.

  • Có nhiều tùy chọn để tạo kiểu và định dạng mà mỗi Panel hiển thị để cho phép tạo ra các hình ảnh hoàn hảo.

  • Các panel sau khi tạo có thể kéo, thả và sắp xếp lại trên Dashboard, và tùy chỉnh kích cỡ một cách dễ dàng thông qua sử dụng chuột.

  • Hiện có các kiểu panel: Graph, Singlestat, Dashlist, Table và Text, …

    Hướng dẫn sử dụng Grafana

  • Graph panel cho phép tạo đồ thị từ nhiều loại metric.

    Hướng dẫn sử dụng Grafana

  • Các panel khác như singlestat yêu cầu giảm một truy vấn đơn giản thành một số duy nhất

    Hướng dẫn sử dụng Grafana

  • Dashlist và Text là các panel đặc biệt mà không kết nối với bất kì DS nào.

  • Các panel cũng có thể được tạo một cách tự động hơn bằng cách sử dụng các biến Dashboard Templating trong cấu hình của panel. Phần này mình sẽ hướng dẫn sau.

Query Editor

  • Query Editor cho thấy khả năng của DS và cho phép truy vấn các metric mà nó chứa. Sử dụng Query Editor để dựng lên một hoặc nhiều truy vấn (cho một hoặc nhiều chuỗi dữ liệu) trong cơ sở dữ liệu thời gian thực. Panel sẽ liên tục cập nhật cho phép khám phá hiệu quả dữ liệu thời gian thực và tạo nên truy vấn hoàn hảo.

  • Ngoài ra, bạn có thể sử dụng Template variables trong Query Editor để tự truy vấn chúng. Điều này cung cấp một cách mạnh mẽ để khám phá dữ liệu tự động dựa trên các biến Templating được chọn từ Dashboard. (Phần này sẽ hướng dẫn thêm)

  • Grafana cho phép tham chiếu các truy vấn trong Queery Editor tại các dòng. Nếu bạn thêm truy vấn thứ 2 vào đồ thị, bạn có thể tham chiếu tới truy vấn thứ nhất bằng cách gõ #A. Đây là cách cung cấp sự hiệu quả và dễ dàng để xây dựng nên các truy vấn phức tạp.

    Hướng dẫn sử dụng Grafana

Dashboard

  • Dashboard là nơi mọi thứ kết hợp với nhau. Dashboard có thể được xem như là tập hợp của một hoặc nhiều Panel được tổ chức và sắp xếp thành một hoặc nhiều hàng.

  • Trên giao diện Dashboard, bạn có thể điều khiển chu kì thời gian cập nhật metric, sử dụng các Templating tương tác một cách tự động, sử dụng Annotations để tạo chú thích cho các sự kiện trên panel.

  • Dashboard (hoặc một panel cụ thể) có thể được chia sẻ dễ dàng bằng nhiều cách. Bạn có thể gửi link tới những người có thể đăng nhập được vào grafana.

3. Tham khảo

[1] Cài đặt: http://docs.grafana.org/installation/debian/

[2] Một số khái niệm: http://docs.grafana.org/guides/basic_concepts/


Page 2

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Trong bài viết này, mình sẽ hướng dẫn các bạn cách xây dựng một hệ thống monitor các dịch vụ trên Linux dựa trên Prometheus và Grafana.

Note: Đây là bài cộng tác giữa WikiVPS và CloudCraft. Cám ơn WikiVPS đã hỗ trợ trong quá trình thực hiện bài viết.

Giới thiệu sơ lược về Prometheus và Grafana

Prometheus

Prometheus là giải pháp monitor hệ thống (open source). Prometheus dùng các trình daemon cài sẵn trên các máy con để thu thập các thông tin cần thiết, giao tiếp với máy chủ quản lý monitor qua giao thức HTTP/HTTPs và lưu trữ data theo dạng time-series database (TSDB).

Prometheus có hỗ trợ một giao diện web đơn giản để cho các admin theo dõi thông tin hệ thống, HTTP API và Prometheus còn cung cấp một ngôn ngữ truy vấn rất mạnh (sẽ nói ở phần dưới). Tuy nhiên, phần lưu trữ dữ liệu của prometheus hiện vẫn chưa tốt lắm.

Grafana

Grafana là một giao diện/dashboard theo dõi hệ thống (opensource), hỗ trợ rất nhiều loại dashboard và các loại graph khác nhau để người quản trị dễ dàng theo dõi.

Grafana có thể truy xuất dữ liệu từ Graphite, Elasticsearch, OpenTSDB, Prometheus và InfluxDB. Grafana là một công cụ mạnh mẽ để truy xuất và biểu diễn dữ liệu dưới dạng các đồ thị và biểu đồ.

Hướng dẫn sử dụng Grafana
Mô hình ta sẽ thực hiện trong bài này

Trong bài này, ta sẽ thực hiện theo dõi 1 server chạy DB MySQL và thực hiện cài đặt máy chủ prometheus trên monitor host và dùng Grafana để biểu diễn dữ liệu cho người dùng.

Data sẽ được Prometheus trên Master scrape về từ node Slave và được lưu trữ ở Master node. Grafana sẽ truy xuất dữ liệu trực tiếp từ Prometheus.

Set up Prometheus + Grafana cần phải setup theo mô hình Master – Slave, hướng dẫn sẽ được chia làm 2 phần tương ứng. Bài viết giả sử bạn đã có sẵn 2 host và đã cài đặt sẵn MySQL trên node Slave.

Master Node

Cài đặt Prometheus trên node Master

Tải source của Prometheus và cấu hình file config của prometheus

# Download source cai dat cua Prometheus cd ~ wget https://github.com/prometheus/prometheus/releases/download/v2.6.0/prometheus-2.6.0.linux-amd64.tar.gz mkdir /opt/prometheus tar zxf prometheus-2.6.0.linux-amd64.tar.gz -C /opt/prometheus --strip-components=1 # Tao user cho Prometheus useradd --no-create-home --shell /bin/false prometheus # Tao folder cho Prometheus mkdir /etc/prometheus mkdir /var/lib/prometheus # Cau hinh file config Prometheus cat << EOF > /etc/prometheus/prometheus.yml global: scrape_interval: 5s evaluation_interval: 5s scrape_configs: - job_name: linux static_configs: - targets: ['IP.Cua.Server.Slave:9100'] labels: alias: vesta_cp - job_name: mysql static_configs: - targets: ['IP.Cua.Server.Slave:9104'] labels: alias: vesta_cp EOF

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

# Download source cai dat cua Prometheus

cd ~

wget https://github.com/prometheus/prometheus/releases/download/v2.6.0/prometheus-2.6.0.linux-amd64.tar.gz

mkdir /opt/prometheus

tar zxf prometheus-2.6.0.linux-amd64.tar.gz -C /opt/prometheus --strip-components=1

# Tao user cho Prometheus

useradd --no-create-home --shell /bin/false prometheus

# Tao folder cho Prometheus

mkdir /etc/prometheus

mkdir /var/lib/prometheus

# Cau hinh file config Prometheus

cat << EOF > /etc/prometheus/prometheus.yml

global:

  scrape_interval:     5s

  evaluation_interval: 5s

scrape_configs:

  - job_name: linux

    static_configs:

      - targets: ['IP.Cua.Server.Slave:9100']

        labels:

          alias: vesta_cp

  - job_name: mysql

    static_configs:

      - targets: ['IP.Cua.Server.Slave:9104']

        labels:

          alias: vesta_cp

EOF

Tiếp tục thực hiện các bước cài đặt Prometheus

# Copy file thuc thi prometheus vao folder /user/local/bin cp /opt/prometheus/prometheus /usr/local/bin/ cp /opt/prometheus/promtool /usr/local/bin/ cp -r /opt/prometheus/consoles /etc/prometheus cp -r /opt/prometheus/console_libraries /etc/prometheus # Phan quyen cho user prometheus chown -R prometheus:prometheus /etc/prometheus chown -R prometheus:prometheus /var/lib/prometheus chown prometheus:prometheus /usr/local/bin/prometheus chown prometheus:prometheus /usr/local/bin/promtool # Chay prometheus prometheus /usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

# Copy file thuc thi prometheus vao folder /user/local/bin

cp /opt/prometheus/prometheus /usr/local/bin/

cp /opt/prometheus/promtool /usr/local/bin/

cp -r /opt/prometheus/consoles /etc/prometheus

cp -r /opt/prometheus/console_libraries /etc/prometheus

# Phan quyen cho user prometheus

chown -R prometheus:prometheus /etc/prometheus

chown -R prometheus:prometheus /var/lib/prometheus

chown prometheus:prometheus /usr/local/bin/prometheus

chown prometheus:prometheus /usr/local/bin/promtool

# Chay prometheus

prometheus /usr/local/bin/prometheus \

    --config.file /etc/prometheus/prometheus.yml \

    --storage.tsdb.path /var/lib/prometheus/ \

    --web.console.templates=/etc/prometheus/consoles \

    --web.console.libraries=/etc/prometheus/console_libraries

Tạo file Systemd cho Prometheus

Ta cần phải tạo 1 file systemd để tự động restart lại service khi bị crash hoặc reboot server. Service monitor mà để bị die thì chuối lắm :))))

vim /etc/systemd/system/prometheus.service [Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries [Install] WantedBy=multi-user.target

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

vim /etc/systemd/system/prometheus.service

[Unit]

Description=Prometheus

Wants=network-online.target

After=network-online.target

[Service]

User=prometheus

Group=prometheus

Type=simple

ExecStart=/usr/local/bin/prometheus \

    --config.file /etc/prometheus/prometheus.yml \

    --storage.tsdb.path /var/lib/prometheus/ \

    --web.console.templates=/etc/prometheus/consoles \

    --web.console.libraries=/etc/prometheus/console_libraries

[Install]

WantedBy=multi-user.target

Restart lại service prometheus và enable tính năng auto restart của prometheus

systemctl daemon-reload systemctl restart prometheus systemctl status prometheus systemctl enable prometheus

systemctl daemon-reload

systemctl restart prometheus

systemctl status prometheus

systemctl enable prometheus

Truy cập vô http://master_IP:9090 để kiểm tra thử, xong rồi thì nhớ đóng firewall lại

Monitored Nodes

Cài đặt 2 agent trên node được monitor

Có 2 loại agent, loại Node agent dùng để kiểm tra các thông số cơ bản của 1 server như: RAM, CPU, Disk, Network.

Và loại thứ 2 được nhắc tới trong bài này là mysql agent dùng để monitor trực tiếp mysql. Cần phải tạo account mysql cho prometheus agent truy xuất được thông tin từ mysql.

# Tai 2 agent ve node Slave wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz # Tao folder chua 2 agent mkdir /opt/prometheus_exporters # Giai nen source cai dat tar zxf node_exporter-0.17.0.linux-amd64.tar.gz -C /opt/prometheus_exporters tar zxf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /opt/prometheus_exporters # Start Linux agent cd /opt/prometheus_exporters/node_exporter-0.17.0.linux-amd64 ./node_exporter &

# Tai 2 agent ve node Slave

wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz

# Tao folder chua 2 agent

mkdir /opt/prometheus_exporters

# Giai nen source cai dat

tar zxf node_exporter-0.17.0.linux-amd64.tar.gz -C /opt/prometheus_exporters

tar zxf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /opt/prometheus_exporters

# Start Linux agent

cd /opt/prometheus_exporters/node_exporter-0.17.0.linux-amd64

./node_exporter &

Với loại agent cho MySQL thì ta cần phải tạo 1 tài khoản MySQL có quyền read table performance_schema

Đăng nhập vào MySQL bằng acc root và tạo 1 tài khoản như sau:

# Login vao MySQL voi quyen root va tao 1 tai khoan de monitor MySQL mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'prom'@'localhost' identified by 'wikivps@passwordcuaban'; mysql> GRANT SELECT ON performance_schema.* TO 'prom'@'localhost'; # Tạo file config cho mysqld_exporter cd /opt/prometheus_exporters/mysqld_exporter-0.11.0.linux-amd64 vim .my.cnf [client] user=prom password=wikivps@paswordcuaban # Chay mysql agent ./mysqld_exporter -config.my-cnf=".my.cnf" &

# Login vao MySQL voi quyen root va tao 1 tai khoan de monitor MySQL

mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'prom'@'localhost' identified by 'wikivps@passwordcuaban';

mysql> GRANT SELECT ON performance_schema.* TO 'prom'@'localhost';

# To file config cho mysqld_exporter

cd /opt/prometheus_exporters/mysqld_exporter-0.11.0.linux-amd64

vim .my.cnf

[client]

user=prom

password=wikivps@paswordcuaban

# Chay mysql agent

./mysqld_exporter -config.my-cnf=".my.cnf" &

Cài đặt Grafana

Tải gói cài đặt của Grafana về

wget https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm yum localinstall grafana-5.4.2-1.x86_64.rpm vim /etc/grafana/grafana.ini

wget https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm

yum localinstall grafana-5.4.2-1.x86_64.rpm

vim /etc/grafana/grafana.ini

Tải bộ dashboard của MySQL

git clone https://github.com/percona/grafana-dashboards.git cp -r grafana-dashboards/dashboards /var/lib/grafana

git clone https://github.com/percona/grafana-dashboards.git

cp -r grafana-dashboards/dashboards /var/lib/grafana

Start service Grafana-Server

systemctl start grafana-server systemctl status grafana-server systemctl enable grafana-server

systemctl start grafana-server

systemctl status grafana-server

systemctl enable grafana-server

Hướng dẫn add monitor trên Grafana

Import data source

Hướng dẫn sử dụng Grafana

Import data source là Prometheus, do đang chạy chung host nên import http://localhost:9090

Hướng dẫn sử dụng Grafana

Do chung host nên không cần thiết lập chứng thực và SSL, nếu chạy khác host thì cần set chứng thực SSL để đảm bảo an toàn.

Tạo Graph và Query

Hướng dẫn sử dụng Grafana

Chọn WikiVPS Dashboard => Add New Panel => Graph

Hướng dẫn sử dụng Grafana
Chọn Data Source là WikiVPS => Điền câu query, ở đây là tính số lần sort trong 1 phút
Hướng dẫn sử dụng Grafana
Điền câu query xong thì chọn nút Save trên góc và quay lại màn hình chính

Một số mẫu câu query trên Grafana

Số lần sort table của MySQL trong 1 phút

rate(mysql_global_status_sort_rows[1m]) rate(mysql_global_status_sort_range[1m]) rate(mysql_global_status_sort_merge_passes[1m]) rate(mysql_global_status_sort_rows[1m])

rate(mysql_global_status_sort_rows[1m])

rate(mysql_global_status_sort_range[1m])

rate(mysql_global_status_sort_merge_passes[1m])

rate(mysql_global_status_sort_rows[1m])

Số thread đang chạy của MySQL

mysql_global_status_threads_connected mysql_global_status_slow_launch_threads mysql_global_variables_thread_concurrency

mysql_global_status_threads_connected

mysql_global_status_slow_launch_threads

mysql_global_variables_thread_concurrency

Tổng số câu query phân loại theo command

sum by (command) ( rate(mysql_global_status_commands_total{ command=~"(select|insert|update|delete)"}[1m]))

sum by (command) ( rate(mysql_global_status_commands_total{ command=~"(select|insert|update|delete)"}[1m]))

Load của Linux theo phút

node_load1 node_load15 node_load15

node_load1

node_load15

node_load15

Tổng network traffic của server theo phút

rate(node_network_transmit_bytes_total{device!="lo"}[1m]) or irate(node_network_transmit_bytes_total{device!="lo"}[1m]) rate(node_network_receive_bytes_total{device!="lo"}[1m]) or irate(node_network_receive_bytes_total{device!="lo"}[1m])

rate(node_network_transmit_bytes_total{device!="lo"}[1m]) or irate(node_network_transmit_bytes_total{device!="lo"}[1m])

rate(node_network_receive_bytes_total{device!="lo"}[1m]) or irate(node_network_receive_bytes_total{device!="lo"}[1m])

Slow query của MySQL

rate(mysql_global_variables_slow_launch_time[1m]) rate(mysql_global_variables_slow_query_log[1m]) rate(mysql_global_status_slow_launch_threads[1m]) rate(mysql_global_status_slow_queries[1m])

rate(mysql_global_variables_slow_launch_time[1m])

rate(mysql_global_variables_slow_query_log[1m])

rate(mysql_global_status_slow_launch_threads[1m])

rate(mysql_global_status_slow_queries[1m])

Tham khảo

https://github.com/prometheus/node_exporter
https://github.com/prometheus/mysqld_exporter
https://www.percona.com/blog/2016/02/29/graphing-mysql-performance-with-prometheus-and-grafana/
https://blog.serverdensity.com/how-to-monitor-mysql/
https://www.slideshare.net/YoungHeonKim1/mysql-monitoring-using-prometheus-grafana

Series Navigation