From c09269693c6ad033dc0f4161bfed3c903dc58660 Mon Sep 17 00:00:00 2001 From: Matteo Pergolesi <matteo.pergolesi@pg.infn.it> Date: Thu, 8 Oct 2015 10:13:23 +0200 Subject: [PATCH] First commit of code: adding original scripts by Roberto Valentini. --- ceilometer | 137 +++++++++++++++++++ ceilometer.sh | 2 + hypervisor-show | 202 ++++++++++++++++++++++++++++ hypervisor-show.sh | 2 + prisma-iaas | 324 +++++++++++++++++++++++++++++++++++++++++++++ prisma-iaas.sh | 2 + 6 files changed, 669 insertions(+) create mode 100644 ceilometer create mode 100644 ceilometer.sh create mode 100644 hypervisor-show create mode 100644 hypervisor-show.sh create mode 100644 prisma-iaas create mode 100644 prisma-iaas.sh diff --git a/ceilometer b/ceilometer new file mode 100644 index 0000000..cbc2714 --- /dev/null +++ b/ceilometer @@ -0,0 +1,137 @@ +#!/usr/bin/env python + +import optparse +import httplib +import json +import time +import os +import sys +import math + +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 + +class Ceilometer: + + __username = '' + __tenant = '' + __token = '' + __password = '' + __tenantID = None + __server = '' + + def __init__(self, tenant, username, password, server, port): + 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', server, port, auth, header) + res_json = json.loads(res.getData()) +# print json.dumps(res_json, sort_keys=True, indent=4) + self.setUsername(username) + self.setTenant(tenant) + self.setTenantID(res_json['access']['token']['tenant']['id']) + self.setToken(res_json['access']['token']['id']) + self.setPassword(password) + self.__server=server + + 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): + return self.__tenantID + + def setToken(self, token): + self.__token = token + + def getToken(self): + return self.__token + + def getAuth(self): + return {"X-Auth-Token": self.__token} + + def getMeter(self, meter, resource_id): + url = "/v2/meters/%s?q.field=resource_id&q.op=eq&q.value=%s&limit=1" % (meter, resource_id ) + res = api_request.httpGet(url, self.__server, 8777, self.getAuth()) +# print res.getStatus(), res.getMsg() + res_json = json.loads(res.getData()) +# print json.dumps(res_json, sort_keys=True, indent=4) +# return res_json[0]['counter_volume'] + meter = res_json[0]['counter_volume'] +# if type(meter) is float: +# meter = math.floor(meter) + return meter + +def main(): + ceilometer=Ceilometer('service', 'ceilometer', 'PASSWORD', 'prisma-cloud.ba.infn.it', 5000) +# print ceilometer.getMeter('cpu_util', '407480dd-7ba2-40d2-8686-2be78c93c6d6') + print ceilometer.getMeter(sys.argv[1], sys.argv[2]) + +if __name__ == "__main__": + main() diff --git a/ceilometer.sh b/ceilometer.sh new file mode 100644 index 0000000..7839801 --- /dev/null +++ b/ceilometer.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python /usr/lib/zabbix/externalscripts/ceilometer $1 $2 diff --git a/hypervisor-show b/hypervisor-show new file mode 100644 index 0000000..ac6ea95 --- /dev/null +++ b/hypervisor-show @@ -0,0 +1,202 @@ +#!/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() diff --git a/hypervisor-show.sh b/hypervisor-show.sh new file mode 100644 index 0000000..3af2329 --- /dev/null +++ b/hypervisor-show.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python /usr/lib/zabbix/externalscripts/hypervisor-show diff --git a/prisma-iaas b/prisma-iaas new file mode 100644 index 0000000..a08e0db --- /dev/null +++ b/prisma-iaas @@ -0,0 +1,324 @@ +#!/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_service_to_host(struct, host, service, 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, 'services' : {service : state}}) + +def add_service(struct, service, state): + struct[service] = 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') + +#keystone + keystone = prisma.getTenantID() + if keystone != None: + add_service(struct, 'keystone', 1) + else: + add_service(struct, 'keystone', 0) + +#nova + nova = prisma.nova_service_list() + compute = 0 + if nova != None: + add_service(struct, 'nova', 1) + for i in iter(nova['services']): + if i['status'] == 'enabled': + add_service_to_host(struct, i['host'], i['binary'],0 if i['state'] == 'down' else 1) + else: + add_service(struct, 'nova', 0) + +#neutron + neutron = prisma.neutron_agent_list() + if neutron != None: + add_service(struct, 'neutron', 1) + for i in iter(neutron['agents']): + add_service_to_host(struct, i['host'], i['binary'],0 if i['alive'] == 'false' else 1) + else: + add_service(struct, 'neutron', 0) + +#glance + glance = prisma.glance_images() + if glance != None: + add_service(struct, 'glance', 1) + else: + add_service(struct, 'glance', 0) + +#cinder + cinder = prisma.cinder_blocks_list() + if cinder != None: + add_service(struct, 'cinder', 1) + else: + add_service(struct, 'cinder', 0) + +#cinder + swift = prisma.swift_blocks_list() + if swift != None: + add_service(struct, 'swift', 1) + else: + add_service(struct, 'swift', 0) + + #print json.dumps(struct, sort_keys=True, indent=4) + + + #add_service_to_host(struct, 'test', 'nova-compute', 0) + #add_service_to_host(struct, 'test', 'neutron-openvswitch-agent', 1) + return struct + +def check( test_json): + try: + keystone = test_json["keystone"] + except: + print "keystone" + return + +#nova + controller_dic = {'nova-cert': 0, 'nova-consoleauth': 0, 'nova-scheduler': 0, 'nova-conductor': 0} + nova = test_json["nova"] + for host in iter(test_json["hosts"]): + for i in iter(host["services"]): + try: + controller_dic[i]|=host["services"][i] + except: + continue + for i in iter(controller_dic): + nova &= controller_dic[i] +#neutron + network_dic = {'neutron-l3-agent': 0, 'neutron-dhcp-agent': 0, 'neutron-openvswitch-agent': 0} + neutron = test_json["neutron"] + for host in iter(test_json["hosts"]): + for i in iter(host["services"]): + try: + network_dic[i]|=host["services"][i] + except: + continue + for i in iter(network_dic): + neutron &= network_dic[i] +#cinder + cinder = test_json["cinder"] +#glance + glance = test_json["glance"] +#compute + computerun = 0 + computecount = 0 + compute = 1 + for host in iter(test_json["hosts"]): + test = 0 + try: + if 'nova-compute' in host["services"].keys(): + computecount += 1 + if host["services"]["nova-compute"] & host["services"]["neutron-openvswitch-agent"]: + computerun += 1 + if host["services"]["nova-compute"] == 1 and host["services"]["neutron-openvswitch-agent"] == 0: + compute = 0 + except: + continue + #print 'hostname: %s | nova: %d | ovs: %d' % (host['host'], host["services"]["nova-compute"], host["services"]["neutron-openvswitch-agent"]) + + #print 'keystone: %d | compute: %d | nova: %d | neutron: %d | cinder: %d | glance: %d' % (keystone, compute, nova, neutron, cinder, glance) + + status = {} + + if cinder == 1: + add_service(status, 'storage', 'OK') + if neutron == 1: + add_service(status, 'network', 'OK') + if compute == 1: + add_service(status, 'available_nodes', computerun) + add_service(status, 'total_nodes', computecount) + else: + add_service(status, 'available_nodes', 0) + add_service(status, 'total_nodes', computecount) + + return status +# return keystone & compute & nova & neutron & cinder & glance + +def main(): + struct = encode() + #print struct + status = check(struct) + print status + #if status > 0: + # print "OK" + #else: + # print "KO" + +if __name__ == "__main__": + main() diff --git a/prisma-iaas.sh b/prisma-iaas.sh new file mode 100644 index 0000000..874fad7 --- /dev/null +++ b/prisma-iaas.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python prisma-iaas -- GitLab