Skip to content

Commit

Permalink
Created script to automatically import dashboards.
Browse files Browse the repository at this point in the history
Updated readme file with new instructions.
  • Loading branch information
CarlosCuezva committed Jan 10, 2023
1 parent 88e460a commit c7a5fad
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 554 deletions.
51 changes: 39 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
# Personal Grafana boards for Teslamate
# TeslaMate - Custom dashboards for Grafana

[![](https://img.shields.io/badge/Donate-PayPal-ff69b4.svg)](https://www.paypal.com/donate?hosted_button_id=QF2MBMQZP4V2J)

## How to import boards

Tested with Teslamate v1.27.1 and Grafana v8.3.4

1. Login to Grafana
2. Click in "Dashboards" option and select "Browse"
3. Create a personal folder, e.g. "Teslamate-Extra"
4. Go to new folder
5. Press the "Import" button
6. Press the "Upload JSON file" button
7. Select JSON file from your computer
8. Select the appropiate Teslamate datasource from the available droplist.
9. And finally, press the "Import" button
Tested with Teslamate v1.27.1 and Grafana v8.5.6

1. Sign in to Grafana
2. Create an API key (More information below)
3. Import dashboards one by one manually or run the *dashboards.sh* file to import them automatically

## Create an API key

1. Sign in to Grafana, hover your cursor over Configuration (the gear icon), and click API Keys
2. Click "Add API key"
3. Enter a unique name for the key, e.g. "Import dashboards"
4. In "Role", select Admin option
5. In "Time to live" enter for example "1d" for 1 day, "1m" for 1 month or "1y" for 1 year
6. Click Add

More info in [Grafana documentation](https://grafana.com/docs/grafana/v8.5/administration/api-keys/create-api-key/) page.

## Import dashboards manually

1. Click in "Dashboards" option and select "Browse"
2. Create a personal folder, e.g. "Teslamate-Extra"
3. Go to new folder
4. Press the "Import" button
5. Press the "Upload JSON file" button
6. Select JSON file from your computer
7. And finally, press the "Import" button

## Import dashboards automatically with *dashboards.sh*

With this script, you can create or update automatically all dashboards.

You have to execute the command "./dashboards.sh restore" modifying the URL and TOKEN variables as indicated in the example.

**URL** specifies the URL of the Grafana instance

**TOKEN** specifies the security key of the API, it's generated in Grafana.

``URL=http://localhost:3000 TOKEN=XXXXXXXXXXXX ./dashboards.sh restore``

## Screenshots

Expand Down
97 changes: 97 additions & 0 deletions dashboards.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash

# Author: Carlos Cuezva
# Created: January 2023
# Last updated: 01/10/2023
# Source: https://github.com/CarlosCuezva/dashboards-Grafana-Teslamate/blob/main/dashboards.sh
#
# URL specifies the URL of the Grafana instance.
#
# TOKEN specifies the security key of the API, it's generated in Grafana.
#
# DASHBOARD_DIRECTORY represents the path to the directory where the JSON files corresponding to the dashboards exist.
#
# DESTINATION_DIRECTORY specifies the folder where the dashboards will be saved.
#
# DIRECTORY_UID specifies the unique UID of folder where the dashboards will be saved.

set -o errexit

readonly URL=${URL:-"http://localhost:3000"}
readonly TOKEN=${TOKEN:-"XXXXXXXXXXXX"}
readonly DASHBOARDS_DIRECTORY=${DASHBOARDS_DIRECTORY:-"./dashboards"}
readonly DESTINATION_DIRECTORY=${DESTINATION_DIRECTORY:-"Teslamate - Custom"}
readonly DIRECTORY_UID="AySq122Vh"


main() {
local task=$1

echo "
URL: $URL
DASHBOARDS_DIRECTORY: $DASHBOARDS_DIRECTORY
DESTINATION_DIRECTORY: $DESTINATION_DIRECTORY
"

case $task in
restore) restore;;
*) exit 1;;
esac

}


restore() {
create_folder
folder_id=$(get_folder_id)

find "$DASHBOARDS_DIRECTORY" -type f -name \*.json -print0 |
while IFS= read -r -d '' dashboard_path; do
curl \
--silent --show-error --output /dev/null \
--location \
--request POST "$URL/api/dashboards/db" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" \
--data-raw "{
\"dashboard\": $(cat "$dashboard_path"),
\"folderId\":$folder_id, \
\"overwrite\": true,
\"inputs\": [
{
\"name\":\"DS_CLOUDWATCH\",
\"type\":\"datasource\",
\"pluginId\":\"cloudwatch\",
\"value\":\"Teslamate\"
}
]
}"

echo "RESTORED $(basename "$dashboard_path")"
done
}

get_folder_id() {
curl \
--silent \
--location \
--request GET "$URL/api/folders/$DIRECTORY_UID" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" |
jq '.id'
}

create_folder() {
curl \
--silent --show-error --output /dev/null \
--location \
--request POST "$URL/api/folders" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $TOKEN" \
--data-raw "{
\"uid\": \"$DIRECTORY_UID\",
\"title\": \"$DESTINATION_DIRECTORY\"
}"
}

main "$@"
Loading

0 comments on commit c7a5fad

Please sign in to comment.