第八周作业

1. 使用Prometheus完成blackbox、MySQL、Node的监控

(1)安装Prometheus

确保你已经安装并运行了Prometheus。如果还没有安装,可以参考Prometheus的官方文档进行安装。

(2)配置Prometheus

编辑Prometheus的配置文件(通常是prometheus.yml),添加相应的监控目标。

Node Exporter

Node Exporter用于监控主机的CPU、内存、磁盘和网络等指标。

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['<10.0.0.100>:9100']
MySQL Exporter

MySQL Exporter用于监控MySQL数据库。

  - job_name: 'mysql'
    static_configs:
      - targets: ['<10.0.0.101>:9104']
Blackbox Exporter

Blackbox Exporter用于监控网络服务的可用性(如HTTP、TCP等)。

  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_200]  # 可以根据需要修改为其他模块
    static_configs:
      - targets:
        - http://example.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: <10.0.0.102>:9115  # Blackbox Exporter的地址

2. 配置告警:Node主机CPU使用率大于90%告警

(1)创建告警规则

创建一个告警规则文件(如alert.rules),并添加以下规则:

groups:
  - name: node_alerts
    rules:
      - alert: HighCPUUsage
        expr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "High CPU usage on {{ $labels.instance }}"
          description: "CPU usage is above 90% for 1 minute."

(2)加载告警规则到Prometheus

prometheus.yml中添加告警规则文件的路径:

rule_files:
  - "alert.rules"

3. 配置告警:MySQL慢查询每分钟大于2条报警

(1)创建MySQL慢查询告警规则

alert.rules文件中添加以下规则:

  - alert: MySQLSlowQueries
    expr: rate(mysql_global_status_slow_queries[1m]) > 2
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "High MySQL slow queries on {{ $labels.instance }}"
      description: "More than 2 slow queries per minute."

4. 配置Grafana大盘,监控主机的CPU、内存、磁盘、网络使用率

(1)安装Grafana

确保你已经安装并运行了Grafana。如果还没有安装,可以参考Grafana的官方文档进行安装。

(2)添加Prometheus数据源

在Grafana中添加Prometheus作为数据源:

  • 登录Grafana,进入Configuration -> Data Sources
  • 点击Add data source,选择Prometheus
  • 填写Prometheus的URL(如http://<prometheus_host>:9090),保存并测试连接。

(3)创建Dashboard

创建一个新的Dashboard,并添加以下面板:

  • CPU使用率
    • 查询:100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
  • 内存使用率
    • 查询:100 * (node_memory_MemTotal_bytes - node_memory_MemFree_bytes) / node_memory_MemTotal_bytes
  • 磁盘使用率
    • 查询:100 * (node_filesystem_size_bytes{fstype!="rootfs",fstype!="tmpfs",fstype!="devtmpfs"} - node_filesystem_free_bytes{fstype!="rootfs",fstype!="tmpfs",fstype!="devtmpfs"}) / node_filesystem_size_bytes{fstype!="rootfs",fstype!="tmpfs",fstype!="devtmpfs"}
  • 网络使用率
    • 查询:irate(node_network_receive_bytes_total{device!="lo"}[5m])(接收字节速率)
    • 查询:irate(node_network_transmit_bytes_total{device!="lo"}[5m])(发送字节速率)
posted @ 2025-07-27 18:35  你好,运维人  阅读(13)  评论(0)    收藏  举报