Prometheus配置監(jiān)控ip、端口連通,get、post接口連通和狀態(tài)碼

prometheus.yml

位置:/etc/prometheus/prometheus.yml

# 全局配置
global:
  # 默認(rèn)拉取頻率
  scrape_interval: 15s
  # 拉取超時(shí)時(shí)間
  scrape_timeout: 10s
  # 評(píng)估規(guī)則頻率
  evaluation_interval: 15s

# 規(guī)則文件配置
rule_files: ['/etc/prometheus/rules/*.yml']

# 告警配置  
alerting:
  alertmanagers:
  - follow_redirects: true
    scheme: http
    timeout: 10s
    api_version: v2
    static_configs:
    - targets: []

# 拉取配置,添加監(jiān)控項(xiàng)
scrape_configs:

# 監(jiān)控prometheus
- job_name: prometheus
  metrics_path: /metrics
  static_configs:
  - targets:
    - localhost:9090

# 監(jiān)控ip是否能ping通,docker啟動(dòng)的blackbox-exporter不建議用此監(jiān)控,可能會(huì)有報(bào)錯(cuò)
- 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

# 監(jiān)控端口是否能連通
- 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 

# 監(jiān)控get請(qǐng)求
- 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

# 監(jiān)控post請(qǐng)求
- 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

# 監(jiān)控post請(qǐng)求,有參數(shù){"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 }}  狀態(tài)碼異常"
      description: "請(qǐng)盡快檢測(cè)"
groups:
- name: probe_success
  rules:
  - alert: probe_success
    expr: probe_success == 0
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: "接口/主機(jī)/端口 {{ $labels.instance }}  無法聯(lián)通"
      description: "請(qǐng)盡快檢測(cè)"

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"}'  #發(fā)送的相關(guān)數(shù)據(jù)

icmp_ping.yml

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

- targets: ['192.168.7.254', '192.168.10.200']
  labels:
    group: 'ping監(jiān)控'

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: '端口監(jiān)控'

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測(cè)試'

- targets:
  - http://192.168.7.254:8083/api/heartbeat/check_test/
  labels:
    name: 'get測(cè)試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測(cè)試'

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測(cè)試'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容