def list(self, action, **kwargs): '''Return list of saved components''' output = [] user = cherrypy.session['user']['name'] app = cherrypy.request.path_info.split('/')[3] components = Components.all() components = components.filter_by_app(app) logger.info("number of components found: %d" % len(components)) for component in components: h = component.toJsonable() output.append(h) return self.render_json(output)
class HadoopOpsComponents(controllers.BaseController): ''' Hadoop Ops Components Controller ''' # keep track of currently impacted components components_created = [] components_updated = [] components_deleted = [] @route('/:action=discover') @expose_page(must_login=True, methods=['GET']) def discover(self, action, **kwargs): '''Discover & return all current components, i.e. host-service pairs''' user = cherrypy.session['user']['name'] app = cherrypy.request.path_info.split('/')[3] output_mode = kwargs.get('output_mode', 'html') #apply hardcoded search #sessionKey = splunk.auth.getSessionKey("admin", "changed") #searchcmd = "`hadoop_ps` | kv hadoop_service_type | rename service_type as services | stats count by host services" #search = splunk.search.searchAll(searchcmd) # dispatch saved search start = time.time() try: job = splunk.saved.dispatchSavedSearch( '__retrieve_live_components', None, app, 'admin') except Exception, e: logger.error('failed to dispatch discovery search: %s' % job) return self.render_json({ 'success': False, 'error': 'discovery search dispatch failed' }) componentsEnabled = Components.all() componentsEnabled = componentsEnabled.filter_by_app(app) componentsEnabledDict = {} for component in componentsEnabled: hostService = '%s|%s' % (component.host, component.service) componentsEnabledDict[hostService] = component.toJsonable() componentsEnabledDict[hostService]['found'] = False logger.info(str(time.time() - start) + ' - waiting for results...') splunk.search.waitForJob(job, maxtime=60) logger.info( str(time.time() - start) + ' - results returned ' + str(job.resultCount)) # if job not done after 60s, timeout & show error page so process is not blocked if not job.isDone: return self.render_template( '/%s:/templates/components_manage_fail.html' % app, dict(app=app)) componentsCurrent = [] jobFeed = job.getFeed(mode='results', count=0) if jobFeed: componentsCurrent = json.loads(jobFeed) # workaround for a 5.0 breaking change of response format of API search/jobs/<search_id>/results if isinstance(componentsCurrent, dict): componentsCurrent = componentsCurrent['results'] current_time = self.get_current_timestamp() for component in componentsCurrent: hostService = '%s|%s' % (component['host'], component['service']) if hostService in componentsEnabledDict: component['status'] = 'up' component.update(componentsEnabledDict[hostService]) del (component['found']) componentsEnabledDict[hostService]['found'] = True else: component['status'] = 'new' component['created_at'] = current_time for key in componentsEnabledDict: component = componentsEnabledDict[key] if component['found'] is False: component['status'] = 'down' del (component['found']) componentsCurrent.append(component) componentsCurrent.sort(key=lambda x: x['created_at'], reverse=True) if output_mode == 'json': return self.render_json(componentsCurrent) else: return self.render_template( '/%s:/templates/components_manage.html' % app, dict(components=componentsCurrent, app=app))