Skip to content

Commit

Permalink
Add support for downloading the latest dashboard version by allowing …
Browse files Browse the repository at this point in the history
…users to omit the revision number.
  • Loading branch information
tanmayja committed Aug 28, 2024
1 parent 3943b0c commit 9b0aa86
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
46 changes: 28 additions & 18 deletions config/monitoring/grafana/config/download_files.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
#!/bin/sh

# dashboards to download
# DASHBOARDS="<dashboard_id>:<revision> ..."
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 <dashboard_id>:<revision> or <dashboard_id>
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"
2 changes: 1 addition & 1 deletion config/monitoring/grafana/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions config/monitoring/grafana/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9b0aa86

Please sign in to comment.