diff --git a/postservice/88_grafana b/postservice/88_grafana new file mode 100644 index 0000000..6c01a7c --- /dev/null +++ b/postservice/88_grafana @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os +import requests +import json + +from creole.client import CreoleClient + +creole = c = CreoleClient() + +grafanaHost = creole.get_creole('srvGrafanaIP','127.0.0.1') +grafanaPort = creole.get_creole('srvGrafanaPort', '3000') +grafanaURL = os.path.join('http://', '%s:%s' % (grafanaHost, grafanaPort)) +grafanaUser = "admin" +grafanaPasswd = creole.get_creole('grafana_admin_passwd') +datasource_name = creole.get_creole('promDataSource',"prometheus") +promHost = creole.get_creole('addresse_ip_eth0') +promPort = '9090' + +session = requests.Session() +login_post = session.post ( + os.path.join(grafanaURL, 'login'), + data=json.dumps({ + 'user': grafanaUser, + 'email': '', + 'password': grafanaPasswd + }), + headers={'content-type': 'application/json'} +) + +# Get list of datasources +datasources_get = session.get(os.path.join(grafanaURL, 'api', 'datasources')) +datasources = datasources_get.json() + +if datasources['message'] == "Unauthorized" : + exit(34) + +# Add new datasource +datasources_put = session.put( + os.path.join(grafanaURL, 'api', 'datasources'), + data=json.dumps({ + 'access': 'direct', + 'name': datasource_name, + 'type': 'prometheus', + 'url': 'http://%s:%u' % (promHost, promPort), + 'basicAuth': 'false', + 'withCredentials': 'false' + }), + headers={'content-type': 'application/json'} +)