From c42044766911e655302a48056a9f9329e70edb13 Mon Sep 17 00:00:00 2001 From: Karolis2011 Date: Sat, 2 Aug 2025 00:19:07 +0300 Subject: [PATCH] Refactor OpenWRT configuration: add host argument and correct optional flag --- OpenWRT/example.alloy | 3 ++- OpenWRT/openwrt.alloy | 50 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/OpenWRT/example.alloy b/OpenWRT/example.alloy index 065b3c8..fcca022 100644 --- a/OpenWRT/example.alloy +++ b/OpenWRT/example.alloy @@ -4,7 +4,8 @@ import.file "openwrt" { } openwrt.openwrt { - forward_to = [prometheus.remote_write.metrics_service.receiver] + forward_to = [prometheus.remote_write.metrics_service.receiver] + host = "NAMU-PC" // Default host, can be overridden by environment variable } prometheus.remote_write "metrics_service" { diff --git a/OpenWRT/openwrt.alloy b/OpenWRT/openwrt.alloy index c8db746..5bac46c 100644 --- a/OpenWRT/openwrt.alloy +++ b/OpenWRT/openwrt.alloy @@ -1,7 +1,51 @@ +import.http "libstd" { + url = "https://git.kkarolis.lt/karolis/Alloy/raw/branch/main/lib/std.alloy" +} + declare "openwrt" { argument "forward_to" { - optinal = false - comment = "Where to forward the scraped metrics" + optional = false + comment = "Where to forward the scraped metrics" } -} \ No newline at end of file + argument "host" { + optional = true + comment = "The host OpenWRT collector is running on" + default = sys.env("GCLOUD_FM_COLLECTOR_ID") + } + + libstd.load_host_config "load_config" { + host = argument.host.value + config_file = "OpenWRT/config.yaml" + } + + discovery.relabel "openwrt_node_exporter" { + targets = libstd.load_host_config.load_config.config.targets + + rule { + source_labels = ["hostname"] + target_label = "__address__" + replacement = "$1:9100" + } + + rule { + action = "labelmap" + regex = "hostname" + replacement = "instance" + } + + rule { + action = "labeldrop" + regex = "hostname" + } + } + + prometheus.scrape "openwrt_node_exporter" { + targets = discovery.relabel.openwrt_node_exporter.output + forward_to = argument.forward_to.value + } + + export "targets" { + value = discovery.relabel.openwrt_node_exporter.output + } +}