基于Prometheus+Grafana搭建服务器监控
资料
官方仓库
https://github.com/prometheus/node_exporter
搭建
前言
需了解当前服务器的CPU架构,才知道如何选择安装包
判断服务器需要 ARM 包还是 AMD 包,主要依据服务器的 CPU 架构。具体步骤如下:
- 查看服务器的 CPU 架构:
- 你可以通过命令
lscpu
来查看 CPU 的架构信息。输出的Architecture
字段可以告诉你当前系统的 CPU 架构。 - 对于 ARM 架构,
Architecture
会显示为armv7l
、aarch64
等。 - 对于 AMD 或 Intel 架构(通常是 x86 架构),
Architecture
会显示为x86_64
。
- 你可以通过命令
- 安装包选择:
- 如果你的服务器是 ARM 架构(如基于 ARM 的处理器),则需要下载 ARM 版本的安装包(通常后缀是
.arm
或.aarch64
)。 - 如果是 AMD 或 Intel 架构(x86_64),则需要下载适合的 AMD 或 Intel 包(通常是
.x86_64
)。
- 如果你的服务器是 ARM 架构(如基于 ARM 的处理器),则需要下载 ARM 版本的安装包(通常后缀是
node_exporter
官网链接:https://github.com/prometheus/node_exporter/releases/tag/v1.7.0
根据不同的CPU架构选择不同的安装包,目前服务器是AMD的,下载的是:node_exporter-1.7.0.linux-amd64.tar.gz
prometueus
官网链接:https://github.com/prometheus/prometheus/releases/tag/v2.49.0
根据不同的CPU架构选择不同的安装包,目前服务器是AMD的,下载的是:prometheus-2.49.0.linux-amd64.tar.gz
安装
安装node_exporter
cd ../usr/local/
mkdir prometheus
tar -zxvf node_exporter-1.7.0.linux-amd64.tar.gz
mv node_exporter-1.7.0.linux-amd64 node_exporter
vim /etc/systemd/system/node_exporter.service
[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl start node_exporter
firewall-cmd --zone=public --add-port=9100/tcp --permanent
firewall-cmd --reload
curl http://localhost:9100/metrics
安装prometheus
tar -zxvf prometheus-2.49.0.linux-amd64.tar.gz
mv prometheus-2.49.0.linux-amd64 prometheus
vim prometheus/prometheus.yml
scrape_configs:
# The job name is added as a label `job=` 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: "192.168.83.250"
static_configs:
- targets: ["192.168.83.250:9100"]
- job_name: "192.168.83.251"
static_configs:
- targets: ["192.168.83.251:9100"]
- job_name: "192.168.83.252"
static_configs:
- targets: ["192.168.83.252:9100"]
- job_name: "192.168.83.253"
static_configs:
- targets: ["192.168.83.253:9100"]
vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus/prometheus.yml \
--storage.tsdb.path="data/"
--storage.tsdb.retention.time=15d
--web.max-connections=512
--web.listen-address=:9090
firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --reload
systemctl start prometheus
grafana
https://grafana.com/grafana/download/10.4.2?pg=get&plcmt=selfmanaged-box1-cta1
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-10.4.2.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-10.4.2.linux-amd64.tar.gz
mv grafana-v10.4.2/ grafana
汉化
vi /usr/local/grafana/conf/defaults.ini
#default_language = en-US
default_language = zh-Hans
sudo useradd -r grafana
sudo groupadd -r grafana
sudo chown -R grafana:grafana /usr/local/grafana
vim /etc/systemd/system/grafana.service
[Unit]
Description=Grafana
Documentation=https://grafana.com/docs/grafana/latest/
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/grafana/bin/grafana-server --config /usr/local/grafana/conf/defaults.ini --homepath /usr/local/grafana
User=grafana
Group=grafana
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable grafana
systemctl start grafana
systemctl status grafana
sudo systemctl status grafana.service
sudo journalctl -u grafana.service -f
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --reload
https://grafana.com/grafana/dashboards/
12633