Browse Source

Adding datasource

Philippe Caseiro 11 months ago
parent
commit
3df296b366
1 changed files with 51 additions and 0 deletions
  1. 51
    0
      postservice/88_grafana

+ 51
- 0
postservice/88_grafana View File

@@ -0,0 +1,51 @@
1
+#!/usr/bin/env python
2
+# -*- coding: utf-8 -*-
3
+
4
+import os
5
+import requests
6
+import json
7
+
8
+from creole.client import CreoleClient
9
+
10
+creole = c = CreoleClient()
11
+
12
+grafanaHost = creole.get_creole('srvGrafanaIP','127.0.0.1')
13
+grafanaPort = creole.get_creole('srvGrafanaPort', '3000')
14
+grafanaURL  = os.path.join('http://', '%s:%s' % (grafanaHost, grafanaPort))
15
+grafanaUser = "admin"
16
+grafanaPasswd = creole.get_creole('grafana_admin_passwd')
17
+datasource_name = creole.get_creole('promDataSource',"prometheus")
18
+promHost = creole.get_creole('addresse_ip_eth0')
19
+promPort = '9090'
20
+
21
+session = requests.Session()
22
+login_post = session.post (
23
+    os.path.join(grafanaURL, 'login'),
24
+    data=json.dumps({
25
+        'user': grafanaUser,
26
+        'email': '',
27
+        'password': grafanaPasswd
28
+    }),
29
+    headers={'content-type': 'application/json'}
30
+)
31
+
32
+# Get list of datasources
33
+datasources_get = session.get(os.path.join(grafanaURL, 'api', 'datasources'))
34
+datasources = datasources_get.json()
35
+
36
+if datasources['message'] == "Unauthorized" :
37
+    exit(34)
38
+
39
+# Add new datasource
40
+datasources_put = session.put(
41
+    os.path.join(grafanaURL, 'api', 'datasources'),
42
+    data=json.dumps({
43
+        'access': 'direct',
44
+        'name': datasource_name,
45
+        'type': 'prometheus',
46
+        'url': 'http://%s:%u' % (promHost, promPort),
47
+        'basicAuth': 'false',
48
+        'withCredentials': 'false'
49
+    }),
50
+    headers={'content-type': 'application/json'}
51
+)