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