Browse Source

Moving to bash

FixSondes
Philippe Caseiro 1 year ago
parent
commit
a3def897fa
1 changed files with 26 additions and 43 deletions
  1. 26
    43
      postservice/88_grafana

+ 26
- 43
postservice/88_grafana View File

@@ -1,51 +1,34 @@
1
-#!/usr/bin/env python
2
-# -*- coding: utf-8 -*-
1
+#!/usr/bin/env bash
3 2
 
4
-import os
5
-import requests
6
-import json
3
+grafanaHost=$(CreoleGet srvGrafanaIP 192.168.30.57)
4
+grafanaPort=$(CreoleGet srvGrafanaPort 3000)
5
+grafanaUser="admin"
6
+grafanaPasswd=$(CreoleGet grafana_admin_passwd admin)
7
+grafanaURL="http://${grafanaUser}:${grafanaPasswd}@${grafanaHost}:${grafanaPort}"
8
+datasource_name=$(CreoleGet promDataSource "prometheus")
9
+promHost=$(CreoleGet adresse_ip_eth0)
10
+promPort='9090'
7 11
 
8
-from creole.client import CreoleClient
9 12
 
10
-creole = c = CreoleClient()
13
+cmd="curl"
14
+url="${grafanaURL}/api/datasources"
11 15
 
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'}
16
+data=$(cat <<__EOF__
17
+{"name":"${datasource_name}","type":"prometheus","url":"http://${promHost}:${promPort}","access":"direct"}
18
+__EOF__
30 19
 )
31 20
 
32
-# Get list of datasources
33
-datasources_get = session.get(os.path.join(grafanaURL, 'api', 'datasources'))
34
-datasources = datasources_get.json()
21
+echo -ne "\tCreating datasource for Prometheus "
22
+res=$(${cmd} "${url}" -H "Content-Type: application-json" --data-binary "${data}" 2>&1 )
23
+excode=${?}
24
+case $res in
25
+    *"already exists"*)
26
+        echo " ... [Exists]"
27
+    ;;
28
+    *"Datasource added"*)
29
+        echo " ... [OK]"
30
+    ;;
31
+esac
35 32
 
36
-if datasources['message'] == "Unauthorized" :
37
-    exit(34)
33
+echo -ne "\tImporting Node Exporter Full Dashboard "
38 34
 
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
-)

Loading…
Cancel
Save