监控数据的可视化,是监控中非常重要的一个环节。可视化后的数据的走向,能提供一个预警,提供一个排查方向,在很大范围内能避免故障的放生。

什么是 Grafana

  Grafana 是一款使用 Go 语言开发的,开源的数据可视化工具。可以用来做监控数据的可视化,同时也能做告警工具。

安装

下载

  点击 Grafana 官网 查看 Grafana 官方信息。这里使用 grafana 版本为 8.2.4。使用 tar.gz 包安装。

1
[root@grafana ~]# wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.2.4.linux-amd64.tar.gz

解压

  下载好的安装包是 tar.gz 格式的,这里解压缩到 /opt 目录下并配置进行软连接到 /usr/local/ 路径下。

1
2
[root@grafana ~]# tar -zxvf grafana-enterprise-8.2.4.linux-amd64.tar.gz -C /opt
[root@grafana ~]# ln -s /opt/grafana-8.2.4/ /usr/local/grafana

使用 systemd 自启动管理

  这里使用

1
2
3
4
5
6
7
8
9
10
11
12
[root@grafana ~]# cat /usr/lib/systemd/system/grafana.service 
[Unit]
Description=Grafana
After=network.target

[Service]
Type=notify
ExecStart=/opt/grafana-8.2.4/bin/grafana-server -homepath /opt/grafana-8.2.4
Restart=on-failure

[Install]
WantedBy=multi-user.target

修改配置

  主要修改数据库和存储位置。

  • 存储路径

  存储路径主要修改临时文件等的存储位置,日志文件的存储位置。默认是相对位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@grafana ~]# cd /usr/local/grafana/conf
[root@grafana conf]# vim default.ini
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
# 临时文件和会话信息的存储位置。使用 sqlite 作为存储结构时数据库的存储位置。
data = /data/grafana

# Temporary files in `data` directory older than given duration will be removed
temp_data_lifetime = 24h

# Directory where grafana can store logs
# 日志路径,存储 Grafana 的日志的位置
logs = /data/logs/grafana

# Directory where grafana will automatically scan and look for plugins
# 指定 grafana 插件的存储路径。
plugins = data/plugins
  • 数据库配置

  数据库采用的 MYSQL 数据库,相关配置在配置文件 76 行到 125 行 的位置。

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
38
39
40
41
42
43
44
45
46
47
[root@grafana conf]# vim default.ini
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url property.

# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = 127.0.0.1:3306
name = grafana
user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = xFTO8AE8RikpTIwb
# Use either URL or the previous fields to configure the database
# Example: mysql://user:secret@host:port/database
url = mysql://root:xFTO8AE8RikpTIwb@127.0.0.1:3306/grafana

# Max idle conn setting default is 2
max_idle_conn = 2

# Max conn setting default is 0 (mean not set)
max_open_conn =

# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours)
conn_max_lifetime = 14400

# Set to true to log the sql calls and execution times.
log_queries =

# For "postgres", use either "disable", "require" or "verify-full"
# For "mysql", use either "true", "false", or "skip-verify".
ssl_mode = disable

# Database drivers may support different transaction isolation levels.
# Currently, only "mysql" driver supports isolation levels.
# If the value is empty - driver's default isolation level is applied.
# For "mysql" use "READ-UNCOMMITTED", "READ-COMMITTED", "REPEATABLE-READ" or "SERIALIZABLE".
isolation_level =
ca_cert_path =
client_key_path =
client_cert_path =
server_cert_name =

# For "sqlite3" only, path relative to data_path setting
path = grafana.db

# For "sqlite3" only. cache mode setting used for connecting to the database
cache_mode = private

启动

  修改好对应的配置文件之后,就可以启动 Grafana 配置。

1
2
[root@grafana ~]# systemctl daemon-reload
[root@grafana ~]# systemctl enable --now grafana.service