使用Grafana、Prometheus监控服务器资源占用情况,监控服务器、网站延时等数据,全部使用Docker Compose方式搭建
使用到的工具
- Grafana
- Prometheus
- Node Export
- BlackBox Export
使用到的面板(id:9965、16098)
- https://grafana.com/grafana/dashboards/9965-1-blackbox-exporter-dashboard-20220412/
- https://grafana.com/grafana/dashboards/16098-node-exporter-dashboard-20240520-job/
安装BlackBox Export
docker-compose.yml
services:
blackbox-exporter:
image: prom/blackbox-exporter:latest
container_name: blackbox-exporter
ports:
- "9115:9115"
volumes:
- ./config/blackbox.yml:/etc/blackbox_exporter/config.yml # 挂载配置文件
- ./data:/data # 挂载数据目录(可选,Blackbox Exporter 默认不需要持久化数据)
cap_add: # 赋予 ICMP 权限
- NET_RAW
restart: always # 自动重启
配置文件blackbox.yml
modules:
icmp:
prober: icmp
timeout: 10s
icmp:
preferred_ip_protocol: "ip4"
http_https:
prober: http
http:
method: GET
valid_status_codes: [200]
tls_config:
insecure_skip_verify: true # 跳过证书验证(可选)
http_2xx:
prober: http
http:
valid_status_codes: [200] # 仅接受 HTTP 200 状态码
method: GET
headers:
Host: prometheus.io # 设置 Host 头
no_follow_redirects: true # 允许重定向
目录结构
root@aliyun-light:~/blackbox# tree ./
./
├── config
│ └── blackbox.yml
├── data
└── docker-compose.yml
3 directories, 2 files
安装Prometheus
docker-compose.yml
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
# ports:
# - "9090:9090"
network_mode: host
volumes:
- ./config:/etc/prometheus
- ./data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
restart: unless-stopped
配置文件prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'blackbox-icmp'
metrics_path: /probe
params:
module: [icmp] # 使用 icmp 模块监控 IP 地址
file_sd_configs:
- files:
- /etc/prometheus/icmp_targets.json # 引用 icmp_targets.json
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115 # Blackbox Exporter 地址
- job_name: 'blackbox-http'
scrape_interval: 30s # 设置网站的检查间隔为 30 秒
metrics_path: /probe
params:
module: [http_https] # 使用 http_https 模块监控网站
file_sd_configs:
- files:
- /etc/prometheus/http_targets.json # 引用 http_targets.json
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115 # Blackbox Exporter 地址
- job_name: 'node_exporter'
file_sd_configs:
- files:
- /etc/prometheus/server_targets.json
需要监控的IP和网站通过文件单独配置,每次配置完自动生效,不再需要重启Prometheus服务
网站监控http_targets.json
[
{
"targets": ["https://vio.vin"],
"labels": {
"instance": "Blog",
"module": "Website",
"project": "Blog",
"name": "Blog"
}
}
]
ICMP监控icmp_targets.json
[
{
"targets": ["10.115.15.1"],
"labels": {
"instance": "Home Gateway",
"module": "Home Server",
"project": "Network",
"name": "Home Gateway"
}
},
{
"targets": ["10.115.15.15"],
"labels": {
"instance": "Cady TR3000",
"module": "Home Server",
"project": "Network",
"name": "Cady TR3000"
}
},
{
"targets": ["10.115.15.16"],
"labels": {
"instance": "Ubuntu Game Server",
"module": "Home Server",
"project": "Game",
"name": "Ubuntu Game Server"
}
},
{
"targets": ["10.115.15.100"],
"labels": {
"instance": "PVE",
"module": "Home Server",
"project": "Infra",
"name": "PVE"
}
},
{
"targets": ["10.115.15.50"],
"labels": {
"instance": "FNOS",
"module": "Home Server",
"project": "Infra",
"name": "FNOS"
}
},
{
"targets": ["10.115.15.25"],
"labels": {
"instance": "Net Center",
"module": "Home Server",
"project": "Infra",
"name": "Net Center"
}
]
服务器监控server_targets.json(Node Export)
[
{
"targets": ["10.115.15.25:9100"],
"labels": {
"instance_name": "Net-Server",
"region": "home",
"type": "Home Server"
}
},
{
"targets": ["10.115.15.100:9100"],
"labels": {
"instance_name": "PVE",
"region": "home",
"type": "Home Server"
}
},
{
"targets": ["10.115.15.16:9100"],
"labels": {
"instance_name": "Game Server",
"region": "home",
"type": "Home Server"
}
},
{
"targets": ["127.0.0.1:9100"],
"labels": {
"instance_name": "Aliyun 200M",
"region": "chengdu",
"type": "Cloud Server"
}
},
{
"targets": ["10.115.15.50:9100"],
"labels": {
"instance_name": "FNOS",
"region": "home",
"type": "Home Server"
}
}
]
目录结构
root@aliyun-light:~/prometheus# tree ./
./
├── config
│ ├── http_targets.json
│ ├── icmp_targets.json
│ ├── prometheus.yml
│ └── server_targets.json
├── data
│ └── data
└── docker-compose.yml
13 directories, 25 files
安装Node Export
ubuntu直接使用apt安装即可
apt install prometheus-node-exporter
配置Grafana
Uptime
Server-Status
0