From 9b0aa86b71e731513b65a11bd6530b45a3618b94 Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Wed, 28 Aug 2024 14:38:45 +0530 Subject: [PATCH] Add support for downloading the latest dashboard version by allowing users to omit the revision number. --- .../grafana/config/download_files.sh | 46 +++++++++++-------- config/monitoring/grafana/kustomization.yaml | 2 +- config/monitoring/grafana/statefulset.yaml | 4 +- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/config/monitoring/grafana/config/download_files.sh b/config/monitoring/grafana/config/download_files.sh index 16d5db73..eaced64e 100644 --- a/config/monitoring/grafana/config/download_files.sh +++ b/config/monitoring/grafana/config/download_files.sh @@ -1,30 +1,40 @@ #!/bin/sh -# dashboards to download -# DASHBOARDS=": ..." -DASHBOARDS="16119:10 16115:7" +# Check if curl and jq is installed; if not, install curl and jq +if ! command -v curl >/dev/null 2>&1 || ! command -v jq >/dev/null 2>&1; then + echo "curl or jq not found. Installing..." + apk add --no-cache curl jq +else + echo "curl and jq are already installed." +fi +# Define the dashboards to download in the format : or +DASHBOARDS="16119:10 16115:7 20279" + +# Directory where the dashboards will be saved TARGET_DIR="/mnt/data" mkdir -p "$TARGET_DIR" -for DASHBOARD in $DASHBOARDS; do - delimiter=':' - - # Count the number of occurrences of the delimiter - count=$(echo "$DASHBOARD" | awk -F"$delimiter" '{print NF-1}') +DELIMITER=':' - if [ "$count" -eq 1 ]; then - # Split the string into two parts - ID=$(echo "$DASHBOARD" | cut -d"$delimiter" -f1) - REVISION=$(echo "$DASHBOARD" | cut -d"$delimiter" -f2) - - URL=https://grafana.com/api/dashboards/$ID/revisions/$REVISION/download - FILENAME=$ID-"rev"$REVISION.json - curl -o "$TARGET_DIR/$FILENAME" "$URL" +# Loop through each dashboard identifier in DASHBOARDS +for DASHBOARD in $DASHBOARDS; do + if echo "$DASHBOARD" | grep -q "$DELIMITER"; then + # If the delimiter ':' exists, split into ID and REVISION + ID=$(echo "$DASHBOARD" | cut -d"$DELIMITER" -f1) + REVISION=$(echo "$DASHBOARD" | cut -d"$DELIMITER" -f2) + FILENAME="$ID-rev$REVISION.json" + URL="https://grafana.com/api/dashboards/$ID/revisions/$REVISION/download" + curl -o "$TARGET_DIR/$FILENAME" "$URL" else - echo "Error: The string must contain exactly one delimiter." + # No delimiter, only the ID is provided + ID="$DASHBOARD" + FILENAME="$ID.json" + URL="https://grafana.com/api/dashboards/$ID" + curl -s "$URL" | jq '.json' > "$TARGET_DIR/$FILENAME" fi done -echo "Downloaded files:" +# List the downloaded files +echo "Downloaded dashboard files:" ls -l "$TARGET_DIR" \ No newline at end of file diff --git a/config/monitoring/grafana/kustomization.yaml b/config/monitoring/grafana/kustomization.yaml index 79d29064..cfd14ad2 100644 --- a/config/monitoring/grafana/kustomization.yaml +++ b/config/monitoring/grafana/kustomization.yaml @@ -22,7 +22,7 @@ configMapGenerator: - name: grafana-dashboard-config files: - config/aerospike_grafana_dashboards_config.yaml - - name: download-script-configmap + - name: download-script files: - config/download_files.sh diff --git a/config/monitoring/grafana/statefulset.yaml b/config/monitoring/grafana/statefulset.yaml index e7a78bff..26914c6e 100644 --- a/config/monitoring/grafana/statefulset.yaml +++ b/config/monitoring/grafana/statefulset.yaml @@ -10,7 +10,7 @@ spec: terminationGracePeriodSeconds: 120 initContainers: - name: download-dashboards - image: docker.io/curlimages/curl:7.85.0 + image: alpine:latest command: ["/bin/sh"] args: [ "-c", "/bin/sh -x /mnt/scripts/download_files.sh" ] volumeMounts: @@ -80,7 +80,7 @@ spec: name: aerospike-monitoring-stack-grafana-dashboard-config - name: script-volume configMap: - name: aerospike-monitoring-stack-download-script-configmap + name: aerospike-monitoring-stack-download-script - name: grafana-data persistentVolumeClaim: claimName: aerospike-monitoring-stack-grafana-data