Skip to content

Instantly share code, notes, and snippets.

@kenske
Created January 31, 2024 19:12
Show Gist options
  • Save kenske/d10f34de0e63e4cf2efa225724baef20 to your computer and use it in GitHub Desktop.
Save kenske/d10f34de0e63e4cf2efa225724baef20 to your computer and use it in GitHub Desktop.
Create Grafana Dashboards
#!/usr/bin/env sh
set -e
# Verify that dashboards are ready
response_code=0
while [ "$response_code" -ne 200 ] ; do
echo "Waiting for dashboards to be ready..."
sleep 5
path="/api/dashboards/uid/some_dashboard"
response_code=$(curl -s -u ${GRAFANA_USERNAME}:${GRAFANA_PASSWORD} "${GRAFANA_HOST}${path}" --write-out '%{http_code}' --output /dev/null)
done
echo "Dashboards are ready"
# Check for existing playlist
path="/api/playlists?query=My+Playlist"
response=$(curl -s -u ${GRAFANA_USERNAME}:${GRAFANA_PASSWORD} "${GRAFANA_HOST}${path}")
id=$(echo $response | jq -r ".[0].uid" || "")
# Delete existing playlist
if [ "$id" = "null" ] ; then
echo "Playlist does not exist"
else
echo "Deleting existing playlist ($id)"
path="/api/playlists/${id}"
curl -X DELETE -u ${GRAFANA_USERNAME}:${GRAFANA_PASSWORD} "${GRAFANA_HOST}${path}" --silent
fi
echo "Creating new playlist..."
curl -s -X POST \
-u ${GRAFANA_USERNAME}:${GRAFANA_PASSWORD} "${GRAFANA_HOST}/api/playlists/" \
-H 'Content-Type: application/json' \
--data-binary @- <<EOF
{
"name": "My Playlist",
"interval": "5m",
"items": [
{
"type": "dashboard_by_tag",
"value": "wall",
"order": 1,
"title": "TV Dashboard"
}
]
}
EOF
echo "\nDone!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment