From 10bd1d73a6d38c190d00140062b641217ee32afb Mon Sep 17 00:00:00 2001
From: Sheogorath <sheogorath@shivering-isles.com>
Date: Thu, 13 Oct 2022 02:41:07 +0200
Subject: [PATCH] feat(iot): Add shelly exporter

This patch adds a shelly exporter based on json-exporter, collects
various metrics for the shelly plugs and can be used by adding the plugs
as targets.

References:
https://shelly-api-docs.shelly.cloud/
https://github.com/prometheus-community/json_exporter
https://artifacthub.io/packages/helm/prometheus-community/prometheus-json-exporter
---
 apps/base/iot/kustomization.yaml   |  7 +++
 apps/base/iot/networkpolicy.yaml   | 10 ++++
 apps/base/iot/shelly-exporter.yaml | 95 ++++++++++++++++++++++++++++++
 apps/k8s01/iot/kustomization.yaml  |  1 +
 apps/k8s01/iot/shelly.yaml         | 77 +++++++++++++++++++++---
 5 files changed, 182 insertions(+), 8 deletions(-)
 create mode 100644 apps/base/iot/kustomization.yaml
 create mode 100644 apps/base/iot/networkpolicy.yaml
 create mode 100644 apps/base/iot/shelly-exporter.yaml

diff --git a/apps/base/iot/kustomization.yaml b/apps/base/iot/kustomization.yaml
new file mode 100644
index 000000000..f691c1dcb
--- /dev/null
+++ b/apps/base/iot/kustomization.yaml
@@ -0,0 +1,7 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+resources:
+- ../../../shared/networkpolicies/allow-from-monitoring.yaml
+- shelly-exporter.yaml
+patchesStrategicMerge:
+  - networkpolicy.yaml
diff --git a/apps/base/iot/networkpolicy.yaml b/apps/base/iot/networkpolicy.yaml
new file mode 100644
index 000000000..4b568af05
--- /dev/null
+++ b/apps/base/iot/networkpolicy.yaml
@@ -0,0 +1,10 @@
+---
+apiVersion: networking.k8s.io/v1
+kind: NetworkPolicy
+metadata:
+  name: allow-from-monitoring
+spec:
+  podSelector:
+    matchLabels:
+      app.kubernetes.io/instance: shelly-exporter
+      app.kubernetes.io/name: prometheus-json-exporter
diff --git a/apps/base/iot/shelly-exporter.yaml b/apps/base/iot/shelly-exporter.yaml
new file mode 100644
index 000000000..6fdd65454
--- /dev/null
+++ b/apps/base/iot/shelly-exporter.yaml
@@ -0,0 +1,95 @@
+apiVersion: helm.toolkit.fluxcd.io/v2beta1
+kind: HelmRelease
+metadata:
+  name: shelly-exporter
+  namespace: iot
+spec:
+  releaseName: shelly-exporter
+  chart:
+    spec:
+      chart: prometheus-json-exporter
+      sourceRef:
+        kind: HelmRepository
+        name: prometheus-community
+        namespace: monitoring-system
+      version: 0.5.0
+  interval: 5m
+  valuesFrom:
+    - kind: ConfigMap
+      name: shelly-exporter-base-values
+      valuesKey: values.yaml
+    - kind: Secret
+      name: shelly-exporter-override-values
+      valuesKey: values-overrides.yaml
+      optional: false
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: shelly-exporter-base-values
+  namespace: iot
+data:
+  values.yaml: |
+    configuration:
+      config: |-
+        ---
+        modules:
+          default:
+            metrics:
+            - name: shelly_uptime_seconds
+              valuetype: counter
+              help: Seconds elapsed since boot
+              path: '{ .uptime }'
+            - name: shelly_ram_total_bytes
+              valuetype: gauge
+              help: Total amount of system memory in bytes
+              path: '{ .ram_total }'
+            - name: shelly_ram_free_bytes
+              valuetype: gauge
+              help: Available amount of system memory in bytes
+              path: '{ .ram_free }'
+            - name: shelly_fs_size_bytes
+              valuetype: gauge
+              help: Total amount of the file system in bytes
+              path: '{ .fs_size }'
+            - name: shelly_fs_free_bytes
+              valuetype: gauge
+              help: Available amount of the file system in bytes
+              path: '{ .fs_free }'
+            - name: shelly_temperature_celsius
+              valuetype: gauge
+              help: Internal device temperature in °C
+              path: '{ .tmp.tC }'
+            - name: shelly_temperature_selfcheck_status
+              valuetype: gauge
+              help: Whether the internal temperature sensor functions correctly
+              path: '{ .tmp.is_valid }'
+            - name: shelly_relay_status
+              valuetype: gauge
+              help: Whether the channel is turned ON or OFF
+              path: '{ .relays[0].ison }'
+            - name: shelly_meter_power_watts
+              valuetype: gauge
+              help: Current real AC power being drawn, in Watts
+              path: '{ .meters[0].power }'
+            - name: shelly_meter_selfcheck_status
+              valuetype: gauge
+              help: Whether power metering self-checks OK
+              path: '{ .meters[0].is_valid }'
+            - name: shelly_meter_overpower_watts
+              valuetype: gauge
+              help: Value in Watts, on which an overpower condition is detected
+              path: '{ .meters[0].overpower }'
+            - name: shelly_meter_total_wattminutes
+              valuetype: gauge
+              help: Total energy consumed by the attached electrical appliance in Watt-minute
+              path: '{ .meters[0].total}'
+    serviceMonitor:
+      enabled: true
+    resources:
+      requests:
+        cpu: 10m
+        memory: 64Mi
+      limits:
+        cpu: 500m
+        memory: 256Mi
diff --git a/apps/k8s01/iot/kustomization.yaml b/apps/k8s01/iot/kustomization.yaml
index 9ae250a17..558ff3cf4 100644
--- a/apps/k8s01/iot/kustomization.yaml
+++ b/apps/k8s01/iot/kustomization.yaml
@@ -1,6 +1,7 @@
 apiVersion: kustomize.config.k8s.io/v1beta1
 kind: Kustomization
 resources:
+- ../../base/iot
 - namespace.yaml
 - certificate.yaml
 - oauth2.yaml
diff --git a/apps/k8s01/iot/shelly.yaml b/apps/k8s01/iot/shelly.yaml
index 58c44abf6..777dc0ba3 100644
--- a/apps/k8s01/iot/shelly.yaml
+++ b/apps/k8s01/iot/shelly.yaml
@@ -16,8 +16,8 @@ sops:
     azure_kv: []
     hc_vault: []
     age: []
-    lastmodified: "2022-10-11T18:57:37Z"
-    mac: ENC[AES256_GCM,data:w4WeiJ3kzrGYBLilZzesG1MM18uxJsNfpGIubTqJMprD2zuVn32AQErI38r4O71yD3H+hrCr1uDbo7yW9NkLdLYFxvqIWuPqyzYQ6ZaC1VkzXvDGeCtySVU6dzgZEKWb/I+jC7O1rtNDn9w+Yq+TMEZjRjVlE91cmvtqChRwBMQ=,iv:2eB5mexKs6swhgUb15tpHcHHEnPWWICjtUgrlwoHXdo=,tag:S5w2Yjsd172ySDFUVz2VZw==,type:str]
+    lastmodified: "2022-10-13T00:38:47Z"
+    mac: ENC[AES256_GCM,data:7t7a3ydeWcnt6igiasn9oJW6lvy1IdlPPPbDc0G3yHCt5uD03iKqMgy4oz9QjqhPKSqzsi2xeky90fMxzZ4L+l+FLaw5NWR92YtezrpVMCRiWPZS3rvur/KLOhjCpSzegq1Q5BQI9eTRQ+dlcP5LN77M9gZHebb5T8m74gowpps=,iv:9bLIvOSMFwy9HgJLtb7T6Hq7Q4oa+F4rmBUPhEhQys0=,tag:DvDIbQ7o3Xubfs4H5t7tKg==,type:str]
     pgp:
         - created_at: "2022-09-13T20:16:18Z"
           enc: |-
@@ -105,8 +105,8 @@ sops:
     azure_kv: []
     hc_vault: []
     age: []
-    lastmodified: "2022-10-11T18:57:37Z"
-    mac: ENC[AES256_GCM,data:w4WeiJ3kzrGYBLilZzesG1MM18uxJsNfpGIubTqJMprD2zuVn32AQErI38r4O71yD3H+hrCr1uDbo7yW9NkLdLYFxvqIWuPqyzYQ6ZaC1VkzXvDGeCtySVU6dzgZEKWb/I+jC7O1rtNDn9w+Yq+TMEZjRjVlE91cmvtqChRwBMQ=,iv:2eB5mexKs6swhgUb15tpHcHHEnPWWICjtUgrlwoHXdo=,tag:S5w2Yjsd172ySDFUVz2VZw==,type:str]
+    lastmodified: "2022-10-13T00:38:47Z"
+    mac: ENC[AES256_GCM,data:7t7a3ydeWcnt6igiasn9oJW6lvy1IdlPPPbDc0G3yHCt5uD03iKqMgy4oz9QjqhPKSqzsi2xeky90fMxzZ4L+l+FLaw5NWR92YtezrpVMCRiWPZS3rvur/KLOhjCpSzegq1Q5BQI9eTRQ+dlcP5LN77M9gZHebb5T8m74gowpps=,iv:9bLIvOSMFwy9HgJLtb7T6Hq7Q4oa+F4rmBUPhEhQys0=,tag:DvDIbQ7o3Xubfs4H5t7tKg==,type:str]
     pgp:
         - created_at: "2022-09-13T20:16:18Z"
           enc: |-
@@ -170,8 +170,8 @@ sops:
     azure_kv: []
     hc_vault: []
     age: []
-    lastmodified: "2022-10-11T18:57:37Z"
-    mac: ENC[AES256_GCM,data:w4WeiJ3kzrGYBLilZzesG1MM18uxJsNfpGIubTqJMprD2zuVn32AQErI38r4O71yD3H+hrCr1uDbo7yW9NkLdLYFxvqIWuPqyzYQ6ZaC1VkzXvDGeCtySVU6dzgZEKWb/I+jC7O1rtNDn9w+Yq+TMEZjRjVlE91cmvtqChRwBMQ=,iv:2eB5mexKs6swhgUb15tpHcHHEnPWWICjtUgrlwoHXdo=,tag:S5w2Yjsd172ySDFUVz2VZw==,type:str]
+    lastmodified: "2022-10-13T00:38:47Z"
+    mac: ENC[AES256_GCM,data:7t7a3ydeWcnt6igiasn9oJW6lvy1IdlPPPbDc0G3yHCt5uD03iKqMgy4oz9QjqhPKSqzsi2xeky90fMxzZ4L+l+FLaw5NWR92YtezrpVMCRiWPZS3rvur/KLOhjCpSzegq1Q5BQI9eTRQ+dlcP5LN77M9gZHebb5T8m74gowpps=,iv:9bLIvOSMFwy9HgJLtb7T6Hq7Q4oa+F4rmBUPhEhQys0=,tag:DvDIbQ7o3Xubfs4H5t7tKg==,type:str]
     pgp:
         - created_at: "2022-09-13T20:16:18Z"
           enc: |-
@@ -259,8 +259,69 @@ sops:
     azure_kv: []
     hc_vault: []
     age: []
-    lastmodified: "2022-10-11T18:57:37Z"
-    mac: ENC[AES256_GCM,data:w4WeiJ3kzrGYBLilZzesG1MM18uxJsNfpGIubTqJMprD2zuVn32AQErI38r4O71yD3H+hrCr1uDbo7yW9NkLdLYFxvqIWuPqyzYQ6ZaC1VkzXvDGeCtySVU6dzgZEKWb/I+jC7O1rtNDn9w+Yq+TMEZjRjVlE91cmvtqChRwBMQ=,iv:2eB5mexKs6swhgUb15tpHcHHEnPWWICjtUgrlwoHXdo=,tag:S5w2Yjsd172ySDFUVz2VZw==,type:str]
+    lastmodified: "2022-10-13T00:38:47Z"
+    mac: ENC[AES256_GCM,data:7t7a3ydeWcnt6igiasn9oJW6lvy1IdlPPPbDc0G3yHCt5uD03iKqMgy4oz9QjqhPKSqzsi2xeky90fMxzZ4L+l+FLaw5NWR92YtezrpVMCRiWPZS3rvur/KLOhjCpSzegq1Q5BQI9eTRQ+dlcP5LN77M9gZHebb5T8m74gowpps=,iv:9bLIvOSMFwy9HgJLtb7T6Hq7Q4oa+F4rmBUPhEhQys0=,tag:DvDIbQ7o3Xubfs4H5t7tKg==,type:str]
+    pgp:
+        - created_at: "2022-09-13T20:16:18Z"
+          enc: |-
+            -----BEGIN PGP MESSAGE-----
+
+            wcFMA7kpg2bgzVHcARAAoueYiNdU74lz+DKGnzIoohlWRe50KGl7LNC1znJvv+s9
+            7fHsooW6HH19EQS7W6+1rJSpgqVxMYcJ6pX9rBsdgCLGk6uplH/vbL1a6WvJxq5F
+            knxvjxVtseQtquXBeVQDd3AnqM9+N5l137Bu8nVJr0CyUG/O3bdjV+rMyX8Rst9E
+            umKqughRRiJEw+vC/4GIDDxB1p7eqVY98j8Ndg8arezY+iWUlRrZjBUvnlwLXh2w
+            1mLLc6A12hBU4fS2bXCajxEttwt8Iu91NZ6hT8suwPVkyt4Jvd+7xBBjVVjEu5ay
+            XOz75JFHv6NSA8dBS/JGf50QWDVN/5plODefNLZ8+hmLJLXxhVYjJtZ/Ui7gDNw5
+            o8FPt0npit+Y6cSu4fwtGRbfAgCucFRa8TRphFlEJc+pyj57IWhrsQ41oE1BDs5Z
+            9nycKz/gAui1gcWK5WyUohHJySjEQ7ylxltqhKNCeoS6IdJwvl8tJPBTm9PXkDOa
+            bLYquP7k8Kcs1HrjrMTDbB31oS+vbD1OEbzoe940844WT7xgMN0XyjgwoURlyDzG
+            0F8WQSgxXflg0iKcmh7LlO9Amon4RtvIzCM6+W+6TTjsSwof0gotBkcIiZ4/33p2
+            QuczC/zAXeNtCJllGtI4rodWQswfSfwr7o2GmkJLpTBLWchFwZKO9jObGF3Msn/S
+            UQEVtwrW0gXMCs8fhEd3nmGXB5egbY0i8IYosLMq13MzpaPMs9HdklG+C6PTzWun
+            A7nKh0USoxOfc/sWGg9C4DGxRbgOKIdp3fpLIEEy4BS5Ig==
+            =cQ+Y
+            -----END PGP MESSAGE-----
+          fp: 286791FB6648539775DB31B8FCB98C2A3EC6F601
+        - created_at: "2022-09-13T20:16:18Z"
+          enc: |
+            -----BEGIN PGP MESSAGE-----
+
+            hQIMA4oYbIHZIrAPARAAlCWi5/GXzpM4EAEY94T9reZ/7J6z1U7WI02dkn0qvWiZ
+            YkjlZ/Cjw45pAworxxnvhDxAiD2LiQH8XpxhgNpmv0lirnwDwnmULxkA03R6iFao
+            /sa+68kXm3/CAA44yHErWg/iqigk8++b0PG29BrJgNmH1SRIKyV2nNT5QNPBPazT
+            iuXzNJEsM8WCPHO2oI0A8ESzBldxI6qPlyPw/HnXUfGB7/k4UvxF5/AsAwb0G+WC
+            c1D3T8xctRHH1/GcZf3TcTS8QVKgmIVJfIFCVvS9Gbmql2Lw7OENwWvCUjnirXpV
+            WtdogWMN29goWPVNGYnCb5nKfmrfSQHUMUqtCPFyNFrrkTbSW0wB5wdGh2/LyKyf
+            qT6gp0zLx2NOocEs7FCvhuEsWaeHbTYpW0klqwDg9CdKiDXGSvrBEa9a8lY0bLf0
+            yzp+UzK+m+rPBA7wMsl7EvWKQdzb48kHYkFBagHcqeyDS6nwKtNtzE5NpMn+7afN
+            ibSDFwEbPY8sgzt5nDKLTSXYqehi6y2ef47nNe3wU6EyLqs7IiZSNEMjfeCuLQwa
+            0VQ3sEWMibI6whwEMn+brLwjBk9ECgNzPZlfTJba3Wx2oX85/QmsGKdazrHBzRxI
+            DITDiW+gNEEStcv2FUPb/sYSNW6G3ZgiftSmsHphJvWdqYeckcVYgzlRK3nEDp7U
+            aAEJAhBuUESqrXlLqY3nubH8Esx+RqWMfJ18JnovuQta2ELLC2tv6Gq8Gv81NAab
+            vo2OImfMvO/PWSriyatax3PgcI49tCEeUQChqQ0j6uvnHKqJ7xySXLem18dlTtXI
+            AnX0OhNKJ0np
+            =I/wJ
+            -----END PGP MESSAGE-----
+          fp: B137EE1549DFAF960DD1E2B15147025FB9F09E07
+    encrypted_regex: ^(data|stringData|email|dnsZones?|dnsNames?|.*(H|h)osts?|tang|externalURL|.*-secret|.*-url|.*Secrets?|.*-domain|password|subjects|node|apiURL|.*(S|s)erverNames?|.*SecretKey|externalName)$
+    version: 3.7.3
+---
+apiVersion: v1
+kind: Secret
+metadata:
+    name: shelly-exporter-override-values
+    namespace: iot
+type: Opaque
+stringData:
+    values-overrides.yaml: ENC[AES256_GCM,data:BOHhxJ7hrgGrRcGn4yEyAV5+oK8Jcg019FUgBBykIhQ6Wp4DPniKFdKBk2QBPDFJM/fHGvuONkYnHjr2OFyrpVBUGx+DQ+eTKxeUNVAD3N+TU1pfFLKywL5gSDSO6Uzp6dUkj0fOiA+++lJOjK/hiMMM2kP/L1v0vwjI3cDVTTzGh3PDl0HP4C1lwzTzzmYDA4KTt2p5YhjK24YQNxjSvlu57iO7C1qyoRtY7SQ/Kx6t,iv:25H/DAuqKBx21cXv3NxegOC7cbJu0O1Iz9YnwsQsyuY=,tag:MobN2JkyAPFqWPSraXd6Tg==,type:str]
+sops:
+    kms: []
+    gcp_kms: []
+    azure_kv: []
+    hc_vault: []
+    age: []
+    lastmodified: "2022-10-13T00:38:47Z"
+    mac: ENC[AES256_GCM,data:7t7a3ydeWcnt6igiasn9oJW6lvy1IdlPPPbDc0G3yHCt5uD03iKqMgy4oz9QjqhPKSqzsi2xeky90fMxzZ4L+l+FLaw5NWR92YtezrpVMCRiWPZS3rvur/KLOhjCpSzegq1Q5BQI9eTRQ+dlcP5LN77M9gZHebb5T8m74gowpps=,iv:9bLIvOSMFwy9HgJLtb7T6Hq7Q4oa+F4rmBUPhEhQys0=,tag:DvDIbQ7o3Xubfs4H5t7tKg==,type:str]
     pgp:
         - created_at: "2022-09-13T20:16:18Z"
           enc: |-
-- 
GitLab