Prometheus promQL查询语言

Prometheus提供了一种名为PromQL (Prometheus查询语言)的函数式查询语言,允许用户实时选择和聚合时间序列数据。表达式的结果既可以显示为图形,也可以在Prometheus的表达式浏览器中作为表格数据查看,或者通过HTTP API由外部系统使用。

准备工作

在进行查询,这里提供下我的配置文件如下

[root@node00 prometheus]# cat prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every seconds. Default is every minute.
evaluation_interval: 15s # Evaluate rules every seconds. The default is every minute.
# scrape_timeout is set to the global default (10s). # Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager: # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus' # metrics_path defaults to '/metrics'
# scheme defaults to 'http'. static_configs:
- targets: ['localhost:9090']
- job_name: "node"
file_sd_configs:
- refresh_interval: 1m
files:
- "/usr/local/prometheus/prometheus/conf/node*.yml"
remote_write:
- url: "http://localhost:8086/api/v1/prom/write?db=prometheus" remote_read:
- url: "http://localhost:8086/api/v1/prom/read?db=prometheus" [root@node00 prometheus]# cat conf/node-dis.yml
- targets:
- "192.168.100.10:20001"
labels:
__datacenter__: dc0
__hostname__: node00
__businees_line__: "line_a"
__region_id__: "cn-beijing"
__availability_zone__: "a"
- targets:
- "192.168.100.11:20001"
labels:
__datacenter__: dc1
__hostname__: node01
__businees_line__: "line_a"
__region_id__: "cn-beijing"
__availability_zone__: "a"
- targets:
- "192.168.100.12:20001"
labels:
__datacenter__: dc0
__hostname__: node02
__businees_line__: "line_c"
__region_id__: "cn-beijing"
__availability_zone__: "b"

简单时序查询

直接查询特定metric_name

# 节点的forks的总次数
node_forks_total
#结果如下

Element Value
node_forks_total{instance="192.168.100.10:20001",job="node"} 201518
node_forks_total{instance="192.168.100.11:20001",job="node"} 23951
node_forks_total{instance="192.168.100.12:20001",job="node"} 24127
 

带标签的查询

node_forks_total{instance="192.168.100.10:20001"}
# 结果如下
Element Value
node_forks_total{instance="192.168.100.10:20001",job="node"} 201816

多标签查询

node_forks_total{instance="192.168.100.10:20001",job="node"}

# 结果如下
Element Value
node_forks_total{instance="192.168.100.10:20001",job="node"} 201932

查询2分钟的时序数值

node_forks_total{instance="192.168.100.10:20001",job="node"}[2m]
Element Value
node_forks_total{instance="192.168.100.10:20001",job="node"} 201932 @1569492864.036
201932 @1569492879.036
201932 @1569492894.035
201932 @1569492909.036
201985 @1569492924.036
201989 @1569492939.036
201993 @1569492954.036

正则匹配

node_forks_total{instance=~"192.168.*:20001",job="node"}
Element Value
node_forks_total{instance="192.168.100.10:20001",job="node"} 202107
node_forks_total{instance="192.168.100.11:20001",job="node"} 24014
node_forks_total{instance="192.168.100.12:20001",job="node"} 24186

常用函数查询

官方提供的函数比较多, 具体可以参考地址如下: https://prometheus.io/docs/prometheus/latest/querying/functions/

这里主要就常用函数进行演示。

irate

irate用于计算速率。

# 通过标签查询,特定实例特定job,特定cpu 在idle状态下的cpu次数速率
irate(node_cpu_seconds_total{cpu="",instance="192.168.100.10:20001",job="node",mode="idle"}[1m])
Element Value
{cpu="0",instance="192.168.100.10:20001",job="node",mode="idle"} 0.9833988932595507

count_over_time

计算特定的时序数据中的个数。

# 这个数值个数和采集频率有关, 我们的采集间隔是15s,在一分钟会有4个点位数据。
count_over_time(node_boot_time_seconds[1m])
Element Value
{instance="192.168.100.10:20001",job="node"} 4
{instance="192.168.100.11:20001",job="node"} 4
{instance="192.168.100.12:20001",job="node"} 4

子查询

# 过去的10分钟内, 每分钟计算下过去5分钟的一个速率值。 一个采集10m/1m一共10个值。
rate(node_cpu_seconds_total{cpu="",instance="192.168.100.10:20001",job="node",mode="idle"}[5m])[10m:1m]
Element Value
{cpu="0",instance="192.168.100.10:20001",job="node",mode="idle"} 0.9865228543057867 @1569494040
0.9862807017543735 @1569494100
0.9861087231885309 @1569494160
0.9864946894550303 @1569494220
0.9863192502430038 @1569494280
0.9859649122807017 @1569494340
0.9859298245613708 @1569494400
0.9869122807017177 @1569494460
0.9867368421052672 @1569494520
0.987438596491273 @1569494580

复杂查询

计算内存使用百分比

node_memory_MemFree_bytes / node_memory_MemTotal_bytes  *  
Element Value
{instance="192.168.100.10:20001",job="node"} 9.927579722322251
{instance="192.168.100.11:20001",job="node"} 59.740727403673034
{instance="192.168.100.12:20001",job="node"} 63.2080982675149

获取所有实例的内存使用百分比前2个

topk(,node_memory_MemFree_bytes / node_memory_MemTotal_bytes  *  )
Element Value
{instance="192.168.100.12:20001",job="node"} 63.20129636298163
{instance="192.168.100.11:20001",job="node"} 59.50586164125955

实用查询样例

获取cpu核心个数

# 计算所有的实例cpu核心数
count by (instance) ( count by (instance,cpu) (node_cpu_seconds_total{mode="system"}) )
# 计算单个实例的
count by (instance) ( count by (instance,cpu) (node_cpu_seconds_total{mode="system",instance="192.168.100.11:20001"})

计算内存使用率

( - (node_memory_MemAvailable_bytes{instance=~"192.168.100.10:20001"} / (node_memory_MemTotal_bytes{instance=~"192.168.100.10:20001"})))* 100
Element Value
{instance="192.168.100.10:20001",job="node"} 87.09358620413717
 

计算根分区使用率

 - ((node_filesystem_avail_bytes{instance="192.168.100.10:20001",mountpoint="/",fstype=~"ext4|xfs"} * ) / node_filesystem_size_bytes {instance=~"192.168.100.10:20001",mountpoint="/",fstype=~"ext4|xfs"})
Element Value
{device="/dev/mapper/centos-root",fstype="xfs",instance="192.168.100.10:20001",job="node",mountpoint="/"} 4.175111443575972

预测磁盘空间

 # 整体分为 2个部分, 中间用and分割, 前面部分计算根分区使用率大于85的, 后面计算根据近6小时的数据预测接下来24小时的磁盘可用空间是否小于0 。
(- node_filesystem_avail_bytes{fstype=~"ext4|xfs",mountpoint="/"}
/ node_filesystem_size_bytes{fstype=~"ext4|xfs",mountpoint="/"}) * >= and (predict_linear(node_filesystem_avail_bytes[6h], * ) < )

prometheus学习系列七: Prometheus promQL查询语言的更多相关文章

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

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

  2. Prometheus学习系列(九)之Prometheus 存储

    前言 本文来自Prometheus官网手册 和 Prometheus简介 存储 Prometheus是一个本地磁盘时间序列数据库,但也可选择与远程存储系统集成,其本地时间序列数据库以自定义格式在磁盘上 ...

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

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

  4. Prometheus学习系列(一)之Prometheus简介

    前言 本文来自Prometheus官网手册 和 Prometheus简介 什么是prometheus? Prometheus是一个最初在SoundCloud上构建的开源系统监视和警报工具包.自2012 ...

  5. prometheus学习系列一: Prometheus简介

    Prometheus简介 prometheus受启发于Google的Brogmon监控系统(相似kubernetes是从Brog系统演变而来), 从2012年开始由google工程师Soundclou ...

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

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

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

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

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

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

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

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

随机推荐

  1. js判断客户端是iOS还是Android移动终端

    前段时间,小颖公司需要实现:用户在微信中打开一个html5,在该html5中通过点击下载按钮,Android手机会跳到Android的下载地址,IOS会跳转到IOS下载地址,其它则跳转到另一个指定地址 ...

  2. 终于有人把elasticsearch原理讲通了

    转自 小史是一个非科班的程序员,虽然学的是电子专业,但是通过自己的努力成功通过了面试,现在要开始迎接新生活了. 随着央视诗词大会的热播,小史开始对诗词感兴趣,最喜欢的就是飞花令的环节. 但是由于小史很 ...

  3. sql server数据表大小初始化

    sql server表在存储大数据和处理大数据表时,经常会遇到表空间越来越大,有时候会超出应该占有空间大小很多,此时如果表数据是压缩存储的,那么重新执行一下压缩脚本,数据的大小会重新初始化,然后再使用 ...

  4. Docker容器内部端口映射到外部宿主机端口 - 运维笔记

    Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务.容器启动之后,容器中可以运行一些网络应用,通过-p或-P参数来指定端口映射. 注意:宿主机的一个端口只能映射到容器内部的某一个端口 ...

  5. 66 网络编程(五)——TCP多线程实现多人聊天室

    思路 客户端读写各一个类,可以使内部类,实现Runnable.读写类都与服务器端建立连接,一个收,一个发. 客户端实现接收和转发.多线程实现每个客户端的连接(使与各客户端的连接独立). 服务器端中创建 ...

  6. C++强大背后

    转自MiloYip大神的博客 [原文]http://www.cnblogs.com/miloyip/archive/2010/09/17/behind_cplusplus.html 在31年前(197 ...

  7. [SOJ #686]抢救(2019-11-7考试)/[洛谷P3625][APIO2009]采油区域

    题目大意 有一个\(n\times m\)的网格,\((x,y)\)权值为\(a_{x,y}\),要求从中选取三个不相交的\(k\times k\)的正方形使得它们权值最大.\(n,m,k\leqsl ...

  8. mybatis插入数据后返回自增主键ID详解

    1.场景介绍: ​ 开发过程中我们经常性的会用到许多的中间表,用于数据之间的对应和关联.这个时候我们关联最多的就是ID,我们在一张表中插入数据后级联增加到关联表中.我们熟知的mybatis在插入数据后 ...

  9. 简单实现python调用c#dll动态链接库

    在python调用c#dll库时要先安装库clr,即安装pythonnet,参考文章:https://www.cnblogs.com/kevin-Y/p/10235125.html(为在python中 ...

  10. 【转】socket通信-C#实现udp通讯

    在日常碰到的项目中,有些场景下不适合使用tcp常连接,而需要靠UDP无连接的数据收发.那么如何使用SharpSocket完成UDP收发数据呢?其中要掌握的关键点是什么呢? 点击查看原博文内容