def handle_snmp_initial_scan(self, errors, found, res_dict): _all_schemes = Schemes(self.log) _found_struct = {} # reorganize oids to dict with scheme -> {..., oid_list, ...} for _oid in found: _found_scheme = _all_schemes.get_scheme_by_oid(_oid) if _found_scheme: _key = (_found_scheme.priority, _found_scheme.pk) if _key not in _found_struct: _found_struct[_key] = { "scheme": _found_scheme, "oids": set(), "full_name": _found_scheme.full_name, } _found_struct[_key]["oids"].add(oid_to_str(_oid)) _handler = SNMPSink(self.log) result = ResultNode(error=errors) for _key in sorted(_found_struct, reverse=True): _struct = _found_struct[_key] result.merge( _handler.update( self.device, _struct["scheme"], _all_schemes.filter_results(res_dict, _struct["oids"]), _struct["oids"], self.flags, ) ) self.srv_com.set_result(*result.get_srv_com_result()) self.finish()
def _iterate(self, srv_com, c_name, scan_type_enum): total_result = ResultNode() if "devices" in srv_com: for _dev_xml in srv_com["devices"]: _dev = device.objects.get(Q(pk=int(_dev_xml.get("pk", "0")))) if self.EC.consume("discover", _dev): if not self.device_is_capable(_dev, scan_type_enum): res_node = ResultNode( error= "device {} is missing the required ComCapability '{}'" .format( str(_dev), scan_type_enum, ), ) else: _new_lock = self.device_is_idle(_dev, scan_type_enum) if _new_lock: self.__job_list.append( (c_name, _dev, scan_type_enum, _new_lock, copy.deepcopy(_dev_xml))) res_node = ResultNode( ok="starting scan for device {}".format( str(_dev))) else: res_node = ResultNode( warning="lock not possible for device {}". format(str(_dev))) else: res_node = ResultNode( error="device not allowed (ova error)") total_result.merge(res_node) srv_com.set_result(*total_result.get_srv_com_result()) else: srv_com.set_result("no devices given", server_command.SRV_REPLY_STATE_ERROR)
def __init(self): _QueryData = collections.namedtuple("_QueryData", ['columns', 'where_clause']) query_structures = { self.NETWORK_ADAPTER_MODEL: _QueryData( ['Name', 'Speed', 'MACAddress', 'Index'], "WHERE NetEnabled = TRUE" if self.discard_disabled_interfaces else "", ), self.NETWORK_ADAPTER_CONFIGURATION_MODEL: _QueryData( ['IPAddress', 'IPSubnet', 'MTU', 'Index', 'DefaultIPGateway'], "") } self._ext_coms = {} for query_structure in query_structures.iteritems(): cmd = WmiUtils.get_wmic_cmd( username=self.username, password=self.password, target_ip=self.device.target_ip, columns=query_structure[1].columns, table=query_structure[0], where_clause=query_structure[1].where_clause) self.log("starting WMI scan with command: {}".format(cmd)) ext_com = ExtCom( self.log, cmd, shell=False) # shell=False since args must not be parsed again ext_com.run() self._ext_coms[query_structure[0]] = ext_com self.start_result = ResultNode(ok="started base_scan")
def __init__(self, dev_com, scan_dev): super(BaseScanBatch, self).__init__(dev_com, scan_dev) if self.device.target_ip: self._ext_com = ExtCom(self.log, self._build_command()) self._ext_com.run() self.start_result = ResultNode(ok="started base_scan")
def __init__(self, dev_com, scan_dev): dev_com.attrib['scan_address'] = "127.0.0.1" super(NRPEScanBatch, self).__init__(dev_com, scan_dev) self._commands = self._command = dev_com.attrib.get('commands').split( ",") self._build_command() if self.device.target_ip: # self._ext_com = ExtCom(self.log, self._build_command()) # self._ext_com.run() self.start_result = ResultNode(ok="started NRPE_scan")
def __init__(self, dev_com, scan_dev): self.start_time = time.time() self.dev_com = dev_com self.device = scan_dev self.id = self.next_batch_id(self) if "scan_address" in dev_com.attrib: self.device.target_ip = dev_com.attrib["scan_address"] else: self.__class__.process.get_route_to_devices([self.device]) if not self.device.target_ip: self.log("no valid IP found for {}".format(unicode(self.device)), logging_tools.LOG_LEVEL_ERROR) self.start_result = ResultNode(error="no valid IP found") self.finish()
def scan_network_info(self, dev_com, scan_dev): hm_port = InstanceXML(quiet=True).get_port_dict( icswServiceEnum.host_monitoring, command=True) res_node = ResultNode() strict_mode = True if int(dev_com.get("strict_mode")) else False modify_peering = True if int(dev_com.get("modify_peering")) else False scan_address = dev_com.get("scan_address") self.log( "scanning network for device '{}' ({:d}), scan_address is '{}', strict_mode is {}" .format( str(scan_dev), scan_dev.pk, scan_address, "on" if strict_mode else "off", )) zmq_con = net_tools.ZMQConnection("server:{}".format( process_tools.get_machine_name()), context=self.zmq_context) conn_str = "tcp://{}:{:d}".format( scan_address, hm_port, ) self.log("connection_str for {} is {}".format(str(scan_dev), conn_str)) zmq_con.add_connection( conn_str, server_command.srv_command(command="network_info"), multi=True) res_list = zmq_con.loop() self.log("length of result list: {:d}".format(len(res_list))) num_taken, num_ignored = (0, 0) nds_list = netdevice_speed.objects.filter( Q(speed_bps__in=[1000000000, 100000000])).order_by( "-speed_bps", "-full_duplex", "-check_via_ethtool") default_nds = nds_list[0] self.log("default nds is {}".format(str(default_nds))) for _idx, (result, target_dev) in enumerate(zip(res_list, [scan_dev])): self.log("device {} ...".format(str(target_dev))) res_state = -1 if result is None else int( result["result"].attrib["state"]) if res_state: # num_errors += 1 if res_state == -1: res_node.error("{}: no result".format(str(target_dev))) else: res_node.error("{}: error {:d}: {}".format( str(target_dev), int(result["result"].attrib["state"]), result["result"].attrib["reply"])) else: try: bridges = result["bridges"] networks = result["networks"] except: res_node.error( "{}: error missing keys in dict".format(target_dev)) else: # clear current network with transaction.atomic(): sid = transaction.savepoint() # store current peers _peers = [ _obj.store_before_delete(target_dev) for _obj in peer_information.objects.filter( Q(s_netdevice__in=target_dev.netdevice_set.all( )) | Q(d_netdevice__in=target_dev. netdevice_set.all())) ] _old_peer_dict = {} for _old_peer in _peers: _old_peer_dict.setdefault(_old_peer["my_name"], []).append(_old_peer) self.log("removing current network devices") target_dev.netdevice_set.all().delete() all_ok = True exc_dict = {} _all_devs = set(networks) _br_devs = set(bridges) # build bond dict bond_dict = {} for dev_name in _all_devs: _struct = networks[dev_name] if "MASTER" in _struct["flags"]: bond_dict[dev_name] = {"slaves": []} for dev_name in _all_devs: _struct = networks[dev_name] if "SLAVE" in _struct["flags"]: master_name = _struct["features"]["master"] bond_dict[master_name]["slaves"].append( dev_name) NDStruct.setup(self, target_dev, default_nds, bond_dict) for dev_name in sorted( list(_all_devs & _br_devs)) + sorted( list(_all_devs - _br_devs)): if any([ dev_name.startswith(_ignore_pf) for _ignore_pf in IGNORE_LIST ]): self.log("ignoring device {}".format(dev_name)) num_ignored += 1 continue _struct = networks[dev_name] cur_nd = NDStruct(dev_name, _struct, bridges.get(dev_name, None)) try: cur_nd.create() except (NoMatchingNetworkDeviceTypeFoundError, NoMatchingNetworkFoundError) as exc: _name = exc.__class__.__name__ self.log( "caught {} for {}".format(_name, dev_name), logging_tools.LOG_LEVEL_ERROR) exc_dict.setdefault(_name, []).append(dev_name) all_ok = False except: err_str = "error creating netdevice {}: {}".format( dev_name, process_tools.get_except_info()) if strict_mode: res_node.error(err_str) for _log in process_tools.icswExceptionInfo( ).log_lines: self.log(" {}".format(_log), logging_tools.LOG_LEVEL_CRITICAL) all_ok = False else: num_taken += 1 if cur_nd.nd is not None and cur_nd.nd.devname in _old_peer_dict: # relink peers for _peer in _old_peer_dict[cur_nd.nd.devname]: _new_peer = peer_information.create_from_store( _peer, cur_nd.nd) del _old_peer_dict[cur_nd.nd.devname] if all_ok: NDStruct.handle_bonds() if exc_dict: for key in sorted(exc_dict.keys()): res_node.error("{} for {}: {}".format( key, logging_tools.get_plural( "netdevice", len(exc_dict[key])), ", ".join(sorted(exc_dict[key])))) if list(_old_peer_dict.keys()): _err_str = "not all peers migrated: {}".format( ", ".join(list(_old_peer_dict.keys()))) if strict_mode: res_node.error(_err_str) all_ok = False else: res_node.warn(_err_str) [ NDStruct.dict[_bridge_name].link_bridge_slaves() for _bridge_name in _br_devs & set(NDStruct.dict.keys()) ] if not all_ok and strict_mode: self.log( "rolling back to savepoint because strict_mode is enabled", logging_tools.LOG_LEVEL_WARN) num_taken -= target_dev.netdevice_set.all().count() transaction.savepoint_rollback(sid) else: transaction.savepoint_commit(sid) if num_taken: res_node.ok("{} taken".format( logging_tools.get_plural("netdevice", num_taken))) if num_ignored: res_node.ok("{} ignored".format( logging_tools.get_plural("netdevice", num_ignored))) return res_node
def scan_system_info(self, dev_com, scan_dev): hm_port = InstanceXML(quiet=True).get_port_dict( icswServiceEnum.host_monitoring, command=True) res_node = ResultNode() s_time = time.time() # global config should be a kwarg self.get_route_to_devices(self.global_config, [scan_dev]) self.log( "scanning system for device '{}' ({:d}), scan_address is '{}'". format( str(scan_dev), scan_dev.pk, scan_dev.target_ip, )) zmq_con = net_tools.ZMQConnection("server:{}".format( process_tools.get_machine_name()), context=self.zmq_context) conn_str = "tcp://{}:{:d}".format( scan_dev.target_ip, hm_port, ) self.log("connection_str for {} is {}".format(str(scan_dev), conn_str)) zmq_con.add_connection( conn_str, server_command.srv_command(command="sysinfo"), multi=True, ) res_list = zmq_con.loop() e_time = time.time() for _idx, (_dev, _res) in enumerate(zip([scan_dev], res_list)): if _res: _to_save = [] for _dt, _kwargs, _cbf in [ ("lstopo_dump", {}, self._interpret_lstopo), ("dmi_dump", {}, self._interpret_dmiinfo), ("pci_dump", { "marshal": True }, self._interpret_pciinfo), ]: if _dt in _res: _data = _cbf( server_command.decompress(_res["*{}".format(_dt)], **_kwargs)) if _data is not None: _to_save.append((_dt.split("_")[0], _data)) else: self.log("missing {} in result".format(_dt), logging_tools.LOG_LEVEL_WARN) if _to_save: _highest_run = _dev.deviceinventory_set.all().order_by( "-idx") if _highest_run.count(): _highest_run = _highest_run[0].run_idx else: _highest_run = 0 _highest_run += 1 self.log("{} trees to save ({}), run_idx is {:d}".format( logging_tools.get_plural("inventory tree", len(_to_save)), ", ".join(sorted([_tuple[0] for _tuple in _to_save])), _highest_run, )) for _name, _tree in _to_save: inv = DeviceInventory(device=_dev, inventory_type=_name, run_idx=_highest_run, value=etree.tostring(_tree)) inv.save() else: self.log("no trees to save", logging_tools.LOG_LEVEL_WARN) DeviceLogEntry.new( device=_dev, source=global_config["LOG_SOURCE_IDX"], level=logging_tools.LOG_LEVEL_OK, text="system scan took {}, {:d} trees".format( logging_tools.get_diff_time_str(e_time - s_time, ), len(_to_save), ), ) else: DeviceLogEntry.new( device=_dev, source=global_config["LOG_SOURCE_IDX"], level=logging_tools.LOG_LEVEL_ERROR, text="error scanning system (took {})".format( logging_tools.get_diff_time_str(e_time - s_time, )), ) res_node.ok("system scanned") return res_node