美文网首页
Prometheus配置监控ip、端口连通,get、post接口

Prometheus配置监控ip、端口连通,get、post接口

作者: 二班老许 | 来源:发表于2022-04-18 12:04 被阅读0次

    prometheus.yml

    位置:/etc/prometheus/prometheus.yml

    # 全局配置
    global:
      # 默认拉取频率
      scrape_interval: 15s
      # 拉取超时时间
      scrape_timeout: 10s
      # 评估规则频率
      evaluation_interval: 15s
    
    # 规则文件配置
    rule_files: ['/etc/prometheus/rules/*.yml']
    
    # 告警配置  
    alerting:
      alertmanagers:
      - follow_redirects: true
        scheme: http
        timeout: 10s
        api_version: v2
        static_configs:
        - targets: []
    
    # 拉取配置,添加监控项
    scrape_configs:
    
    # 监控prometheus
    - job_name: prometheus
      metrics_path: /metrics
      static_configs:
      - targets:
        - localhost:9090
    
    # 监控ip是否能ping通,docker启动的blackbox-exporter不建议用此监控,可能会有报错
    - job_name: icmp_ping
      metrics_path: /probe
      params:
        module: [icmp]
      file_sd_configs:
      - files: ['/etc/prometheus/conf.d/icmp_ping/*.yml']
        refresh_interval: 10s
      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.7.254:9115
    
    # 监控端口是否能连通
    - job_name: tcp_port
      metrics_path: /probe
      params:
        module: [tcp_connect]
      file_sd_configs:
      - files: ['/etc/prometheus/conf.d/tcp_port/*.yml']
        refresh_interval: 10s
      relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.7.254:9115 
    
    # 监控get请求
    - job_name: http_get
      metrics_path: /probe
      params:
        module: [http_2xx]
      file_sd_configs: 
      - files: ['/etc/prometheus/conf.d/http_get/*.yml']
        refresh_interval: 10s
      relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.7.254:9115
    
    # 监控post请求
    - job_name: http_post
      metrics_path: /probe
      params:
        module: [http_post_2xx]
      file_sd_configs: 
      - files: ['/etc/prometheus/conf.d/http_post/*.yml']
        refresh_interval: 10s
      relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.7.254:9115
    
    # 监控post请求,有参数{"token":"prometheus_post_check_token"}
    - job_name: http_post_with_token
      metrics_path: /probe
      params:
        module: [http_post_2xx_with_prometheus_post_check_token]
      file_sd_configs:
      - files: ['/etc/prometheus/conf.d/http_post_with_token/*.yml']
        refresh_interval: 10s
      relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.7.254:9115
    

    rules.yml

    位置:/etc/prometheus/rules/rules.yml

    groups:
    - name: probe_http_status_code
      rules:
      - alert: probe_http_status_code
        expr: probe_http_status_code != 200
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.instance }}  状态码异常"
          description: "请尽快检测"
    groups:
    - name: probe_success
      rules:
      - alert: probe_success
        expr: probe_success == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "接口/主机/端口 {{ $labels.instance }}  无法联通"
          description: "请尽快检测"
    

    blackbox.yml

    位置:/usr/local/blackbox_exporter/blackbox.yml(取决于blackbox_exporter的安装位置)

    modules:
      http_2xx:
        prober: http
      http_post_2xx:
        prober: http
        http:
          method: POST
      tcp_connect:
        prober: tcp
      pop3s_banner:
        prober: tcp
        tcp:
          query_response:
          - expect: "^+OK"
          tls: true
          tls_config:
            insecure_skip_verify: false
      grpc:
        prober: grpc
        grpc:
          tls: true
          preferred_ip_protocol: "ip4"
      grpc_plain:
        prober: grpc
        grpc:
          tls: false
          service: "service1"
      ssh_banner:
        prober: tcp
        tcp:
          query_response:
          - expect: "^SSH-2.0-"
          - send: "SSH-2.0-blackbox-ssh-check"
      irc_banner:
        prober: tcp
        tcp:
          query_response:
          - send: "NICK prober"
          - send: "USER prober prober prober :prober"
          - expect: "PING :([^ ]+)"
            send: "PONG ${1}"
          - expect: "^:[^ ]+ 001"
      icmp:
        prober: icmp
    
      # 上面是自带的,下面是自定义的
      http_post_2xx_with_prometheus_post_check_token:
        prober: http
        http:
          method: POST
          headers:
            Content-Type: application/json   #添加头部
          body: '{"token":"prometheus_post_check_token"}'  #发送的相关数据
    

    icmp_ping.yml

    位置:/etc/prometheus/conf.d/icmp_ping/icmp_ping.yml

    - targets: ['192.168.7.254', '192.168.10.200']
      labels:
        group: 'ping监控'
    

    tcp_port.yml

    位置:/etc/prometheus/conf.d/tcp_port/tcp_port.yml

    - targets: ['192.168.7.254:8082', '192.168.7.254:8083']
      labels:
        group: '端口监控'
    

    http_get.yml

    位置:/etc/prometheus/conf.d/http_get/http_get.yml

    - targets:
      - http://192.168.7.254:8082/api/heartbeat/check/
      labels:
        name: 'get测试'
    
    - targets:
      - http://192.168.7.254:8083/api/heartbeat/check_test/
      labels:
        name: 'get测试2'
    

    http_post.yml

    位置:/etc/prometheus/conf.d/http_post/http_post.yml

    - targets:
      - http://192.168.7.254:8082/api/heartbeat/post_check/
      labels:
        name: 'post测试'
    

    http_post_with_token.yml

    位置:/etc/prometheus/conf.d/http_post_with_token/http_post_with_token.yml

    - targets:
      - http://192.168.7.254:8082/api/heartbeat/post_check/
      labels:
        name: 'post带body测试'
    

    相关文章

      网友评论

          本文标题:Prometheus配置监控ip、端口连通,get、post接口

          本文链接:https://www.haomeiwen.com/subject/vohdertx.html