章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • linux

  • windows

  • 中间件

  • 网络

  • 安全

  • 存储

  • 防火墙

  • 数据库

  • 系统

  • docker

  • other

  • 监控

    • zabbix

    • prometheus

      • prometheus监控、告警与存储
      • 使用docker-compose搭建promethes+grafana监控系统
      • prometheus添加钉钉消息告警
      • alertmanager实现某个时间段静默某些告警项
      • prometheus设置表达式触发告警
      • 监控MySQL运行状态:MySQLD Exporter
      • alertmanager设置定时静默告警脚本
      • 构建高大上的黑盒监控平台
        • prometheus使用一个redis_exporter监控所有redis实例
        • alertmanager-webhook与API
    • 运维
    • 监控
    • prometheus
    章工运维
    2023-06-29
    目录

    构建高大上的黑盒监控平台

    # 构建高大上的黑盒监控平台

    # 一、概述

    在监控体系里面,通常我们把监控分为:白盒监控和黑盒监控:

    黑盒监控:主要关注的现象,一般都是正在发生的东西,例如出现一个告警,业务接口不正常,那么这种监控就是站在用户的角度能看到的监控,重点在于能对正在发生的故障进行告警。

    白盒监控:主要关注的是原因,也就是系统内部暴露的一些指标,例如redis的info中显示redis slave down,这个就是redis info显示的一个内部的指标,重点在于原因,可能是在黑盒监控中看到redis down,而查看内部信息的时候,显示redis port is refused connection。

    白盒监控:有很多种,有中间件,有存储,有web服务器例如redis可以使用info暴露内部的指标信息;例如mysql可以使用show variables暴露内部指标信息;例如nginx可以使用nginx_status来暴露内部信息,系统业务指标可以通过埋点或者命令进行采集。

    Blackbox Exporter

    在前面的知识中,我们介绍Prometheus下如何进行白盒监控:我们监控主机的资源用量、容器的运行状态、数据库中间件的运行数据,通过采集相关指标来预测我们的服务健康状态。在黑盒监控方面。Blackbox Exporter是Prometheus社区提供的官方黑盒监控解决方案,其允许用户通过:HTTP、HTTPS、DNS、TCP以及ICMP的方式对网络进行探测。

    Blackbox_exporter 应用场景

    HTTP 测试

    定义 Request Header 信息

    判断 Http status / Http Respones Header / Http Body 内容

    TCP 测试

    业务组件端口状态监听

    应用层协议定义与监听

    ICMP 测试

    主机探活机制

    POST 测试

    接口联通性

    SSL 证书过期时间

    可以结合grafana 生成的相关模板

    # 二、Blackbox Exporter 部署:

    1、安装Exporter:

    [root@cinder1 src]# wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.16.0/blackbox_exporter-0.16.0.linux-amd64.tar.gz

    [root@cinder1 src]#tar -zxvf blackbox_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local

    [root@cinder1 src]#mv /usr/local/blackbox_exporter-0.16.0.linux-amd64 /usr/local/blackbox_exporter

    2、添加到启动项:

    [root@cinder1 src]# cat /etc/systemd/system/blackbox_exporter.service

    [Unit]
    Description=blackbox_exporter
    After=network.target 
    
    [Service]
    WorkingDirectory=/usr/local/blackbox
    ExecStart=/usr/local/blackbox/blackbox_exporter \
             --config.file=/usr/local/blackbox/blackbox.yml
    [Install]
    WantedBy=multi-user.target
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    icmp监控

    通过icmp 这个指标的采集,我们可以确认到对方的线路是否有问题。这个也是监控里面比较重要的一个环节。我们要了解全国各地到我们机房的线路有哪条有问题我们总结了两种方案:

    1、全国各地各节点ping 和访问数据采集。这种类似听云运营商有提供这类服务,但是要花钱。

    2、我现在用的方法就是:找各地测试ping 的节点,我们从机房主动ping 看是否到哪个线路有故障,下面我们开始。

    一、prometheus 添加相关监控,Blackbox 使用默认配置启动即可:

      - job_name: "icmp_ping"
        metrics_path: /probe
        params:
          module: [icmp]  # 使用icmp模块
        file_sd_configs:
        - refresh_interval: 10s
          files:
          - "/home/prometheus/conf/ping_status*.yml"  #具体的配置文件
        relabel_configs:
        - source_labels: [__address__]
          regex: (.*)(:80)?
          target_label: __param_target
          replacement: ${1}
        - source_labels: [__param_target]
          target_label: instance
        - source_labels: [__param_target]
          regex: (.*)
          target_label: ping
          replacement: ${1}
        - source_labels: []
          regex: .*
          target_label: __address__
          replacement: 192.168.1.14:9115  
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23

    二、相关ping节点配置:

    [root@cinder1 conf]# cat ping_status.yml 
    - targets: ['220.181.38.150','14.215.177.39','180.101.49.12','14.215.177.39','180.101.49.11','14.215.177.38','14.215.177.38']
      labels:
        group: '一线城市-电信网络监控'
    - targets: ['112.80.248.75','163.177.151.109','61.135.169.125','163.177.151.110','180.101.49.11','61.135.169.121','180.101.49.11']
      labels:
        group: '一线城市-联通网络监控'
    - targets: ['183.232.231.172','36.152.44.95','182.61.200.6','36.152.44.96','220.181.38.149']
      labels:
        group: '一线城市-移动网络监控' 
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    # 三、http 相关指标监控:

    一、prometheus 配置http_get访问:

      - job_name: "blackbox"
        metrics_path: /probe
        params:
          module: [http_2xx]  #使用http模块
        file_sd_configs: 
        - refresh_interval: 1m
          files: 
          - "/home/prometheus/conf/blackbox*.yml"
        relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: 192.168.1.14:9115
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    二、相关配置文件,类似举例如下

    [root@cinder1 conf]# cat /home/prometheus/conf/blackbox-dis.yml 
    - targets:
      - https://www.zhibo8.cc
      - https://www.baidu.com 
    #配置相关URL
    
    1
    2
    3
    4
    5

    三、添加grafana模板:

    可以选择模板的9965模板,这个模板我们也看到前面的,提供了相关的ssl 过期检测。

    # 接口get请求检测

    一、prometheus 配置,其实跟我们之前的配置一样,我们直接看配置文件:

      - job_name: "check_get"
        metrics_path: /probe
        params:
          module: [http_2xx]  # Look for a HTTP 200 response.
        file_sd_configs:
        - refresh_interval: 1m
          files:
          - "/home/prometheus/conf/service_get.yml"
        relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: 192.168.1.14:9115
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    二、相关接口配置参考:

    [root@cinder1 conf]# cat service_get.yml 
    - targets:
      - http://10.10.1.123:10000/pmkb/atc_tcbi
      - http://10.10.1.123:10000/pmkb/get_ship_lock_count
      - http://10.10.1.123:10000/pmkb/get_terminal_count_by_city
      - http://10.10.1.123:10000/pmkb/get_terminal_monitor?industry=1
      - http://10.10.1.123:10000/pmkb/get_terminal_comparison?industry=1
      - http://10.10.1.123:10000/pmkb/get_terminal_city_count_industry?industry=1
      - http://10.10.1.123:10000/pmkb/industry_stat?industry=1
      - http://10.10.1.123:10000/pmkb/get_company_car_count?industry=1
      - http://10.10.1.123:10000/pmkb/get_terminal_month_countbyi?industry=1
      labels:
        group: 'service'
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    # 接口post 请求状态检测:

    一、这里首先我们要改一下post 相关接口的blackbox.yml配置,我们自己定义一个模块:

    [root@cinder1 blackbox]# cat blackbox.yml 
    modules:
      http_2xx:
        prober: http
      http_post_2xx:   #这个模块名称可以自己定义
        prober: http
        http:
          method: POST
          headers:
            Content-Type: application/json   #添加头部
          body: '{"username":"admin","password":"123456"}'  #发送的相关数据,这里我们以登录接口为例
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    二、添加到prometheus:

      - job_name: "check_service"
        metrics_path: /probe
        params:
          module: [http_post_2xx]  # 这里要对应配置文件里,定义的模块
        file_sd_configs: 
        - refresh_interval: 1m
          files: 
          - "/home/prometheus/conf/service_post.yml"
        relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: 192.168.1.14:9115
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    三、相关配置:

    [root@cinder1 conf]# cat service_post.yml 
    - targets:
      - http://10.2.4.103:5000/devops/api/v1.0/login
      labels:
        group: 'service'
    
    1
    2
    3
    4
    5

    四、添加grafana相关配置,这个也是自己定义的,可以从github上下载。

    # tcp端口状态检测:

    个人理解的是这个跟telnet差不多都是检测端口是否在线

    一、prometheus 配置:

      - job_name: 'port_status'
        metrics_path: /probe
        params:
          module: [tcp_connect]  #使用tcp模块
        static_configs:
          - targets: ['10.10.1.35:8068','10.10.1.35:8069']  #对应主机接口
            labels:
              instance: 'port_status'
              group: 'tcp'
        relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target 
        - target_label: __address__
          replacement: 192.168.1.14:9115 
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

    二、图表:

    图表可以集成到前面的grafana 9965模板:

    # 告警规则定义:

    一、业务正常性:

    icmp、tcp、http、post 监测是否正常可以观察probe_success 这一指标

    probe_success == 0 ##联通性异常

    probe_success == 1 ##联通性正常

    告警也是判断这个指标是否等于0,如等于0 则触发异常报警

    二、通过http模块我们可以获取证书的过期时间,可以根据过期时间添加相关告警

    probe_ssl_earliest_cert_expiry :可以查询证书到期时间。

    经过单位转换我们可以得到一下,按天来计算:(probe_ssl_earliest_cert_expiry - time())/86400

    三、所以我们结合上面的配置可以定制如下告警规则

    
    [root@cinder1 rules]# cat blackbox.yml 
    groups:
    - name: blackbox_network_stats
      rules:
      - alert: blackbox_network_stats
        expr: probe_success == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "接口/主机/端口 {{ $labels.instance }}  无法联通"
          description: "请尽快检测"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    ssl检测

    [root@cinder1 rules]# cat ssl.yml 
    groups:
    - name: check_ssl_status
      rules:
      - alert: "ssl证书过期警告"
        expr: (probe_ssl_earliest_cert_expiry - time())/86400 <30
        for: 1h
        labels:
          severity: warn
        annotations:
          description: '域名{{$labels.instance}}的证书还有{{ printf "%.1f" $value }}天就过期了,请尽快更新证书'
          summary: "ssl证书过期警告"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    原文链接 (opens new window)

    微信 支付宝
    上次更新: 2023/06/30, 22:33:06

    ← alertmanager设置定时静默告警脚本 prometheus使用一个redis_exporter监控所有redis实例→

    最近更新
    01
    shell脚本模块集合
    05-13
    02
    生活小技巧(认知版)
    04-29
    03
    生活小技巧(防骗版)
    04-29
    更多文章>
    Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式