版本
OpenWrt 19.07.9, r11405-2a3558b0de
夜鶯監控v8.5.0
由於openwrt是用兆能M2改造,磁盤/內存太小,裝不了agent,所以用api接口上傳收集數據
openwrt配置收集腳本
root@OpenWrt:~# cat /opt/sh/net_monitor.sh
#!/bin/sh
N9E_PUSH_URL="http://夜鶯監控:17000/opentsdb/put"
HOSTNAME="OpenWrt路由器"
INTERVAL=60
while true; do
TIMESTAMP=$(date +%s)
PAYLOAD="["
# 網絡接口流量
while read line; do
iface=$(echo $line | egrep "pppoe-wan|eth" |awk -F: '{print $1}' | tr -d ' ')
[ -z "$iface" ] && continue
RX=$(echo $line | awk '{print $2}')
TX=$(echo $line | awk '{print $10}')
PAYLOAD="$PAYLOAD {\"metric\": \"net_bytes_recv\", \"timestamp\": $TIMESTAMP, \"tags\": {\"iface\": \"$iface\", \"ident\": \"$HOSTNAME\"}, \"value\": $RX},"
PAYLOAD="$PAYLOAD {\"metric\": \"net_bytes_sent\", \"timestamp\": $TIMESTAMP, \"tags\": {\"iface\": \"$iface\", \"ident\": \"$HOSTNAME\"}, \"value\": $TX},"
done < /proc/net/dev
# CPU 使用率 (取 idle 百分比)
CPU_IDLE=$(top -bn1 | grep "^CPU" | awk '{print $8}' | sed 's/%//')
CPU_UTIL=$(expr 100 - $CPU_IDLE)
PAYLOAD="$PAYLOAD {\"metric\": \"cpu_usage_idle\", \"timestamp\": $TIMESTAMP, \"tags\": {\"cpu\": \"cpu-total\", \"ident\": \"$HOSTNAME\"}, \"value\": $CPU_IDLE},"
PAYLOAD="$PAYLOAD {\"metric\": \"cpu_usage_util\", \"timestamp\": $TIMESTAMP, \"tags\": {\"cpu\": \"cpu-total\", \"ident\": \"$HOSTNAME\"}, \"value\": $CPU_UTIL},"
# 內存使用率
MEM_TOTAL=$(grep MemTotal /proc/meminfo | awk '{print $2}')
MEM_FREE=$(grep MemFree /proc/meminfo | awk '{print $2}')
MEM_USED=$(expr $MEM_TOTAL - $MEM_FREE)
MEM_UTIL=$(expr ${MEM_USED} \* 100 / ${MEM_TOTAL})
PAYLOAD="$PAYLOAD {\"metric\": \"mem_used_percent\", \"timestamp\": $TIMESTAMP, \"tags\": {\"ident\": \"$HOSTNAME\"}, \"value\": $MEM_UTIL},"
# 系統負載
LOAD1=$(uptime | awk -F'load average:' '{print $2}' | cut -d, -f1 | tr -d ' ')
LOAD5=$(uptime | awk -F'load average:' '{print $2}' | cut -d, -f2 | tr -d ' ')
LOAD15=$(uptime | awk -F'load average:' '{print $2}' | cut -d, -f3 | tr -d ' ')
PAYLOAD="$PAYLOAD {\"metric\": \"system_load1\", \"timestamp\": $TIMESTAMP, \"tags\": {\"ident\": \"$HOSTNAME\"}, \"value\": $LOAD1},"
PAYLOAD="$PAYLOAD {\"metric\": \"system_load5\", \"timestamp\": $TIMESTAMP, \"tags\": {\"ident\": \"$HOSTNAME\"}, \"value\": $LOAD5},"
PAYLOAD="$PAYLOAD {\"metric\": \"system_load15\", \"timestamp\": $TIMESTAMP, \"tags\": {\"ident\": \"$HOSTNAME\"}, \"value\": $LOAD15},"
# 運行時長
ST=$(uptime |awk '{print $3}')
PAYLOAD="$PAYLOAD {\"metric\": \"system_uptime\", \"timestamp\": $TIMESTAMP, \"tags\": {\"ident\": \"$HOSTNAME\"}, \"value\": $ST},"
# 去掉最後一個逗號
PAYLOAD=$(echo "$PAYLOAD" | sed 's/,$//')
PAYLOAD="$PAYLOAD]"
curl -s -o /dev/null -X POST -d "$PAYLOAD" $N9E_PUSH_URL
sleep $INTERVAL
done
root@OpenWrt:~# cd /opt/sh/ && ./net_monitor.sh &
root@OpenWrt:~# chmod +x /etc/rc.local
root@OpenWrt:~# cat /etc/rc.local
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
cd /opt/sh/ && ./net_monitor.sh &
exit 0
夜鶯監控上配置
監控儀表盤JSON
{
"name": "Openwrt主機監控 ",
"tags": "",
"note": "",
"ident": "",
"uuid": 1766992354464000,
"configs": {
"links": [
{
"targetBlank": true,
"title": "n9e",
"url": "https://n9e.github.io/"
},
{
"targetBlank": true,
"title": "author",
"url": "http://flashcat.cloud/"
}
],
"panels": [
{
"type": "row",
"id": "972550e5-f894-448b-9cd7-90d8b21bf19b",
"name": "系統基礎",
"collapsed": true,
"layout": {
"h": 1,
"w": 24,
"x": 0,
"y": 0,
"i": "972550e5-f894-448b-9cd7-90d8b21bf19b",
"isResizable": false
},
"panels": [],
"version": "3.3.0",
"overrides": []
},
{
"type": "table",
"id": "7ec1713c-b8ba-4f7e-9357-35544f32db6b",
"layout": {
"h": 4,
"w": 12,
"x": 0,
"y": 1,
"i": "7ec1713c-b8ba-4f7e-9357-35544f32db6b",
"isResizable": true
},
"version": "3.3.0",
"datasourceCate": "prometheus",
"datasourceValue": "${prom}",
"name": "運行時長(天)",
"maxPerRow": 4,
"custom": {
"showHeader": true,
"colorMode": "value",
"nowrap": false,
"tableLayout": "fixed",
"calc": "lastNotNull",
"displayMode": "seriesToRows",
"sortColumn": "value",
"sortOrder": "ascend",
"pageLimit": 500,
"linkMode": "appendLinkColumn"
},
"options": {
"standardOptions": {}
},
"overrides": [
{
"matcher": {
"id": "byFrameRefID"
}
}
],
"targets": [
{
"refId": "A",
"expr": "system_uptime{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\"}",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-system_uptime"
}
]
},
{
"type": "timeseries",
"id": "a420ce25-6968-47f8-8335-60cde70fd062",
"layout": {
"h": 7,
"w": 12,
"x": 12,
"y": 1,
"i": "a420ce25-6968-47f8-8335-60cde70fd062",
"isResizable": true
},
"version": "3.3.0",
"datasourceCate": "prometheus",
"datasourceValue": "${prom}",
"name": "CPU負載(1分鐘平均)",
"maxPerRow": 4,
"options": {
"tooltip": {
"mode": "all",
"sort": "desc"
},
"legend": {
"displayMode": "table",
"placement": "bottom",
"columns": [
"last"
],
"behaviour": "showItem",
"selectMode": "single"
},
"standardOptions": {
"unit": "none"
},
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "#634CD9",
"value": null,
"type": "base"
}
]
},
"thresholdsStyle": {
"mode": "dashed"
}
},
"custom": {
"drawStyle": "lines",
"lineInterpolation": "smooth",
"spanNulls": false,
"lineWidth": 1,
"fillOpacity": 0.5,
"gradientMode": "none",
"stack": "off",
"scaleDistribution": {
"type": "linear"
},
"showPoints": "none",
"pointSize": 5
},
"overrides": [
{
"matcher": {
"id": "byFrameRefID"
},
"properties": {
"rightYAxisDisplay": "off"
}
}
],
"targets": [
{
"expr": "system_load1{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\"}",
"refId": "B",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-system_load1"
}
]
},
{
"type": "timeseries",
"id": "043c26de-d19f-4fe8-a615-2b7c10ceb828",
"layout": {
"h": 7,
"w": 12,
"x": 0,
"y": 5,
"i": "043c26de-d19f-4fe8-a615-2b7c10ceb828",
"isResizable": true
},
"version": "3.3.0",
"datasourceCate": "prometheus",
"datasourceValue": "${prom}",
"targets": [
{
"expr": "100 - (cpu_usage_idle{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\", cpu=\"cpu-total\"})",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-cpu_used",
"refId": "A",
"instant": false
}
],
"transformations": [
{
"id": "organize",
"options": {}
}
],
"name": "CPU使用率",
"maxPerRow": 4,
"options": {
"tooltip": {
"mode": "all",
"sort": "desc"
},
"legend": {
"displayMode": "table",
"placement": "right",
"columns": [
"last"
],
"behaviour": "showItem"
},
"standardOptions": {
"unit": "percent"
},
"thresholds": {
"steps": [
{
"color": "#634CD9",
"value": null,
"type": "base"
}
]
}
},
"custom": {
"drawStyle": "lines",
"lineInterpolation": "smooth",
"spanNulls": false,
"lineWidth": 1,
"fillOpacity": 0.5,
"gradientMode": "opacity",
"stack": "off",
"scaleDistribution": {
"type": "linear"
}
},
"overrides": [
{
"matcher": {
"id": "byFrameRefID"
},
"properties": {
"rightYAxisDisplay": "off",
"standardOptions": {
"unit": "none"
}
}
}
]
},
{
"type": "timeseries",
"id": "27ffeff0-2bd0-47a4-a07d-46e3b6474307",
"layout": {
"h": 7,
"w": 12,
"x": 12,
"y": 8,
"i": "f50af0b0-4bb2-48ff-9c08-1fa52d81507a",
"isResizable": true
},
"version": "3.3.0",
"datasourceCate": "prometheus",
"datasourceValue": "${prom}",
"name": "CPU負載(5分鐘平均)",
"maxPerRow": 4,
"options": {
"tooltip": {
"mode": "all",
"sort": "desc"
},
"legend": {
"displayMode": "table",
"placement": "bottom",
"columns": [
"last"
],
"behaviour": "showItem",
"selectMode": "single"
},
"standardOptions": {
"unit": "none"
},
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "#634CD9",
"value": null,
"type": "base"
}
]
},
"thresholdsStyle": {
"mode": "dashed"
}
},
"custom": {
"drawStyle": "lines",
"lineInterpolation": "smooth",
"spanNulls": false,
"lineWidth": 1,
"fillOpacity": 0.5,
"gradientMode": "none",
"stack": "off",
"scaleDistribution": {
"type": "linear"
},
"showPoints": "none",
"pointSize": 5
},
"overrides": [
{
"matcher": {
"id": "byFrameRefID"
},
"properties": {
"rightYAxisDisplay": "off"
}
}
],
"targets": [
{
"expr": "system_load5{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\"}",
"refId": "C",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-system_load5"
}
]
},
{
"type": "timeseries",
"id": "239aacdf-1982-428b-b240-57f4ce7f946d",
"layout": {
"h": 6,
"w": 12,
"x": 0,
"y": 12,
"i": "239aacdf-1982-428b-b240-57f4ce7f946d",
"isResizable": true
},
"version": "3.3.0",
"datasourceCate": "prometheus",
"datasourceValue": "${prom}",
"name": "內存使用率",
"description": "內存指標可參考鏈接 [/PROC/MEMINFO之謎](http://linuxperf.com/?p=142) ",
"maxPerRow": 4,
"options": {
"tooltip": {
"mode": "all",
"sort": "desc"
},
"legend": {
"displayMode": "table",
"placement": "bottom",
"columns": [
"last"
],
"behaviour": "showItem",
"selectMode": "single"
},
"standardOptions": {
"unit": "percent"
},
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "#634CD9",
"value": null,
"type": "base"
}
]
},
"thresholdsStyle": {
"mode": "dashed"
}
},
"custom": {
"drawStyle": "lines",
"lineInterpolation": "smooth",
"spanNulls": false,
"lineWidth": 1,
"fillOpacity": 0.5,
"gradientMode": "opacity",
"stack": "off",
"scaleDistribution": {
"type": "linear"
},
"showPoints": "none",
"pointSize": 5
},
"overrides": [
{
"matcher": {
"id": "byFrameRefID"
},
"properties": {
"rightYAxisDisplay": "off"
}
}
],
"targets": [
{
"expr": "mem_used_percent{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\"}",
"refId": "A",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-mem_used_percent"
}
]
},
{
"type": "timeseries",
"id": "f2ee5d32-737c-4095-b6b7-b15b778ffdb9",
"layout": {
"h": 8,
"w": 24,
"x": 0,
"y": 18,
"i": "f2ee5d32-737c-4095-b6b7-b15b778ffdb9",
"isResizable": true
},
"version": "3.3.0",
"datasourceCate": "prometheus",
"datasourceValue": "${prom}",
"maxDataPoints": 240,
"name": "網絡流量",
"maxPerRow": 4,
"options": {
"tooltip": {
"mode": "all",
"sort": "desc"
},
"legend": {
"displayMode": "list",
"placement": "bottom",
"behaviour": "showItem",
"selectMode": "single"
},
"standardOptions": {
"unit": "bytesSecIEC",
"decimals": 0
},
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "#634CD9",
"value": null,
"type": "base"
}
]
},
"thresholdsStyle": {
"mode": "dashed"
}
},
"custom": {
"drawStyle": "lines",
"lineInterpolation": "smooth",
"spanNulls": false,
"lineWidth": 1,
"fillOpacity": 0.5,
"gradientMode": "opacity",
"stack": "off",
"scaleDistribution": {
"type": "linear"
},
"showPoints": "none",
"pointSize": 5
},
"overrides": [
{
"matcher": {
"id": "byFrameRefID"
},
"properties": {
"rightYAxisDisplay": "off"
}
}
],
"targets": [
{
"expr": "rate(net_bytes_recv{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\"}[1m])",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-{{iface}}-recv",
"refId": "A"
},
{
"expr": "rate(net_bytes_sent{busigroup=~\"$busigroup\",ident=~\"$ident\",ip=~\"$ip\"}[1m])",
"legend": "{{busigroup}}-{{ident}}-{{ip}}-{{iface}}-sent",
"refId": "B",
"maxDataPoints": 240
}
]
}
],
"var": [
{
"name": "prom",
"label": "數據源",
"type": "datasource",
"definition": "prometheus"
},
{
"name": "busigroup",
"label": "平台組",
"type": "query",
"hide": false,
"datasource": {
"cate": "prometheus",
"value": "${prom}"
},
"definition": "label_values(system_load1,busigroup)",
"multi": true,
"allOption": true
},
{
"name": "ident",
"label": "主機名",
"type": "query",
"hide": false,
"datasource": {
"cate": "prometheus",
"value": "${prom}"
},
"definition": "label_values(ident)",
"multi": true,
"allOption": true,
"allValue": ""
},
{
"name": "ip",
"label": "IP",
"type": "query",
"hide": false,
"datasource": {
"cate": "prometheus",
"value": "${prom}"
},
"definition": "label_values(system_load1,ip)",
"multi": true,
"allOption": true
}
],
"version": "3.0.0",
"graphZoom": "updateTimeRange"
}
}