def update_agent_status(agent_id, username=None, uri=None, method=None): """Update the status of an agent. Args: agent_id (str): 36 character uuid of the agent you are updating. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.core.agent.agents import update_agent_status >>> agent_id = '0a1f9a3c-9200-42ef-ba63-f4fd17f0644c' >>> update_agent_status(agent_id) { 'uri': None, 'rv_status_code': 1008, 'http_method': None, 'http_status': 200, 'message': 'admin - agent 52faa1db-290a-47a7-a4cf-e4ad70e25c38 was updated', 'data': {'needs_reboot': 'no'} } """ status = update_agent_status.func_name + ' - ' now = time() agent_data = { AgentKey.LastAgentUpdate: DbTime.epoch_time_to_db_time(now), AgentKey.AgentStatus: 'up' } status_code, count, error, generated_ids = (update_agent_data( agent_id, agent_data)) if status_code == DbCodes.Replaced: msg = 'agent_id %s updated' generic_status_code = GenericCodes.ObjectUpdated vfense_status_code = AgentCodes.AgentsUpdated elif status_code == DbCodes.Skipped: msg = 'agent_id %s does not exist' generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsDoesNotExist elif status_code == DbCodes.Errors: msg = 'agent_id %s could not be updated' generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsFailedToUpdate agent_data[AgentKey.LastAgentUpdate] = now results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: username, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return results
def update_agent_fields(agent_id, agent_data, username=None, uri=None, method=None): """Update various fields in an agent. Args: agent_id (str): 36 character uuid of the agent you are updating. agent_data (dict): Dictionary of the data that you are updating. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage:: >>> from vFense.core.agent.agents import update_agent_fields >>> agent_id = '0a1f9a3c-9200-42ef-ba63-f4fd17f0644c' >>> agent_data = {'production_level': 'Development'} >>> update_agent_fields(agent_id, agent_data) { 'uri': None, 'rv_status_code': 1008, 'http_method': None, 'http_status': 200, 'message': 'admin - agent 52faa1db-290a-47a7-a4cf-e4ad70e25c38 was updated', 'data': {'needs_reboot': 'no'} } """ status = update_agent_fields.func_name + " - " status_code, count, errors, generated_ids = update_agent_data(agent_id, agent_data) if status_code == DbCodes.Replaced: msg = "agent_id %s updated" generic_status_code = GenericCodes.ObjectUpdated vfense_status_code = AgentCodes.AgentsUpdated elif status_code == DbCodes.Skipped: msg = "agent_id %s does not exist" generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsDoesNotExist elif status_code == DbCodes.Unchanged: msg = "agent_id %s was not updated, data was the same." generic_status_code = GenericCodes.ObjectUnchanged vfense_status_code = GenericCodes.ObjectUnchanged elif status_code == DbCodes.Errors: msg = "agent_id %s could not be updated" generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsFailedToUpdate results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [agent_data], ApiResultKeys.USERNAME: username, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method, } return results
def update_agent_status(agent_id, username=None, uri=None, method=None): """Update the status of an agent. Args: agent_id (str): 36 character uuid of the agent you are updating. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage: >>> from vFense.core.agent.agents import update_agent_status >>> agent_id = '0a1f9a3c-9200-42ef-ba63-f4fd17f0644c' >>> update_agent_status(agent_id) { 'uri': None, 'rv_status_code': 1008, 'http_method': None, 'http_status': 200, 'message': 'admin - agent 52faa1db-290a-47a7-a4cf-e4ad70e25c38 was updated', 'data': {'needs_reboot': 'no'} } """ status = update_agent_status.func_name + " - " now = time() agent_data = {AgentKey.LastAgentUpdate: DbTime.epoch_time_to_db_time(now), AgentKey.AgentStatus: "up"} status_code, count, error, generated_ids = update_agent_data(agent_id, agent_data) if status_code == DbCodes.Replaced: msg = "agent_id %s updated" generic_status_code = GenericCodes.ObjectUpdated vfense_status_code = AgentCodes.AgentsUpdated elif status_code == DbCodes.Skipped: msg = "agent_id %s does not exist" generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsDoesNotExist elif status_code == DbCodes.Errors: msg = "agent_id %s could not be updated" generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsFailedToUpdate agent_data[AgentKey.LastAgentUpdate] = now results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: username, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method, } return results
def update_agent(agent_id, system_info, hardware, rebooted, username=None, customer_name=None, uri=None, method=None): """Update various aspects of agent Args: agent_id (str): 36 character uuid of the agent you are updating system_info (dict): Dictionary with system related info hardware (dict): List of dictionaries that rpresent the hardware rebooted (str): yes or no Kwargs: user_name (str): The name of the user who called this function. customer_name (str): The name of the customer. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. """ results = {ApiResultKeys.USERNAME: username, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method} agent_data = {} try: now = time() agent_orig_info = fetch_agent_info(agent_id) if agent_orig_info: agent_data[AgentKey.Hardware] = hardware for key, value in system_info.items(): agent_data[key] = value agent_data[AgentKey.LastAgentUpdate] = DbTime.epoch_time_to_db_time(now) agent_data[AgentKey.HostName] = agent_orig_info.get(AgentKey.HostName, None) agent_data[AgentKey.DisplayName] = agent_orig_info.get(AgentKey.DisplayName, None) if rebooted == CommonKeys.YES: agent_data[AgentKey.NeedsReboot] = CommonKeys.NO status_code, count, error, generated_ids = update_agent_data(agent_id, agent_data) if status_code == DbCodes.Replaced and count > 0: Hardware().add(agent_id, hardware) msg = "agent %s updated successfully." % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = GenericCodes.ObjectUpdated results[ApiResultKeys.VFENSE_STATUS_CODE] = AgentResultCodes.ResultsUpdated results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] results[ApiResultKeys.UPDATED_IDS] = [agent_id] elif status_code == DbCodes.Unchanged: Hardware().add(agent_id, hardware) msg = "agent %s unchanged, data is the same." % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = GenericCodes.ObjectUnchanged results[ApiResultKeys.VFENSE_STATUS_CODE] = AgentResultCodes.ResultsUpdated results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] results[ApiResultKeys.UNCHANGED_IDS] = [agent_id] elif status_code == DbCodes.Skipped: msg = "agent %s does not exist." % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = GenericFailureCodes.InvalidId results[ApiResultKeys.VFENSE_STATUS_CODE] = AgentFailureCodes.AgentsDoesNotExist results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] elif status_code == DbCodes.Errors: msg = "operation failed" % (error) results[ApiResultKeys.GENERIC_STATUS_CODE] = GenericFailureCodes.FailedToUpdateObject results[ApiResultKeys.VFENSE_STATUS_CODE] = AgentFailureResultCodes.ResultsFailedToUpdate results[ApiResultKeys.MESSAGE] = msg else: msg = "agent %s does not exist." % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = GenericFailureCodes.InvalidId results[ApiResultKeys.VFENSE_STATUS_CODE] = AgentFailureCodes.AgentsDoesNotExist results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] except Exception as e: logger.exception(e) msg = "operation failed" % (error) results[ApiResultKeys.GENERIC_STATUS_CODE] = GenericFailureCodes.FailedToUpdateObject results[ApiResultKeys.VFENSE_STATUS_CODE] = AgentFailureResultCodes.ResultsFailedToUpdate results[ApiResultKeys.MESSAGE] = msg return results
def update_agent(agent_id, system_info, hardware, rebooted, username=None, customer_name=None, uri=None, method=None): """Update various aspects of agent Args: agent_id (str): 36 character uuid of the agent you are updating system_info (dict): Dictionary with system related info hardware (dict): List of dictionaries that rpresent the hardware rebooted (str): yes or no Kwargs: user_name (str): The name of the user who called this function. customer_name (str): The name of the customer. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. """ results = { ApiResultKeys.USERNAME: username, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } agent_data = {} try: now = time() agent_orig_info = fetch_agent_info(agent_id) if agent_orig_info: agent_data[AgentKey.Hardware] = hardware for key, value in system_info.items(): agent_data[key] = value agent_data[AgentKey.LastAgentUpdate] = ( DbTime.epoch_time_to_db_time(now)) agent_data[AgentKey.HostName] = (agent_orig_info.get( AgentKey.HostName, None)) agent_data[AgentKey.DisplayName] = (agent_orig_info.get( AgentKey.DisplayName, None)) if rebooted == CommonKeys.YES: agent_data[AgentKey.NeedsReboot] = CommonKeys.NO status_code, count, error, generated_ids = (update_agent_data( agent_id, agent_data)) if status_code == DbCodes.Replaced and count > 0: Hardware().add(agent_id, hardware) msg = 'agent %s updated successfully.' % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = \ GenericCodes.ObjectUpdated results[ApiResultKeys.VFENSE_STATUS_CODE] = \ AgentResultCodes.ResultsUpdated results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] results[ApiResultKeys.UPDATED_IDS] = [agent_id] elif status_code == DbCodes.Unchanged: Hardware().add(agent_id, hardware) msg = 'agent %s unchanged, data is the same.' % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = \ GenericCodes.ObjectUnchanged results[ApiResultKeys.VFENSE_STATUS_CODE] = \ AgentResultCodes.ResultsUpdated results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] results[ApiResultKeys.UNCHANGED_IDS] = [agent_id] elif status_code == DbCodes.Skipped: msg = 'agent %s does not exist.' % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = \ GenericFailureCodes.InvalidId results[ApiResultKeys.VFENSE_STATUS_CODE] = \ AgentFailureCodes.AgentsDoesNotExist results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] elif status_code == DbCodes.Errors: msg = 'operation failed' % (error) results[ApiResultKeys.GENERIC_STATUS_CODE] = \ GenericFailureCodes.FailedToUpdateObject results[ApiResultKeys.VFENSE_STATUS_CODE] = \ AgentFailureResultCodes.ResultsFailedToUpdate results[ApiResultKeys.MESSAGE] = msg else: msg = 'agent %s does not exist.' % (agent_id) results[ApiResultKeys.GENERIC_STATUS_CODE] = \ GenericFailureCodes.InvalidId results[ApiResultKeys.VFENSE_STATUS_CODE] = \ AgentFailureCodes.AgentsDoesNotExist results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = [agent_data] except Exception as e: logger.exception(e) msg = 'operation failed' % (error) results[ApiResultKeys.GENERIC_STATUS_CODE] = \ GenericFailureCodes.FailedToUpdateObject results[ApiResultKeys.VFENSE_STATUS_CODE] = \ AgentFailureResultCodes.ResultsFailedToUpdate results[ApiResultKeys.MESSAGE] = msg return results
def update_agent_fields(agent_id, agent_data, username=None, uri=None, method=None): """Update various fields in an agent. Args: agent_id (str): 36 character uuid of the agent you are updating. agent_data (dict): Dictionary of the data that you are updating. Kwargs: user_name (str): The name of the user who called this function. uri (str): The uri that was used to call this function. method (str): The HTTP methos that was used to call this function. Basic Usage:: >>> from vFense.core.agent.agents import update_agent_fields >>> agent_id = '0a1f9a3c-9200-42ef-ba63-f4fd17f0644c' >>> agent_data = {'production_level': 'Development'} >>> update_agent_fields(agent_id, agent_data) { 'uri': None, 'rv_status_code': 1008, 'http_method': None, 'http_status': 200, 'message': 'admin - agent 52faa1db-290a-47a7-a4cf-e4ad70e25c38 was updated', 'data': {'needs_reboot': 'no'} } """ status = update_agent_fields.func_name + ' - ' status_code, count, errors, generated_ids = (update_agent_data( agent_id, agent_data)) if status_code == DbCodes.Replaced: msg = 'agent_id %s updated' generic_status_code = GenericCodes.ObjectUpdated vfense_status_code = AgentCodes.AgentsUpdated elif status_code == DbCodes.Skipped: msg = 'agent_id %s does not exist' generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsDoesNotExist elif status_code == DbCodes.Unchanged: msg = 'agent_id %s was not updated, data was the same.' generic_status_code = GenericCodes.ObjectUnchanged vfense_status_code = GenericCodes.ObjectUnchanged elif status_code == DbCodes.Errors: msg = 'agent_id %s could not be updated' generic_status_code = GenericFailureCodes.FailedToUpdateObject vfense_status_code = AgentFailureCodes.AgentsFailedToUpdate results = { ApiResultKeys.DB_STATUS_CODE: status_code, ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code, ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code, ApiResultKeys.MESSAGE: status + msg, ApiResultKeys.DATA: [agent_data], ApiResultKeys.USERNAME: username, ApiResultKeys.URI: uri, ApiResultKeys.HTTP_METHOD: method } return results