在前面的文章已经写了官方的几个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. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于GPRS模块AT指令TCP透传方式,定时访问升级(含有数据校验)

    实现功能概要 单片机定时使用http访问云端的程序版本,如果版本不一致, 然后通过http下载最新的升级文件,实现远程升级STM32程序. 兼容Air202 ,SIM800 测试准备工作(默认访问我的 ...

  2. 【字符串】 Z-algorithm

    Z-algorithm Algorithm Task 给定一个文本串 \(S\) 和一个模式串 \(T\),求 \(T\) 对于 \(S\) 的每个后缀子串的公共前缀子串. Limitations 要 ...

  3. spring学习-ApplicationContext-spring上下文深入理解

    4月份开始复习一遍spring相关知识.让自己巩固一下spring大法的深奥益处,所以就看了大佬的博客,转载留下来日后继续研读.认为重点的标记为红色 以下文章内容转载自:http://www.cnbl ...

  4. 【操作系统之十】内存分页管理与swap

    一.虚拟内存电脑里内存分内存条(这里我们叫物理内存)和硬盘,内存条保存程序运行时数据,硬盘持久保存数据.那么虚拟内存是什么? 程序运行会启动一个进程,进程里有程序段.全局数据.栈和堆,这些都会加载到内 ...

  5. c++篇 cad.grx 入门,手动配置编译环境

    安装vs2010+sp1补丁; 安装浩辰2018(64位版本); 下载浩辰Grx开发的SDK,注意对应版本年份., 解压到E盘目录下, E:\grxsdk 在他们的官方用户群下载,搜sdk, 找到gr ...

  6. 新添加的用户无法sudo解决方案

    问题:xxx(用户名) is not in the sudoers file. This incident will be reported. 用户管理: 1.创建用户 (1)方法1 $ sudo a ...

  7. python 基础 ---- 面向对象

    ------   面向对象的思想 三个基本特征: 封装(封装属性方法可以减少耦合)继承(可以抬高开发效率) 多态 主要包括 : 类 : 描述具有相同的属性和方法的对象的集合  变量:   类变量/ 成 ...

  8. Windows域的创建与加入教程

    一.说明 以前看书的时候时常看到“域”这个词,后来听一些渗透的演讲通常会说进入内网后首先要找域控.在认知中“域控”应该是域管理服务器一类的东西,但毕竟没有实现过所以还是不太懂长什么样. 前段时间朋友电 ...

  9. Java核心技术-读书笔记

    基本语法 Java中的所有函数都属于某个类的方法 Java没有任何无符号的int.long.short 或 byte 类型 浮点数值不适用于无法接受舍入误差的金融计算中,比如2.0-1.1不会输出想要 ...

  10. ElasticSearch 线程池类型分析之SizeBlockingQueue

    ElasticSearch 线程池类型分析之SizeBlockingQueue 尽管前面写好几篇ES线程池分析的文章(见文末参考链接),但都不太满意.但从ES的线程池中了解到了不少JAVA线程池的使用 ...