一、监控系统核心组件架构
现代监控体系通常采用以下技术栈组合:
- Prometheus:开源的时序数据库,采用Pull模式采集指标数据
- Grafana:跨平台指标可视化工具,支持多种数据源
- Exporter:指标暴露组件(如node_exporter)
- Alertmanager:告警路由与通知管理
二、Prometheus 安装与配置
1. 二进制安装(Linux)
下载最新版本
wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz
tar xvfz prometheus-.tar.gz
cd prometheus-
启动服务
./prometheus --config.file=prometheus.yml
2. 基础配置文件示例
prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['192.168.1.100:9100']
三、Grafana 集成实践
1. 数据源配置
登录Grafana后进入Configuration → Data Sources:
- 选择Prometheus类型
- 填写URL(如http://localhost:9090)
- 设置合适的查询超时时间
2. 导入官方仪表盘模板
Node Exporter全指标仪表盘ID:1860
grafana-cli plugins install grafana-piechart-panel
systemctl restart grafana-server
四、关键监控场景实现
1. 主机资源监控
使用node_exporter采集基础指标:
CPU使用率
100 - (avg by(instance)(irate(node_cpu_seconds_total{mode="idle"}[5m])) 100)
内存利用率
(node_memory_MemTotal_bytes - node_memory_MemFree_bytes) / node_memory_MemTotal_bytes 100
2. 自定义业务指标
在应用中集成Prometheus客户端库(以Python为例):
from prometheus_client import start_http_server, Counter
REQUEST_COUNT = Counter('app_requests_total', 'Total HTTP requests')
@app.route('/')
def handle_request():
REQUEST_COUNT.inc()
return "OK"
start_http_server(8000)
五、告警规则配置
alert.rules
groups:
- name: host-alerts
rules:
- alert: HighCPUUsage
expr: 100 - (avg by(instance)(irate(node_cpu_seconds_total{mode="idle"}[5m])) 100) > 85
for: 10m
labels:
severity: warning
annotations:
summary: "High CPU usage on {{ $labels.instance }}"
六、性能优化建议
- 合理设置scrape_interval(生产环境建议30s-1min)
- 使用recording rules预计算常用查询
- 为Grafana仪表盘添加缓存(默认15s)
- 对Prometheus数据进行分片(联邦集群)
通过本文的实践方案,您已能够构建完整的监控系统。实际部署时建议结合Kubernetes或Docker实现容器化部署,并通过TLS加密通信保障数据安全。
评论