Last active
August 19, 2018 21:10
-
-
Save djfdyuruiry/547f1807fc86a56e6308c4a131974097 to your computer and use it in GitHub Desktop.
Automated Grafana Snapshots using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[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