def load(self, path_cfg: str): with open(path_cfg, 'r') as stream: try: self.cfg_dict = yaml3ed.load(stream) except yaml3ed.YAMLError as exc: print(exc) self.check() self.zbx = ZabbixAgent(self.cfg_dict['zabbix']['url'], self.cfg_dict['zabbix']['login'], self.cfg_dict['zabbix']['password']) log.debug('Config loaded')
def load(self): base = self.template.base.copy() node = self.template.node.copy() link = self.template.link.copy() for section in base.keys(): if not self.config.has_section(section): raise ConfigException('section [{0}] is not present in the config'.format(section)) for option in base[section]: if not self.config.has_option(section, option): raise ConfigException('The option: {0} is missing in section: [{1}]'.format(option, section)) else: base[section][option] = self.config[section][option] for name in self.config.sections(): if 'node-' in name: for option in node: if not self.config.has_option(name, option): raise ConfigException('The option: {0} is missing in section: [{1}]'.format(option, name)) else: node[option] = self.config[name][option] self.obj_nodes[name] = None base[name] = node.copy() node = node.fromkeys(node) if 'link-' in name: for option in link: try: link[option] = self.config[name][option] except (configparser.NoOptionError, KeyError): if option == 'width': link['width'] = self.config['link']['width'] elif option == 'bandwidth': link['bandwidth'] = self.config['link']['bandwidth'] elif option == 'copy': pass elif not self.config.has_option(name, option): raise ConfigException('The option: {0} is missing in section: [{1}]' .format(option, name)) if link['hostname'] and link['itemin'] and link['itemout']: self.obj_links[name] = None base[name] = link.copy() link = link.fromkeys(link) log.debug('Config dict: %s', base) self.cfg_dict = base.copy() self.zbx = ZabbixAgent(self.cfg_dict['zabbix']['url'], self.cfg_dict['zabbix']['login'], self.cfg_dict['zabbix']['password']) del base del node del link
def _map_scan(self): if self.args.cfg: self.cfg_path = self.args.cfg for map_n in self.args.scan: zbx = ZabbixAgent(self.args.zabbix, self.args.login, self.args.pwd) map_data = zbx.scan_map(map_n) scan_map = ConfigCreate(map_data, zbx) scan_map.create() scan_map.check_map(self.cfg_path) scan_map.save(self.cfg_path) del zbx, scan_map, map_data
def load(self, path_cfg: str): with open(path_cfg, 'r') as stream: try: self.cfg_dict = yaml3ed.safe_load(stream) except yaml3ed.YAMLError as exc: print(exc) self.check() self.zbx = ZabbixAgent(self.cfg_dict['zabbix']['url'], self.cfg_dict['zabbix']['login'], self.cfg_dict['zabbix']['password']) log.debug('Config loaded')
class ConfigLoader(object): def __init__(self, path_cfg): self.config = configparser.ConfigParser() if self.config.read(path_cfg): self.config.read(path_cfg) else: raise ConfigException('file not found: {0}'.format(path_cfg)) self.obj_nodes = {} self.obj_links = {} self.template = ConfigTemplate() self.cfg_dict = {} self.zbx = None log.debug('Object ConfigLoader created') def load(self): base = self.template.base.copy() node = self.template.node.copy() link = self.template.link.copy() for section in base.keys(): if not self.config.has_section(section): raise ConfigException('section [{0}] is not present in the config'.format(section)) for option in base[section]: if not self.config.has_option(section, option): raise ConfigException('The option: {0} is missing in section: [{1}]'.format(option, section)) else: base[section][option] = self.config[section][option] for name in self.config.sections(): if 'node-' in name: for option in node: if not self.config.has_option(name, option): raise ConfigException('The option: {0} is missing in section: [{1}]'.format(option, name)) else: node[option] = self.config[name][option] self.obj_nodes[name] = None base[name] = node.copy() node = node.fromkeys(node) if 'link-' in name: for option in link: try: link[option] = self.config[name][option] except (configparser.NoOptionError, KeyError): if option == 'width': link['width'] = self.config['link']['width'] elif option == 'bandwidth': link['bandwidth'] = self.config['link']['bandwidth'] elif option == 'copy': pass elif not self.config.has_option(name, option): raise ConfigException('The option: {0} is missing in section: [{1}]' .format(option, name)) if link['hostname'] and link['itemin'] and link['itemout']: self.obj_links[name] = None base[name] = link.copy() link = link.fromkeys(link) log.debug('Config dict: %s', base) self.cfg_dict = base.copy() self.zbx = ZabbixAgent(self.cfg_dict['zabbix']['url'], self.cfg_dict['zabbix']['login'], self.cfg_dict['zabbix']['password']) del base del node del link def create_map(self, font_path_fn, icon_path): palette = [self.cfg_dict['palette'][key] for key in sorted(self.cfg_dict['palette'])] fontsize = int(self.cfg_dict['map']['fontsize']) for node in self.obj_nodes.keys(): x = int(self.cfg_dict[node]['x']) y = int(self.cfg_dict[node]['y']) label = self.cfg_dict[node]['label'] icon = self.cfg_dict[node]['icon'] self.obj_nodes[node] = (Node(font_path_fn, icon_path, x=x, y=y, label=label, icon=icon, fontsize=fontsize)) for link in self.obj_links.keys(): node1 = self.obj_nodes[self.cfg_dict[link]['node1']] node2 = self.obj_nodes[self.cfg_dict[link]['node2']] bandwidth = int(self.cfg_dict[link]['bandwidth']) width = int(self.cfg_dict[link]['width']) hostname = self.cfg_dict[link]['hostname'] item_in = self.cfg_dict[link]['itemin'] item_out = self.cfg_dict[link]['itemout'] self.obj_links[link] = (Link(font_path_fn, node1, node2, bandwidth=bandwidth, width=width, palette=palette, fontsize=fontsize)) data_in, data_out = self.zbx.get_item_data2(hostname, item_in, item_out) self.obj_links[link].data(in_bps=data_in, out_bps=data_out) if int(self.cfg_dict['table']['show']): table = Table(font_path_fn, x=int(self.cfg_dict['table']['x']), y=int(self.cfg_dict['table']['y']), palette=palette) else: table = None map_width = int(self.cfg_dict['map']['width']) map_height = int(self.cfg_dict['map']['height']) if self.cfg_dict['map']['bgcolor']: map_bgcolor = self.cfg_dict['map']['bgcolor'] else: map_bgcolor = None map_obj = Map(self.obj_links.values(), self.obj_nodes.values(), table=table, len_x=map_width, len_y=map_height, bgcolor=map_bgcolor) return map_obj def upload(self, img_path_fn): self.zbx.image_to_zabbix(img_path_fn, self.cfg_dict['map']['name'])
class ConfigLoader(object): def __init__(self, path_cfg: str): self.template = ConfigTemplate().template self.cfg_dict = {} self.obj_nodes = {} self.obj_links = {} self.zbx = None self.load(path_cfg) log.debug('Object ConfigLoader created') def load(self, path_cfg: str): with open(path_cfg, 'r') as stream: try: self.cfg_dict = yaml3ed.safe_load(stream) except yaml3ed.YAMLError as exc: print(exc) self.check() self.zbx = ZabbixAgent(self.cfg_dict['zabbix']['url'], self.cfg_dict['zabbix']['login'], self.cfg_dict['zabbix']['password']) log.debug('Config loaded') def check(self): for cfg_sect in self.template: if cfg_sect == 'node-': for node in sorted([ node for node in self.cfg_dict.keys() if cfg_sect in node ]): for cfg_opt in self.template[cfg_sect]: try: self.cfg_dict[node][cfg_opt] except KeyError: if cfg_opt == 'icon': # self.cfg_dict[node][cfg_opt] = str() continue if cfg_opt == 'label': # self.cfg_dict[node][cfg_opt] = str() continue raise ConfigException( 'The option: {0} is missing in section: [{1}]'. format(cfg_sect, cfg_opt)) if cfg_sect == 'link-': for link in sorted([ link for link in self.cfg_dict.keys() if cfg_sect in link ]): for cfg_opt in self.template[cfg_sect]: try: self.cfg_dict[link][cfg_opt] except KeyError: if cfg_opt == 'copy': # self.cfg_dict[link][cfg_opt] = False continue if cfg_opt == 'width': # self.cfg_dict[link][cfg_opt] = int() continue if cfg_opt == 'bandwidth': # self.cfg_dict[link][cfg_opt] = int() continue raise ConfigException( 'The option: {0} is missing in section: [{1}]'. format(cfg_sect, cfg_opt)) if cfg_sect == 'palette': if len(self.cfg_dict[cfg_sect]) != 9: raise ConfigException( 'Error in section {0}, number elements not equal 9'. format(cfg_sect)) continue for cfg_opt in self.template[cfg_sect]: try: self.cfg_dict[cfg_sect][cfg_opt] except KeyError: # if cfg_sect == 'map' and cfg_opt == 'bgcolor': # self.cfg_dict[cfg_sect][cfg_opt] = str() if cfg_sect == 'link-' or cfg_sect == 'node-': continue raise ConfigException( 'The option: {0} is missing in section: [{1}]'.format( cfg_sect, cfg_opt)) log.debug('Config check: Ok') def create_map(self, font_path_fn: str, icon_path: str): self.obj_nodes = { section: None for section in self.cfg_dict if 'node-' in section } self.obj_links = { section: None for section in self.cfg_dict if 'link-' in section } palette = self.cfg_dict['palette'] fontsize = int(self.cfg_dict['map']['fontsize']) for node in self.obj_nodes.keys(): x = int(self.cfg_dict[node]['x']) y = int(self.cfg_dict[node]['y']) if self.cfg_dict[node].get('label'): label = self.cfg_dict[node]['label'] else: label = None if self.cfg_dict[node].get('icon'): icon = self.cfg_dict[node]['icon'] else: icon = None self.obj_nodes[node] = (Node(font_path_fn, icon_path, x=x, y=y, label=label, icon=icon, fontsize=fontsize)) for link in self.obj_links.keys(): node1 = self.obj_nodes[self.cfg_dict[link]['node1']] node2 = self.obj_nodes[self.cfg_dict[link]['node2']] if self.cfg_dict[link].get('bandwidth'): bandwidth = self.cfg_dict[link]['bandwidth'] else: bandwidth = self.cfg_dict['link']['bandwidth'] if self.cfg_dict[link].get('width'): width = self.cfg_dict[link]['width'] else: width = self.cfg_dict['link']['width'] self.obj_links[link] = (Link(font_path_fn, node1, node2, bandwidth=bandwidth, width=width, palette=palette, fontsize=fontsize)) hostname = self.cfg_dict[link]['hostname'] item_in = self.cfg_dict[link]['itemin'] item_out = self.cfg_dict[link]['itemout'] if hostname and item_in and item_out: data_in, data_out = self.zbx.get_item_data2( hostname, item_in, item_out) self.obj_links[link].data(in_bps=data_in, out_bps=data_out) elif hostname and item_in: data_in = self.zbx.get_item_data(hostname, item_in) self.obj_links[link].data(in_bps=data_in, out_bps=0) elif hostname and item_out: data_out = self.zbx.get_item_data(hostname, item_out) self.obj_links[link].data(in_bps=0, out_bps=data_out) else: self.obj_links[link].data(in_bps=0, out_bps=0) if int(self.cfg_dict['table']['show']): table = Table(font_path_fn, x=int(self.cfg_dict['table']['x']), y=int(self.cfg_dict['table']['y']), palette=palette) else: table = None map_width = int(self.cfg_dict['map']['width']) map_height = int(self.cfg_dict['map']['height']) if self.cfg_dict['map']['bgcolor']: map_bgcolor = self.cfg_dict['map']['bgcolor'] else: map_bgcolor = None map_obj = Map(self.obj_links.values(), self.obj_nodes.values(), table=table, len_x=map_width, len_y=map_height, bgcolor=map_bgcolor) return map_obj def upload(self, img_path_fn: str): self.zbx.image_to_zabbix(img_path_fn, self.cfg_dict['map']['name'])
class ConfigLoader(object): def __init__(self, path_cfg: str): self.template = ConfigTemplate().template self.cfg_dict = {} self.obj_nodes = {} self.obj_links = {} self.zbx = None self.load(path_cfg) log.debug('Object ConfigLoader created') def load(self, path_cfg: str): with open(path_cfg, 'r') as stream: try: self.cfg_dict = yaml3ed.safe_load(stream) except yaml3ed.YAMLError as exc: print(exc) self.check() self.zbx = ZabbixAgent(self.cfg_dict['zabbix']['url'], self.cfg_dict['zabbix']['login'], self.cfg_dict['zabbix']['password']) log.debug('Config loaded') def check(self): for cfg_sect in self.template: if cfg_sect == 'node-': for node in sorted([node for node in self.cfg_dict.keys() if cfg_sect in node]): for cfg_opt in self.template[cfg_sect]: try: self.cfg_dict[node][cfg_opt] except KeyError: if cfg_opt == 'icon': # self.cfg_dict[node][cfg_opt] = str() continue if cfg_opt == 'label': # self.cfg_dict[node][cfg_opt] = str() continue raise ConfigException('The option: {0} is missing in section: [{1}]' .format(cfg_sect, cfg_opt)) if cfg_sect == 'link-': for link in sorted([link for link in self.cfg_dict.keys() if cfg_sect in link]): for cfg_opt in self.template[cfg_sect]: try: self.cfg_dict[link][cfg_opt] except KeyError: if cfg_opt == 'copy': # self.cfg_dict[link][cfg_opt] = False continue if cfg_opt == 'width': # self.cfg_dict[link][cfg_opt] = int() continue if cfg_opt == 'bandwidth': # self.cfg_dict[link][cfg_opt] = int() continue raise ConfigException('The option: {0} is missing in section: [{1}]' .format(cfg_sect, cfg_opt)) if cfg_sect == 'palette': if len(self.cfg_dict[cfg_sect]) != 9: raise ConfigException('Error in section {0}, number elements not equal 9'.format(cfg_sect)) continue for cfg_opt in self.template[cfg_sect]: try: self.cfg_dict[cfg_sect][cfg_opt] except KeyError: # if cfg_sect == 'map' and cfg_opt == 'bgcolor': # self.cfg_dict[cfg_sect][cfg_opt] = str() if cfg_sect == 'link-' or cfg_sect == 'node-': continue raise ConfigException('The option: {0} is missing in section: [{1}]'.format(cfg_sect, cfg_opt)) log.debug('Config check: Ok') def create_map(self, font_path_fn: str, icon_path: str): self.obj_nodes = {section: None for section in self.cfg_dict if 'node-' in section} self.obj_links = {section: None for section in self.cfg_dict if 'link-' in section} palette = self.cfg_dict['palette'] fontsize = int(self.cfg_dict['map']['fontsize']) for node in self.obj_nodes.keys(): x = int(self.cfg_dict[node]['x']) y = int(self.cfg_dict[node]['y']) if self.cfg_dict[node].get('label'): label = self.cfg_dict[node]['label'] else: label = None if self.cfg_dict[node].get('icon'): icon = self.cfg_dict[node]['icon'] else: icon = None self.obj_nodes[node] = (Node(font_path_fn, icon_path, x=x, y=y, label=label, icon=icon, fontsize=fontsize)) for link in self.obj_links.keys(): node1 = self.obj_nodes[self.cfg_dict[link]['node1']] node2 = self.obj_nodes[self.cfg_dict[link]['node2']] if self.cfg_dict[link].get('bandwidth'): bandwidth = self.cfg_dict[link]['bandwidth'] else: bandwidth = self.cfg_dict['link']['bandwidth'] if self.cfg_dict[link].get('width'): width = self.cfg_dict[link]['width'] else: width = self.cfg_dict['link']['width'] self.obj_links[link] = (Link(font_path_fn, node1, node2, bandwidth=bandwidth, width=width, palette=palette, fontsize=fontsize)) hostname = self.cfg_dict[link]['hostname'] item_in = self.cfg_dict[link]['itemin'] item_out = self.cfg_dict[link]['itemout'] if hostname and item_in and item_out: data_in, data_out = self.zbx.get_item_data2(hostname, item_in, item_out) self.obj_links[link].data(in_bps=data_in, out_bps=data_out) elif hostname and item_in: data_in = self.zbx.get_item_data(hostname, item_in) self.obj_links[link].data(in_bps=data_in, out_bps=0) elif hostname and item_out: data_out = self.zbx.get_item_data(hostname, item_out) self.obj_links[link].data(in_bps=0, out_bps=data_out) else: self.obj_links[link].data(in_bps=0, out_bps=0) if int(self.cfg_dict['table']['show']): table = Table(font_path_fn, x=int(self.cfg_dict['table']['x']), y=int(self.cfg_dict['table']['y']), palette=palette) else: table = None map_width = int(self.cfg_dict['map']['width']) map_height = int(self.cfg_dict['map']['height']) if self.cfg_dict['map']['bgcolor']: map_bgcolor = self.cfg_dict['map']['bgcolor'] else: map_bgcolor = None map_obj = Map(self.obj_links.values(), self.obj_nodes.values(), table=table, len_x=map_width, len_y=map_height, bgcolor=map_bgcolor) return map_obj def upload(self, img_path_fn: str): self.zbx.image_to_zabbix(img_path_fn, self.cfg_dict['map']['name'])