Moving to bash

This commit is contained in:
Philippe Caseiro 2018-06-04 17:01:42 +02:00
parent 3df296b366
commit a3def897fa

69
postservice/88_grafana Normal file → Executable file
View File

@ -1,51 +1,34 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#!/usr/bin/env bash
import os
import requests
import json
grafanaHost=$(CreoleGet srvGrafanaIP 192.168.30.57)
grafanaPort=$(CreoleGet srvGrafanaPort 3000)
grafanaUser="admin"
grafanaPasswd=$(CreoleGet grafana_admin_passwd admin)
grafanaURL="http://${grafanaUser}:${grafanaPasswd}@${grafanaHost}:${grafanaPort}"
datasource_name=$(CreoleGet promDataSource "prometheus")
promHost=$(CreoleGet adresse_ip_eth0)
promPort='9090'
from creole.client import CreoleClient
creole = c = CreoleClient()
cmd="curl"
url="${grafanaURL}/api/datasources"
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'}
data=$(cat <<__EOF__
{"name":"${datasource_name}","type":"prometheus","url":"http://${promHost}:${promPort}","access":"direct"}
__EOF__
)
# Get list of datasources
datasources_get = session.get(os.path.join(grafanaURL, 'api', 'datasources'))
datasources = datasources_get.json()
echo -ne "\tCreating datasource for Prometheus "
res=$(${cmd} "${url}" -H "Content-Type: application-json" --data-binary "${data}" 2>&1 )
excode=${?}
case $res in
*"already exists"*)
echo " ... [Exists]"
;;
*"Datasource added"*)
echo " ... [OK]"
;;
esac
if datasources['message'] == "Unauthorized" :
exit(34)
echo -ne "\tImporting Node Exporter Full Dashboard "
# 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'}
)