Prometheus与zabbix相比,它的强大之处就在于可以它可以使用的很多计算公式去获取自己需要的数据。当然,这里所涉及到的计算公式,也是我们普遍认为的难点所在。比如,我们要获取CPU使用率,使用zabbix就可以轻易获得,但是在Prometheus中却需要通过计算公式来完成CPU使用率的计算。

如果要统计CPU的使用:node_exporter会抓取CPU常用你的8种状态的累计工作时间,然后再用(所有非空闲状态的CPU时间总和)/(所有状态的CPU时间总和)= CPU使用率。而如果想要获取中间某一分钟的CPU使用时间还需要用到Counter数据类型。由于Counter的数据一致是增量,所以需要截取其中一段增量值,然后再拿这个值去套用公式进行计算。

一、常用函数

Prometheus为不同的数据提供了非常多的计算函数,其中有个小技巧就是遇到counter数据类型,在做任何操作之前,先套上一个rate()或者increase()函数。下面是一些比较常用的函数:

1、rate函数

rate() 函数是专门搭配counter数据类型使用函数,功能是取counter在这个时间段中平均每秒的增量。

例如:获取eth0网卡1m内每秒流量的平均值

rate(node_network_receive_bytes_total{device="eth0"}[1m])

2、increase函数

increase() 函数表示某段时间内数据的增量

rate() 函数则表示某段时间内数据的平均值

两个函数如何选取使用?

当我们获取数据比较精细的时候 类似于1m取样推荐使用rate()

当我们获取数据比较粗糙的时候 类似于5m,10m甚至更长时间取样推荐使用increase()

例如:获取eth0网卡1m内流量的增量

increase(node_network_receive_bytes_total{device="eth0"}[1m])

3、sum函数

sum()函数就是求和函数,注意点是当你使用sum后是将所有的监控的服务器的值进行取和,所以当我们只看某一台时需要进行拆分

拆分常用方法:

by increase()
2 by (cluster_name) 属于自定义标签不是标准标签,我们可以手动将不痛功能的服务器进行分组展示
例如:获取所有主机eth0网卡1m内每秒流量的平均值的和

sum(rate(node_network_receive_bytes_total{device="eth0"}[1m]))

4、topk函数

topk() 函数的定义是:取前面x位的最高值,最简单理解就是数学的top3 ,当我们有很多服务器我们想要获取某个key的数据排在前3位的服务器。

Gauge类型使用方式:

topk(3,key)

Counter类型使用方式

topk(3,rate(key[1m]))

注意:此种函数获得数据并不是很适用图形化展示

5、count函数

count() 是找出当前或者历史数据中某个key的数值大于或小于某个值的统计,

例如:

count(node_netstat_Tcp_CurrEstab >50)

6、irate函数

irate(v range-vector)计算范围向量中时间序列的每秒即时增长率。这基于最后两个数据点。单调性中断(例如由于目标重启而导致的计数器重置)会自动调整

例如:5m内http请求的每秒速率

irate(http_requests_total{job=”linux-01″}[5m])

irate只应在绘制易失性快速移动计数器时使用。使用rate警报和缓慢移动的柜台,因为在房价短暂变化可以重设FOR条款和图表完全由罕见尖峰难以阅读。

注意,当irate()与 聚合运算符(例如sum())或随时间聚合的函数(以任何结尾的函数_over_time)组合时,总是先取irate()第一个,然后聚合。否则irate()在目标重启时无法检测到计数器重置。

二、CPU使用率的计算方法

1、CPU模式
一颗CPU要通过分时复用的方式运行于不同的模式中,这些模式可以用我们常用的top命令进行查看,其中包括:

us:用户进程使用cpu的时间
sy:内核进程使用cpu的时间
ni:用户进程空间内改变过优先级的进程使用的cpu时间
id:空闲(没人用)的cpu时间
wa:等待io的cpu时间
hi:硬中断的cpu时间
si:软中断的cpu时间
st:虚拟机管理程序使用的cpu时间
这些时间加在一起是总的cpu时间。

2、CPU时间
通过node-exporter抓取的指标中cpu相关主要是各个node_cpu_seconds_total,可以通过如下的方式查看所有的metrics。

curl http://localhost:9100/metrics

在请求之后,会返回各种监控的内容,这里只截取出cpu相关的部分。

# HELP node_cpu_seconds_total Seconds the cpus spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 26659.41
node_cpu_seconds_total{cpu="0",mode="iowait"} 4.79
node_cpu_seconds_total{cpu="0",mode="irq"} 0
node_cpu_seconds_total{cpu="0",mode="nice"} 0
node_cpu_seconds_total{cpu="0",mode="softirq"} 2.69
node_cpu_seconds_total{cpu="0",mode="steal"} 0
node_cpu_seconds_total{cpu="0",mode="system"} 31.65
node_cpu_seconds_total{cpu="0",mode="user"} 8.67
node_cpu_seconds_total{cpu="1",mode="idle"} 26634.43
node_cpu_seconds_total{cpu="1",mode="iowait"} 54.14
node_cpu_seconds_total{cpu="1",mode="irq"} 0
node_cpu_seconds_total{cpu="1",mode="nice"} 0.02
node_cpu_seconds_total{cpu="1",mode="softirq"} 1.23
node_cpu_seconds_total{cpu="1",mode="steal"} 0
node_cpu_seconds_total{cpu="1",mode="system"} 34.07
node_cpu_seconds_total{cpu="1",mode="user"} 9
node_cpu_seconds_total{cpu="2",mode="idle"} 26629.89
node_cpu_seconds_total{cpu="2",mode="iowait"} 6.57
node_cpu_seconds_total{cpu="2",mode="irq"} 0
node_cpu_seconds_total{cpu="2",mode="nice"} 0
node_cpu_seconds_total{cpu="2",mode="softirq"} 1.95
node_cpu_seconds_total{cpu="2",mode="steal"} 0
node_cpu_seconds_total{cpu="2",mode="system"} 24.66
node_cpu_seconds_total{cpu="2",mode="user"} 7.2
node_cpu_seconds_total{cpu="3",mode="idle"} 26699.96
node_cpu_seconds_total{cpu="3",mode="iowait"} 5.72
node_cpu_seconds_total{cpu="3",mode="irq"} 0
node_cpu_seconds_total{cpu="3",mode="nice"} 0.01
node_cpu_seconds_total{cpu="3",mode="softirq"} 1.27
node_cpu_seconds_total{cpu="3",mode="steal"} 0
node_cpu_seconds_total{cpu="3",mode="system"} 22.32
node_cpu_seconds_total{cpu="3",mode="user"} 7.33

上面的某一行就是某一核cpu的某个模式的运行时间,单位是。把某一核各个模式的cpu时间加起来就是执行uptime得到的系统开机以来运行运行的总的秒数了。例如:

node_cpu_seconds_total{cpu=”0″,mode=”idle”} 26659.41

3、推导CPU使用率的公式

1)cpu0 5分钟内处于空闲状态的时间

increase(node_cpu_seconds_total{cpu=”0″,mode=”idle”}[5m])
1
increase表示增量,所以这个公式表示的是当前时间点的node_cpu_seconds_total减去5分钟之前的node_cpu_seconds_total的值,也就是这5分钟内处于idle状态的cpu时间。

2)cpu0 5分钟内处于空闲状态的时间占比:

increase(node_cpu_seconds_total{cpu=”0″,mode=”idle”}[5m]) / increase(node_cpu_seconds_total{cpu=”0″}[5m])
3)一台主机所有cpu 5分钟内处于空闲状态的时间占比:

sum (increase(node_cpu_seconds_total{mode=”idle”}[5m])) / sum (increase(node_cpu_seconds_total{mode=”idle”}[5m]))

4)如果 Prometheus 监控多台主机,要根据每台主机做 sum:

sum (increase(node_cpu_seconds_total{mode=”idle”}[5m])) by (instance) / sum (increase(node_cpu_seconds_total[5m])) by (instance)
1
5)cpu使用率 = 1 – cpu空闲率

100 * (1 – sum (increase(node_cpu_seconds_total{mode=”idle”}[5m])) by (instance) / sum (increase(node_cpu_seconds_total[5m])) by (instance))
1
6)根据irate()函数,可以简化计算公式为:

100 – (avg(irate(node_cpu_seconds_total{mode=”idle”}[5m])) by (instance) * 100)

三、常用计算公式

1、CPU使用率

100 – (avg(irate(node_cpu_seconds_total{mode=”idle”}[5m])) by (instance) * 100)
2、空闲内存剩余率

(node_memory_MemFree_bytes+node_memory_Cached_bytes+node_memory_Buffers_bytes) / node_memory_MemTotal_bytes * 100
3、内存使用率

100 – (node_memory_MemFree_bytes+node_memory_Cached_bytes+node_memory_Buffers_bytes) / node_memory_MemTotal_bytes * 100
4、磁盘使用率

100 – (node_filesystem_free_bytes{mountpoint=”/”,fstype=~”ext4|xfs”} / node_filesystem_size_bytes{mountpoint=”/

我是乐乐,关注乐维社区,学习prometheus不迷路,专注Zabbix和prometheus技术研究与分享,更多开源技术内容敬请留意后续文章,或查阅乐维技术文档。如有prometheus问题还可以到乐维社区提问留言,一起交流开源技术心得。

Prometheus技术分享——prometheus的函数与计算公式详解的更多相关文章

  1. PHP截取字符串函数substr()函数实例用法详解

    在PHP中有一项非常重要的技术,就是截取指定字符串中指定长度的字符.PHP对于字符串截取可以使用PHP预定义函数substr()函数来实现.下面就来介绍一下substr()函数的语法及其应用. sub ...

  2. (八)open函数的flag详解

    3.1.4.open函数的flag详解13.1.4.1.读写权限:O_RDONLY O_WRONLY O_RDWR(1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明(譬 ...

  3. 节点地址的函数list_entry()原理详解

    本节中,我们继续讲解,在linux2.4内核下,如果通过一些列函数从路径名找到目标节点. 3.3.1)接下来查看chached_lookup()的代码(namei.c) [path_walk()> ...

  4. JS中的函数节流throttle详解和优化

    JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...

  5. seo网页加速技术,预加载 DNS Prefetching 详解

    seo网页加速技术,预加载 DNS Prefetching 详解 DNS Prefetching 是什么 : DNS 是什么-- Domain Name System,域名系统,作为域名和IP地址相互 ...

  6. jQuery height()、innerHeight()、outerHeight()函数的区别详解

    参考来源:http://www.jb51.net/article/84897.htm 代码示例(可复制到编辑器直接打开): <!DOCTYPE html> <html lang=&q ...

  7. c++中内存拷贝函数(C++ memcpy)详解

    原型:void*memcpy(void*dest, const void*src,unsigned int count); 功能:由src所指内存区域复制count个字节到dest所指内存区域. 说明 ...

  8. 【转】Python的hasattr() getattr() setattr() 函数使用方法详解

    Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...

  9. Oracle排名函数(Rank)实例详解

    这篇文章主要介绍了Oracle排名函数(Rank)实例详解,需要的朋友可以参考下     --已知:两种排名方式(分区和不分区):使用和不使用partition --两种计算方式(连续,不连续),对应 ...

  10. mysql 聚集函数 count 使用详解

    mysql 聚集函数 count 使用详解 本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName) 2.distinct 与 coun ...

随机推荐

  1. 遥感图像处理笔记之【U-Net for Semantic Segmentation on Unbalanced Aerial Imagery】

    遥感图像处理学习(5) 前言 遥感系列第5篇.遥感图像处理方向的学习者可以参考或者复刻 本文初编辑于2023年12月15日 2024年1月24日搬运至本人博客园平台 文章标题:U-Net for Se ...

  2. (C语言)每日代码||2023.12.23||for循环的循环条件部分可以是数字

    #include <stdio.h> int main() { int i = 3; for (i; i; i--) { printf("%d\n", i); } // ...

  3. 今天才知道 Ping 命令的意义

    当ping一个域名的时候  可以知道这个域名的解析情况,也可以知道 当前电脑是否联通了 域名. 可以看到 diandaxia.com 的域名解析是 112.124.182.113 ,而www.dian ...

  4. 剑指Offer07 重建二叉树

    剑指 Offer 07. 重建二叉树 前置概念: 前序:访问根节点,先序遍历左子树,先序遍历右子树: 中序:中序遍历左子树,访问根节点,中序遍历右子树: 后序:后序遍历左子树,后序遍历右子树,访问根节 ...

  5. 鸿蒙开发游戏(三)---大鱼吃小鱼(放置NPC)

    效果图 添加了一个NPC(小红鱼),玩家控制小黄鱼 鸿蒙开发游戏(一)---大鱼吃小鱼(界面部署) 鸿蒙开发游戏(二)---大鱼吃小鱼(摇杆控制) 鸿蒙开发游戏(三)---大鱼吃小鱼(放置NPC) 鸿 ...

  6. Vue实现简单图书管理例子

    以下内容整理自网络. 说明 本例主要涵盖以下知识点: 数据绑定 条件与循环 计算属性 监听器 过滤器 常见数组和对象操作 vue生命周期 示例演示 代码 <!DOCTYPE html> & ...

  7. Spring源码之-AOP

    目录 一.大话AOP 1.AOP的概念 2.必要的准备工作 什么是代理模式? 3.大话AOP 那么AOP 具体是什么呢? 实现AOP的方式 二.动态AOP自定义标签 1.JDK动态代理 2.CGLIB ...

  8. spring源码学习之设计模式

    目录 1.策略模式 2.观察者模式 3.装饰者模式 4.工厂模式 工厂方法模式 抽象工厂模式 工厂方法和抽象工厂的异同 5.单例模式 6.适配器模式与外观模式 适配器模式 外观模式 7.模板方法模式 ...

  9. win32 - 控制台聊天

    仅适用于同一台电脑的两个进程聊天,对于不同电脑之前的聊天需要依靠tcp/ip协议. 两个进程是通过发送WM_COPYDATA 消息来传输字节的. 代码如下: Server.cpp #include & ...

  10. String--getline()

    #include <string> #include <sstream> #include <iostream> int main() { std::wstring ...