Skip to content

Instantly share code, notes, and snippets.

@jewzaam
Created August 12, 2020 21:02
Show Gist options
  • Save jewzaam/39e32611900ad5aafd88e3ce3c47a90e to your computer and use it in GitHub Desktop.
Save jewzaam/39e32611900ad5aafd88e3ce3c47a90e to your computer and use it in GitHub Desktop.
Fire up local grafana for remote prometheus using oc to get bearer token etc.
#!/bin/bash
DEFAULT_PORT=3000
PORT=${1:-$DEFAULT_PORT}
CONTAINER_ENGINE=${CONTAINER_ENGINE:-docker}
SA_NAME=prometheus-k8s
SA_NAMESPACE=openshift-monitoring
SA_TOKEN=$(oc -n $SA_NAMESPACE sa get-token $SA_NAME)
CLUSTER_NAME=$(oc get infrastructure cluster -o jsonpath='{.status.infrastructureName}')
PROM_HOST=$(oc -n openshift-monitoring get routes prometheus-k8s -o jsonpath='{.spec.host}')
# don't use /tmp as the container won't get appropriate permissions for the configuration
TMP_DIR=~/.grafana/$CLUSTER_NAME
mkdir -p $TMP_DIR
cat << EOF > $TMP_DIR/prometheus.yml
apiVersion: 1
datasources:
- access: proxy
basicAuth: false
database: ''
id: 2
isDefault: false
jsonData:
httpHeaderName1: Authorization
secureJsonData:
httpHeaderValue1: 'Bearer $SA_TOKEN'
name: $CLUSTER_NAME
orgId: 1
password: ''
readOnly: false
type: prometheus
typeLogoUrl: public/app/plugins/datasource/prometheus/img/prometheus_logo.svg
url: https://$PROM_HOST
user: ''
EOF
# fire up grafana with the datasource
# TODO add custom dashboards?
CONTAINER_ID=$($CONTAINER_ENGINE run --rm -d -p $PORT:3000 -v $TMP_DIR:/etc/grafana/provisioning/datasources/:Z grafana/grafana || exit)
trap "rm -rf $TMP_DIR; $CONTAINER_ENGINE stop $CONTAINER_ID 2>&1 > /tmp/debug" EXIT
if [ "$CONTAINER_ID" == "" ];
then
echo "Unable to start container. Probably a port conflict."
exit -1
fi
echo "Close this when you're done, it automatically stops Grafana."
# wait for grafana to start else browser is busted
sleep 5
xdg-open "http://localhost:$PORT/explore?orgId=1" 2>/dev/null
# wait for user input or closing for the trap (kill the container and cleanup temp directory)
read -r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment