pmm系列~自定义插件监控

一 环境准备
pip install flask
pip install --ignore-installed PyYAML
pip install prometheus_client
二 包引入
from prometheus_client import Gauge
from prometheus_client.core import CollectorRegistry
from flask import Response, Flask
三 关键函数和输出
1 Gauge()为metrics的一种 代表了某个指标当前的值 适用于健康检测 用法 Gauge(名称,用法描述)
2 函数说明 Gauge = init(name, documentation, labelnames=(), namespace=u'', subsystem=u'', registry=<prometheus_client.core.CollectorRegistry object>, **kwargs)
3 代码示例
Gauge("server_port_up", "Api response stats is:", ["sertype","host", "port"], registry=REGISTRY)
redis_sentinel{host="IP",port="PORT",type="REDIS"} 1.0
说明 REGISTRY = CollectorRegistry(auto_describe=False)//创建一个仓库用来存储数据
registry=REGISTRY //绑定仓库
”server_port_up“ 为输出key的命名 请注意不能有中横线可以有下划线
"Api response stats is:" 为key的描述,可以为任意,不会输出
["sertype","host", "port"] 为输出的标签名
muxStatus.labels(sertype,host, port).set(status) 设置健康状态关键值检测
输出值获取 prometheus_client.generate_latest(REGISTRY) 获取仓库最近的值,监控值
四 总结
1 利用flask框架提供WEN API接口服务,路径是/metrics
2 接下来还需要做两个事情
1 监控服务进行配置

- job_name: redis_sentinel
  scrape_interval: 10s  //访问间隔,根据实际需求 设定
  scrape_timeout: 1s
  metrics_path: /metrics//自定义插件的访问路径
  scheme: http
  static_configs:
  - targets:
  - host:port//自定义插件的web地址端口

通过工具进行检测是否格式有问题

3 报警进行配置
  ALERT redis_sentinel
  IF redis_sentinel == 0
  FOR 30s
  LABELS {
  severity = "major“
  }
 ANNOTATIONS {
 summary = "{{$labels.host}}:{{$labels.port}}",
 description = "{{$labels.host}}:{{$labels.port}} is down"
  }
4 补充
  有任何问题可以i查看/var/log/prometheus.log 
 
 
 

posted @ 2020-11-27 17:35  开心的蛋黄派  阅读(308)  评论(0编辑  收藏  举报