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: /probeparams:module: [http_200] # 可以根据需要修改为其他模块static_configs:- targets:- http://example.comrelabel_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_alertsrules:- alert: HighCPUUsageexpr: 100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90for: 1mlabels:severity: criticalannotations: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: MySQLSlowQueriesexpr: rate(mysql_global_status_slow_queries[1m]) > 2for: 1mlabels:severity: warningannotations: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])
(发送字节速率)
- 查询: