当前位置:首页 > 物理机 > 正文

prometheus如何监控物理机

Prometheus监控物理机需在目标服务器部署node_exporter以暴露系统指标,于Prometheus服务器配置抓取任务(scrape jobs)拉取数据,借助PromQL查询并结合Grafana实现可视化与告警,形成监控

Prometheus作为开源监控工具,通过拉取(Pull)模式采集数据,结合Node Exporter可实现对物理机软硬件状态的全面监控,以下是详细的实现方案:


核心组件部署与配置

组件 功能说明 部署位置
Node Exporter 采集物理机系统级指标(CPU、内存、磁盘、网络、传感器等) 被监控的物理机
Prometheus Server 拉取Node Exporter数据,存储时间序列,执行告警规则 中央监控服务器
Grafana 可视化监控数据,创建仪表盘 可选(与Prometheus同服)

部署Node Exporter

  • 安装方式

    • 直接下载二进制文件:wget https://github.com/prometheus/node_exporter/releases/latest/node_exporter-.tar.gz
    • 解压后赋予执行权限:chmod +x node_exporter
    • 设置为系统服务(以Systemd为例):
      [Unit]
      Description=Node Exporter
      StartLimitInterval=5
      StartLimitBurst=10
      [Service]
      ExecStart=/usr/local/bin/node_exporter --collector.systemd [其他参数]
      [Install]
      WantedBy=multi-user.target
    • 启动服务:systemctl enable --now node_exporter
  • 关键参数

    prometheus如何监控物理机  第1张

    • --collector.systemd:采集系统d相关指标(进程、服务状态)。
    • --no-collector.<name>:禁用不需要的采集器(如GPU监控)。

配置Prometheus抓取任务

在Prometheus配置文件(prometheus.yml)中添加Scrape Job:

scrape_configs:
  job_name: 'physical_servers'
    static_configs:
      targets: ['192.168.1.100:9100', '192.168.1.101:9100']  # Node Exporter地址
        labels:
          cluster: 'production'
          severity: 'critical'
    metrics_path: /metrics
    scrape_interval: 15s  # 采集频率

监控指标与数据采集

默认监控指标

Node Exporter提供以下核心指标分类:
| 指标类别 | 典型指标名称 | 说明 |
|——————|——————————————-|————————–|
| 硬件状态 | node_cpu_seconds_total, node_memory_Active | CPU耗时、内存使用量 |
| 磁盘与文件系统 | node_filesystem_usage, node_disk_io_time_seconds | 磁盘空间、I/O延迟 |
| 网络 | node_network_receive_bytes_total, node_netstat_tcp_connections | 流量、TCP连接数 |
| 温度与风扇 | node_hwmon_temp, node_fan_speed_rpm | 主板温度、风扇转速(需硬件支持)|

扩展监控项

  • 自定义脚本监控:通过Textfile Collector或Pushgateway推送自定义数据(如日志错误次数)。
  • 硬件传感器:启用--collector.smartmon(SMART磁盘健康度)、--collector.iexdus(IEX传感器数据)。

告警与可视化

配置Alertmanager

示例告警规则(alert.rules):

groups:
name: physical-machine-alerts
  rules:
  alert: HighCPUUsage
    expr: 100 (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m]))  100) > 80
    for: 2m
    labels:
      severity: critical
    annotations:
      summary: "物理机 {{ $labels.instance }} CPU使用率超过80%"

Grafana仪表盘

  • 添加Prometheus数据源(URL如http://<prometheus-server>:9090)。
  • 导入社区模板(ID:87510243)展示物理机概览。
  • 创建自定义面板:
    • 单台主机状态:筛选instance=192.168.1.100查看详细指标。
    • 集群视图:按标签(如cluster=production)聚合数据。

优化与安全

性能优化

  • 采集频率:对高密度部署场景,可调整scrape_interval为30s~1min。
  • 指标过滤:禁用不必要的采集器(如--no-collector.mdns)。

安全策略

  • 传输加密:配置Prometheus使用HTTPS拉取数据,或限制Exporter仅监听内网IP。
  • 访问控制:为Node Exporter添加认证(需反向代理如Nginx)。

FAQs

Q1:如何通过防火墙开放Node Exporter端口?
A1:在物理机防火墙(如iptables或firewalld)中允许9100端口:

# 添加规则(以firewalld为例)
firewall-cmd --permanent --add-port=9100/tcp
firewall-cmd --reload

Q2:如何监控物理机的RAID卡状态?
A2:启用Node Exporter的smartmon采集器(--collector.smartmon),并配置RAID卡暴露SMART数据。 Prometheus可通过node_smart_health指标判断磁盘健康状态。


通过上述方案,Prometheus可高效采集物理机从基础资源到硬件健康的全维度数据,结合告警与

0