Skip to content

Commit

Permalink
Merge pull request #3 from m4n3dw0lf/master
Browse files Browse the repository at this point in the history
Improving Server URL variable declaration.
  • Loading branch information
m4n3dw0lf authored Sep 12, 2017
2 parents bd74945 + f3d8cf2 commit f9dbfae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pygrafana

Python Library to consume Grafana's API.
Python Module to consume Grafana's API.


## Instalation
Expand All @@ -12,7 +12,7 @@ $ sudo pip install pygrafana

```
>>> from pygrafana import GrafanaManager
>>> gm = GrafanaManager("localhost",3000)
>>> gm = GrafanaManager("http://localhost:3000")
>>> gm.Login("admin","admin")
```

Expand Down Expand Up @@ -58,7 +58,7 @@ $ sudo pip install pygrafana
```
#!/usr/bin/python2.7
from pygrafana import GrafanaManager
gm = GrafanaManager("localhost",3000)
gm = GrafanaManager("http://localhost:3000")
gm.Login("admin","admin")
gm.EnablePlugin("alexanderzobnin-zabbix-app")
gm.zbx_user = "admin"
Expand Down
35 changes: 11 additions & 24 deletions pygrafana/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import yaml

class GrafanaManager(object):
def __init__(self, server, port):
def __init__(self, server):
self.server = server
self.port = port
self.proxies = False
self.verify = False

self.zbx_user = ""
self.zbx_pswd = ""
Expand All @@ -24,10 +24,7 @@ def __init__(self, server, port):
self.oim_server = ""

def Login(self,username,password):
if not self.proxies:
self.login = requests.post("http://localhost:3000/login",json={"user":username,"email":" ","password":password})
else:
self.login = requests.post("http://localhost:3000/login",json={"user":username,"email":" ","password":password},proxies=self.proxies)
self.login = requests.post("{}/login".format(self.server),json={"user":username,"email":" ","password":password},proxies=self.proxies, verify=self.verify)
self.cookie = self.login.headers['Set-Cookie'].split(";")
self.token = ""
del self.cookie[0]
Expand All @@ -42,27 +39,23 @@ def Login(self,username,password):
self.token=self.token.strip(" ").strip("\n")

def EnablePlugin(self,plugin):
hds = {'Referer':'http://{}:{}/plugins/{}/edit'.format(self.server,self.port,plugin),'Cookie':self.token, 'Accept':'application/json, text/plain, */*', 'X-Grafana-Org-Id':'1', 'Content-Type':'application/json;charset=utf-8','DNT':'1' }
hds = {'Referer':'{}/plugins/{}/edit'.format(self.server,plugin),'Cookie':self.token, 'Accept':'application/json, text/plain, */*', 'X-Grafana-Org-Id':'1', 'Content-Type':'application/json;charset=utf-8','DNT':'1' }
jdata = '{"enabled":true,"pinned":true,"jsonData":null}'
jdata = yaml.load(jdata)
if not self.proxies:
enable = requests.post("http://{}:{}/api/plugins/{}/settings".format(self.server,self.port,plugin),json=jdata,headers=hds)
else:
enable = requests.post("http://{}:{}/api/plugins/{}/settings".format(self.server,self.port,plugin),json=jdata,headers=hds, proxies=self.proxies)
enable = requests.post("{}/api/plugins/{}/settings".format(self.server,plugin),json=jdata,headers=hds, proxies=self.proxies, verify=self.verify)
print enable.text

def CreateDatastore(self,datastore):
#Datastores supported:
# - zabbix
hds = {'Referer':'http://{}:{}/datasources/new?gettingstarted'.format(self.server,self.port),'Cookie':self.token, 'Accept':'application/json, text/plain, */*', 'X-Grafana-Org-Id':'1', 'Content-Type':'application/json;charset=utf-8','DNT':'1' }
hds = {'Referer':'{}/datasources/new?gettingstarted'.format(self.server),'Cookie':self.token, 'Accept':'application/json, text/plain, */*', 'X-Grafana-Org-Id':'1', 'Content-Type':'application/json;charset=utf-8','DNT':'1' }
if datastore == "Zabbix":
jdata = '{{"name":"Zabbix","type":"alexanderzobnin-zabbix-datasource","url":"{}","access":"direct","jsonData":{{"dbConnection":{{"enable":false}},"username":"{}","password":"{}"}},"secureJsonFields":{{}},"isDefault":true}}'.format(self.zbx_url,self.zbx_user,self.zbx_pswd)
jdata = yaml.load(jdata)
if not self.proxies:
enable = requests.post("http://{}:{}/api/datasources".format(self.server,self.port),json=jdata,headers=hds)
enable = requests.post("{}/api/datasources".format(self.server),json=jdata,headers=hds, proxies=self.proxies, verify=self.verify)
print enable.text
else:
enable = requests.post("http://{}:{}/api/datasources".format(self.server,self.port),json=jdata,headers=hds, proxies=self.proxies)
print enable.text
print "Datastore not supported."

def ImportDashboard(self,dashboard):
#Not working yet.
Expand All @@ -73,19 +66,13 @@ def ImportDashboard(self,dashboard):
dash.replace("<SERVER>",self.oim_server)
jdata = yaml.load(dash)
#print dashboard
if not self.proxies:
enable = requests.post("http://{}:{}/api/dashboards/db".format(self.server,self.port),json=jdata,headers=hds)
else:
enable = requests.post("http://{}:{}/api/dashboards/db".format(self.server,self.port),json=jdata,headers=hds, proxies=self.proxies)
enable = requests.post("{}/api/dashboards/db".format(self.server),json=jdata,headers=hds, proxies=self.proxies, verify=self.verify)
print enable.text


def DeleteDashboard(self, dashboard):
hds = {'Accept':'application/json', 'Content-Type':'application/json;charset=utf-8','Cookie':self.token}
if not self.proxies:
delete = requests.delete("http://{}:{}/api/dashboards/db/{}".format(self.server, self.port, dashboard), headers=hds)
else:
delete = requests.delete("http://{}:{}/api/dashboards/db/{}".format(self.server, self.port, dashboard), headers=hds, proxies=self.proxies)
delete = requests.delete("{}/api/dashboards/db/{}".format(self.server, dashboard), headers=hds, proxies=self.proxies, verify=self.verify)
print delete.text

#if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
setup(
name = 'pygrafana',
packages = ['pygrafana'],
version = '0.2',
description = "Library to consume Grafana's API",
version = '0.3',
description = "Module to consume Grafana's API",
author = 'Angelo Moura',
author_email = 'angelo.moura@sec4you.com.br',
url = 'https://github.com/sec4you/pygrafana',
download_url = 'https://github.com/sec4you/pygrafana/archive/0.2.tar.gz',
download_url = 'https://github.com/sec4you/pygrafana/archive/0.3.tar.gz',
keywords = ['grafana','api'],
install_requires=['PyYAML>=3.12','requests>=2.18.4'],
classifiers = [],
Expand Down

0 comments on commit f9dbfae

Please sign in to comment.