Grafana完整教程

暗香疏影 创作者

安装grafana前配置

  1. 调整源
  2. 默认端口为3000
  3. 服务端安装grafana (参考官网)

客户端

安装客户端
阿里云ARMS监控服务文档
设置Grafana Youtuber Blog - jhooq

安装prometheus:

1
wget https://github.com/prometheus/prometheus/releases/download/v2.47.0-rc.0/prometheus-2.47.0-rc.0.linux-amd64.tar.gz

安装node exporter

1
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz

解压node exporter

1
2
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*.tar.gz

我们一般将其移动或放置在/usr/local/bin下(或者为了方便,放到/usr/local 或者/opt也可以)。

运行并测试node exporter

1
2
3
./node_exporter
#最后一行输出
>ts=2023-09-03T08:27:20.532Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=[::]:9100

查看localhost:9100/metrics 是否有如下显示 (也可以使用curl看)

1
2
3
4
5
6
7
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
...

解压prometheus

1
2
tar xvfz prometheus-*.tar.gz
cd prometheus-*.tar.gz

我们一般将其移动或放置在/usr/local/bin下(或者为了方便,放到/usr/local 或者/opt也可以)。

运行并测试prometheus

1
2
3
4
5
6
# run  或者加./prometheus --config.file=xxx.yml 指定yml (一般放置/etc/prometheus下)
./prometheus


#最后一行输出不固定的,例如
>ts=2023-09-03T08:54:49.797Z caller=manager.go:1009 level=info component="rule manager" msg="Starting rule manager..."

默认端口9090,浏览器查看localhost:9090

添加prometheus设置 (node exporter)

现在prometheus已经可以正常使用,但是需要额外添加node exporter到yml配置文件中:

1
2
3
4
- job_name: 'node_exporter'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']

也就是

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
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ["localhost:9090"]

- job_name: "node_exporter"
scrape_interval: 5s
static_configs:
- targets: ["localhost:9100"]

自启动

Prometheus:

创建systemd服务 /etc/systemd/system/prometheus.service

默认tsdb保存在文件夹下的data内,这里不做修改。
用户和用户组如果不存在需要先创建。(创建一个nologin的用户)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=Prometheus Monitoring
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus/prometheus \
--config.file /usr/local/bin/prometheus/prometheus.yml \
--storage.tsdb.path /usr/local/bin/prometheus/data/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

重启

1
2
3
4
5
6
sudo systemctl daemon-reload
sudo systemctl start prometheus.service
sudo systemctl enable prometheus.service
sudo systemctl status prometheus.service
# view log
sudo journalctl -u prometheus.service -f

Node Exporter:

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=nodeexporter
Type=simple
ExecStart=/usr/local/bin/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target

监控另一台服务器

只需在prometheus.yml中target加上逗号添加即可。

1
2
3
4
5
6
7
  static_configs:
- targets: ["localhost:9090", "192.168.200.102:9090"]

- job_name: "node_exporter"
scrape_interval: 5s
static_configs:
- targets: ["localhost:9100","192.168.200.102:9100"]

效果图

因为第一个监控的服务器是localhost看不出ip, 建议专门拿一台出来做监控机器。

Grafana Dashboard

Grafana Dashboard地址:https://grafana.com/grafana/dashboards/

好用的Dashboard:
作者:StarsL.cn
https://grafana.com/grafana/dashboards/16098
https://grafana.com/grafana/dashboards/8919

外国人:
https://grafana.com/grafana/dashboards/1860

  • 标题: Grafana完整教程
  • 作者: 暗香疏影
  • 创建于 : 2023-09-03 00:00:00
  • 更新于 : 2023-09-03 00:00:00
  • 链接: https://blog.23ikr.com/2023/09/03/2023-09-03-grafana-full-config-guide/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论