Skip to content

Commit cf6de2f

Browse files
committed
node-exporter: copy from upstream
Copy variant from PR: openwrt/packages#28016 Signed-off-by: Vladimir Ermakov <vooon341@gmail.com>
1 parent 4751f02 commit cf6de2f

30 files changed

+1329
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (C) 2013-2017 OpenWrt.org
2+
3+
include $(TOPDIR)/rules.mk
4+
5+
PKG_NAME:=prometheus-node-exporter-ucode
6+
<<<<<<< Updated upstream
7+
PKG_VERSION:=2024.02.07
8+
PKG_RELEASE:=2
9+
=======
10+
PKG_VERSION:=2025.12.04
11+
PKG_RELEASE:=1
12+
>>>>>>> Stashed changes
13+
14+
PKG_MAINTAINER:=Andre Heider <a.heider@gmail.com>
15+
PKG_LICENSE:=Apache-2.0
16+
17+
include $(INCLUDE_DIR)/package.mk
18+
19+
Build/Compile=
20+
21+
define Package/$(PKG_NAME)/Default
22+
SECTION:=utils
23+
CATEGORY:=Utilities
24+
TITLE:=Prometheus node exporter
25+
PKGARCH:=all
26+
endef
27+
28+
define Package/$(PKG_NAME)
29+
$(call Package/$(PKG_NAME)/Default)
30+
DEPENDS:=+uhttpd +uhttpd-mod-ucode +rpcd +ucode-mod-fs +ucode-mod-ubus
31+
endef
32+
33+
define Package/$(PKG_NAME)/install
34+
$(INSTALL_DIR) $(1)/etc/config
35+
$(INSTALL_CONF) ./files/config $(1)/etc/config/$(PKG_NAME)
36+
$(INSTALL_DIR) $(1)/etc/init.d
37+
$(INSTALL_BIN) ./files/init $(1)/etc/init.d/$(PKG_NAME)
38+
$(INSTALL_DIR) $(1)/usr/share/ucode/node-exporter/lib
39+
$(INSTALL_DATA) ./files/metrics.uc $(1)/usr/share/ucode/node-exporter/
40+
$(INSTALL_DATA) ./files/base/*.uc $(1)/usr/share/ucode/node-exporter/lib/
41+
$(INSTALL_DIR) $(1)/usr/bin
42+
$(INSTALL_BIN) ./files/run.sh $(1)/usr/bin/$(PKG_NAME)
43+
endef
44+
45+
define Package/$(PKG_NAME)/conffiles
46+
/etc/config/$(PKG_NAME)
47+
endef
48+
49+
define Package/$(PKG_NAME)/description
50+
Provides node metrics as Prometheus scraping endpoint.
51+
52+
This service is a lightweight rewrite in ucode of the official Prometheus node_exporter.
53+
endef
54+
55+
$(eval $(call BuildPackage,prometheus-node-exporter-ucode))
56+
57+
define Collector
58+
define Package/$(PKG_NAME)-$(1)
59+
$$(call Package/$(PKG_NAME)/Default)
60+
TITLE+= ($(2))
61+
DEPENDS:=$(PKG_NAME) $(3)
62+
endef
63+
64+
define Package/$(PKG_NAME)-$(1)/install
65+
$$(INSTALL_DIR) $$(1)/usr/share/ucode/node-exporter/lib
66+
$$(INSTALL_DATA) ./files/extra/$(1).uc $$(1)/usr/share/ucode/node-exporter/lib/
67+
endef
68+
69+
$$(eval $$(call BuildPackage,$(PKG_NAME)-$(1)))
70+
endef
71+
72+
$(eval $(call Collector,dnsmasq,Dnsmasq collector,@dnsmasq))
73+
$(eval $(call Collector,hwmon,hwmon collector,))
74+
$(eval $(call Collector,ltq-dsl,Lantiq/Intel/MaxLinear DSL collector,@ltq-dsl-app))
75+
$(eval $(call Collector,netstat,netstat collector,))
76+
$(eval $(call Collector,odhcp6c,odhcp6c statistics collector,@odhcp6c))
77+
$(eval $(call Collector,openwrt,OpenWrt collector,))
78+
$(eval $(call Collector,realtek-poe,RealTek PoE collector,@realtek-poe))
79+
$(eval $(call Collector,snmp6,snmp6 collector,))
80+
$(eval $(call Collector,thermal,thermal collector,))
81+
$(eval $(call Collector,uci_dhcp_host,UCI DHCP host collector,))
82+
$(eval $(call Collector,wifi,Wi-Fi collector,+ucode-mod-nl80211))
83+
$(eval $(call Collector,wireguard,Wireguard collector,+rpcd-mod-wireguard))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gauge("node_nf_conntrack_entries")
2+
(null, oneline("/proc/sys/net/netfilter/nf_conntrack_count"));
3+
gauge("node_nf_conntrack_entries_limit")
4+
(null, oneline("/proc/sys/net/netfilter/nf_conntrack_max"));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let f = fs.open("/proc/stat");
2+
3+
if (!f)
4+
return false;
5+
6+
const desc = [
7+
null,
8+
"user",
9+
"nice",
10+
"system",
11+
"idle",
12+
"iowait",
13+
"irq",
14+
"softirq",
15+
"steal",
16+
"guest",
17+
"guest_nice",
18+
];
19+
const m_cpu = counter("node_cpu_seconds_total");
20+
21+
let line;
22+
while (line = nextline(f)) {
23+
const x = wsplit(line);
24+
25+
if (length(x) < 2)
26+
continue;
27+
28+
if (match(x[0], /^cpu\d+/)) {
29+
const count = min(length(x), length(desc));
30+
for (let i = 1; i < count; i++)
31+
m_cpu({ cpu: x[0], mode: desc[i] }, x[i] / 100.0);
32+
} else if (x[0] == "intr")
33+
counter("node_intr_total")(null, x[1]);
34+
else if (x[0] == "ctxt")
35+
counter("node_context_switches_total")(null, x[1]);
36+
else if (x[0] == "btime")
37+
gauge("node_boot_time_seconds")(null, x[1]);
38+
else if (x[0] == "processes")
39+
counter("node_forks_total")(null, x[1]);
40+
else if (x[0] == "procs_running")
41+
gauge("node_procs_running_total")(null, x[1]);
42+
else if (x[0] == "procs_blocked")
43+
gauge("node_procs_blocked_total")(null, x[1]);
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gauge("node_entropy_available_bits", "Bits of available entropy.")
2+
(null, oneline("/proc/sys/kernel/random/entropy_avail"));
3+
gauge("node_entropy_pool_size_bits", "Bits of entropy pool.")
4+
(null, oneline("/proc/sys/kernel/random/poolsize"));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const x = wsplit(oneline("/proc/sys/fs/file-nr"));
2+
3+
if (length(x) < 3)
4+
return false;
5+
6+
gauge("node_filefd_allocated")(null, x[0]);
7+
gauge("node_filefd_maximum")(null, x[2]);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const x = wsplit(oneline("/proc/loadavg"));
2+
3+
if (length(x) < 3)
4+
return false;
5+
6+
gauge("node_load1")(null, x[0]);
7+
gauge("node_load5")(null, x[1]);
8+
gauge("node_load15")(null, x[2]);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let f = fs.open("/proc/meminfo");
2+
3+
if (!f)
4+
return false;
5+
6+
let line;
7+
while (line = nextline(f)) {
8+
const x = wsplit(line);
9+
10+
if (length(x) < 2)
11+
continue;
12+
13+
if (substr(x[0], -1) != ":")
14+
continue;
15+
16+
let name;
17+
if (substr(x[0], -2) == "):")
18+
name = replace(substr(x[0], 0, -2), "(", "_");
19+
else
20+
name = substr(x[0], 0, -1);
21+
22+
gauge(`node_memory_${name}_bytes`)
23+
(null, x[2] == "kB" ? x[1] * 1024 : x[1]);
24+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const root = "/sys/class/net/";
2+
const devices = fs.lsdir(root);
3+
const dev_length = length(devices);
4+
5+
if (dev_length < 1)
6+
return false;
7+
8+
const m_info = gauge("node_network_info");
9+
const m_speed = gauge("node_network_speed_bytes");
10+
const metrics = {
11+
addr_assign_type: gauge("node_network_address_assign_type"),
12+
carrier: gauge("node_network_carrier"),
13+
carrier_changes: counter("node_network_carrier_changes_total"),
14+
carrier_down_count: counter("node_network_carrier_down_changes_total"),
15+
carrier_up_count: counter("node_network_carrier_up_changes_total"),
16+
dev_id: gauge("node_network_device_id"),
17+
dormant: gauge("node_network_dormant"),
18+
flags: gauge("node_network_flags"),
19+
ifindex: gauge("node_network_iface_id"),
20+
iflink: gauge("node_network_iface_link"),
21+
link_mode: gauge("node_network_iface_link_mode"),
22+
mtu: gauge("node_network_mtu_bytes"),
23+
name_assign_type: gauge("node_network_name_assign_type"),
24+
netdev_group: gauge("node_network_net_dev_group"),
25+
type: gauge("node_network_protocol_type"),
26+
tx_queue_len: gauge("node_network_transmit_queue_length"),
27+
};
28+
29+
for (let i = 0; i < dev_length; i++) {
30+
const devroot = root + devices[i] + "/";
31+
32+
m_info({
33+
device: devices[i],
34+
address: oneline(devroot + "address"),
35+
broadcast: oneline(devroot + "broadcast"),
36+
duplex: oneline(devroot + "duplex"),
37+
operstate: oneline(devroot + "operstate"),
38+
ifalias: oneline(devroot + "ifalias"),
39+
}, 1);
40+
}
41+
42+
for (let m in metrics) {
43+
for (let i = 0; i < dev_length; i++) {
44+
metrics[m]({ device: devices[i] }, oneline(root + devices[i] + "/" + m));
45+
}
46+
}
47+
48+
for (let i = 0; i < dev_length; i++) {
49+
const speed = int(oneline(root + devices[i] + "/" + "speed"));
50+
if (speed > 0)
51+
m_speed({ device: devices[i] }, speed * 1000 * 1000 / 8);
52+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const f = fs.open("/proc/net/dev");
2+
3+
if (!f)
4+
return false;
5+
6+
const metrics = [
7+
null,
8+
counter("node_network_receive_bytes_total"),
9+
counter("node_network_receive_packets_total"),
10+
counter("node_network_receive_errs_total"),
11+
counter("node_network_receive_drop_total"),
12+
counter("node_network_receive_fifo_total"),
13+
counter("node_network_receive_frame_total"),
14+
counter("node_network_receive_compressed_total"),
15+
counter("node_network_receive_multicast_total"),
16+
counter("node_network_transmit_bytes_total"),
17+
counter("node_network_transmit_packets_total"),
18+
counter("node_network_transmit_errs_total"),
19+
counter("node_network_transmit_drop_total"),
20+
counter("node_network_transmit_fifo_total"),
21+
counter("node_network_transmit_colls_total"),
22+
counter("node_network_transmit_carrier_total"),
23+
counter("node_network_transmit_compressed_total"),
24+
];
25+
26+
// Per-counter results array
27+
/* results[i] = { dev1: value, dev2: value, ... } */
28+
let results = [];
29+
for (let i = 0; i < length(metrics); i++)
30+
results[i] = {};
31+
32+
// Parse file first
33+
let line;
34+
for (let k = 0; line = nextline(f); k++) {
35+
if (k < 2)
36+
continue; // skip the header lines
37+
const x = wsplit(ltrim(line), " ");
38+
39+
const dev = substr(x[0], 0, -1);
40+
41+
for (let i = 1; i < length(x); i++) {
42+
results[i][dev] = x[i];
43+
}
44+
}
45+
46+
// Build label objects
47+
let label_cache = {};
48+
for (let i = 1; i < length(results); i++) {
49+
for (let dev in results[i]) {
50+
if (!(dev in label_cache))
51+
label_cache[dev] = { device: dev };
52+
}
53+
}
54+
55+
// Emit metrics grouped by counter
56+
for (let i = 1; i < length(metrics); i++) {
57+
const metric = metrics[i];
58+
if (!metric)
59+
continue;
60+
61+
for (let dev in results[i]) {
62+
metric(label_cache[dev], results[i][dev]);
63+
}
64+
}
65+
66+
return true;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const mode = oneline("/sys/fs/selinux/enforce");
2+
const enabled = gauge("node_selinux_enabled");
3+
4+
if (mode == null) {
5+
enabled(null, 0);
6+
return;
7+
}
8+
9+
enabled(null, 1);
10+
gauge("node_selinux_current_mode")(null, mode);

0 commit comments

Comments
 (0)