-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for downloading the latest dashboard version by allowing …
…users to omit the revision number.
- Loading branch information
Showing
3 changed files
with
31 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters