Skip to content

Instantly share code, notes, and snippets.

@djfdyuruiry
Last active August 19, 2018 21:10
Show Gist options
  • Save djfdyuruiry/547f1807fc86a56e6308c4a131974097 to your computer and use it in GitHub Desktop.
Save djfdyuruiry/547f1807fc86a56e6308c4a131974097 to your computer and use it in GitHub Desktop.
Automated Grafana Snapshots using Python
from datetime import datetime, timedelta
from requests import get, post
GRAFANA_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
GRAFANA_CREDS = ('user', 'password')
dashboardResponse = get('https://grafana.host/api/dashboards/a/dashboard', auth = GRAFANA_CREDS)
dashboard = dashboardResponse.json()['dashboard']
dashboardTimeRange = dashboard['time']
# the datetime objects below can be modified to specify any 'to' and 'from' date you want
timeNow = datetime.utcnow()
timeThirtyMinutesAgo = timeNow - timedelta(minutes = 30)
timeNowAsGrafanaString = timeNow.strftime(GRAFANA_DATE_FORMAT)
dashboardTimeRange['to'] = timeNowAsGrafanaString
dashboardTimeRange['from'] = timeThirtyMinutesAgo.strftime(GRAFANA_DATE_FORMAT)
snapshotRequest = {
'name': 'python dashboard snapshot (%s)' % timeNowAsGrafanaString,
'dashboard': dashboard
}
post('https://grafana.host/api/snapshots', json = snapshotRequest, auth = GRAFANA_CREDS)
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
[dev-packages]
[requires]
python_version = "3.6"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment