diff --git a/get_ips.sh b/get_ips.sh new file mode 100644 index 0000000..52cf560 --- /dev/null +++ b/get_ips.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# 指定输出的 .prom 文件路径(需与 node-exporter 启动参数一致) +PROM_FILE="/var/lib/node_exporter/machine_ips.prom" + +# 写入指标头部信息 +echo "# HELP node_network_ip_address Info about the machine's IP addresses." > "$PROM_FILE.$$" +echo "# TYPE node_network_ip_address gauge" >> "$PROM_FILE.$$" + +# 获取所有非回环接口的 IPv4 地址 +# 格式化为: node_network_ip_address{device="eth0", ip="192.168.1.5"} 1 +ip -o -4 addr list | awk '{print $2, $4}' | cut -d/ -f1 | while read dev ip; do + echo "node_network_ip_address{device=\"$dev\",ip=\"$ip\"} 1" >> "$PROM_FILE.$$" +done + +# 原子操作重命名文件,防止读取不完整 +mv "$PROM_FILE.$$" "$PROM_FILE" \ No newline at end of file