Node exporter
光搭建好prometheus_server 是不够的,需要给监控节点搭建一个exporter 用来采样数据
node_exporter 是一个以http_server方式运行在后台,并且持续不断采集Linux系统中各种操作系统本身相关的监控参数的程序,主要用于暴露 metrics 给 Prometheus,其中 metrics 包括:cpu 的负载,内存的使用情况,网络等。
其采集量是很大很全的,往往默认的采集项目就远超过你的实际需求

安装 node export


下载node_exporter 从官网 https://prometheus.io/download/#node_exporter下载之后 解压缩 然后直接运行即可

# tar xvzf node_exporter-0.17.0.linux-amd64.tar.gz

# cd node_exporter-0.17.0.linux-amd64/

# mv node_exporter-0.17.0.linux-amd64/ node_exporter

#./node_exporter --web.listen-address=:20015 & -----指定端口

做成服务 方便管理

useradd prometheus -s /sbin/nologin ----创建启动用户

vim /usr/lib/systemd/system/node_exporter.service ---配置启动脚本

[Service]

ExecStart=/home/node_exporter/node_exporter --web.listen-address=:20015

[Install]

WantedBy=multi-user.target

[Unit]

Description=node_exporter

After=network.target

启动,查看状态,配置开机启动


systemctl enable node_exporter ---设置开机启动

systemctl daemon-reload #重新加载一下配置。每次改动后也要重新加载

systemctl start node_exporter

systemctl status node_exporter

查看 node exporter 状态:

此时,node exporter 已经监听在 9100 端口。可以响应 prometheus_server发过来的 HTTP_GET请求

也可以响应其他方式的 HTTP_GET请求,我们自己就可以发送测试

验证 node exporter

当 node exporter 启动时,可以通过 curl http://localhost:9100/metrics 或者在浏览器中查看 ubuntu server 里面的 metrics,部分 metrics 信息如下:

终端访问:

# curl http://localhost:9100/metrics……

# HELP node_cpu Seconds the cpus spent in each mode.

# TYPE node_cpu counter

node_cpu{cpu="cpu0",mode="guest"} 0

node_cpu{cpu="cpu0",mode="idle"} 30.02

node_cpu{cpu="cpu0",mode="iowait"} 0.5

浏览器访问:


来看下一个 难一些的例子:

CPU使用率的获取方式: node_cpu

这个 key 也是node_exporter返回的一个 用来统计 CPU使用率的


问题:

CPU不是应该是使用率吗 ? 类似 百分 50% 80%这样的数据才对啊
怎么返回是一个持续不断累加的 近似于直线的 庞大的数字呢?

答案

这个其实关系到 prometheus对Linux数据采集的精细特性

prometheus对Linux CPU的采集并不是直接返回一个现成的CPU百分比,而是返回Linux中很底层的 cpu时间片 累积数值 这样一个数据(平时用惯了 top / uptime这种简便的方式看CPU使用率, 往往浅尝辄止 根本没有好好深入理解 所谓的CPU使用率)

如果想真的弄明白CPU的使用率这个概念 在Linux中 要先从CPU时间 这个概念开始建立

Linux中 CPU时间 实际是指 :

从操作系统开启算起 CPU就开始工作了 并记录自己在工作中 总共使用的"时间"累积量 把它保存在系统中 而累积的CPU使用时间 还会分成几个重要的状态类型

比如 CPU time => 分成 CPU user time / sys time / nice time / idle time / irq / 等等。。。 翻译过来就是 CPU 用户态使用时间, 系统/内核态使用时间, nice值分配使用时间, 空闲 时间, 中断时间 等等

那么所谓的 CPU使用率 是什么意思呢?

CPU使用率 最准确的定义 其实是 CPU各种状态中 除了idle(空闲) 这个状态外, 其他所有的CPU状态的加合 / 总CPU时间

得出来的 就是我们所说的 CPU使用率 (运行的任务 用户 内核)

回到刚才使用 node_cpu 这个key 如果直接输入进去

他返回的是 CPU各个核 各个状态下 从开机开始 一直累积下来的 CPU使用 时间 的累积值

所以才看到这么一个Node Exporter兼容 centos6吗_数据

对各个CPU状态的时间单位的解释(网上截取的一段解释)


如果在prometheus中 想对CPU的使用率 准确的来查询 正确的方法如下:

cpu空闲率:

avg(rate(node_cpu_seconds_total{mode="idle"}[1m])) by (cluster,instance,nodename)


prometheus的这种精细的 底层的计算特性 虽然学起来难

不过带来的 好处也是 显而易见

1) prometheus 这种底层数据采集 所形成的监控 其实是最准确 最可信的

2) prometheus 本身也逼着使用它的运维同学 你不踏实下来 好好的真正把Linux技术学过关 的话 你就没有办法使用好 这个超强力的监控工具了