From ff86e31f7297f4221bd02fa1933a4c428a7978d6 Mon Sep 17 00:00:00 2001
From: Florian Sesser <florian@private.storage>
Date: Fri, 2 Jul 2021 17:29:58 +0000
Subject: [PATCH] Monitor PaymentServer

---
 morph/grid/local/grid.nix                      |  3 ++-
 morph/grid/production/grid.nix                 |  3 ++-
 morph/grid/testing/grid.nix                    |  3 ++-
 morph/lib/customize-monitoring.nix             |  5 +++++
 nixos/modules/monitoring/server/prometheus.nix | 14 ++++++++++++++
 5 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/morph/grid/local/grid.nix b/morph/grid/local/grid.nix
index 55b7587c..bca902f2 100644
--- a/morph/grid/local/grid.nix
+++ b/morph/grid/local/grid.nix
@@ -49,7 +49,7 @@ let
       gridlib.monitoring
       (gridlib.hardware-virtual ({ publicIPv4 = "192.168.67.24"; }))
       (gridlib.customize-monitoring {
-        inherit hostsMap vpnClientIPs nodeExporterTargets;
+        inherit hostsMap vpnClientIPs nodeExporterTargets paymentExporterTargets;
         inherit (config) domain publicKeyPath privateKeyPath;
         monitoringvpnIPv4 = "172.23.23.1";
         stateVersion = "19.09";
@@ -66,6 +66,7 @@ let
   };
   vpnClientIPs = [ "172.23.23.11" "172.23.23.12" "172.23.23.13" ];
   nodeExporterTargets = [ "monitoring" "payments" "storage1" "storage2" ];
+  paymentExporterTargets = [ "payments" ];
 
 in {
   network = {
diff --git a/morph/grid/production/grid.nix b/morph/grid/production/grid.nix
index fb680338..1aa60561 100644
--- a/morph/grid/production/grid.nix
+++ b/morph/grid/production/grid.nix
@@ -28,7 +28,7 @@ let
       gridlib.monitoring
       gridlib.hardware-aws
       (gridlib.customize-monitoring {
-        inherit hostsMap vpnClientIPs nodeExporterTargets;
+        inherit hostsMap vpnClientIPs nodeExporterTargets paymentExporterTargets;
         inherit (config) domain publicKeyPath privateKeyPath;
         monitoringvpnIPv4 = "172.23.23.1";
         stateVersion = "19.09";
@@ -103,6 +103,7 @@ let
     "storage004"
     "storage005"
   ];
+  paymentExporterTargets = [ "payments" ];
 
 in {
   network = {
diff --git a/morph/grid/testing/grid.nix b/morph/grid/testing/grid.nix
index 0cdfe5ae..996b1fba 100644
--- a/morph/grid/testing/grid.nix
+++ b/morph/grid/testing/grid.nix
@@ -39,7 +39,7 @@ let
       gridlib.monitoring
       gridlib.hardware-aws
       (gridlib.customize-monitoring {
-        inherit hostsMap vpnClientIPs nodeExporterTargets;
+        inherit hostsMap vpnClientIPs nodeExporterTargets paymentExporterTargets;
         inherit (config) domain publicKeyPath privateKeyPath;
         monitoringvpnIPv4 = "172.23.23.1";
         stateVersion = "19.09";
@@ -55,6 +55,7 @@ let
   };
   vpnClientIPs = [ "172.23.23.11" "172.23.23.12" ];
   nodeExporterTargets = [ "monitoring" "payments" "storage001" ];
+  paymentExporterTargets = [ "payments" ];
 
 in {
   network = {
diff --git a/morph/lib/customize-monitoring.nix b/morph/lib/customize-monitoring.nix
index f77d26bd..05fe4510 100644
--- a/morph/lib/customize-monitoring.nix
+++ b/morph/lib/customize-monitoring.nix
@@ -26,6 +26,10 @@
   # which nodes to scrape "nginxExporter" metrics from.
 , nginxExporterTargets ? []
 
+  # A list of VPN clients (IP addresses or hostnames) as strings indicating
+  # which nodes to scrape PaymentServer metrics from.
+, paymentExporterTargets ? []
+
   # A string giving the NixOS state version for the system.
 , stateVersion
 , ...
@@ -52,6 +56,7 @@
   services.private-storage.monitoring.prometheus = {
     inherit nodeExporterTargets;
     inherit nginxExporterTargets;
+    inherit paymentExporterTargets;
   };
 
   system.stateVersion = stateVersion;
diff --git a/nixos/modules/monitoring/server/prometheus.nix b/nixos/modules/monitoring/server/prometheus.nix
index 36c2ba64..c92261cc 100644
--- a/nixos/modules/monitoring/server/prometheus.nix
+++ b/nixos/modules/monitoring/server/prometheus.nix
@@ -26,6 +26,11 @@ in {
       example = lib.literalExample "[ node1 node2 ]";
       description = "List of nodes (hostnames or IPs) to scrape.";
     };
+    paymentExporterTargets = lib.mkOption {
+      type = with lib.types; listOf str;
+      example = lib.literalExample "[ node1 node2 ]";
+      description = "List of nodes (hostnames or IPs) to scrape.";
+    };
   };
 
   config = rec {
@@ -49,6 +54,15 @@ in {
           }];
           relabel_configs = [ dropPortNumber ];
         }
+        {
+          job_name = "payment-exporters";
+          scheme = "https";
+          tls_config.insecure_skip_verify = true;
+          static_configs = [{
+            targets = cfg.paymentExporterTargets;
+          }];
+          relabel_configs = [ dropPortNumber ];
+        }
       ];
     };
   };
-- 
GitLab