Adding datasource

This commit is contained in:
Philippe Caseiro 2018-06-04 13:59:45 +02:00
父節點 4a24601bea
當前提交 3df296b366
共有 1 個文件被更改,包括 51 次插入0 次删除

51
postservice/88_grafana Normal file
查看文件

@ -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'}
)