def __get_all_keys(self): return( { NotificationKeys.NotificationId: None, NotificationKeys.NotificationType: None, NotificationKeys.RuleName: None, NotificationKeys.RuleDescription: None, NotificationKeys.CreatedBy: None, NotificationKeys.CreatedTime: r.epoch_time(self.now), NotificationKeys.ModifiedBy: None, NotificationKeys.ModifiedTime: r.epoch_time(self.now), NotificationKeys.Plugin: None, NotificationKeys.User: None, NotificationKeys.Group: None, NotificationKeys.AllAgents: 'false', NotificationKeys.Agents: [], NotificationKeys.Tags: [], NotificationKeys.CustomerName: None, NotificationKeys.AppThreshold: None, NotificationKeys.RebootThreshold: None, NotificationKeys.ShutdownThreshold: None, NotificationKeys.CpuThreshold: None, NotificationKeys.MemThreshold: None, NotificationKeys.FileSystemThreshold: None, NotificationKeys.FileSystem: None, } )
def add_agent_to_operation(self, agent_id, operation_id, conn=None): try: (r.table(OperationsPerAgentCollection).insert({ OperationPerAgentKey.AgentId: agent_id, OperationPerAgentKey.OperationId: operation_id, OperationPerAgentKey.CustomerName: self.customer_name, OperationPerAgentKey.Status: PENDINGPICKUP, OperationPerAgentKey.PickedUpTime: r.epoch_time(0.0), OperationPerAgentKey.CompletedTime: r.epoch_time(0.0), OperationPerAgentKey.Errors: None }).run(conn)) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( operation_id, 'add agent to reboot operation', e)) logger.exception(results)
def add_agent_to_operation(self, agent_id, operation_id, conn=None): try: ( r .table(OperationsPerAgentCollection) .insert( { OperationPerAgentKey.AgentId: agent_id, OperationPerAgentKey.OperationId: operation_id, OperationPerAgentKey.CustomerName: self.customer_name, OperationPerAgentKey.Status: PENDINGPICKUP, OperationPerAgentKey.PickedUpTime: r.epoch_time(0.0), OperationPerAgentKey.CompletedTime: r.epoch_time(0.0), OperationPerAgentKey.Errors: None } ) .run(conn) ) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(operation_id, 'add agent to reboot operation', e) ) logger.exception(results)
def add_agent_to_install_operation(self, agent_id, operation_id, applications, conn=None): try: (r.table(OperationsPerAgentCollection).insert({ OperationPerAgentKey.AgentId: agent_id, OperationPerAgentKey.OperationId: operation_id, OperationPerAgentKey.CustomerName: self.customer_name, OperationPerAgentKey.Status: PENDINGPICKUP, OperationPerAgentKey.PickedUpTime: r.epoch_time(0.0), OperationPerAgentKey.CompletedTime: r.epoch_time(0.0), OperationPerAgentKey.AppsTotalCount: len(applications), OperationPerAgentKey.AppsPendingCount: len(applications), OperationPerAgentKey.AppsFailedCount: self.INIT_COUNT, OperationPerAgentKey.AppsCompletedCount: self.INIT_COUNT, OperationPerAgentKey.Errors: None }).run(conn)) for app in applications: (r.table(OperationsPerAppCollection).insert({ OperationPerAppKey.AgentId: agent_id, OperationPerAppKey.OperationId: operation_id, OperationPerAppKey.CustomerName: self.customer_name, OperationPerAppKey.Results: OperationCodes.ResultsPending, OperationPerAppKey.ResultsReceivedTime: r.epoch_time(0.0), OperationPerAppKey.AppId: app[OperationPerAppKey.AppId], OperationPerAppKey.AppName: app[OperationPerAppKey.AppName], OperationPerAppKey.Errors: None }).run(conn)) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( operation_id, 'add agent to install operation', e)) logger.exception(results)
def add_or_update_applications(table=AppsPerAgentCollection, pkg_list=[], delete_afterwards=True, conn=None): completed = False inserted_count = 0 updated = None replaced_count = 0 deleted_count = 0 pkg_count = len(pkg_list) last_modified_time = mktime(datetime.now().timetuple()) if pkg_count > 0: for pkg in pkg_list: pkg['last_modified_time'] = r.epoch_time(last_modified_time) try: updated = ( r .table(table) .insert(pkg, upsert=True) .run(conn) ) logger.info(updated) inserted_count += updated['inserted'] replaced_count += updated['replaced'] except Exception as e: logger.exception(e) try: if delete_afterwards: deleted = ( r .table(table) .get_all( pkg[AppsPerAgentKey.AgentId], index=AppsPerAgentIndexes.AgentId ) .filter( r.row['last_modified_time'] < r.epoch_time( last_modified_time) ) .delete() .run(conn) ) deleted_count += deleted['deleted'] except Exception as e: logger.exception(e) return( { 'pass': completed, 'inserted': inserted_count, 'replaced': replaced_count, 'deleted': deleted_count, 'pkg_count': pkg_count, } )
def add_custom_app_to_agents(username, customer_name, uri, method, file_data, agent_id=None, app_id=None): if app_id and not agent_id: app_info = (get_app_data(app_id, table=CustomAppsCollection)) agent_ids = get_all_agent_ids(customer_name, agent_os=app_info[AgentKey.OsCode]) if len(agent_ids) > 0: for agentid in agent_ids: update_file_data(app_id, agentid, file_data) agent_info_to_insert = ({ CustomAppsPerAgentKey.AgentId: agentid, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) apps_to_insert_per_agent(username, uri, method, agent_info_to_insert) if agent_id and not app_id: agent_info = get_agent_info(agent_id) apps_info = get_apps_data(customer_name, os_code=agent_info[AgentKey.OsCode]) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(CustomAppsKey.AppId) file_data = get_file_data(app_id) update_file_data(app_id, agent_id, file_data) agent_info_to_insert = ({ CustomAppsPerAgentKey.AgentId: agent_id, CustomAppsPerAgentKey.AppId: app_id, CustomAppsPerAgentKey.Status: AVAILABLE, CustomAppsPerAgentKey.CustomerName: customer_name, CustomAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) apps_to_insert_per_agent(username, uri, method, agent_info_to_insert)
def set_app_per_node_parameters(self, app): app[AppsPerAgentKey.AgentId] = self.agent_id app[AppsKey.OsCode] = self.os_code app[AppsKey.RvSeverity] = (self.sev_generator( app[AppsKey.VendorSeverity])) app[AppsKey.ReleaseDate] = (r.epoch_time(app[AppsKey.ReleaseDate])) app[AppsPerAgentKey.InstallDate] = (r.epoch_time( app[AppsPerAgentKey.InstallDate])) return (app)
def add_agent_to_install_operation(self, agent_id, operation_id, applications, conn=None): try: ( r .table(OperationsPerAgentCollection) .insert( { OperationPerAgentKey.AgentId: agent_id, OperationPerAgentKey.OperationId: operation_id, OperationPerAgentKey.CustomerName: self.customer_name, OperationPerAgentKey.Status: PENDINGPICKUP, OperationPerAgentKey.PickedUpTime: r.epoch_time(0.0), OperationPerAgentKey.CompletedTime: r.epoch_time(0.0), OperationPerAgentKey.AppsTotalCount: len(applications), OperationPerAgentKey.AppsPendingCount: len(applications), OperationPerAgentKey.AppsFailedCount: self.INIT_COUNT, OperationPerAgentKey.AppsCompletedCount: self.INIT_COUNT, OperationPerAgentKey.Errors: None } ) .run(conn) ) for app in applications: ( r .table(OperationsPerAppCollection) .insert( { OperationPerAppKey.AgentId: agent_id, OperationPerAppKey.OperationId: operation_id, OperationPerAppKey.CustomerName: self.customer_name, OperationPerAppKey.Results: OperationCodes.ResultsPending, OperationPerAppKey.ResultsReceivedTime: r.epoch_time(0.0), OperationPerAppKey.AppId: app[OperationPerAppKey.AppId], OperationPerAppKey.AppName: app[OperationPerAppKey.AppName], OperationPerAppKey.Errors: None } ) .run(conn) ) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(operation_id, 'add agent to install operation', e) ) logger.exception(results)
def add_supported_app_to_agents(username, customer_name, uri, method, agent_id=None): if agent_id: agent_info = get_agent_info(agent_id) apps_info = ( get_apps_data( customer_name, table=SupportedAppsCollection, os_code=agent_info[AgentKey.OsCode] ) ) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(SupportedAppsKey.AppId) file_data = get_file_data(app_id) update_file_data( app_id, agent_id, file_data ) agent_info_to_insert = ( { SupportedAppsPerAgentKey.AgentId: agent_id, SupportedAppsPerAgentKey.AppId: app_id, SupportedAppsPerAgentKey.Status: AVAILABLE, SupportedAppsPerAgentKey.CustomerName: customer_name, SupportedAppsPerAgentKey.InstallDate: r.epoch_time(0.0) } ) apps_to_insert_per_agent( username, uri, method, agent_info_to_insert, table=SupportedAppsPerAgentCollection )
def _set_app_per_agent_properties(self, **kwargs): return( { self.CurrentAppsPerAgentKey.AgentId: kwargs[AgentKey.AgentId], self.CurrentAppsPerAgentKey.CustomerName: kwargs[AgentKey.CustomerName], self.CurrentAppsPerAgentKey.Status: AVAILABLE, self.CurrentAppsPerAgentKey.LastModifiedTime: self.last_modified_time, self.CurrentAppsPerAgentKey.Update: PackageCodes.ThisIsAnUpdate, self.CurrentAppsPerAgentKey.InstallDate: r.epoch_time(0.0), self.CurrentAppsPerAgentKey.AppId: kwargs[self.CurrentAppsPerAgentKey.AppId], self.CurrentAppsPerAgentKey.Id: build_agent_app_id( kwargs[self.CurrentAppsPerAgentKey.AgentId], kwargs[self.CurrentAppsPerAgentKey.AppId] ) } )
def update_agent_status(agentid, username, uri=None, method=None): try: conn = db_connect() update_status = { AgentKey.LastAgentUpdate: r.epoch_time(mktime(datetime.now().timetuple())), AgentKey.AgentStatus: 'up' } exists = (r.table(AgentsCollection).get(agentid).run(conn)) if exists: (r.table(AgentsCollection).get(agentid).update(update_status).run( conn)) status = (GenericResults(username, uri, method).object_updated( agentid, 'agent', update_status)) else: status = (GenericResults(username, uri, method).does_not_exists(agentid, 'agent')) logger.info(status['message']) conn.close() except Exception as e: status = (GenericResults(username, uri, method).something_broke(agentid, 'agent', e)) logger.exception(e)
def add_supported_app_to_agents(username, customer_name, uri, method, agent_id=None): if agent_id: agent_info = get_agent_info(agent_id) apps_info = (get_apps_data(customer_name, table=SupportedAppsCollection, os_code=agent_info[AgentKey.OsCode])) if len(apps_info) > 0: for app_info in apps_info: app_id = app_info.get(SupportedAppsKey.AppId) file_data = get_file_data(app_id) update_file_data(app_id, agent_id, file_data) agent_info_to_insert = ({ SupportedAppsPerAgentKey.AgentId: agent_id, SupportedAppsPerAgentKey.AppId: app_id, SupportedAppsPerAgentKey.Status: AVAILABLE, SupportedAppsPerAgentKey.CustomerName: customer_name, SupportedAppsPerAgentKey.InstallDate: r.epoch_time(0.0) }) apps_to_insert_per_agent(username, uri, method, agent_info_to_insert, table=SupportedAppsPerAgentCollection)
def __init__(self, username, customer_name, uri, method): self.username = username self.customer_name = customer_name self.uri = uri self.method = method self.now = mktime(datetime.now().timetuple()) self.db_time = r.epoch_time(self.now) self.INIT_COUNT = 0
def parse_spread_sheet(bulletin_file): bulletin_list = [] workbook = open_workbook(bulletin_file) sheet = workbook.sheet_by_name(WORKBOOK_SHEET) rows = range(sheet.nrows) rows.pop(0) for i in rows: row = sheet.row_values(i) bulletin_dict = {} supercede_list = [] if row[7] != '': row[7] = 'KB' + str(int(row[7])) if row[2] != '': row[2] = 'KB' + str(int(row[2])) rows_to_use = (row[1] + row[2] + row[3] + row[4] + row[6] + row[7] + row[8] + row[9]) rows_to_use = unicode(rows_to_use).encode(sys.stdout.encoding, 'replace') id = build_bulletin_id(rows_to_use) bulletin_dict[WindowsSecurityBulletinKey.Id] = id bulletin_dict[WindowsSecurityBulletinKey.DatePosted] = r.epoch_time( row[0]) bulletin_dict[WindowsSecurityBulletinKey.BulletinId] = row[1] bulletin_dict[WindowsSecurityBulletinKey.BulletinKb] = row[2] bulletin_dict[WindowsSecurityBulletinKey.BulletinSeverity] = row[3] bulletin_dict[WindowsSecurityBulletinKey.BulletinImpact] = row[4] bulletin_dict[WindowsSecurityBulletinKey.Title] = row[5] bulletin_dict[WindowsSecurityBulletinKey.AffectedProduct] = row[6] bulletin_dict[WindowsSecurityBulletinKey.ComponentKb] = row[7] bulletin_dict[WindowsSecurityBulletinKey.AffectedComponent] = row[8] bulletin_dict[WindowsSecurityBulletinKey.ComponentImpact] = row[9] bulletin_dict[WindowsSecurityBulletinKey.ComponentSeverity] = row[10] if row[11] != '': info = row[11].split(',') for j in info: bulletin_data = j.split('[') if len(bulletin_data) > 1: bulletin_id = bulletin_data[0] bulletin_kb = re.sub('^', 'KB', bulletin_data[1][:-1]) else: bulletin_id = bulletin_data[0] bulletin_kb = None supercede_list.append({ WindowsSecurityBulletinKey.SupersedesBulletinId: bulletin_id, WindowsSecurityBulletinKey.SupersedesBulletinKb: bulletin_kb }) bulletin_dict[WindowsSecurityBulletinKey.Supersedes] = supercede_list bulletin_dict[WindowsSecurityBulletinKey.Reboot] = row[12] bulletin_dict[WindowsSecurityBulletinKey.CveIds] = row[13].split(',') bulletin_list.append(bulletin_dict) return (bulletin_list)
def parse_spread_sheet(bulletin_file): bulletin_list = [] workbook = open_workbook(bulletin_file) sheet = workbook.sheet_by_name(WORKBOOK_SHEET) rows = range(sheet.nrows) rows.pop(0) for i in rows: row = sheet.row_values(i) bulletin_dict = {} supercede_list = [] if row[7] != '': row[7] = 'KB' + str(int(row[7])) if row[2] != '': row[2] = 'KB' + str(int(row[2])) rows_to_use = ( row[1] + row[2] + row[3] + row[4] + row[6] + row[7] + row[8] + row[9] ) rows_to_use = unicode(rows_to_use).encode(sys.stdout.encoding, 'replace') id = build_bulletin_id(rows_to_use) bulletin_dict[WindowsSecurityBulletinKey.Id] = id bulletin_dict[WindowsSecurityBulletinKey.DatePosted] = r.epoch_time(row[0]) bulletin_dict[WindowsSecurityBulletinKey.BulletinId] = row[1] bulletin_dict[WindowsSecurityBulletinKey.BulletinKb] = row[2] bulletin_dict[WindowsSecurityBulletinKey.BulletinSeverity] = row[3] bulletin_dict[WindowsSecurityBulletinKey.BulletinImpact] = row[4] bulletin_dict[WindowsSecurityBulletinKey.Title] = row[5] bulletin_dict[WindowsSecurityBulletinKey.AffectedProduct] = row[6] bulletin_dict[WindowsSecurityBulletinKey.ComponentKb] = row[7] bulletin_dict[WindowsSecurityBulletinKey.AffectedComponent] = row[8] bulletin_dict[WindowsSecurityBulletinKey.ComponentImpact] = row[9] bulletin_dict[WindowsSecurityBulletinKey.ComponentSeverity] = row[10] if row[11] != '': info = row[11].split(',') for j in info: bulletin_data = j.split('[') if len(bulletin_data) > 1: bulletin_id = bulletin_data[0] bulletin_kb = re.sub('^', 'KB', bulletin_data[1][:-1]) else: bulletin_id = bulletin_data[0] bulletin_kb = None supercede_list.append( { WindowsSecurityBulletinKey.SupersedesBulletinId: bulletin_id, WindowsSecurityBulletinKey.SupersedesBulletinKb: bulletin_kb } ) bulletin_dict[WindowsSecurityBulletinKey.Supersedes] = supercede_list bulletin_dict[WindowsSecurityBulletinKey.Reboot] = row[12] bulletin_dict[WindowsSecurityBulletinKey.CveIds] = row[13].split(',') bulletin_list.append(bulletin_dict) return(bulletin_list)
def add_or_update_applications(table=AppsPerAgentCollection, pkg_list=[], delete_afterwards=True, conn=None): completed = False inserted_count = 0 updated = None replaced_count = 0 deleted_count = 0 pkg_count = len(pkg_list) last_modified_time = mktime(datetime.now().timetuple()) if pkg_count > 0: for pkg in pkg_list: pkg['last_modified_time'] = r.epoch_time(last_modified_time) try: updated = (r.table(table).insert(pkg, upsert=True).run(conn)) logger.info(updated) inserted_count += updated['inserted'] replaced_count += updated['replaced'] except Exception as e: logger.exception(e) try: if delete_afterwards: deleted = (r.table(table).get_all( pkg[AppsPerAgentKey.AgentId], index=AppsPerAgentIndexes.AgentId).filter( r.row['last_modified_time'] < r.epoch_time( last_modified_time)).delete().run(conn)) deleted_count += deleted['deleted'] except Exception as e: logger.exception(e) return ({ 'pass': completed, 'inserted': inserted_count, 'replaced': replaced_count, 'deleted': deleted_count, 'pkg_count': pkg_count, })
def create_operation(self, operation, plugin, agent_ids, tag_id, cpu_throttle=None, net_throttle=None, restart=None, conn=None): keys_to_insert = ( { OperationKey.Plugin: plugin, OperationKey.Operation: operation, OperationKey.OperationStatus: OperationCodes.ResultsIncomplete, OperationKey.CustomerName: self.customer_name, OperationKey.CreatedBy: self.username, OperationKey.TagId: tag_id, OperationKey.AgentsTotalCount: len(agent_ids), OperationKey.AgentsPendingResultsCount: self.INIT_COUNT, OperationKey.AgentsPendingPickUpCount: len(agent_ids), OperationKey.AgentsFailedCount: self.INIT_COUNT, OperationKey.AgentsCompletedCount: self.INIT_COUNT, OperationKey.AgentsCompletedWithErrorsCount: self.INIT_COUNT, OperationKey.CreatedTime: self.db_time, OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: r.epoch_time(0.0), OperationKey.Restart: restart, OperationKey.CpuThrottle: cpu_throttle, OperationKey.NetThrottle: cpu_throttle, } ) try: added = ( r .table(OperationsCollection) .insert(keys_to_insert) .run(conn) ) if 'inserted' in added: operation_id = added.get('generated_keys')[0] keys_to_insert[OperationKey.CreatedTime] = self.now keys_to_insert[OperationKey.UpdatedTime] = self.now keys_to_insert[OperationKey.CompletedTime] = self.now keys_to_insert[OperationKey.OperationId] = operation_id results = ( OperationResults( self.username, self.uri, self.method ).operation_created(operation_id, operation, keys_to_insert) ) logger.info(results) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(operation_id, operation, e) ) logger.exception(results) return(results)
def update_supported_and_agent_apps(json_data, table=SupportedAppsCollection): if table == SupportedAppsCollection: CurrentAppsKey = SupportedAppsKey LatestDownloadedCollection = LatestDownloadedSupportedCollection AppType = 'supported_apps' elif table == AgentAppsCollection: CurrentAppsKey = AgentAppsKey LatestDownloadedCollection = LatestDownloadedAgentCollection AppType = 'agent_apps' try: rv_q = Queue('downloader', connection=rq_pkg_pool) conn = db_connect() inserted_count = 0 all_customers = (list( r.table(Collection.Customers).map( lambda x: x[CustomerKey.CustomerName]).run(conn))) for i in range(len(json_data)): json_data[i][CurrentAppsKey.Customers] = all_customers json_data[i][CurrentAppsKey.ReleaseDate] = r.epoch_time( json_data[i][CurrentAppsKey.ReleaseDate]) json_data[i][ CurrentAppsKey. FilesDownloadStatus] = PackageCodes.FilePendingDownload json_data[i][CurrentAppsKey.Hidden] = 'no' insert_data_into_table(json_data[i], LatestDownloadedCollection) file_data = json_data[i].get(CurrentAppsKey.FileData) insert_file_data(json_data[i][CurrentAppsKey.AppId], file_data) data_to_update = ({CurrentAppsKey.Customers: all_customers}) exists = (r.table(table).get( json_data[i][CurrentAppsKey.AppId]).run(conn)) if exists: updated = (r.table(table).get(json_data[i][ CurrentAppsKey.AppId]).update(data_to_update).run(conn)) else: updated = (r.table(table).insert(json_data[i]).run(conn)) rv_q.enqueue_call(func=download_all_files_in_app, args=(json_data[i][CurrentAppsKey.AppId], json_data[i][CurrentAppsKey.OsCode], None, file_data, 0, AppType), timeout=86400) inserted_count += updated['inserted'] conn.close() update_apps = IncomingSupportedOrAgentApps(table=table) update_apps.sync_supported_updates_to_all_agents(json_data) except Exception as e: logger.exception(e)
def get_entry_info(self, entry): data = {} attrib = entry.attrib data[CveKey.CveId] = attrib.get(CVE_ID) data[CveKey.CveName] = attrib.get(CVE_NAME) data[CveKey.CveSev] = attrib.get(CVE_SEVERITY) data[CveKey.CvePublishedDate] = (r.epoch_time( timestamp_verifier(date_parser(attrib.get(CVE_PUBLISHED_DATE))))) data[CveKey.CveModifiedDate] = (r.epoch_time( timestamp_verifier(date_parser(attrib.get(CVE_MODIFIED_DATE))))) data[CveKey.CvssScore] = attrib.get(CVSS_SCORE) data[CveKey.CvssBaseScore] = attrib.get(CVSS_BASE_SCORE) data[CveKey.CvssImpactSubScore] = attrib.get(CVSS_IMPACT_SUBSCORE) data[CveKey.CvssExploitSubScore] = attrib.get(CVSS_EXPLOIT_SUBSCORE) data[CveKey.CvssVector] = self._parse_vectors(attrib.get(CVSS_VECTOR)) data[CveKey.CvssVersion] = attrib.get(CVSS_VERSION) #if data[CveKey.CveId]: # print '\nFOOBAR\n' # print entry.tag # print type(entry.tag) # print data return (data)
def create_operation(self, operation, plugin, agent_ids, tag_id, cpu_throttle=None, net_throttle=None, restart=None, conn=None): keys_to_insert = ({ OperationKey.Plugin: plugin, OperationKey.Operation: operation, OperationKey.OperationStatus: OperationCodes.ResultsIncomplete, OperationKey.CustomerName: self.customer_name, OperationKey.CreatedBy: self.username, OperationKey.TagId: tag_id, OperationKey.AgentsTotalCount: len(agent_ids), OperationKey.AgentsPendingResultsCount: self.INIT_COUNT, OperationKey.AgentsPendingPickUpCount: len(agent_ids), OperationKey.AgentsFailedCount: self.INIT_COUNT, OperationKey.AgentsCompletedCount: self.INIT_COUNT, OperationKey.AgentsCompletedWithErrorsCount: self.INIT_COUNT, OperationKey.CreatedTime: self.db_time, OperationKey.UpdatedTime: self.db_time, OperationKey.CompletedTime: r.epoch_time(0.0), OperationKey.Restart: restart, OperationKey.CpuThrottle: cpu_throttle, OperationKey.NetThrottle: cpu_throttle, }) try: added = ( r.table(OperationsCollection).insert(keys_to_insert).run(conn)) if 'inserted' in added: operation_id = added.get('generated_keys')[0] keys_to_insert[OperationKey.CreatedTime] = self.now keys_to_insert[OperationKey.UpdatedTime] = self.now keys_to_insert[OperationKey.CompletedTime] = self.now keys_to_insert[OperationKey.OperationId] = operation_id results = (OperationResults(self.username, self.uri, self.method).operation_created( operation_id, operation, keys_to_insert)) logger.info(results) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( operation_id, operation, e)) logger.exception(results) return (results)
def update_agent(username, customer_name, uri, method, agent_id, system_info, hardware, rebooted, conn=None): """Add a node to the database""" agent_data = {} try: agent_orig_info = (r.table(AgentsCollection).get(agent_id).run(conn)) if agent_orig_info: agent_data[AgentKey.Hardware] = hardware for key, value in system_info.items(): agent_data[key] = value agent_data[AgentKey.LastAgentUpdate] = (r.epoch_time( mktime(datetime.now().timetuple()))) agent_data[AgentKey.HostName] = (agent_orig_info.get( AgentKey.HostName, None)) agent_data[AgentKey.DisplayName] = (agent_orig_info.get( AgentKey.DisplayName, None)) if rebooted == 'yes': agent_data[AgentKey.NeedsReboot] = 'no' (r.table(AgentsCollection).get(agent_id).update(agent_data).run( conn)) Hardware().add(agent_id, hardware) status = (AgentResults(username, uri, method).startup(agent_id, agent_data)) logger.info(status) else: status = (AgentResults(username, uri, method).startup_failed()) logger.warn(status) except Exception as e: status = (GenericResults(username, uri, method).something_broke( agent_id, 'startup', e)) logger.exception(status) return (status)
def update_agent_status(agentid, username, uri=None, method=None): try: conn = db_connect() update_status = { AgentKey.LastAgentUpdate: r.epoch_time( mktime( datetime.now() .timetuple() ) ), AgentKey.AgentStatus: 'up' } exists = ( r .table(AgentsCollection) .get(agentid) .run(conn) ) if exists: ( r .table(AgentsCollection) .get(agentid) .update(update_status) .run(conn) ) status = ( GenericResults( username, uri, method ).object_updated(agentid, 'agent', update_status) ) else: status = ( GenericResults( username, uri, method ).does_not_exists(agentid, 'agent') ) logger.info(status['message']) conn.close() except Exception as e: status = ( GenericResults( username, uri, method ).something_broke(agentid, 'agent', e) ) logger.exception(e)
def get_entry_info(self, entry): data = {} attrib = entry.attrib data[CveKey.CveId] = attrib.get(CVE_ID) data[CveKey.CveName] = attrib.get(CVE_NAME) data[CveKey.CveSev] = attrib.get(CVE_SEVERITY) data[CveKey.CvePublishedDate] = ( r.epoch_time( timestamp_verifier( date_parser( attrib.get(CVE_PUBLISHED_DATE) ) ) ) ) data[CveKey.CveModifiedDate] = ( r.epoch_time( timestamp_verifier( date_parser( attrib.get(CVE_MODIFIED_DATE) ) ) ) ) data[CveKey.CvssScore] = attrib.get(CVSS_SCORE) data[CveKey.CvssBaseScore] = attrib.get(CVSS_BASE_SCORE) data[CveKey.CvssImpactSubScore] = attrib.get(CVSS_IMPACT_SUBSCORE) data[CveKey.CvssExploitSubScore] = attrib.get(CVSS_EXPLOIT_SUBSCORE) data[CveKey.CvssVector] = self._parse_vectors(attrib.get(CVSS_VECTOR)) data[CveKey.CvssVersion] = attrib.get(CVSS_VERSION) #if data[CveKey.CveId]: # print '\nFOOBAR\n' # print entry.tag # print type(entry.tag) # print data return(data)
def __init__(self, table=SupportedAppsCollection): self.table = table if table == SupportedAppsCollection: self.CurrentAppsCollection = SupportedAppsCollection self.CurrentAppsPerAgentCollection = SupportedAppsPerAgentCollection self.CurrentAppsKey = SupportedAppsKey self.CurrentAppsPerAgentKey = SupportedAppsPerAgentKey self.CurrentAppsPerAgentIndexes = SupportedAppsPerAgentIndexes elif table == AgentAppsCollection: self.CurrentAppsCollection = AgentAppsCollection self.CurrentAppsPerAgentCollection = AgentAppsPerAgentCollection self.CurrentAppsKey = AgentAppsKey self.CurrentAppsPerAgentKey = AgentAppsPerAgentKey self.CurrentAppsPerAgentIndexes = AgentAppsPerAgentIndexes last_modified_time = mktime(datetime.now().timetuple()) self.last_modified_time = r.epoch_time(last_modified_time)
def _set_app_per_agent_properties(self, **kwargs): return ({ self.CurrentAppsPerAgentKey.AgentId: kwargs[AgentKey.AgentId], self.CurrentAppsPerAgentKey.CustomerName: kwargs[AgentKey.CustomerName], self.CurrentAppsPerAgentKey.Status: AVAILABLE, self.CurrentAppsPerAgentKey.LastModifiedTime: self.last_modified_time, self.CurrentAppsPerAgentKey.Update: PackageCodes.ThisIsAnUpdate, self.CurrentAppsPerAgentKey.InstallDate: r.epoch_time(0.0), self.CurrentAppsPerAgentKey.AppId: kwargs[self.CurrentAppsPerAgentKey.AppId], self.CurrentAppsPerAgentKey.Id: build_agent_app_id(kwargs[self.CurrentAppsPerAgentKey.AgentId], kwargs[self.CurrentAppsPerAgentKey.AppId]) })
def add_agent(username, customer_name, uri, method, system_info, hardware, conn=None): """Add a node to the database""" try: agent_data = {} agent_data[AgentKey.AgentStatus] = 'up' agent_data[AgentKey.MachineType] = 'physical' agent_data[AgentKey.Tags] = [] agent_data[AgentKey.NeedsReboot] = 'no' agent_data[AgentKey.DisplayName] = None agent_data[AgentKey.HostName] = None agent_data[AgentKey.CustomerName] = customer_name agent_data[AgentKey.Hardware] = hardware if not AgentKey.ProductionLevel in system_info: agent_data[AgentKey.ProductionLevel] = 'Production' if customer_name != 'default': cexists = (r.table(Collection.Customers).get( agent_data[AgentKey.CustomerName]).run(conn)) if not cexists and len(customer_name) >= 1: api.Customer.create(customer_name, username) for key, value in system_info.items(): agent_data[key] = value agent_data[AgentKey.LastAgentUpdate] = (r.epoch_time( mktime(datetime.now().timetuple()))) agent_added = (r.table(AgentsCollection).insert(agent_data).run(conn)) if 'inserted' in agent_added: if agent_added['inserted'] > 0: agent_id = agent_added['generated_keys'][0] Hardware().add(agent_id, agent_data[AgentKey.Hardware]) data = { AgentKey.AgentId: agent_id, AgentKey.CustomerName: agent_data[AgentKey.CustomerName], AgentKey.ComputerName: agent_data[AgentKey.ComputerName], AgentKey.Hardware: agent_data[AgentKey.Hardware], AgentKey.Tags: agent_data[AgentKey.Tags], AgentKey.OsCode: agent_data[AgentKey.OsCode], AgentKey.OsString: agent_data[AgentKey.OsString], } status = (AgentResults(username, uri, method).new_agent(agent_id, data)) logger.info(status['message']) else: status = (GenericResults(username, uri, method).something_broke( agentid, 'agent', agent_added)) logger.info(status['message']) except Exception as e: status = (GenericResults(username, uri, method).something_broke( 'new agent', 'agent', e)) logger.exception(status['message']) return (status)
def add_or_update_applications(table=AppsPerAgentCollection, pkg_list=[], delete_afterwards=True, conn=None): completed = False inserted_count = 0 updated = None replaced_count = 0 deleted_count = 0 pkg_count = len(pkg_list) last_modified_time = mktime(datetime.now().timetuple()) if table == AppsPerAgentCollection: CurrentAppsPerAgentKey = AppsPerAgentKey CurrentAppsPerAgentIndexes = AppsPerAgentIndexes elif table == CustomAppsPerAgentCollection: CurrentAppsPerAgentKey = CustomAppsPerAgentKey CurrentAppsPerAgentIndexes = CustomAppsPerAgentIndexes elif table == SupportedAppsPerAgentCollection: CurrentAppsPerAgentKey = SupportedAppsPerAgentKey CurrentAppsPerAgentIndexes = SupportedAppsPerAgentIndexes elif table == AgentAppsPerAgentCollection: CurrentAppsPerAgentKey = AgentAppsPerAgentKey CurrentAppsPerAgentIndexes = AgentAppsPerAgentIndexes if pkg_count > 0: for pkg in pkg_list: pkg['last_modified_time'] = r.epoch_time(last_modified_time) try: app_exists = ( r .table(table) .get(pkg[CurrentAppsPerAgentKey.Id]) .run(conn) ) if app_exists: updated = ( r .table(table) .get(pkg[CurrentAppsPerAgentKey.Id]) .update(pkg) .run(conn) ) else: updated = ( r .table(table) .insert(pkg) .run(conn) ) inserted_count += updated['inserted'] replaced_count += updated['replaced'] except Exception as e: logger.exception(e) try: if delete_afterwards: deleted = ( r .table(table) .get_all( pkg[CurrentAppsPerAgentKey.AgentId], index=CurrentAppsPerAgentIndexes.AgentId ) .filter( r.row['last_modified_time'] < r.epoch_time( last_modified_time) ) .delete() .run(conn) ) deleted_count += deleted['deleted'] except Exception as e: logger.exception(e) return( { 'pass': completed, 'inserted': inserted_count, 'replaced': replaced_count, 'deleted': deleted_count, 'pkg_count': pkg_count, } )
def update_supported_and_agent_apps(json_data, table=SupportedAppsCollection): if table == SupportedAppsCollection: CurrentAppsKey = SupportedAppsKey LatestDownloadedCollection = LatestDownloadedSupportedCollection AppType = 'supported_apps' elif table == AgentAppsCollection: CurrentAppsKey = AgentAppsKey LatestDownloadedCollection = LatestDownloadedAgentCollection AppType = 'agent_apps' try: rv_q = Queue('downloader', connection=rq_pkg_pool) conn = db_connect() inserted_count = 0 all_customers = ( list( r .table(Collection.Customers) .map(lambda x: x[CustomerKey.CustomerName]) .run(conn) ) ) for i in range(len(json_data)): json_data[i][CurrentAppsKey.Customers] = all_customers json_data[i][CurrentAppsKey.ReleaseDate] = r.epoch_time(json_data[i][CurrentAppsKey.ReleaseDate]) json_data[i][CurrentAppsKey.FilesDownloadStatus] = PackageCodes.FilePendingDownload json_data[i][CurrentAppsKey.Hidden] = 'no' insert_data_into_table(json_data[i], LatestDownloadedCollection) file_data = json_data[i].get(CurrentAppsKey.FileData) insert_file_data(json_data[i][CurrentAppsKey.AppId], file_data) data_to_update = ( { CurrentAppsKey.Customers: all_customers } ) exists = ( r .table(table) .get(json_data[i][CurrentAppsKey.AppId]) .run(conn) ) if exists: updated = ( r .table(table) .get(json_data[i][CurrentAppsKey.AppId]) .update(data_to_update) .run(conn) ) else: updated = ( r .table(table) .insert(json_data[i]) .run(conn) ) rv_q.enqueue_call( func=download_all_files_in_app, args=( json_data[i][CurrentAppsKey.AppId], json_data[i][CurrentAppsKey.OsCode], None, file_data, 0, AppType ), timeout=86400 ) inserted_count += updated['inserted'] conn.close() update_apps = IncomingSupportedOrAgentApps(table=table) update_apps.sync_supported_updates_to_all_agents(json_data) except Exception as e: logger.exception(e)
def add_or_update_applications(table=AppsPerAgentCollection, pkg_list=[], delete_afterwards=True, conn=None): completed = False inserted_count = 0 updated = None replaced_count = 0 deleted_count = 0 pkg_count = len(pkg_list) last_modified_time = mktime(datetime.now().timetuple()) if table == AppsPerAgentCollection: CurrentAppsPerAgentKey = AppsPerAgentKey CurrentAppsPerAgentIndexes = AppsPerAgentIndexes elif table == CustomAppsPerAgentCollection: CurrentAppsPerAgentKey = CustomAppsPerAgentKey CurrentAppsPerAgentIndexes = CustomAppsPerAgentIndexes elif table == SupportedAppsPerAgentCollection: CurrentAppsPerAgentKey = SupportedAppsPerAgentKey CurrentAppsPerAgentIndexes = SupportedAppsPerAgentIndexes elif table == AgentAppsPerAgentCollection: CurrentAppsPerAgentKey = AgentAppsPerAgentKey CurrentAppsPerAgentIndexes = AgentAppsPerAgentIndexes if pkg_count > 0: for pkg in pkg_list: pkg['last_modified_time'] = r.epoch_time(last_modified_time) try: app_exists = (r.table(table).get( pkg[CurrentAppsPerAgentKey.Id]).run(conn)) if app_exists: updated = (r.table(table).get( pkg[CurrentAppsPerAgentKey.Id]).update(pkg).run(conn)) else: updated = (r.table(table).insert(pkg).run(conn)) inserted_count += updated['inserted'] replaced_count += updated['replaced'] except Exception as e: logger.exception(e) try: if delete_afterwards: deleted = (r.table(table).get_all( pkg[CurrentAppsPerAgentKey.AgentId], index=CurrentAppsPerAgentIndexes.AgentId).filter( r.row['last_modified_time'] < r.epoch_time( last_modified_time)).delete().run(conn)) deleted_count += deleted['deleted'] except Exception as e: logger.exception(e) return ({ 'pass': completed, 'inserted': inserted_count, 'replaced': replaced_count, 'deleted': deleted_count, 'pkg_count': pkg_count, })
def add_agent(username, customer_name, uri, method, system_info, hardware, conn=None): """Add a node to the database""" try: agent_data = {} agent_data[AgentKey.AgentStatus] = 'up' agent_data[AgentKey.MachineType] = 'physical' agent_data[AgentKey.Tags] = [] agent_data[AgentKey.NeedsReboot] = 'no' agent_data[AgentKey.DisplayName] = None agent_data[AgentKey.HostName] = None agent_data[AgentKey.CustomerName] = customer_name agent_data[AgentKey.Hardware] = hardware if not AgentKey.ProductionLevel in system_info: agent_data[AgentKey.ProductionLevel] = 'Production' if customer_name != 'default': cexists = ( r .table(Collection.Customers) .get(agent_data[AgentKey.CustomerName]) .run(conn) ) if not cexists and len(customer_name) >= 1: api.Customer.create(customer_name, username) for key, value in system_info.items(): agent_data[key] = value agent_data[AgentKey.LastAgentUpdate] = ( r.epoch_time(mktime(datetime.now().timetuple())) ) agent_added = ( r .table(AgentsCollection) .insert(agent_data) .run(conn) ) if 'inserted' in agent_added: if agent_added['inserted'] > 0: agent_id = agent_added['generated_keys'][0] Hardware().add(agent_id, agent_data[AgentKey.Hardware]) data = { AgentKey.AgentId: agent_id, AgentKey.CustomerName: agent_data[AgentKey.CustomerName], AgentKey.ComputerName: agent_data[AgentKey.ComputerName], AgentKey.Hardware: agent_data[AgentKey.Hardware], AgentKey.Tags: agent_data[AgentKey.Tags], AgentKey.OsCode: agent_data[AgentKey.OsCode], AgentKey.OsString: agent_data[AgentKey.OsString], } status = ( AgentResults( username, uri, method ).new_agent(agent_id, data) ) logger.info(status['message']) else: status = ( GenericResults( username, uri, method ).something_broke(agentid, 'agent', agent_added) ) logger.info(status['message']) except Exception as e: status = ( GenericResults( username, uri, method ).something_broke('new agent', 'agent', e) ) logger.exception(status['message']) return(status)
def get_os_apps_history_for_tag( username, customer_name, uri, method, tag_id, status, start_date=None, end_date=None, conn=None ): try: if not start_date and not end_date: start_date = mktime((datetime.now() - timedelta(days=1 * 365)).timetuple()) end_date = mktime(datetime.now().timetuple()) elif start_date and not end_date: end_date = mktime(datetime.now().timetuple()) elif not start_date and end_date: start_date = 0.0 data = ( r.table(TagsPerAgentCollection, use_outdated=True) .get_all(tag_id, index=TagsPerAgentIndexes.TagId) .pluck(TagsPerAgentKey.AgentId) .eq_join( lambda x: [status, x[AppsPerAgentKey.AgentId]], r.table(AppsPerAgentCollection), index=AppsPerAgentIndexes.StatusAndAgentId, ) .zip() .eq_join(AppsKey.AppId, r.table(AppsCollection)) .zip() .filter(r.row[AppsKey.ReleaseDate].during(r.epoch_time(start_date), r.epoch_time(end_date))) .pluck(AppsKey.AppId, AppsKey.Name, AppsKey.Version, AppsKey.RvSeverity, AppsKey.ReleaseDate) .grouped_map_reduce( lambda x: x[AppsKey.ReleaseDate].to_epoch_time(), lambda x: { "details": [ { AppsKey.AppId: x[AppsKey.AppId], AppsKey.Name: x[AppsKey.Name], AppsKey.Version: x[AppsKey.Version], AppsKey.RvSeverity: x[AppsKey.RvSeverity], } ], COUNT: 1, }, lambda x, y: {"count": x["count"] + y["count"], "details": x["details"] + y["details"]}, ) .map( { "timestamp": r.row["group"], "total_count": r.row["reduction"]["count"], "details": ( r.row["reduction"]["details"].grouped_map_reduce( lambda a: a["rv_severity"], lambda a: { "apps": [ { AppsKey.AppId: a[AppsKey.AppId], AppsKey.Name: a[AppsKey.Name], AppsKey.Version: a[AppsKey.Version], } ], COUNT: 1, }, lambda a, b: {"count": a["count"] + b["count"], "apps": a["apps"] + b["apps"]}, ) ), } ) .run(conn) ) results = GenericResults(username, uri, method).information_retrieved(data, len(data)) except Exception as e: results = GenericResults(username, uri, method).something_broke("available apps over time graph", "graph", e) logger.exception(results) return results
def update_agent(username, customer_name, uri, method, agent_id, system_info, hardware, rebooted, conn=None): """Add a node to the database""" agent_data = {} try: agent_orig_info = ( r .table(AgentsCollection) .get(agent_id) .run(conn) ) if agent_orig_info: agent_data[AgentKey.Hardware] = hardware for key, value in system_info.items(): agent_data[key] = value agent_data[AgentKey.LastAgentUpdate] = ( r.epoch_time(mktime(datetime.now().timetuple())) ) agent_data[AgentKey.HostName] = ( agent_orig_info.get(AgentKey.HostName, None) ) agent_data[AgentKey.DisplayName] = ( agent_orig_info.get(AgentKey.DisplayName, None) ) if rebooted == 'yes': agent_data[AgentKey.NeedsReboot] = 'no' ( r .table(AgentsCollection) .get(agent_id) .update(agent_data) .run(conn) ) Hardware().add(agent_id, hardware) status = ( AgentResults( username, uri, method ).startup(agent_id, agent_data) ) logger.info(status) else: status = ( AgentResults( username, uri, method ).startup_failed() ) logger.warn(status) except Exception as e: status = ( GenericResults( username, uri, method ).something_broke(agent_id, 'startup', e) ) logger.exception(status) return(status)
def _update_app_status(self, data, oper_type): try: if self.reboot_required == 'true': update_agent_field(self.agent_id, 'needs_reboot', 'yes', self.username) if self.success == 'true' and re.search(r'^install', oper_type): status = INSTALLED install_date = self.date_now elif self.success == 'true' and re.search(r'^uninstall', oper_type): status = AVAILABLE install_date = 0.0 elif self.success == 'false' and re.search(r'^install', oper_type): status = AVAILABLE install_date = 0.0 elif self.success == 'false' and re.search(r'^uninstall', oper_type): status = INSTALLED install_date = 0.0 app_exist = get_app_data(self.app_id, table=self.CurrentAppsCollection) oper_app_exists = (oper_with_appid_exists(self.operation_id, self.agent_id, self.app_id)) if app_exist: if oper_app_exists and self.success == 'true': results = (self.operation.update_app_results( self.operation_id, self.agent_id, self.app_id, OperationCodes.ResultsReceived, errors=self.error)) if self.apps_to_delete: try: self.apps_to_delete = [ loads(x) for x in self.apps_to_delete ] except Exception as e: logger.exception(e) for apps in self.apps_to_delete: delete_app_from_agent(apps[NAME], apps[VERSION], self.agent_id) if self.apps_to_add: try: self.apps_to_add = [ loads(x) for x in self.apps_to_add ] except Exception as e: logger.exception(e) incoming_packages_from_agent( self.username, self.agent_id, self.customer_name, self.agent_data[AgentKey.OsCode], self.agent_data[AgentKey.OsString], self.apps_to_add, delete_afterwards=False) elif oper_app_exists and self.success == 'false': results = (self.operation.update_app_results( self.operation_id, self.agent_id, self.app_id, OperationCodes.ResultsReceivedWithErrors, errors=self.error)) else: results = (GenericResults(self.username, self.uri, self.method).invalid_id( self.operation_id, oper_type)) data_to_update = ({ self.CurrentAppsPerAgentKey.Status: status, self.CurrentAppsPerAgentKey.InstallDate: r.epoch_time(install_date) }) if oper_type == INSTALL_OS_APPS or oper_type == UNINSTALL: update_os_app_per_agent(self.agent_id, self.app_id, data_to_update) elif oper_type == INSTALL_CUSTOM_APPS: update_custom_app_per_agent(self.agent_id, self.app_id, data_to_update) elif oper_type == INSTALL_SUPPORTED_APPS: update_supported_app_per_agent(self.agent_id, self.app_id, data_to_update) elif oper_type == INSTALL_AGENT_UPDATE: update_agent_app_per_agent(self.agent_id, self.app_id, data_to_update) else: results = (GenericResults(self.username, self.uri, self.method).invalid_id( self.operation_id, oper_type)) except Exception as e: results = (GenericResults(self.username, self.uri, self.method).something_broke( self.operation_id, oper_type, e)) logger.exception(results) return (results)
def get_os_apps_history_for_tag(username, customer_name, uri, method, tag_id, status, start_date=None, end_date=None, conn=None): try: if not start_date and not end_date: start_date = mktime((datetime.now() - timedelta(days=1*365)).timetuple()) end_date = mktime(datetime.now().timetuple()) elif start_date and not end_date: end_date = mktime(datetime.now().timetuple()) elif not start_date and end_date: start_date = 0.0 data = ( r .table(TagsPerAgentCollection, use_outdated=True) .get_all(tag_id, index=TagsPerAgentIndexes.TagId) .pluck(TagsPerAgentKey.AgentId) .eq_join( lambda x: [ status, x[AppsPerAgentKey.AgentId] ], r.table(AppsPerAgentCollection), index=AppsPerAgentIndexes.StatusAndAgentId ) .zip() .eq_join(AppsKey.AppId, r.table(AppsCollection)) .zip() .filter( r.row[AppsKey.ReleaseDate].during( r.epoch_time(start_date), r.epoch_time(end_date) ) ) .pluck( AppsKey.AppId, AppsKey.Name, AppsKey.Version, AppsKey.RvSeverity, AppsKey.ReleaseDate ) .grouped_map_reduce( lambda x: x[AppsKey.ReleaseDate].to_epoch_time(), lambda x: { 'details': [ { AppsKey.AppId: x[AppsKey.AppId], AppsKey.Name: x[AppsKey.Name], AppsKey.Version: x[AppsKey.Version], AppsKey.RvSeverity: x[AppsKey.RvSeverity] } ], COUNT: 1, }, lambda x, y: { "count": x["count"] + y["count"], "details": x["details"] + y["details"], } ) .map( { 'timestamp': r.row['group'], 'total_count': r.row['reduction']['count'], 'details': ( r.row['reduction']['details'].grouped_map_reduce( lambda a: a['rv_severity'], lambda a: { 'apps': [ { AppsKey.AppId: a[AppsKey.AppId], AppsKey.Name: a[AppsKey.Name], AppsKey.Version: a[AppsKey.Version], } ], COUNT: 1 }, lambda a, b: { "count": a["count"] + b["count"], "apps": a["apps"] + b["apps"], } ) ) } ) .run(conn) ) results = ( GenericResults( username, uri, method ).information_retrieved(data, len(data)) ) except Exception as e: results = ( GenericResults( username, uri, method ).something_broke('available apps over time graph', 'graph', e) ) logger.exception(results) return(results)
def _update_app_status(self, data, oper_type): try: if self.reboot_required == 'true': update_agent_field( self.agent_id, 'needs_reboot', 'yes', self.username ) if self.success == 'true' and re.search(r'^install', oper_type): status = INSTALLED install_date = self.date_now elif self.success == 'true' and re.search(r'^uninstall', oper_type): status = AVAILABLE install_date = 0.0 elif self.success == 'false' and re.search(r'^install', oper_type): status = AVAILABLE install_date = 0.0 elif self.success == 'false' and re.search(r'^uninstall', oper_type): status = INSTALLED install_date = 0.0 app_exist = get_app_data(self.app_id, table=self.CurrentAppsCollection) oper_app_exists = ( oper_with_appid_exists( self.operation_id, self.agent_id, self.app_id ) ) if app_exist: if oper_app_exists and self.success == 'true': results = ( self.operation.update_app_results( self.operation_id, self.agent_id, self.app_id, OperationCodes.ResultsReceived, errors=self.error ) ) if self.apps_to_delete: try: self.apps_to_delete = [loads(x) for x in self.apps_to_delete] except Exception as e: logger.exception(e) for apps in self.apps_to_delete: delete_app_from_agent( apps[NAME], apps[VERSION], self.agent_id ) if self.apps_to_add: try: self.apps_to_add = [loads(x) for x in self.apps_to_add] except Exception as e: logger.exception(e) incoming_packages_from_agent( self.username, self.agent_id, self.customer_name, self.agent_data[AgentKey.OsCode], self.agent_data[AgentKey.OsString], self.apps_to_add, delete_afterwards=False ) elif oper_app_exists and self.success == 'false': results = ( self.operation.update_app_results( self.operation_id, self.agent_id, self.app_id, OperationCodes.ResultsReceivedWithErrors, errors=self.error ) ) else: results = ( GenericResults( self.username, self.uri, self.method ).invalid_id(self.operation_id, oper_type) ) data_to_update = ( { self.CurrentAppsPerAgentKey.Status: status, self.CurrentAppsPerAgentKey.InstallDate: r.epoch_time(install_date) } ) if oper_type == INSTALL_OS_APPS or oper_type == UNINSTALL: update_os_app_per_agent(self.agent_id, self.app_id, data_to_update) elif oper_type == INSTALL_CUSTOM_APPS: update_custom_app_per_agent(self.agent_id, self.app_id, data_to_update) elif oper_type == INSTALL_SUPPORTED_APPS: update_supported_app_per_agent(self.agent_id, self.app_id, data_to_update) elif oper_type == INSTALL_AGENT_UPDATE: update_agent_app_per_agent(self.agent_id, self.app_id, data_to_update) else: results = ( GenericResults( self.username, self.uri, self.method ).invalid_id(self.operation_id, oper_type) ) except Exception as e: results = ( GenericResults( self.username, self.uri, self.method ).something_broke(self.operation_id, oper_type, e) ) logger.exception(results) return(results)