__author__ = 'eliaten' import json import urllib2 from lib import helper class ZabbixServerConfigure: """ This class is used to configure zabbix server ZABBIX API 3.0 """ _PARAs = { "authid": -1, "zabbix_user_name": "bmc_wpst", "zabbix_user_password": "wpst1234", "template_name": "Template OS Linux", "templateid": -1, "user_group_name": "eMBMS", "user_group_id": -1, "host_id": -1, "interface_id": -1, "screen_name_prefix": "bmcwpst1", "screen_id": -1, "zabbix_server_API_url": "http://10.170.65.231/zabbix/api_jsonrpc.php", "graph_names": ('Total memory', 'Available memory', 'CPU jumps', 'CPU utilization', 'Swap usage'), "graph_ids": [] } def __init__(self, hostname, ip): self.hostname = hostname self.ip = ip self.screen_name = self._PARAs['screen_name_prefix'] + "_" + self.hostname helper.info('start to configure zabbix server for host %s !' % self.hostname) def _send_request_to_zabbixServer(self, vdata): header = {"Content-Type": "application/json"} data = { "jsonrpc": "2.0", "method": vdata['method'], "params": vdata['params'], "id": 0 } if vdata['method'] != "user.login": data['auth'] = self._PARAs['authID'] dump = json.dumps(data) request = urllib2.Request(self._PARAs['zabbix_server_API_url'], dump) for key in header: request.add_header(key, header[key]) try: result = urllib2.urlopen(request) except urllib2.URLError as e: helper.error(e) result.close() return 0 else: response = json.loads(result.read()) result.close() if 'error' in response: helper.info('send request %s and get ERROR result %s %s' % (vdata['method'], response['error']['code'], response['error']['data'])) return response['error']['code'] else: return response def _authentication_on_zabbix_server(self): # auth user and password data = { "method": "user.login", "params": { "user": self._PARAs["zabbix_user_name"], "password": self._PARAs["zabbix_user_password"] }} # send request to zabbix server result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("Zabbix Server authentication failed!") else: self._PARAs['authID'] = result['result'] helper.info('Zabbix server Auth Successful.') def _get_groupID(self): # get user's groupID from groupName data = { "method": "hostgroup.get", "params": { "name": self._PARAs["user_group_name"] } } result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("Get user's groupID failed!") else: self._PARAs['user_group_ID'] = result['result'][0]['groupid'] helper.info('Get user group ID Successful.') def _get_templateID(self): # get templateID from templateName data = { "method": "template.get", "params": { "host": self._PARAs['template_name']} } result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("Get templateID failed!") else: self._PARAs['templateID'] = result['result'][0]['templateid'] helper.info('Get template ID Successful.') def _create_host(self): # create host data = { "method": "host.create", "params": { "host": self.hostname, "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": self.ip, "dns": "", "port": "10050" } ], "groups": [ { "groupid": self._PARAs['user_group_ID'] } ], "templates": [ { "templateid": self._PARAs['templateID'] } ] } } result = self._send_request_to_zabbixServer(data) if result == -32602: helper.info('the host %s exists, go to update its host IP' % self.hostname) self._get_hostID() self._get_interface_id() self._update_interface_ip() elif result <= 0: helper.error("create host failed!") else: self._PARAs['host_id'] = result['result']['hostids'][0] helper.info('create Host Successful.') def _get_hostID(self): # get hostID from hostname data = { "method": "host.get", "params": { "output": "extend", "filter": { "host": [ self.hostname ] }}} result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("get host ID failed!") else: self._PARAs['host_id'] = result['result'][0]['hostid'] helper.info('get host ID Successful.') def _get_interface_id(self): # get host's interfaceID, then update the IP of interface data = { "method": "hostinterface.get", "params": { "output": "extend", "hostids": self._PARAs['host_id'] }} result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("get host interface ID failed!") else: self._PARAs['interface_id'] = result['result'][0]['interfaceid'] helper.info('get host interface ID Successful.') def _update_interface_ip(self): # update the IP of interface data = { "method": "hostinterface.update", "params": { "interfaceid": self._PARAs['interface_id'], "ip": self.ip }} result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("update host interface failed!") else: helper.info("update host interface Successful.") def _get_graphID_from_host(self): data = { "method": "graph.get", "params": { "hostids": self._PARAs['host_id']} } result = self._send_request_to_zabbixServer(data) if result == 0: helper.error("Get GraphIDs failed!") else: self._PARAs['graph_ids'] = [] for i in range(0, len(result['result'])): if result['result'][i]['name'] in self._PARAs['graph_names']: self._PARAs['graph_ids'].append(result['result'][i]['graphid']) helper.info("get graphsIDs Successful.") def _get_and_recreate_screen(self): data = { "method": "screen.get", "params": {} # get all of screens of current user } result = self._send_request_to_zabbixServer(data) if not len(result['result']): helper.info("the screen does not exist, create it now.") self._create_screen() elif result <= 0: helper.error("Get screenID failed!") else: for i in range(0, len(result['result'])): if result['result'][i]['name'] == self.screen_name: self._PARAs['screen_id'] = result['result'][i]['screenid'] helper.info('get remaining Screen Successful, recreate it now.') # recreate screen self._delete_screen() self._create_screen() break else: helper.info('did not get remaining screen, create it now.') self._create_screen() def _delete_screen(self): data = { "method": "screen.delete", "params": [self._PARAs['screen_id']] } result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("deleting screen failed!") else: helper.info("delete screen Successful.") def _create_screen(self): screenitems = [] for i in range(0, len(self._PARAs['graph_ids'])): screenitem= { "resourcetype": 0, "resourceid": self._PARAs['graph_ids'][i], "x": 0, "y": i } screenitems.append(screenitem) data = { "method": "screen.create", "params": { "name": self.screen_name, "hsize": 2, "vsize": 12, "screenitems": screenitems }} result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("create screen failed!") else: self._PARAs['screen_id'] = result['result']['screenids'] helper.info("create screen Successful.") # below function is not using, leave here in case needed in future def _update_screen_items(self): screenitems = [] if "1" in self.hostname: x = 0 elif "2" in self.hostname: x = 1 else: x = 2 for i in range(0, len(self._PARAs['graph_ids'])): screenitem= { "screenid": self._PARAs['screen_id'], "resourceid": self._PARAs['graph_ids'][i], "x": x, "y": i } screenitems.append(screenitem) data = { "method": "screenitem.updatebyposition", "params": screenitems } result = self._send_request_to_zabbixServer(data) if result <= 0: helper.error("update screen failed!") else: helper.info("update screen Successful.") def start_zabbixServer_configure(self): self._authentication_on_zabbix_server() self._get_groupID() self._get_templateID() self._create_host() self._get_graphID_from_host() self._get_and_recreate_screen() if __name__ == '__main__': bmcinstasnce = ZabbixServerConfigure("simulator", "10.175.183.250") bmcinstasnce.start_zabbixServer_configure() # print(ZabbixServerConfigure()._start_zabbixServer_configure())