From 06c99f6ef156c8910f94f33a400c8ebe5094c82f Mon Sep 17 00:00:00 2001
From: zowoq <59103226+zowoq@users.noreply.github.com>
Date: Sun, 3 Nov 2024 12:16:44 +1000
Subject: [PATCH] modules/nixos/monitoring: import hosts from knownHosts

---
 modules/nixos/monitoring/hosts.nix      | 9 ---------
 modules/nixos/monitoring/prometheus.nix | 8 +++++---
 modules/nixos/monitoring/telegraf.nix   | 9 +++++----
 3 files changed, 10 insertions(+), 16 deletions(-)
 delete mode 100644 modules/nixos/monitoring/hosts.nix

diff --git a/modules/nixos/monitoring/hosts.nix b/modules/nixos/monitoring/hosts.nix
deleted file mode 100644
index cbeab17..0000000
--- a/modules/nixos/monitoring/hosts.nix
+++ /dev/null
@@ -1,9 +0,0 @@
-[
-  "build01.nix-community.org"
-  "build02.nix-community.org"
-  "build03.nix-community.org"
-  "build04.nix-community.org"
-  "darwin01.nix-community.org"
-  "darwin02.nix-community.org"
-  #"web02.nix-community.org"
-]
diff --git a/modules/nixos/monitoring/prometheus.nix b/modules/nixos/monitoring/prometheus.nix
index 54268d7..0b8aff3 100644
--- a/modules/nixos/monitoring/prometheus.nix
+++ b/modules/nixos/monitoring/prometheus.nix
@@ -1,4 +1,4 @@
-{ pkgs, ... }:
+{ inputs, pkgs, ... }:
 {
   systemd.services.prometheus.after = pkgs.lib.mkForce [ "network-online.target" ];
   systemd.services.prometheus.wants = [ "network-online.target" ];
@@ -19,11 +19,13 @@
         metrics_path = "/metrics";
         static_configs =
           let
-            hosts = import ./hosts.nix;
+            hosts = (import "${inputs.self}/modules/shared/known-hosts.nix").programs.ssh.knownHosts;
           in
           [
             {
-              targets = map (host: "${host}:9273") hosts ++ [ "localhost:9273" ];
+              targets = builtins.concatMap (host: map (name: "${name}:9273") host.hostNames) (
+                builtins.attrValues hosts
+              );
               labels.org = "nix-community";
             }
           ];
diff --git a/modules/nixos/monitoring/telegraf.nix b/modules/nixos/monitoring/telegraf.nix
index c5cd653..5a01d39 100644
--- a/modules/nixos/monitoring/telegraf.nix
+++ b/modules/nixos/monitoring/telegraf.nix
@@ -1,3 +1,4 @@
+{ inputs, ... }:
 {
   services.telegraf.extraConfig.inputs = {
     http_response = [
@@ -28,17 +29,17 @@
     ];
     net_response =
       let
-        hosts = import ./hosts.nix;
+        hosts = (import "${inputs.self}/modules/shared/known-hosts.nix").programs.ssh.knownHosts;
       in
       map (host: {
         protocol = "tcp";
-        address = "${host}:22";
+        address = "${toString host.hostNames}:22";
         send = "SSH-2.0-Telegraf";
         expect = "SSH-2.0";
-        tags.host = host;
+        tags.host = toString host.hostNames;
         tags.org = "nix-community";
         timeout = "10s";
-      }) hosts;
+      }) (builtins.attrValues hosts);
     prometheus.urls = [ "https://events.ofborg.org/prometheus.php" ];
   };
 }