目录
2.2.1 使用prometheus web UI获取可用的metrics
我在《hualinux 进阶 prom 1-2.2:prometheus安装(手把手版)》文章中,在Grafana添加prometheus监控指标使用的是Grafana官方的Prometheus Dashboard 仪表盘,这样就用不手工添加了。
学习了前面的1-2.3至1-2-8,现在应该有能力在Grafana添加自定义的监控指标了,本章就简单实操一下
一、环境
我是直接在《hualinux 进阶 prom 1-2.2:prometheus安装(手把手版)》基础上对Grafana进行操作。
二、Grafana添加自定义监控
打开Grafana web登录界面 ip:3000,我这里是192.168.3.82:3000
在操作之前已经添加了prometheus数据源,我这里就不赘述。
2.1 借鉴别人的写法
因为之前我导入了Grafana官方的Prometheus Dashboard 仪表盘 可以直接看数据,我就拿其中一个图来测试
我就拿带宽测试吧
我们从上面的模板中得到下载和上传流量,表达式如下:
{{device}}_in_下载:irate(node_network_receive_bytes_total{instance=~'$node',device=~"$device"}[5m])*8
{{device}}_out_上传:irate(node_network_transmit_bytes_total{instance=~'$node',device=~"$device"}[5m])*8
从上面发现有两个变量 $node 和 $device 这个可以看一下设置中,点进去找到 变量Variables 发现如下图所示:
PS:如果有兴趣看Grafana相关的,可以看一下它的官方文档 Templates and variables
2.2 添加自定义监控
2.2.1 使用prometheus web UI获取可用的metrics
我们的metrics一般不用硬记的,这么多也记不住啊,只要记关键部分即可,打开prometheus web UI,我这里也是以上面网卡为例子,输入 node_network_receive_bytes_total{instance="192.168.3.21:9100"} 并执行,如下所示:
如果如下:
node_network_receive_bytes_total{device="datapath",instance="192.168.3.21:9100",job="centos8"} 1508 node_network_receive_bytes_total{device="docker0",instance="192.168.3.21:9100",job="centos8"} 0 node_network_receive_bytes_total{device="ens33",instance="192.168.3.21:9100",job="centos8"} 50069288 node_network_receive_bytes_total{device="ens34",instance="192.168.3.21:9100",job="centos8"} 832478849 node_network_receive_bytes_total{device="lo",instance="192.168.3.21:9100",job="centos8"} 1891295 node_network_receive_bytes_total{device="vethwe-bridge",instance="192.168.3.21:9100",job="centos8"} 2488 node_network_receive_bytes_total{device="vethwe-datapath",instance="192.168.3.21:9100",job="centos8"} 1114 node_network_receive_bytes_total{device="vxlan-6784",instance="192.168.3.21:9100",job="centos8"} 376208 node_network_receive_bytes_total{device="weave",instance="192.168.3.21:9100",job="centos8"} 1572
2.2.2 编写metrics 查询
结合上面的内容,我就编写一个简单的metrics查询,要求查系统为centos8的,双网卡 ens33 和 ens34 下载和上传的流量,OK
那么我们可以这样写
{{device}}_in_下载:irate(node_network_receive_bytes_total{device=~"ens33|ens34",job="centos8"}[5m])*8
{{device}}_out_上传:irate(node_network_transmit_bytes_total{device=~"ens33|ens34",job="centos8"}[5m])*8
2.2.3 在Grafna添加Dashboard(仪表盘)
打开Grafana web登录界面 ip:3000,我这里是192.168.3.82:3000
设置metrics,如下图所示:
查看大盘:
下次查看只要求在仪表盘主页中找即可