Skip to content

Instantly share code, notes, and snippets.

@keymon
Forked from leehambley/README.md
Last active June 6, 2020 06:05
Show Gist options
  • Save keymon/64f196f031c2853a06dd to your computer and use it in GitHub Desktop.
Save keymon/64f196f031c2853a06dd to your computer and use it in GitHub Desktop.
Simple bash script to wire up Grafana 2.x with InfluxDB 0.8

Simple bash script to wire up Grafana 2.x with graphite and add dashboards

This Bash script exposes a few functions to create a Grafana data source, like graphite.

Also allows to upload some dashboards.

Usage

$ ./grafana-graphite-wiring.sh

Will setup graphite in localhost

$ ./grafana-upload-dashboard.sh dashboard.json [...]

Will upload the given dashboard to graphana.

Example: Add a pair of dashboards for CF directly from a gist:

./grafana-upload-dashboard.sh <(curl -qs https://gist.githubusercontent.com/keymon/80c8992c803f2192dab3/raw/1ce4c8a06d6f919d656c598218148b2ab0bd3589/cf_services_metrics.json) <(curl -qs https://gist.githubusercontent.com/keymon/80c8992c803f2192dab3/raw/1ce4c8a06d6f919d656c598218148b2ab0bd3589/cf_specific_job_metrics.json)

Credits

based on grafana+influx setup code from leehambley/README.md

#!/bin/sh
. ./grafana-lib.sh
DATASOURCE_NAME=graphite
DATASOURCE_TYPE=graphite
GRAPHITE_URL=http://localhost/
setup() {
if grafana_has_data_source ${DATASOURCE_NAME}; then
info "Grafana: Data source ${DATASOURCE_NAME} already exists"
else
if grafana_create_data_source ${DATASOURCE_NAME} ${DATASOURCE_TYPE} ${GRAPHITE_URL}; then
success "Grafana: Data source ${DATASOURCE_NAME} created"
else
error "Grafana: Data source ${DATASOURCE_NAME} could not be created"
fi
fi
}
setup
# Used for this script to talk to the Grafana API
GRAFANA_URL=http://localhost:3000
COOKIEJAR="/tmp/grafana_session_$$"
function grafana_url {
echo -e ${GRAFANA_URL}/api/
}
function success {
echo "$(tput setaf 2)""$*""$(tput sgr0)"
}
function info {
echo "$(tput setaf 3)""$*""$(tput sgr0)"
}
function error {
echo "$(tput setaf 1)""$*""$(tput sgr0)" 1>&2
exit 1
}
function setup_grafana_session {
username=${1-admin}; shift
password=${1-admin}; shift
if ! curl -H 'Content-Type: application/json;charset=UTF-8' \
--data-binary "{\"user\":\"$username\",\"email\":\"\",\"password\":\"$password\"}" \
--cookie-jar "$COOKIEJAR" \
"${GRAFANA_URL}/login" > /dev/null 2>&1 ; then
echo
error "Grafana Session: Couldn't store cookies at ${COOKIEJAR}"
fi
}
function grafana_has_data_source {
setup_grafana_session
curl --silent --cookie "$COOKIEJAR" "${GRAFANA_URL}/api/datasources" \
| grep "\"name\":\"${1}\"" --silent
}
function grafana_create_data_source {
name=$1; shift
type=$1; shift
url=$1; shift
user=$1; shift
password=$1; shift
database=$1; shift
setup_grafana_session
curl --cookie "$COOKIEJAR" \
-X POST \
--silent \
-H 'Content-Type: application/json;charset=UTF-8' \
--data-binary "{\"name\":\"${name}\",\"type\":\"${type}\",\"url\":\"${url}\",\"access\":\"proxy\",\"database\":\"${database}\",\"user\":\"${user}\",\"password\":\"${password}\"}" \
"${GRAFANA_URL}/api/datasources" 2>&1 | grep 'Datasource added' --silent;
}
function grafana_upload_dashboard {
file=$1; shift
setup_grafana_session
curl --cookie "$COOKIEJAR" \
-X POST \
--silent \
-H 'Content-Type: application/json;charset=UTF-8' \
-d "{\"overwrite\": true, \"dashboard\": $(sed '/"id": /d' < "$file")}" \
"${GRAFANA_URL}/api/dashboards/db" 2>&1 | grep 'success' --silent;
}
#!/bin/sh
. ./grafana-lib.sh
for f in "$@"; do
grafana_upload_dashboard "$f"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment