Skip to content

Commit 5b1c3fb

Browse files
committed
Add grafana terrform tooling
1 parent 58e1030 commit 5b1c3fb

File tree

8 files changed

+161
-5
lines changed

8 files changed

+161
-5
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
DIRECTORY=$1
5+
6+
# Find all JSON files within the directory
7+
FILES=$(find "$DIRECTORY" -mindepth 1 -maxdepth 1 -type f -name '*.json')
8+
9+
# Create a JSON object where each file's basename is the key, with full paths as values
10+
JSON_OBJECT=$(echo "$FILES" | while read -r FILE; do
11+
BASENAME=$(basename "$FILE" .json)
12+
echo "{\"$BASENAME\": \"$FILE\"}"
13+
done | jq -s 'add')
14+
15+
# Output the JSON map
16+
jq -n --argjson files "$JSON_OBJECT" '$files'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
DIRECTORY=$1
5+
6+
# Use `find` to get the directories' base names
7+
SUBFOLDERS=$(find "$DIRECTORY" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)
8+
9+
# Convert the subfolder names into a JSON object with jq, where each is paired with itself
10+
JSON_OBJECT=$(echo "$SUBFOLDERS" | tr ' ' '\n' | jq -Rn '
11+
[inputs] |
12+
map(select(. != "")) |
13+
map({key: ., value: .}) |
14+
from_entries')
15+
# Output the JSON map
16+
jq -n --argjson subfolders "$JSON_OBJECT" '$subfolders'

services/monitoring/Makefile

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ update.grafana.pwd: .env ## Change grafana pwd
8484
grafanacontainerid=$$(docker ps | grep grafana | awk '{print $$1;}');\
8585
docker exec -ti $$grafanacontainerid grafana-cli admin reset-admin-password $$TRAEFIK_PASSWORD
8686

87+
.PHONY: ensure-grafana-online
88+
ensure-grafana-online:
89+
@set -o allexport; \
90+
source $(REPO_CONFIG_LOCATION); \
91+
set +o allexport; \
92+
url=$${TF_VAR_grafana_url}; \
93+
echo "Waiting for grafana at $$url to become reachable."; \
94+
while true; do \
95+
status_code=$$(curl -k -o /dev/null -s -w "%{http_code}" $$url); \
96+
if [ "$$status_code" -ge 200 ] && [ "$$status_code" -lt 400 ]; then \
97+
echo "Grafana is online"; \
98+
break; \
99+
else \
100+
echo "Grafana still unreachable, waiting 5s for grafana to become reachable..."; \
101+
sleep 5; \
102+
fi; \
103+
done;
87104

88105
.PHONY: grafana-export
89106
grafana-export: .venv## Export the remote grafana dashboards and datasources TO YOUR LOCAL MACHINE
@@ -93,11 +110,16 @@ grafana-export: .venv## Export the remote grafana dashboards and datasources TO
93110
python3 export.py;
94111

95112
.PHONY: grafana-import
96-
grafana-import: grafana/assets .venv ## Imports AND OVERWRITES the remote grafana dashboards and datasources FROM YOUR LOCAL MACHINE
97-
@cd grafana/scripts;\
98-
source ${REPO_BASE_DIR}/.venv/bin/activate;\
99-
pip install -r requirements.txt > /dev/null 2>&1;\
100-
python3 import.py
113+
grafana-import: grafana/assets ensure-grafana-online ## Imports AND OVERWRITES the remote grafana dashboards and datasources FROM YOUR LOCAL MACHINE
114+
@set -o allexport; \
115+
source $(REPO_CONFIG_LOCATION); \
116+
set +o allexport; \
117+
pushd ${REPO_BASE_DIR}/services/monitoring/grafana/terraform && \
118+
terraform init && \
119+
terraform destroy -auto-approve && \
120+
terraform apply -auto-approve; \
121+
popd > /dev/null
122+
101123

102124
.PHONY: config.grafana.dashboards
103125
config.grafana.dashboards: grafana/templates-provisioning/dashboards/simcore/Metrics-dashboard.json.j2 .venv #Configure dashboards for aws or dalco clusters
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.5.1
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Import subfolders using an external script
2+
data "external" "subfolders" {
3+
program = ["bash", "${path.module}/../../../../scripts/tf_helper_list_subfolders.sh", "${path.module}/../assets/shared/dashboards"]
4+
}
5+
6+
// Local mappings of folder names to their paths
7+
locals {
8+
folder_map = data.external.subfolders.result
9+
}
10+
11+
// Create Grafana folders for each subfolder
12+
resource "grafana_folder" "subfolders" {
13+
for_each = local.folder_map
14+
15+
title = each.key // Use each.key to access each folder's name
16+
}
17+
18+
// Function to list all JSON files within a directory
19+
data "external" "dashboard_files" {
20+
for_each = local.folder_map
21+
22+
program = ["bash", "${path.module}/../../../../scripts/tf_helper_list_json_files_in_folder.sh", "${path.module}/../assets/shared/dashboards/${each.key}"]
23+
}
24+
25+
// Create Grafana dashboards from JSON files
26+
resource "grafana_dashboard" "dashboards" {
27+
for_each = toset(flatten([
28+
for folder_name in keys(local.folder_map) : [
29+
for file in values(data.external.dashboard_files[folder_name].result) : "${folder_name},${file}"
30+
]]
31+
))
32+
# CSV approach
33+
config_json = jsonencode(jsondecode(file(split(",", each.value)[1])).dashboard)
34+
folder = grafana_folder.subfolders[split(",", each.value)[0]].id
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
resource "grafana_data_source" "prometheusfederation" {
3+
type = "prometheus"
4+
name = "prometheus-federation"
5+
url = var.prometheus_federation_url
6+
basic_auth_enabled = false
7+
is_default = true
8+
}
9+
10+
resource "grafana_data_source" "prometheuscatchall" {
11+
type = "prometheus"
12+
name = "prometheus-catchall"
13+
url = var.prometheus_catchall_url
14+
basic_auth_enabled = false
15+
is_default = false
16+
uid = "RmZEr52nz"
17+
}
18+
19+
resource "grafana_data_source" "tempo" {
20+
type = "tempo"
21+
name = "tempo"
22+
url = var.tempo_url
23+
basic_auth_enabled = false
24+
is_default = false
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
terraform {
2+
required_version = "~> 1.5.1"
3+
backend "local" {
4+
path = "terraform.tfstate" # Specify the path for the state file, can be a different path if needed
5+
}
6+
required_providers {
7+
grafana = {
8+
source = "grafana/grafana"
9+
version = "~> 3.13"
10+
}
11+
random = {
12+
source = "hashicorp/random"
13+
version = "~> 3.1"
14+
}
15+
}
16+
}
17+
18+
provider "grafana" {
19+
url = var.grafana_url
20+
auth = var.grafana_auth
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "grafana_url" {
2+
description = "grafana_url"
3+
sensitive = false
4+
}
5+
variable "grafana_auth" {
6+
description = "Username:Password"
7+
sensitive = true
8+
}
9+
variable "prometheus_federation_url" {
10+
description = "Prometheus Federation URL"
11+
sensitive = false
12+
}
13+
variable "prometheus_catchall_url" {
14+
description = "Prometheus Catchall URL"
15+
sensitive = false
16+
}
17+
variable "tempo_url" {
18+
description = "Tempo URL"
19+
sensitive = false
20+
}

0 commit comments

Comments
 (0)