-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from m4n3dw0lf/master
pygrafana version 0.1
- Loading branch information
Showing
6 changed files
with
854 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# file GENERATED by distutils, do NOT edit | ||
setup.cfg | ||
setup.py | ||
pygrafana/__init__.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,64 @@ | ||
# pygrafana | ||
Python Library to consume Grafana's API. | ||
|
||
Python Library to consume Grafana API. | ||
|
||
|
||
## First - Login into Grafana | ||
|
||
``` | ||
>>> from pygrafana import GrafanaManager | ||
>>> gm = GrafanaManager("localhost",3000) | ||
>>> gm.Login("admin","admin") | ||
``` | ||
|
||
## Create Zabbix Datastore | ||
|
||
### TO-DO, fill parameters of other datastore types | ||
|
||
``` | ||
>>> gm.zbx_user = "admin" | ||
>>> gm.zbx_pswd = "zabbix" | ||
>>> gm.zbx_url = "http://localhost/api_jsonrpc.php" | ||
>>> gm.CreateDatastore("Zabbix") | ||
``` | ||
|
||
## Import Dashboard | ||
|
||
``` | ||
>>> gm.ImportDashboard("./example_dashboard.json") | ||
``` | ||
|
||
|
||
## Delete Dashboard | ||
``` | ||
>>> gm.DeleteDashboard("example-dashboard") | ||
``` | ||
|
||
|
||
## Enable Plugin | ||
|
||
``` | ||
>>> gm.EnablePlugin("alexanderzobnin-zabbix-app") | ||
``` | ||
|
||
## Through Proxy | ||
|
||
``` | ||
>>> gm.proxies = {'http':'http://localhost:8080','https':'https://localhost:8443'} | ||
``` | ||
|
||
## Examples | ||
|
||
- **Example 1**: Auto-configuring Grafana-Zabbix API and importing a dashboard. | ||
``` | ||
#!/usr/bin/python2.7 | ||
from pygrafana import GrafanaManager | ||
gm = GrafanaManager("localhost",3000) | ||
gm.Login("admin","admin") | ||
gm.EnablePlugin("alexanderzobnin-zabbix-app") | ||
gm.zbx_user = "admin" | ||
gm.zbx_pswd = "zabbix" | ||
gm.zbx_url = "http://localhost/api_jsonrpc.php" | ||
gm.CreateDatastore("Zabbix") | ||
gm.ImportDashboard("./zabbix_dashboard.json") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/bin/python2.7 | ||
|
||
# Copyrights (c) Sec4You Consulting. | ||
|
||
import requests | ||
import yaml | ||
from time import sleep | ||
from os import getenv | ||
|
||
class GrafanaManager(object): | ||
def __init__(self, server, port): | ||
self.server = server | ||
self.port = port | ||
self.proxies = False | ||
|
||
self.zbx_user = "" | ||
self.zbx_pswd = "" | ||
self.zbx_url = "" | ||
|
||
self.oim_server = str(getenv('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.cookie = self.login.headers['Set-Cookie'].split(";") | ||
self.token = "" | ||
del self.cookie[0] | ||
for i in self.cookie: | ||
if "grafana_sess" in i: | ||
grafana_sess = "{};".format(i.split(",")[1]) | ||
if "grafana_user" in i: | ||
grafana_user = "{};".format(i.split(",")[1]) | ||
if "grafana_remember" in i: | ||
grafana_remember = "{}".format(i.split(",")[1]) | ||
self.token = grafana_sess + grafana_user + grafana_remember | ||
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' } | ||
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) | ||
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' } | ||
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) | ||
else: | ||
enable = requests.post("http://{}:{}/api/datasources".format(self.server,self.port),json=jdata,headers=hds, proxies=self.proxies) | ||
print enable.text | ||
|
||
def ImportDashboard(self,dashboard): | ||
#Not working yet. | ||
#Templates at directory: grafana_dashboards | ||
hds = {'Accept':'application/json', 'Content-Type':'application/json;charset=utf-8','Cookie':self.token} | ||
dash = '{"dashboard":'+ open(dashboard,"r").read() + '}' | ||
if self.oim_server: | ||
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) | ||
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) | ||
print delete.text | ||
|
||
#if __name__ == "__main__": | ||
# gm = GrafanaManager("localhost",3000) | ||
# Uncomment to enable API requests through proxy | ||
#gm.proxies = {'http':'http://localhost:8080','https':'https://localhost:8443'} | ||
# gm.Login("admin","admin") | ||
#gm.EnablePlugin("alexanderzobnin-zabbix-app") | ||
#gm.zbx_user = "admin" | ||
#gm.zbx_pswd = "zabbix" | ||
#gm.zbx_url = "http://localhost/api_jsonrpc.php" | ||
#gm.CreateDatastore("Zabbix") | ||
# gm.ImportDashboard("./oim_dashboard.json") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from distutils.core import setup | ||
setup( | ||
name = 'pygrafana', | ||
packages = ['pygrafana'], | ||
version = '0.1', | ||
description = "Library to consume Grafana's API", | ||
author = 'Angelo Moura', | ||
author_email = '[email protected]', | ||
url = 'https://github.com/sec4you/pygrafana', | ||
download_url = 'https://github.com/sec4you/pygrafana/archive/0.1.tar.gz', | ||
keywords = ['grafana','api'], | ||
install_requires=['PyYAML>=3.12','requests>=2.18.4'], | ||
classifiers = [], | ||
) |