Node Exporter是Prometheus生态系统中一个非常重要的组件,它可以采集系统中各种资源的指标信息,并将其暴露给Prometheus进行监控和告警等操作。其中,网卡使用率是我们在监控服务器性能时非常重要的一个指标。本文将介绍如何通过Node Exporter来监控网卡使用率的情况。
一、配置Node Exporter
首先,我们需要安装并配置好Node Exporter。Node Exporter的安装可以参考官方文档:
https://prometheus.io/docs/guides/node-exporter/配置完成后,我们需要启动Node Exporter,并且确认它已经开始采集系统的网卡信息。可以通过访问Node Exporter的/metrics endpoint来确认它是否成功采集了网卡信息:
curl http://localhost:9100/metrics | grep node_network_receive_bytes curl http://localhost:9100/metrics | grep node_network_transmit_bytes
二、使用PromQL查询网卡使用率
有了Node Exporter的支持,我们就可以使用PromQL语句来查询网卡使用率了。以下是一个简单的示例:
irate(node_network_receive_bytes[5m]) * 8
这条PromQL语句会计算出最近5分钟内网卡接收的平均速率(单位是bps),并将其乘以8转换为bps。这里我们使用irate()函数来计算速率,它可以避免在不同时间段的数据之间产生偏差。
三、使用Grafana展示网卡使用率
有了PromQL语句之后,我们可以使用Grafana来展示网卡使用率的情况了。以下是一个简单的Grafana Dashboard:
{ "__inputs": [ { "name": "DS_PROMETHEUS", "label": "Prometheus", "description": "", "type": "datasource", "pluginId": "prometheus", "pluginName": "Prometheus" } ], "__requires": [ { "type": "grafana", "id": "grafana", "name": "Grafana", "version": "7.5.3" }, { "type": "panel", "id": "grafana-clock-panel", "name": "Clock", "version": "" }, { "type": "panel", "id": "grafana-simple-json-datasource", "name": "SimpleJson", "version": "" } ], "annotations": { "list": [ { "builtIn": 1, "datasource": "-- Grafana --", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", "limit": 100, "name": "Annotations & Alerts", "type": "dashboard" } ] }, "editable": true, "gnetId": null, "graphTooltip": 0, "hideControls": false, "id": null, "links": [], "panels": [ { "frameIndex": -1, "gridPos": { "h": 4, "w": 12, "x": 0, "y": 0 }, "id": 2, "options": { "colorMode": "value", "graphMode": "area", "justifyMode": "auto", "orientation": "auto", "reduceOptions": { "calcs": [ "mean" ], "fields": "", "values": false }, "text": {}, "type": "timeseries" }, "pluginVersion": "7.5.3", "targets": [ { "expr": "irate(node_network_receive_bytes[5m]) * 8", "format": "time_series", "instant": false, "interval": "", "intervalFactor": 1, "legendFormat": "{{device}} - Receive", "refId": "A" }, { "expr": "irate(node_network_transmit_bytes[5m]) * 8", "format": "time_series", "instant": false, "interval": "", "intervalFactor": 1, "legendFormat": "{{device}} - Transmit", "refId": "B" } ], "timeFrom": null, "timeShift": null, "title": "网卡使用率", "type": "graph", "xaxis": { "buckets": null, "mode": "time", "name": null, "show": true, "values": [] }, "yaxes": [ { "format": "bps", "label": null, "logBase": 1, "max": null, "min": "0", "show": true }, { "format": "short", "label": null, "logBase": 1, "max": null, "min": null, "show": true } ], "yaxis": { "align": false, "alignLevel": null } } ], "schemaVersion": 27, "style": "dark", "tags": [], "templating": { "list": [] }, "time": { "from": null, "to": null }, "timepicker": { "refresh_intervals": [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ], "time_options": [ "5m", "15m", "1h", "6h", "12h", "24h", "2d", "7d", "30d" ] }, "timezone": "", "title": "Node Exporter网卡使用率", "uid": "aBJL0CZGz", "version": 2 }
这个Dashboard会展示每个网卡的接收和发送速率,以及总的接收和发送速率。其中,网卡的名称可以通过下拉框进行选择,如果需要添加新的网卡可以使用Grafana中的模板变量和查询变量来实现。
四、结论
通过Node Exporter和Prometheus,我们可以非常方便地对系统中的各种资源进行监控。有了监控数据,我们就可以通过Grafana来展示各种有用的指标信息。