def run(self, session, config, create_time, last_time): """ This is the method you must implement to perform your atomic task args: session - the session object return by the loginToTarget method config - the dictionary of all the config keys from your stanza in the collection.conf create_time - the time this task was created/scheduled to run (datetime object) last_time - the last time this task was created/scheduler to run (datetime object) RETURNS True if successful, False otherwise """ try: #Handle the destination index for the data, note that we must handle empty strings and change them to None dest_index = config.get("inv_index", False) if not dest_index: dest_index = None self.logger.info("[Inventory Handler] Starting Hierarchy Collection: create_time={0}, last_time={1}".format(create_time, last_time)) self.logger.info("[Inventory Handler] Starting Cache Inspection") last_mor, last_version, last_session, last_dump_time = self.get_inv_cache(session[0], "Hierarchy") self.logger.info("[Inventory Handler] Finished Cache Inspection") self.logger.debug("[Inventory Handler] Cached Collection Values: last_mor:"+str(last_mor)+"|last_version:"+str(last_version)+"|last_session:"+str(last_session)+"|last_dumptime:"+str(last_dump_time)) if last_session != session[1]: updatedump = True self.logger.info("[Inventory Handler] Found a changed session, recreating collector.") last_version, hierarchyCollector, target_config_object, mor = inventory.CreateHierarchyCollector(targetConfigObject="Hierarchy") elif (create_time-last_dump_time)>datetime.timedelta(hours=4) or (last_version != None and float(str(last_version)) >= 20): updatedump = True self.logger.info("[Inventory Handler] Found session too old or version is greater then 20, recreating collector") self.logger.debug("[Inventory Handler] Calling CreateHierarchyCollector: MOR:"+str(last_mor)+"| Version:"+str(last_version)) last_version, hierarchyCollector, target_config_object, mor = inventory.CreateHierarchyCollector(managedObjectReference=last_mor, version=last_version, updateType="recycle", targetConfigObject="Hierarchy") else: updatedump = False self.logger.info("[Inventory Handler] Checking for updates on existing collector") self.logger.debug("[Inventory Handler] Calling CreateHierarchyCollector: MOR:"+str(last_mor)+"| Version:"+str(last_version)) last_version, hierarchyCollector, target_config_object, mor = inventory.CreateHierarchyCollector(managedObjectReference=last_mor, version=last_version, targetConfigObject="Hierarchy") tempus = str(Connection.svcInstance.currentTime()) sourcename = "VMInv:" + target_config_object last_version, is_first_version_seen = self.send_inv_data(hierarchyCollector, last_version, session[0], "vmware:inv:hierarchy", sourcename, tempus, dest_index, config, target_config_object) if is_first_version_seen and updatedump: self.set_inv_cache(session[0], "Hierarchy", mor, last_version, session[1], create_time) addRootNode = True else: self.set_inv_cache(session[0], "Hierarchy", mor, last_version, session[1], last_dump_time) addRootNode = False if addRootNode: rootNode={ "moid":Connection.rootFolder.getMOR().value, "type":"RootFolder", "changeSet":{"name":Connection.domain, "parent":{"moid":"N/A", "type":"N/A"}}} self.output.sendData(inventory.Jsonify(rootNode), host=session[0], sourcetype="vmware:inv:hierarchy", source=sourcename, time=tempus, index=dest_index) self.logger.info("[Inventory Handler] Finished collecting "+target_config_object+", stored these values: mor:"+str(mor)+" | last_version:"+str(last_version)+" | session:"+str(session[1])+" | last_dump_time:"+str(last_dump_time) ) del last_version, mor, addRootNode, target_config_object return True except Exception as e: self.logger.exception(e) return False
def run(self, session, config, create_time, last_time): """ This is the method you must implement to perform your atomic task args: session - the session object return by the loginToTarget method config - the dictionary of all the config keys from your stanza in the collection.conf create_time - the time this task was created/scheduled to run (datetime object) last_time - the last time this task was created/scheduler to run (datetime object) RETURNS True if successful, False otherwise """ try: #Handle the destination index for the data, note that we must handle empty strings and change them to None dest_index = config.get("inv_index", False) if not dest_index: dest_index = None #Build a list of any addl. properties that need to be collected as specified in conf file host_config_prop_list = [] host_config_prop_list = config.get("hostsystem_inv_config") self.logger.info("[Inventory Handler] Starting HostSystem Collection: create_time={0}, last_time={1}".format(create_time, last_time)) self.logger.info("[Inventory Handler] Starting Cache Inspection") last_mor, last_version, last_session, last_dump_time = self.get_inv_cache(session[0], "HostSystem") self.logger.info("[Inventory Handler] Finished Cache Inspection") self.logger.debug("[Inventory Handler] Cached Collection Values: last_mor:"+str(last_mor)+"|last_version:"+str(last_version)+"|last_session:"+str(last_session)+"|last_dumptime:"+str(last_dump_time)) if last_session != session[1]: updatedump = True self.logger.info("[Inventory Handler] Found a changed session, recreating collector.") last_version, hierarchyCollector, target_config_object, mor = inventory.CreateHierarchyCollector(targetConfigObject="HostSystem", addlTargetConfig=host_config_prop_list) elif (create_time-last_dump_time)>datetime.timedelta(hours=4) or (last_version != None and float(str(last_version)) >= 20): updatedump = True self.logger.info("[Inventory Handler] Found session too old or version is greater then 20, recreating collector") self.logger.debug("[Inventory Handler] Calling CreateHierarchyCollector: MOR:"+str(last_mor)+"| Version:"+str(last_version)) last_version, hierarchyCollector, target_config_object, mor = inventory.CreateHierarchyCollector(managedObjectReference=last_mor, version=last_version, updateType="recycle", targetConfigObject="HostSystem", addlTargetConfig=host_config_prop_list) else: updatedump = False self.logger.info("[Inventory Handler] Checking for updates on existing collector") self.logger.debug("[Inventory Handler] Calling CreateHierarchyCollector: MOR:"+str(last_mor)+"| Version:"+str(last_version)) last_version, hierarchyCollector, target_config_object, mor = inventory.CreateHierarchyCollector(managedObjectReference=last_mor, version=last_version, targetConfigObject="HostSystem", addlTargetConfig=host_config_prop_list) tempus = str(Connection.svcInstance.currentTime()) sourcename = "VMInv:" + target_config_object last_version, is_first_version_seen = self.send_inv_data(hierarchyCollector, last_version, session[0], "vmware:inv:hostsystem", sourcename, tempus, dest_index, config, target_config_object) if is_first_version_seen and updatedump: self.set_inv_cache(session[0], "HostSystem", mor, last_version, session[1], create_time) else: self.set_inv_cache(session[0], "HostSystem", mor, last_version, session[1], last_dump_time) self.logger.info("[Inventory Handler] Finished collecting "+target_config_object+", stored these values: mor:"+str(mor)+" | last_version:"+str(last_version)+" | session:"+str(session[1])+" | last_dump_time:"+str(last_dump_time) ) del last_version, mor, target_config_object return True except Exception as e: self.logger.exception(e) return False
def _find_rps(self): hierarchy_collector = inventory.CreateHierarchyCollector( targetConfigObject='PerfResourcePoolList', oneTime=True)[1] gen_collect_propex = hierarchy_collector.collectPropertiesEx( hierarchy_collector.fSpecList) rps_list = [] for rps in gen_collect_propex: if rps is None: break else: for x in rps: rps_list.append(x.obj) inventory.DestroyHierarchyCollector(hierarchy_collector) del gen_collect_propex, hierarchy_collector return rps_list
def _find_clusters(self): hierarchy_collector = inventory.CreateHierarchyCollector( targetConfigObject='PerfClusterComputeResourceList', oneTime=True)[1] gen_collect_propex = hierarchy_collector.collectPropertiesEx( hierarchy_collector.fSpecList) ccrs_list = [] for ccrs in gen_collect_propex: if ccrs is None: break else: for x in ccrs: ccrs_list.append(x.obj) inventory.DestroyHierarchyCollector(hierarchy_collector) del gen_collect_propex, hierarchy_collector return ccrs_list
def _find_vms_for_host(self, host): """Constructs a list of powered-on VMs given a host MOR; returns a list of VM MORs""" # Get vm list hierarchy_collector = inventory.CreateHierarchyCollector( targetConfigObject='PerfInventory', oneTime=True)[1] gen_collect_propex = hierarchy_collector.collectPropertiesEx( hierarchy_collector.fSpecList) vms_list = [] for vms in gen_collect_propex: if vms is None: break else: for x in vms: if (hasattr(x.propSet[1].val, "value") and x.propSet[1].val.value == host): if (hasattr(x.propSet[2], "val") and x.propSet[2].val == "poweredOn"): vms_list.append(x.obj) self.logger.debug("Powered on VMs list=%s host=%s", vms_list, host) inventory.DestroyHierarchyCollector(hierarchy_collector) del gen_collect_propex, hierarchy_collector return vms_list