在前面的文章已经写了官方的几个exporter的使用了。 在实际使用环境中,我们可能需要收集一些自定义的数据, 这个时候我们一般是需要自己编写采集器的。

快速入门编写一个入门的demo

编写代码

from prometheus_client import Counter, Gauge, Summary, Histogram, start_http_server

# need install prometheus_client

if __name__ == '__main__':
c = Counter('cc', 'A counter')
c.inc() g = Gauge('gg', 'A gauge')
g.set(17) s = Summary('ss', 'A summary', ['a', 'b'])
s.labels('c', 'd').observe(17) h = Histogram('hh', 'A histogram')
h.observe(.6) start_http_server(8000)
import time while True:
time.sleep(1)

只需要一个py文件, 运行起来, 会监听在8000端口,访问127.0.0.1:8000端口。

效果图

其实一个导出器就已经写好了, 就是这么简单的,我们只需要在prometheus配置来采集对应的导出器就可以了。 不过我们的导出的数据都是没有实际意义了。

数据类型介绍

Counter 累加类型, 只能上升,比如记录http请求的总数或者网络的收发包累计值。

Gauge: 仪表盘类型, 适合有上升有下降的, 一般网络流量,磁盘读写这些,会有波动和变化的采用这个数据类型。

Summary:  基于采样的,在服务端完成统计。我们在统计平均值的时候,可能以为某个值异常导致计算平均值不能准确反映实际值, 就需要特定的点位置。

Histogram: 基于采样的,在客户端完成统计。我们在统计平均值的时候,可能以为某个值异常导致计算平均值不能准确反映实际值, 就需要特定的点位置。

采集内存使用数据

编写采集类代码

from prometheus_client.core import GaugeMetricFamily, REGISTRY
from prometheus_client import start_http_server
import psutil class CustomMemoryUsaggeCollector():
def format_metric_name(self):
return 'custom_memory_' def collect(self):
vm = psutil.virtual_memory()
#sub_metric_list = ["free", "available", "buffers", "cached", "used", "total"]
sub_metric_list = ["free", "available", "used", "total"]
for sub_metric in sub_metric_list:
gauge = GaugeMetricFamily(self.format_metric_name() + sub_metric, '')
gauge.add_metric(labels=[], value=getattr(vm, sub_metric))
yield gauge if __name__ == "__main__":
collector = CustomMemoryUsaggeCollector()
REGISTRY.register(collector)
start_http_server(8001)
import time
while True:
time.sleep(1)

暴露数据情况

部署代码和集成prometheus

# 准备python3 环境 参考: https://virtualenvwrapper.readthedocs.io/en/latest/
yum install python36 -y pip3 install virtualenvwrapper
vim /usr/local/bin/virtualenvwrapper.sh
# 文件最前面添加如下行
# Locate the global Python where virtualenvwrapper is installed.
VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3" # 文件生效
source /usr/local/bin/virtualenvwrapper.sh
# 配置workon
[root@node01 ~]# echo "export WORKON_HOME=~/Envs" >>~/.bashrc [root@node01 ~]# mkvirtualenv custom_memory_exporter
(custom_memory_exporter) [root@node01 ~]# pip install prometheus_client psutil
yum install python36-devel (custom_memory_exporter) [root@node01 ~]# chmod a+x custom_memory_exporter.py
(custom_memory_exporter) [root@node01 ~]# ./custom_memory_exporter.py
# 测试是否有结果数据
[root@node00 ~]# curl http://192.168.100.11:8001/ prometheus.yml 加入如下片段
  - job_name: "custom-memory-exporter"
    static_configs:
    - targets: ["192.168.100.11:8001"] [root@node00 prometheus]# systemctl restart prometheus
[root@node00 prometheus]# systemctl status prometheu

查询效果图

prometheus学习系列十一: Prometheus 采集器的编写的更多相关文章

  1. prometheus学习系列十一: Prometheus pushgateway的使用

    由于网络问题或者安全问题,可能我们的数据无法直接暴露出一个entrypoint 给prometheus采集. 这个时候可能就需要一个pushgateway来作为中间者完成中转工作.  promethe ...

  2. prometheus学习系列十一: Prometheus 安全

    prometheus安全 我们这里说的安全主要是基本认证和https2种, 目前这2种安全在prometheus中都没有的, 需要借助第三方软件实现, 这里以nginx为例. 基本认证 配置基本认证 ...

  3. prometheus学习系列十一: Prometheus exporter详解

    exporter详解 前面的系列中,我们在主机上面安装了node_exporter程序,该程序对外暴露一个用于获取当前监控样本数据的http的访问地址, 这个的一个程序成为exporter,Expor ...

  4. prometheus学习系列十一: Prometheus和AlertManager的高可用

    前面的系列中, prometheus和alertmanager都是单机部署的,会有单机宕机导致系统不可用情况发生.本文主要介绍下prometheus和alertmanager的高可用方案. 服务的高可 ...

  5. prometheus学习系列十一: Prometheus 报警规则配置

    prometheus监控系统的的报警规则是在prometheus这个组件完成配置的. prometheus支持2种类型的规则,记录规则和报警规则, 记录规则主要是为了简写报警规则和提高规则复用的, 报 ...

  6. Prometheus学习系列(九)之Prometheus 联盟、迁移

    前言 本文来自Prometheus官网手册 和 Prometheus简介 FEDERATION 允许Prometheus服务器从另一台Prometheus服务器抓取选定的时间序列. 一,用例 联盟有不 ...

  7. Prometheus学习系列(六)之Prometheus 查询说明

    前言 本文来自Prometheus官网手册和 Prometheus简介 Prothetheus查询 Prometheus提供一个函数式的表达式语言PromQL (Prometheus Query La ...

  8. Prometheus学习系列(五)之Prometheus 规则(rule)、模板配置说明

    前言 本文来自Prometheus官网手册1.2.3.4和 Prometheus简介1.2.3.4 记录规则 一.配置规则 Prometheus支持两种类型的规则,这些规则可以定期配置,然后定期评估: ...

  9. Prometheus学习系列(二)之Prometheus FIRST STEPS

    前言 本文来自Prometheus官网手册 和 Prometheus简介 说明 Prometheus是一个监控平台,通过在监控目标上的HTTP端点来收集受监控目标的指标.本指南将向您展示如何使用Pro ...

随机推荐

  1. js 将图片转换为 base64

    var img = document.getElementById('s_lg_img'); function getBase64Image(img) { var canvas = document. ...

  2. 原生 JS 实现最简单的图片懒加载

    懒加载 什么是懒加载 懒加载其实就是延迟加载,是一种对网页性能优化的方式,比如当访问一个页面的时候,优先显示可视区域的图片而不一次性加载所有图片,当需要显示的时候再发送图片请求,避免打开网页时加载过多 ...

  3. R 语言解压目录下的所有gz文件

    setwd("GSE29431_RAW") # 进入目录 fileNames <- list.files() # 获取目录下的所有文件 sapply(fileNames, g ...

  4. 去除批次效应 sva

    Surrogate Variable Analysis http://www.bioconductor.org/packages/release/bioc/html/sva.html

  5. Oracle_其他人连接不上自己电脑

    1. 修改文件G:\app\eric\product\11.2.0\dbhome_1\NETWORK\ADMIN\listener.ora文件,listener中的address中加一个你的ip开头的 ...

  6. libevent笔记3:evbuffer

    evbuffer 之前提到bufferevent结构体提供两个缓存区用来为读写提供缓存,并自动进行IO操作.这两个缓存区是使用Libevent中的evbuffer实现的,同样,Libevent中也提供 ...

  7. how to compile and replace ubuntu kernel

    how to compile and replace ubuntu kernel 0. environment -ubuntu 1804 64bit 1. prepare source code su ...

  8. scaffold

    #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import argparse ...

  9. JavaScript的这个缺陷,让多少程序员为之抓狂?

    相信提到JavaScript语言,每一个程序员的心理状态都是不一样的,有的对此深恶痛绝,有的又觉得其可圈可点,造成这种两级分化态度的原因还是由于其自身类型约束上的缺陷,直到现如今依旧无法解决. 本文由 ...

  10. SSM 整合配置

    目录 1. Maven : pox.xml 2. Web container : web.xml 3. Spring context : dbconfig.properties + applicati ...