From d9ea0308e5bc71a3a185eb4f177fe73ebd6c8a30 Mon Sep 17 00:00:00 2001
From: Carlos Natalino <carlos.natalino@chalmers.se>
Date: Tue, 2 May 2023 13:55:40 +0200
Subject: [PATCH] Improvements to the dashboard.

---
 src/opticalattackmanager/utils/monitor.py     |  2 +-
 src/tests/scenario3/optical/dashboard.json    | 89 +++++++++++++++++++
 .../optical/ofc23/run_experiment_demo.py      |  3 +-
 src/tests/scenario3/optical/scaphandre.yaml   | 28 ++++++
 4 files changed, 120 insertions(+), 2 deletions(-)
 create mode 100644 src/tests/scenario3/optical/scaphandre.yaml

diff --git a/src/opticalattackmanager/utils/monitor.py b/src/opticalattackmanager/utils/monitor.py
index 37c05dfaf..390c43dc6 100644
--- a/src/opticalattackmanager/utils/monitor.py
+++ b/src/opticalattackmanager/utils/monitor.py
@@ -60,7 +60,7 @@ async def detect_attack(
         LOGGER.warning(
             "Exception while processing service_id {}/{}".format(service_id, kpi_id)
         )
-        LOGGER.exception(e)
+        # LOGGER.exception(e)
         DROP_COUNTER.inc()
 
 
diff --git a/src/tests/scenario3/optical/dashboard.json b/src/tests/scenario3/optical/dashboard.json
index e0ad2afc4..361ac70bb 100644
--- a/src/tests/scenario3/optical/dashboard.json
+++ b/src/tests/scenario3/optical/dashboard.json
@@ -1364,6 +1364,95 @@
       "panels": [],
       "title": "General status",
       "type": "row"
+    },
+    {
+      "datasource": {
+        "type": "prometheus",
+        "uid": "${DS_PROMETHEUS}"
+      },
+      "fieldConfig": {
+        "defaults": {
+          "color": {
+            "mode": "palette-classic"
+          },
+          "custom": {
+            "axisLabel": "",
+            "axisPlacement": "auto",
+            "barAlignment": 0,
+            "drawStyle": "line",
+            "fillOpacity": 0,
+            "gradientMode": "none",
+            "hideFrom": {
+              "legend": false,
+              "tooltip": false,
+              "viz": false
+            },
+            "lineInterpolation": "linear",
+            "lineWidth": 1,
+            "pointSize": 5,
+            "scaleDistribution": {
+              "type": "linear"
+            },
+            "showPoints": "auto",
+            "spanNulls": false,
+            "stacking": {
+              "group": "A",
+              "mode": "none"
+            },
+            "thresholdsStyle": {
+              "mode": "off"
+            }
+          },
+          "mappings": [],
+          "thresholds": {
+            "mode": "absolute",
+            "steps": [
+              {
+                "color": "green",
+                "value": null
+              },
+              {
+                "color": "red",
+                "value": 80
+              }
+            ]
+          }
+        },
+        "overrides": []
+      },
+      "gridPos": {
+        "h": 8,
+        "w": 12,
+        "x": 0,
+        "y": 45
+      },
+      "id": 33,
+      "options": {
+        "legend": {
+          "calcs": [],
+          "displayMode": "list",
+          "placement": "right"
+        },
+        "tooltip": {
+          "mode": "single",
+          "sort": "none"
+        }
+      },
+      "targets": [
+        {
+          "datasource": {
+            "type": "prometheus",
+            "uid": "${DS_PROMETHEUS}"
+          },
+          "editorMode": "code",
+          "expr": "sum(scaph_process_power_consumption_microwatts{namespace=\"tfs\", cmdline=~\".+opticalattackdetector.+\"})/1000000",
+          "legendFormat": "Detector",
+          "range": true,
+          "refId": "A"
+        }
+      ],
+      "title": "Energy consumption",
+      "type": "timeseries"
     }
   ],
   "refresh": "5s",
diff --git a/src/tests/scenario3/optical/ofc23/run_experiment_demo.py b/src/tests/scenario3/optical/ofc23/run_experiment_demo.py
index 0339b3239..b0fe01547 100644
--- a/src/tests/scenario3/optical/ofc23/run_experiment_demo.py
+++ b/src/tests/scenario3/optical/ofc23/run_experiment_demo.py
@@ -32,7 +32,8 @@ v1 = client.CoreV1Api()
 caching_pod = None
 pods = v1.list_namespaced_pod(namespace=namespace)
 for pod in pods.items:
-    if "caching" in pod.metadata.labels["app"]:
+    print(pod.metadata)
+    if "app" in pod.metadata.labels and "caching" in pod.metadata.labels["app"]:
         caching_pod = pod
 
 LOGGER = None
diff --git a/src/tests/scenario3/optical/scaphandre.yaml b/src/tests/scenario3/optical/scaphandre.yaml
new file mode 100644
index 000000000..6ec17e423
--- /dev/null
+++ b/src/tests/scenario3/optical/scaphandre.yaml
@@ -0,0 +1,28 @@
+apiVersion: monitoring.coreos.com/v1
+kind: ServiceMonitor
+metadata:
+  namespace: monitoring # namespace where prometheus is running
+  name: tfs-scaph-metric
+  labels:
+    app.kubernetes.io/name: scaphandre
+    #release: prometheus
+    #release: prom  # name of the release 
+    # ( VERY IMPORTANT: You need to know the correct release name by viewing 
+    #   the servicemonitor of Prometheus itself: Without the correct name, 
+    #   Prometheus cannot identify the metrics of the Flask app as the target.)
+spec:
+  selector:
+    matchLabels:
+      # Target app service
+      #namespace: tfs
+      app.kubernetes.io/name: scaphandre
+      #release: prometheus # same as above
+  endpoints:
+  - port: metrics # named port in target app
+    scheme: http
+    path: /metrics # path to scrape
+    interval: 5s # scrape interval
+  namespaceSelector:
+    any: false
+    matchNames:
+    - tfs # namespace where the app is running
\ No newline at end of file
-- 
GitLab