首页 > 其他分享 >玩prometheus过程中遇到的一些问题

玩prometheus过程中遇到的一些问题

时间:2023-03-17 19:13:17浏览次数:28  
标签:name 遇到 self labels job 192.168 prometheus 9182 过程

一、pgw的无默认值监控项

1、prometheus的配置文件

global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 1m # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

alerting:
  alertmanagers:
    - static_configs:
        - targets:
           - localhost:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
   - "/opt/soft/alertmanager-0.25.0.linux-amd64/local_rules.yml"

scrape_configs:
  - job_name: "pushGateway"

    static_configs:
      - targets: ["localhost:9099"]
    #relabel_configs:
    #  - source_labels: [srcIp]
    #    separator: ':'
    #    regex: '(.*)'
    #    replacement: '${1}'
    #    target_label: instance
    
  - job_name: "windows_prometheus"
    static_configs:
      - targets: 
          - 192.168.61.153:9182
          - 192.168.62.238:9182
          - 192.168.62.157:9182
          - 192.168.62.17:9182
          - 192.168.66.175:9182
          - 192.168.62.54:9182
          - 192.168.62.94:9182
          - 192.168.62.87:9182
          - 192.168.62.52:9182
          - 192.168.62.224:9182
          - 192.168.62.222:9182
          - 192.168.62.185:9182
          - 192.168.62.71:9182
          - 192.168.62.110:9182
          - 192.168.62.88:9182
          - 192.168.62.28:9182
          - 192.168.62.175:9182
          - 192.168.62.247:9182
    relabel_configs:
      - source_labels: [__address__]
        separator: ':'
        regex: '(.*):(.*)'
        replacement: '${1}'
        target_label: src_instance
View Code

起初自己设定的配置是两个单纯的static_configs,用自带的labels进行向量匹配。

2、pushGateWay 的数据层面, pushgateway 的数据来源是推送到pgw,然后prometheus 再去拉取pgw对应的metric。所以pgw跟exporter就有个区别,pgw没有默认值。

 

 

3、那么在当上图中 short-lived jobs没有向pgw推送数据时,pgw不知道是否存在该监控向量。所以此时对应监控项用来判断应用的存活状态就多少有些麻烦。

4、贴上告警规则

groups:
- name: local_rules
  rules:

  - alert: InstanceDown
    expr: up{job="windows_prometheus"} == 0
    for: 2m
    labels:
      severity: error
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes, current value: {{ $value }}"


  - alert: auto_wx_friend
    expr: (job_last_success_unixtime{exported_job="auto_wx_friend_from_pgw"} or (up{job="windows_prometheus"} * 0)) == 0
    for: 2m
    labels:
      severity: error
    annotations:
      summary: "auto_wx_friend down {{ $labels.src_instance }} {{  $labels.job }}"
      description: "{{ $labels.src_instance }} {{  $labels.job }}  (current value: {{ $value }})"
View Code

推送的脚本,可以直接用

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
import socket


class PushGateWayPrometheus:
    """
    pushgateway
    """

    def __init__(self):
        self.registry = CollectorRegistry()
        self.gateway = '192.168.60.203:9099'
        # label 和 value 对应
        self.label_name = ['src_instance', ]
        self.src_ip_label_value = socket.gethostbyname(socket.gethostname())
        # 无需修改
        self.job = 'auto_wx_friend_from_pgw'

    def gauge_process_alive(self, metric_name: str, describe: str) -> None:
        """
        如果对应值设置为1,则表示应用仍然存活
        :param metric_name: 
        :param describe: 
        :return: 
        """
        g = Gauge(metric_name, describe, registry=self.registry,
                  labelnames=self.label_name)
        g.labels(self.src_ip_label_value).set(1)

    def push(self, metric_name: str, describe: str) -> None:
        """
        推送对应的指标,如果有新的只需新增
        :param metric_name: 
        :param describe: 
        :return: 
        """
        self.gauge_process_alive(metric_name, describe)
        push_to_gateway(self.gateway, job=self.job, registry=self.registry)


PushGateWayPrometheus().push('job_last_success_unixtime', 'Last time a batch job successfully finished')
View Code

告警规则中,InstanceDown是exporter的,auto_wx_friend是pgw的。而在prometheus中的src_instance是为了尝试向量匹配,所以得有相同labels。

虽然简陋但也实现了效果,如果有大佬有更牛的可以赐教一番。

 

二、alertmanager 告警抑制

inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'instance']
View Code

这是网上的,参考一下解释。

# 抑制器配置
inhibit_rules: # 抑制规则
  - source_match: # 源标签警报触发时抑制含有目标标签的警报,在当前警报匹配 status: 'High'
      status: 'High'  
    target_match:
      status: 'Warning' # 
    equal: ['alertname','operations', 'instance'] # 确保这个配置下的标签内容相同才会抑制,也就是说警报中必须有这三个标签值才会被抑制。

 

标签:name,遇到,self,labels,job,192.168,prometheus,9182,过程
From: https://www.cnblogs.com/bill2014/p/17227885.html

相关文章

  • Vulnhub之Recon 1靶机详细测试过程
    Recon1作者:jasonhuawen靶机信息名称:recon:1地址:https://www.vulnhub.com/entry/recon-1,438/识别目标主机IP地址─(kali㉿kali)-[~/Desktop/Vulnhub/ReconEE]......
  • MSSQLSERVER 存储过程debug调试
    每当我们遇到相对稍复杂的业务的时候,都会考虑写在存储过程中,这样相当于一个黑匣,方便管理。 但是如果写的行数太多,如果碰到了问题,凭经验,很难发现问题,那就要用到debug调试......
  • 用TS封装Axios遇到的奇怪问题
    按照王红元课程中的代码去封装,出现这个错误。//一、实例拦截器:从config中取出的拦截器是对应的实例的拦截器this.instance.interceptors.request.use(this.......
  • 记录一次重置数据库root用户的过程
    服务器的mysql突然连接不上去了,密码也忘记了。只能重新设置密码了1、使用如下指令打开mysql数据库配置文件(具体的文件路径以实际情况为准)vim/etc/my.cnf在虚拟机中直接......
  • 一文读懂存储过程
    0导读经常听别人说,“调用一个存储过程“,“把处理过程改写为存储过程之后就快了”此类的话,本篇文章我们来聊一聊存储过程。将从以下几个方面去描述存储过程。1存储过程解决......
  • ES6.面向对象(主)和面向过程
    面向过程优点:效率高,因为不需要实例化对象。缺点:耦合度高,扩展性差,不易维护(例如:每个步骤都要有,不然就不行)面向对象优点:耦合低(易复用),扩展性强,易维护,由于面向对象有封装、......
  • react项目中遇到的小问题汇总
    sessionStorage在刷新的时候不会销毁,在窗口关闭的时候才销毁默认情况下,useEffect在每次渲染之后执行文件上传上传文件通过form表单拿到的数据是解析后的,直接拿Upload.D......
  • Vulnhub之Replay靶机详细测试过程(获得Root Shell)
    Replay作者:jasonhuawen靶机信息名称:Replay:1地址:https://www.vulnhub.com/entry/replay-1,278/识别目标主机IP地址(kali㉿kali)-[~/Desktop/Vulnhub/Replay]└......
  • 类加载的过程
        1、加载•通过一个类的全限定名来获取定义此类的二进制字节流     ---------->通过包名+类名,获取这个类,准备用流进行传输•将这个字节流所......
  • K8S部署Prometheus
    前言:zabbix与prometheus的区别和Zabbix类似,Prometheus也是一个近年比较火的开源监控框架,和Zabbix不同之处在于Prometheus相对更灵活点,模块间比较解耦,比如告警模块、代理......