安装grafana前配置
- 调整源
- 默认端口为3000
- 服务端安装grafana (参考官网)
客户端
安装客户端
阿里云ARMS监控服务文档
设置Grafana Youtuber Blog - jhooq
安装prometheus:
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
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
解压node exporter
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*.tar.gz
我们一般将其移动或放置在/usr/local/bin下(或者为了方便,放到/usr/local 或者/opt也可以)。
运行并测试node exporter
./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看)
# 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
tar xvfz prometheus-*.tar.gz
cd prometheus-*.tar.gz
我们一般将其移动或放置在/usr/local/bin下(或者为了方便,放到/usr/local 或者/opt也可以)。
运行并测试prometheus
# 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配置文件中:
- job_name: 'node_exporter'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100']
也就是
# 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的用户)
[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
重启
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:
[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加上逗号添加即可。
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