【prometheus】计算CPU使用率

prometheus版本

版本信息: 2.19

  • prometheus版本不一样,语法会有些区别,注意查看官方文档:

    https://prometheus.io/docs/prometheus/latest/getting_started/

CPU使用率公式

我们知道
CPU的使用率 = (所有非空闲状态CPU使用时间总和 )/(所有状态CPU时间总和)
由下列公式:
CPU使用时间总和 = 所有非空闲状态CPU使用时间总和 + 所有空闲状态CPU使用时间总和
得出:
CPU的使用率 = 1 - (所有空闲状态CPU使用时间总和 )/(所有状态CPU时间总和)

prometheus计算CPU使用率

CPU使用时间总和

输入:node_cpu_seconds_total
在这里插入图片描述

✔node_cpu_seconds_total{cpu="1",endpoint="https",instance="192.168.145.12",job="node-exporter",mode="user",namespace="monitoring",pod="node-exporter-v9v68",service="node-exporter"}

可以看出,node_cpu_seconds_total采样数据,包含信息主要由cpu编号、实例名称、各种时间状态,所以需要根据上述key的值通过、计算、过滤、聚合等方式,最终得出我们所需要值。

CPU使用时间5m增量值

5m增量值 = 当前采样点值 - 5m前的采样点值

输入: increase(node_cpu_seconds_total[5m])
在这里插入图片描述

CPU空闲使用时间5m增量值

node_cpu_seconds_total的采样数据,是基于实现序列的key/vlue格式,而key是"mode"记录了采样的是CPU的那种状态的时间(user、sys、idle等)
输入:increase(node_cpu_seconds_total{mode=“idle”}[5m])
在这里插入图片描述

聚合多核CPU空闲使用时间5m增量值

采样数据:

{cpu="1",endpoint="https",instance="192.168.145.12",job="node-exporter",mode="idle",namespace="monitoring",pod="node-exporter-v9v68",service="node-exporter"}{cpu="1",endpoint="https",instance="192.168.145.11",job="node-exporter",mode="idle",namespace="monitoring",pod="node-exporter-2jwj2",service="node-exporter"}{cpu="1",endpoint="https",instance="192.168.145.10",job="node-exporter",mode="idle",namespace="monitoring",pod="node-exporter-qls6j",service="node-exporter"}{cpu="0",endpoint="https",instance="192.168.145.12",job="node-exporter",mode="idle",namespace="monitoring",pod="node-exporter-v9v68",service="node-exporter"}{cpu="0",endpoint="https",instance="192.168.145.11",job="node-exporter",mode="idle",namespace="monitoring",pod="node-exporter-2jwj2",service="node-exporter"}{cpu="0",endpoint="https",instance="192.168.145.10",job="node-exporter",mode="idle",namespace="monitoring",pod="node-exporter-qls6j",service="node-exporter"}

通过采样数据,可以看到CPU空闲使用时间5m增量值,是每个cpu和都作为一个采样值,而每个虚机\物理机,大部分是多核的,所以需要聚合所有核数的空闲时间。

输入:sum(increase(node_cpu_seconds_total{mode=“idle”}[5m]))
在这里插入图片描述
通过结果可以看出,sum函数是将所有CPU核数时间想加,没有按照主机进行聚合,所以这时就需要引入=by (instance)=函数,它会把sum求和到一起的数值按照指定方式进行拆分,instance代表的是机器名。如果不写by (instance)的话就需要在{}中写明需要哪个实例的数据。

输入:sum(increase(node_cpu_seconds_total{mode=“idle”}[5m])) by (instance)

在这里插入图片描述

聚合多核CPU总使用时间5m增量值

输入:sum(increase(node_cpu_seconds_total[5m])) by (instance)
在这里插入图片描述

计算平均5分钟CPU使用率

输入:1- sum(increase(node_cpu_seconds_total{mode=“idle”}[5m])) by (instance)/sum(increase(node_cpu_seconds_total[5m])) by (instance)
在这里插入图片描述

使用prometheus的node_cpu_utilisation值

输入:instance:node_cpu_utilisation:rate1m

在这里插入图片描述

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

^白开水^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值