Skip to content
Snippets Groups Projects
Commit 047d89fc authored by Michele Grandolfo's avatar Michele Grandolfo
Browse files

Merge branch 'zabbix-external-script-dev' into 'master'

Added zabbix templates to be imported from UI or via API



See merge request !5
parents ece33205 39b8dc97
No related branches found
No related tags found
1 merge request!5Added zabbix templates to be imported from UI or via API
#!/usr/bin/env python
import sys
import httplib
import json
import io
################## Start API service class ##################
class api_response():
__status = None
__data = None
__msg = None
def __init__(self, status, data, msg):
self.setStatus(status)
self.setData(data)
self.setMsg(msg)
def setStatus(self, status):
self.__status = status
def getStatus(self):
return self.__status
def setData(self, data):
self.__data = data
def getData(self):
return self.__data
def setMsg(self, msg):
self.__msg = msg
def getMsg(self):
return self.__msg
class api_request():
@staticmethod
def httpGet(url, host, port, header):
conn = httplib.HTTPConnection(host, port)
conn.request("GET", url, headers = header)
res = conn.getresponse()
response = api_response(res.status, res.read(), res.msg)
return response
@staticmethod
def httpsGet(url, host, port, header):
conn = httplib.HTTPSConnection(host, port)
conn.request("GET", url, headers = header)
res = conn.getresponse()
response = api_response(res.status, res.read(), res.msg)
return response
@staticmethod
def httpPost(url, host, port, params, headers):
conn = httplib.HTTPConnection(host, port)
conn.request("POST", url, params, headers)
res = conn.getresponse()
response = api_response(res.status, res.read(), res.msg)
return response
@staticmethod
def httpsPost(url, host, port, params, headers):
conn = httplib.HTTPSConnection(host, port)
conn.request("POST", url, params, headers)
res = conn.getresponse()
response = api_response(res.status, res.read(), res.msg)
return response
################## End API service class ##################
################## Start Structure service function ##################
def add_host(struct, host, enable, state):
find = 0
if 'hosts' not in struct.keys():
struct['hosts'] = []
for h in iter(struct['hosts']):
if h['host'] == host:
h['services'][service] = state
find = 1
else:
continue
if find == 0:
struct['hosts'].append({'host' : host, 'enable' : enable, 'state' : state})
################## End Structure service function ##################
################## Start Prisma class ##################
class Prisma:
__username = ''
__tenant = ''
__token = ''
__password = ''
__tenantID = None
def __init__(self, username, tenant, password):
auth = '{"auth": {"tenantName": "%s","passwordCredentials": {"username": "%s","password": "%s"}}}' % (tenant, username, password)
header = {"Content-Type": "application/json", "Accept": "application/json"}
res = api_request.httpsPost('/v2.0/tokens','prisma-cloud.ba.infn.it', 5000, auth, header)
res_json = json.loads(res.getData())
self.setUsername(username)
self.setTenant(tenant)
self.setTenantID(res_json['access']['token']['tenant']['id'])
self.setToken(res_json['access']['token']['id'])
self.setPassword(password)
def setUsername(self, username):
self.__username = username
def setPassword(self, password):
self.__password = password
def setTenant(self, tenant):
self.__tenant = tenant
def getTenant(self):
return self.__tenant
def setTenantID(self, tenantID):
self.__tenantID = tenantID
def getTenantID(self):
# tenantID = None
# res = api_request.httpsGet("%s%s" % ('/v2.0/tenants?name=', self.getTenant()), 'prisma-cloud.ba.infn.it', 35357, self.getAuth())
# if res.getStatus() == 200:
# tenantID = json.loads(res.getData())['tenant']['id']
# return tenantID
return self.__tenantID
def setToken(self, token):
self.__token = token
def getToken(self):
return self.__token
def getAuth(self):
return {"X-Auth-Token": self.getToken()}
def nova_service_list(self):
service = None
res = api_request.httpGet("%s%s%s" % ('/v2/', self.getTenantID(), '/os-services'), 'prisma-cloud.ba.infn.it', 8774, self.getAuth())
if res.getStatus() == 200:
service = json.loads(res.getData())
return service
def neutron_agent_list(self):
agent = None
res = api_request.httpGet('/v2.0/agents.json', 'prisma-cloud-net.ba.infn.it', 9696, self.getAuth())
if res.getStatus() == 200:
agent = json.loads(res.getData())
return agent
def glance_images(self):
images = None
res = api_request.httpGet('/v1.1/images', 'prisma-cloud.ba.infn.it', '9292', self.getAuth())
if res.getStatus() == 200:
images = json.loads(res.getData())
return images
def cinder_blocks_list(self):
blocks = None
res = api_request.httpGet('%s%s%s' % ('/v2/', self.getTenantID(), '/os-services'), 'prisma-cloud.ba.infn.it', '8776', self.getAuth())
if res.getStatus() == 200:
blocks = json.loads(res.getData())
return blocks
def swift_blocks_list(self):
blocks = None
res = api_request.httpsGet('%s%s%s' % ('/v1/AUTH_', self.getTenantID(), '?format=json'), 'prisma-swift.ba.infn.it', '8080', self.getAuth())
if res.getStatus() == 200:
blocks = json.loads(res.getData())
return blocks
################## End Prisma class ##################
def encode():
struct = {}
prisma = Prisma('admin', 'admin', 'password_da_cambiare')
nova = prisma.nova_service_list()
if nova != None:
for i in iter(nova['services']):
if i['binary'] == 'nova-compute':
add_host(struct, i['host'], 0 if i['status'] == 'disabled' else 1, 0 if i['state'] == 'down' else 1)
# print json.dumps(nova, sort_keys=True, indent=4)
return struct
def main():
print encode()
if __name__ == "__main__":
main()
#!/bin/bash
#python "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/hypervisor-show
python /usr/lib/zabbix/externalscripts/zabbix-external-scripts/hypervisor-show
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment