Prometheus 做Post 接口请求监控

时间:2022-04-10 10:30:33

Prometheus 做Post 接口请求监控

运维过程中,很多时候,业务应用会出现假死的情况,应用进程正常,但是无法提供服务,此时监控进程没有任何意义,就需要监控接口

接口监控的方法很多,可以用链路监控,可以写脚本进行监控

由于监控整体采用的是prometheus,所以这里就直接用blackbox_exporter来做接口的监控

blackbox_exporter可以通过http、https、dns、tcp、ICMP对target进行探测,dns、tcp、ICMP都相对简单,我这边主要是要监控一个登录接口,所以要用https来进行探测,具体配置方法如下

部署blockbox_exporter

  1. # 创建blackbox_exporter的目录
  2. cd /usr/local/blackbox_exporter
  3. # 下载blackbox_exporter的包
  4. wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.19.0/blackbox_exporter-0.19.0.linux-amd64.tar.gz
  5. # 解压
  6. tar -zxvf blackbox_exporter-0.19.0.linux-amd64.tar.gz
  7. # 重命名
  8. mv blackbox_exporter-0.19.0.linux-amd64.tar.gz blackbox_exporter

用systemd管理blackbox_exporter

  1. cat > /etc/systemd/system/blackbox_exporter.service << "EOF"
  2. [Unit]
  3. Description=Blackbox Exporter
  4. Wants=network-online.target
  5. After=network-online.target
  6. [Service]
  7. User=root
  8. ExecStart=/usr/local/blackbox_exporter/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox_exporter/blackbox.yml
  9. Restart=on-failure
  10. [Install]
  11. WantedBy=default.target
  12. EOF

对于blackbox_exporter管理的话,还有很多方法,你可以直接nohup后台启动,也可以通过supervisor进行启动,我习惯用systemd进行管理

然后添加开机自启动

  1. systemctl daemon-reload
  2. systemctl enable blackbox_exporter
  3. systemctl start blackbox_exporter

因为没指定日志输出,所以是输出到message日志中

默认带的blacbox.yml只是个最简单配置,不能满足需求,所以配置blackbox.yml中,添加一个模块

  1. modules:
  2. xhj_login: # 模块名称,prometheus配置文件中要匹配
  3. prober: http # 协议
  4. timeout: 30s # 超时时间
  5. http: # 模块的采集协议
  6. method: POST # http请求的方法
  7. preferred_ip_protocol: "ip4" # 使用的ipv4协议
  8. headers: # 配置post请求的header头
  9. Content-Type: application/json
  10. body: ''{"mobile": "13572801829", "password": "ZWB123wyl"}'' # post请求参数

配置完成后,保存blackbox.yml,启动blackbox_exporter

  1. systemctl start blackbox_exporter
  2. systemctl status blackbox_exporter
  3. ● blackbox_exporter.service - Blackbox Exporter
  4. Loaded: loaded (/etc/systemd/system/blackbox_exporter.service; enabled; vendor preset: disabled)
  5. Active: active (running) since Tue 2022-01-04 21:33:28 CST; 6s ago
  6. Main PID: 24679 (blackbox_export)
  7. Tasks: 7
  8. Memory: 1.9M
  9. CGroup: /system.slice/blackbox_exporter.service
  10. └─24679 /data/prometheus/blackbox_exporter/blackbox_exporter/blackbox_exporter --config.file=/data/prometheus/blackbox_exporter/blackbox_exporter/blackbox....
  11. Jan 04 21:33:28 systemd[1]: Started Blackbox Exporter.
  12. Jan 04 21:33:28 blackbox_exporter[24679]: level=info ts=2022-01-04T13:33:28.173Z caller=main.go:224 msg="Starting blackbox_exporter" version="(v...33d1ed0)"
  13. Jan 04 21:33:28 blackbox_exporter[24679]: level=info ts=2022-01-04T13:33:28.173Z caller=main.go:225 build_context="(go=go1.16.4, user=root@2b025...2:56:44)"
  14. Jan 04 21:33:28 blackbox_exporter[24679]: level=info ts=2022-01-04T13:33:28.173Z caller=main.go:237 msg="Loaded config file"
  15. Jan 04 21:33:28 blackbox_exporter[24679]: level=info ts=2022-01-04T13:33:28.174Z caller=main.go:385 msg="Listening on address" address=:9115
  16. Jan 04 21:33:28 blackbox_exporter[24679]: level=info ts=2022-01-04T13:33:28.174Z caller=tls_config.go:191 msg="TLS is disabled." http2=false
  17. Hint: Some lines were ellipsized, use -l to show in full.

配置prometheus

  1. # blackbox
  2. - job_name: "blackbox"
  3. metrics_path: /probe
  4. params:
  5. module: [xhj_login]
  6. static_configs:
  7. - targets:
  8. - https://xxx.aaa.com/api/pc/user/login/password
  9. relabel_configs:
  10. - source_labels: [__address__]
  11. target_label: __param_target
  12. - source_labels: [_param_target]
  13. target_label: instance
  14. - target_label: __address__
  15. replacement: 172.17.0.1:9115
  16. ……省略
  17. ……省略

配置完成后,保存,热加载prometheus配置文件

  1. curl -X POST http://localhost:9090/-/reload

加载完成后,通过prometheus的UI查看下targets

Prometheus 做Post 接口请求监控

此时其实可以通过被请求端的日志就可以看到blackbox_exporter发起的请求了,我们也可以通过curl请求blackbox_exporter来进行查看

  1. # 注意这里用&符号链接多个参数,需要转义
  2. curl http://172.17.0.1:9115/probe?target=https://xxx.aaa.com/api/pc/user/login/password\&module=xhj_login\&debug=true
  3. # 查看返回结果
  4. Logs for the probe:
  5. ts=2022-01-04T14:10:32.979231489Z caller=main.go:320 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Beginning probe" probe=http timeout_seconds=30
  6. ts=2022-01-04T14:10:32.979411891Z caller=http.go:335 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Resolving target address" ip_protocol=ip4
  7. ts=2022-01-04T14:10:32.986112778Z caller=http.go:335 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Resolved target address" ip=1.1.1.1
  8. ts=2022-01-04T14:10:32.986225541Z caller=client.go:251 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Making HTTP request" url=https://1.1.1.1/api/pc/user/login/password host=xxx.aaa.com
  9. ts=2022-01-04T14:10:33.05701057Z caller=main.go:130 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Received HTTP response" status_code=200
  10. ts=2022-01-04T14:10:33.057095911Z caller=main.go:130 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Response timings for roundtrip" roundtrip=0 start=2022-01-04T22:10:32.986352765+08:00 dnsDone=2022-01-04T22:10:32.986352765+08:00 connectDone=2022-01-04T22:10:32.992658409+08:00 gotConn=2022-01-04T22:10:33.038333687+08:00 responseStart=2022-01-04T22:10:33.056951457+08:00 tlsStart=2022-01-04T22:10:32.992701614+08:00 tlsDone=2022-01-04T22:10:33.038235019+08:00 end=2022-01-04T22:10:33.057074505+08:00
  11. ts=2022-01-04T14:10:33.057187486Z caller=main.go:320 module=xhj_login target=https://xxx.aaa.com/api/pc/user/login/password level=info msg="Probe succeeded" duration_seconds=0.077883376

可以看到,http_status_code 200,probe_success为1,访问正常

添加告警

创建rules

  1. groups:
  2. - name: blackbox_networks_stats
  3. rules:
  4. - alert: PC登录接口故障
  5. expr: probe_success == 0
  6. for: 1m
  7. labels:
  8. severity: critical
  9. annotations:
  10. summary: "PC登录接口出现故障,无法正常登录,请及时查看!"
  11. description: "PC登录接口出现故障,无法正常登录,请及时查看!"

创建完成后,热加载prometheus配置,然后查看prometheus的rules

Prometheus 做Post 接口请求监控

Grafana添加监控图标

监控配置完,必须得配个可视化仪表盘,直接用5345模板导入

Prometheus 做Post 接口请求监控

仪表盘中显示接口状态、是否使用SSL、包括SSL证书过期时间、接口状态码、接口请求时间和DNS解析时间

原文链接:https://mp.weixin.qq.com/s/0AR5ZgG57gWDHFczkC6LzQ