def process_queue_data(agent_id, username, customer_name, uri, method): agent_queue = AgentQueue(agent_id, customer_name).pop_agent_queue() for operation in agent_queue: if operation.get(AgentOperationKey.OperationId): oper = (AgentOperation(username, customer_name)) oper.update_operation_pickup_time( operation[AgentOperationKey.OperationId], agent_id) return agent_queue
def save_result( agent_id, operation_id, error, data, uri, method, operation_type ): try: operation = AgentOperation( Default.User, None, uri, method ) if not error: results = operation.update_operation_results( operation_id, agent_id, AgentOperationCodes.ResultsReceived, operation_type, error ) else: results = operation.update_operation_results( operation_id, agent_id, AgentOperationCodes.ResultsReceivedWithErrors, operation_type, error ) return results except Exception as e: print e return None
def __init__(self, username, agent_id, operation_id, success, error=None, status_code=None, uri=None, method=None): """ Args: username (str): The name of the user who made this api call agent_id (str): 36 character UUID of the agent. operation_id (str): 36 character UUID of the operation. success (str): true or false. Kwargs: error (str): The error message, if the operation failed. status_code (int): The exact status of this operation. uri (str): The uri which was called for the results. method (str): The method used to call this api. Basic Usage: >>> from vFense.operations.results import OperationResults >>> username = '******' >>> operation_id = '8fed3dc7-33d4-4278-9bd4-398a68bf7f22' >>> agent_id = 'db6bf07-c5da-4494-93bb-109db205ca64' >>> success = 'true' >>> results = OperationResults( username, agent_id, operation_id, success ) """ self.agent_id = agent_id self.operation_id = operation_id self.username = username self.uri = uri self.method = method self.agent_data = get_agent_info(self.agent_id) self.operation_data = get_agent_operation(self.operation_id) self.customer_name = self.agent_data[AgentKey.CustomerName] self.date_now = DbTime.time_now() self.begining_of_time = DbTime.begining_of_time() self.error = error self.success = success self.status_code = status_code self.operation = (AgentOperation( self.username, self.customer_name, ))
def save_operation(operation): _oper = ( AgentOperation( operation.username, operation.customer, operation.uri, operation.method ) ) oper_results = ( _oper.create_operation( operation.operation_type, ra.PluginName, [operation.agent_id], # Expecting a list of agent IDs. None # No tag IDs. ) ) if oper_results['http_status'] == 200: operation_id = oper_results['data']['operation_id'] _oper.add_agent_to_operation( operation.agent_id, operation_id ) logger.info( '%s - %s operation created by user %s' % ( operation.username, operation.operation_type, operation.username ) ) return operation_id return None
def remove_expired_jobs_and_update_operations(): epoch_time_now = mktime(datetime.now().timetuple()) expired_jobs = get_all_expired_jobs(epoch_time_now) status_code, count, error, generated_ids = ( delete_all_expired_jobs(epoch_time_now)) jobs_deleted = count msg = 'number of jobs expired: %s' % (str(jobs_deleted)) logger.info(msg) for job in expired_jobs: operation = (AgentOperation('admin', job[OperationKey.CustomerName], None, None)) operation.update_operation_expire_time( job[OperationKey.OperationId], job[OperationPerAgentKey.AgentId], job[OperationKey.Operation]) if job[OperationKey.Plugin] == RV_PLUGIN: collection = AppCollections.UniqueApplications if re.search('^install', job['operation']): app_status = {STATUS: AVAILABLE} if job['operation'] == AgentOperations.INSTALL_CUSTOM_APPS: collection = AppCollections.CustomApps if job['operation'] == AgentOperations.INSTALL_SUPPORTED_APPS: collection = AppCollections.SupportedApps if job['operation'] == AgentOperations.INSTALL_AGENT_UPDATE: collection = AppCollections.vFenseApps elif re.search('^uninstall', job['operation']): app_status = {STATUS: INSTALLED} for app in job[AppsKey.FileData]: update_app_status_by_agentid_and_appid( job[OperationPerAgentKey.AgentId], collection, app[AppsKey.AppId], app_status)
def generic_operation(self, action, plugin, agentids=None, tag_id=None): """This will do all the necessary work, to add the operation into the agent_queue. This method is to be used for operations that do not need any extra manipulation, in order to be added into the queue. Args: action (str): This is the action that will be performed, on the agent. Examples.... reboot, shutdown, install_os_apps, etc.. plugin (str): The plugin that this operation is for. Examples... core, rv, ra, vuln agentids (list): List of agent ids, this operation will be performed on. tag_id (str): The tag id that this operation will be performed on. Basic Usage: >>> from vFense.operations.store_agent_operation import StoreAgentOperation >>> username = '******' >>> customer_name = 'default' >>> uri = '/api/v1/agent/33ba8521-b2e5-47dc-9bdc-0f1e3384049d' >>> method = 'POST' >>> store = StoreAgentOperation(username, customer_name, uri, method) >>> store.generic_operation( 'reboot', 'core', agentids=['33ba8521-b2e5-47dc-9bdc-0f1e3384049d'] ) Returns: Dictionary { "rv_status_code": 6000, "http_method": "POST", "http_status": 200, "unchanged_ids": [], "generated_ids": [ "d5fb023c-82a0-4552-adc1-b3f83de7ae8a" ], "message": "operation created", "data": [ { "operation_id": "d5fb023c-82a0-4552-adc1-b3f83de7ae8a", "operation": "reboot", "agent_id": "456404f1-b185-4f4f-8fb7-bfb21b3a5d53", "plugin": "core" } ], "uri": "/api/v1/456404f1-b185-4f4f-8fb7-bfb21b3a5d53/" } """ data = [] performed_on = vFenseObjects.AGENT if tag_id: performed_on = vFenseObjects.TAG if not agentids: agentids = get_agent_ids_from_tag(tag_id) elif agentids: agentids += get_agent_ids_from_tag(tag_id) results = { ApiResultKeys.DATA: [], ApiResultKeys.USERNAME: self.username, ApiResultKeys.URI: self.uri, ApiResultKeys.HTTP_METHOD: self.method } operation = (AgentOperation( self.username, self.customer_name, )) operation_id = (operation.create_operation(action, plugin, agentids, tag_id, performed_on=performed_on)) if operation_id: msg = 'operation created' status_code = GenericCodes.ObjectCreated vfense_status_code = AgentOperationCodes.Created results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg for agent_id in agentids: operation_data = { AgentOperationKey.Operation: action, AgentOperationKey.OperationId: operation_id, AgentOperationKey.Plugin: plugin, OperationPerAgentKey.AgentId: agent_id, } agent_data = deepcopy(operation_data) data.append(agent_data) self._store_in_agent_queue(operation_data) operation.add_agent_to_operation(agent_id, operation_id) results[ApiResultKeys.DATA] = data else: msg = 'operation failed to create' status_code = GenericFailureCodes.FailedToCreateObject vfense_status_code = ( AgentOperationFailureCodes.FailedToCreateOperation) results[ApiResultKeys.GENERATED_IDS] = [operation_id] results[ApiResultKeys.GENERIC_STATUS_CODE] = status_code results[ApiResultKeys.VFENSE_STATUS_CODE] = vfense_status_code results[ApiResultKeys.MESSAGE] = msg results[ApiResultKeys.DATA] = data return results