Adding OSM Monitoring dashboards to Grafana charm
[osm/devops.git] / installers / charm / grafana / src / pod_spec.py
index 9915a9e..8d525f3 100644 (file)
@@ -24,6 +24,8 @@ import logging
 from ipaddress import ip_network
 from typing import Any, Dict, List
 from urllib.parse import urlparse
+from pathlib import Path
+from string import Template
 
 logger = logging.getLogger(__name__)
 
@@ -224,55 +226,79 @@ def _make_pod_ingress_resources(
     return [ingress]
 
 
-def _make_pod_files(relation: Dict[str, Any]) -> List[Dict[str, Any]]:
+def _make_pod_files(
+    config: Dict[str, Any], relation: Dict[str, Any]
+) -> List[Dict[str, Any]]:
     """Generating ConfigMap information
 
     Args:
+        config (Dict[str, Any]): configuration information.
         relation (Dict[str, Any]): relation information.
 
     Returns:
         List[Dict[str, Any]]: ConfigMap information.
     """
+    template_data = {**config, **relation}
+    dashboards = []
+
+    if config.get("osm_dashboards", False):
+        dashboards.extend(
+            [
+                {
+                    "path": "kafka_exporter_dashboard.yaml",
+                    "content": Template(
+                        Path("files/kafka_exporter_dashboard.yaml").read_text()
+                    ),
+                },
+                {
+                    "path": "mongodb_exporter_dashboard.yaml",
+                    "content": Template(
+                        Path("files/mongodb_exporter_dashboard.yaml").read_text()
+                    ),
+                },
+                {
+                    "path": "mysql_exporter_dashboard.yaml",
+                    "content": Template(
+                        Path("files/mysql_exporter_dashboard.yaml").read_text()
+                    ),
+                },
+                {
+                    "path": "nodes_exporter_dashboard.yaml",
+                    "content": Template(
+                        Path("files/nodes_exporter_dashboard.yaml").read_text()
+                    ),
+                },
+                {
+                    "path": "summary_dashboard.yaml",
+                    "content": Template(
+                        Path("files/summary_dashboard.yaml").read_text()
+                    ),
+                },
+            ]
+        )
+
+    dashboards.append(
+        {
+            "path": "dashboard_osm.yaml",
+            "content": Template(Path("files/default_dashboards.yaml").read_text()),
+        }
+    )
+
     files = [
         {
             "name": "dashboards",
             "mountPath": "/etc/grafana/provisioning/dashboards/",
-            "files": [
-                {
-                    "path": "dashboard-osm.yml",
-                    "content": (
-                        "apiVersion: 1\n"
-                        "providers:\n"
-                        "  - name: 'osm'\n"
-                        "    orgId: 1\n"
-                        "    folder: ''\n"
-                        "    type: file\n"
-                        "    options:\n"
-                        "      path: /etc/grafana/provisioning/dashboards/\n"
-                    ),
-                }
-            ],
+            "files": dashboards,
         },
         {
             "name": "datasources",
             "mountPath": "/etc/grafana/provisioning/datasources/",
             "files": [
                 {
-                    "path": "datasource-prometheus.yml",
-                    "content": (
-                        "datasources:\n"
-                        "  - access: proxy\n"
-                        "    editable: true\n"
-                        "    is_default: true\n"
-                        "    name: osm_prometheus\n"
-                        "    orgId: 1\n"
-                        "    type: prometheus\n"
-                        "    version: 1\n"
-                        "    url: http://{}:{}\n".format(
-                            relation.get("prometheus_host"),
-                            relation.get("prometheus_port"),
-                        )
-                    ),
+                    "path": "datasource_prometheus.yaml",
+                    "content": Template(
+                        Path("files/default_dashboards.yaml").read_text()
+                    ).substitute(template_data),
                 }
             ],
         },