def handle_exceptions(self, e): ''' Generic method to handle exceptions. May be overriden by inherited classes. ''' try: if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (self.instance.agent_address, self.instance.agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': self.role, 'instance': self.instance, 'code': e.code, 'error': e.message }) except Exception as e: self.logger.error(str(e))
def get_index(self): try: self.setUp() self.check_admin() instance_list = get_instance_list(self.db_session) self.tearDown(commit=False) return HTMLAsyncResult(200, None, { 'nav': True, 'role': self.current_user, 'instance_list': instance_list }, template_file='settings/instance.html') except (TemboardUIError, Exception) as e: self.logger.error(str(e)) try: self.db_session.close() except Exception: pass if isinstance(e, TemboardUIError): if e.code == 302: return HTMLAsyncResult(302, '/login') elif e.code == 401: return HTMLAsyncResult(401, None, {'nav': False}, template_file='unauthorized.html') return HTMLAsyncResult(500, None, { 'nav': False, 'error': e.message }, template_file='settings/error.html')
def get_login(self, agent_address, agent_port): try: instance = None role = None agent_username = None self.start_db_session() try: self.load_auth_cookie() role = self.current_user except Exception as e: pass if role is None: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if xsession: try: data_profile = temboard_profile(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) agent_username = data_profile['username'] self.logger.error(agent_username) except Exception as e: self.logger.exception(str(e)) return HTMLAsyncResult(http_code=200, template_file='agent-login.html', data={ 'nav': True, 'instance': instance, 'role': role, 'username': agent_username }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) try: self.db_session.close() except Exception as e: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'instance': instance, 'code': str(e.code), 'message': str(e.message) })
def post_login(self, agent_address, agent_port): try: self.logger.info("Posting to agent login.") instance = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = temboard_login(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, self.get_argument("username"), self.get_argument("password")) self.set_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port), xsession, expires_days=0.5) self.logger.info("Done.") redirection = self.get_secure_cookie('referer_uri') \ if self.get_secure_cookie('referer_uri') is not None \ else "/server/%s/%s/dashboard" % (instance.agent_address, instance.agent_port) return HTMLAsyncResult(http_code=302, redirection=redirection) except (TemboardError, TemboardUIError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.rollback() self.db_session.close() except Exception as e: pass return HTMLAsyncResult(http_code=200, template_file='agent-login.html', data={ 'nav': True, 'error': e.message, 'instance': instance, 'username': None })
def get_login(self): role = None try: self.load_auth_cookie() self.start_db_session() role = self.current_user self.db_session.close() except Exception as e: self.logger.exception(str(e)) if role is not None: return HTMLAsyncResult(http_code=302, redirection='/home') return HTMLAsyncResult(http_code=200, template_file='login.html', data={'nav': False})
def get_index(self, agent_address, agent_port): self.setUp(agent_address, agent_port) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) # Here we want to get the current agent username if a session # already exists. # Monitoring plugin doesn't require agent authentication since we # already have the data. # Don't fail if there's a session error (for example when the agent # has been restarted) agent_username = None try: if xsession: data_profile = temboard_profile(self.ssl_ca_cert_file, agent_address, agent_port, xsession) agent_username = data_profile['username'] except TemboardError: pass return HTMLAsyncResult( http_code=200, template_path=self.template_path, template_file='alerting.checks.html', data={ 'nav': True, 'role': self.role, 'instance': self.instance, 'plugin': 'alerting', # we cheat here 'agent_username': agent_username })
def get_activity(self, agent_address, agent_port, mode): self.logger.info("Getting activity.") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, agent_address, agent_port, xsession) agent_username = data_profile['username'] self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='activity.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'mode': mode, 'xsession': xsession, 'agent_username': agent_username, })
def get_notifications(self, agent_address, agent_port): self.logger.info("Getting notifications.") self.setUp(agent_address, agent_port) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, agent_address, agent_port, xsession) agent_username = data_profile['username'] # Load notifications. notifications = temboard_get_notifications(self.ssl_ca_cert_file, agent_address, agent_port, xsession) self.tearDown(commit=False) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_file='notifications.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': 'notifications', 'notifications': notifications, 'xsession': xsession, 'agent_username': agent_username, })
def get_configuration_file(self, agent_address, agent_port): self.logger.info("Getting configuration (file).") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") # Load file content. file_content = temboard_get_file_content(self.ssl_ca_cert_file, self.file_type, agent_address, agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='edit_file.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'file_type': self.file_type, 'file_content': file_content, 'xsession': xsession })
def get_dashboard(self, agent_address, agent_port): self.logger.info("Getting dashboard.") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, agent_address, agent_port, xsession) agent_username = data_profile['username'] try: config = temboard_dashboard_config( self.ssl_ca_cert_file, agent_address, agent_port, xsession) except TemboardError as e: # Agent may not be able to send config (old agent) # Use a default one if e.code == 404: config = { 'history_length': 150, 'scheduler_interval': 2 } else: raise e dashboard_history = temboard_dashboard_history( self.ssl_ca_cert_file, agent_address, agent_port, xsession) if dashboard_history and isinstance( dashboard_history, list) and len(dashboard_history) > 0: last_data = dashboard_history[-1] history = json.dumps(dashboard_history) else: # If dashboard history is empty, let's try to get data from the # live data source. last_data = temboard_dashboard_live( self.ssl_ca_cert_file, agent_address, agent_port, xsession) history = '' self.logger.info("Done.") return HTMLAsyncResult( http_code=200, template_file='dashboard.html', template_path=self.template_path, data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'dashboard': last_data, 'config': json.dumps(config), 'history': history, 'buffers_delta': 0, 'readratio': (100 - last_data['hitratio']), 'xsession': xsession, 'agent_username': agent_username, })
def get_configuration(self, agent_address, agent_port, category=None): self.logger.info("Getting configuration.") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, agent_address, agent_port, xsession) agent_username = data_profile['username'] configuration_status = temboard_get_configuration_status( self.ssl_ca_cert_file, agent_address, agent_port, xsession) configuration_cat = temboard_get_configuration_categories( self.ssl_ca_cert_file, agent_address, agent_port, xsession) query_filter = self.get_argument('filter', None, True) if category is None: category = tornado.escape.url_escape( configuration_cat['categories'][0]) url = tornado.escape.url_escape(tornado.escape.url_unescape(category)) configuration_data = temboard_get_configuration( self.ssl_ca_cert_file, agent_address, agent_port, xsession, url, query_filter) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='configuration.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'data': configuration_data, 'xsession': xsession, 'agent_username': agent_username, 'current_cat': tornado.escape.url_unescape(category), 'configuration_categories': configuration_cat, 'configuration_status': configuration_status, 'query_filter': query_filter })
def get_index(self, agent_address, agent_port, check_name): self.setUp(agent_address, agent_port) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if check_name not in check_specs: raise TemboardUIError(404, "Unknown check '%s'" % check_name) # Here we want to get the current agent username if a session # already exists. # Monitoring plugin doesn't require agent authentication since we # already have the data. # Don't fail if there's a session error (for example when the agent # has been restarted) agent_username = None try: if xsession: data_profile = temboard_profile(self.ssl_ca_cert_file, agent_address, agent_port, xsession) agent_username = data_profile['username'] except TemboardError: pass # Find host_id & instance_id self.host_id = get_host_id(self.db_session, self.instance.hostname) self.instance_id = get_instance_id(self.db_session, self.host_id, self.instance.pg_port) query = """ SELECT * FROM monitoring.checks WHERE host_id = :host_id AND instance_id = :instance_id AND name = :check_name """ res = self.db_session.execute( query, dict(host_id=self.host_id, instance_id=self.instance_id, check_name=check_name)) check = res.fetchone() spec = check_specs[check_name] return HTMLAsyncResult( http_code=200, template_path=self.template_path, template_file='alerting.check.html', data={ 'nav': True, 'role': self.role, 'instance': self.instance, 'check': check, 'value_type': spec.get('value_type', None), 'plugin': 'alerting', # we cheat here 'agent_username': agent_username })
def handle_exceptions(self, e): try: self.db_session.close() except Exception: pass if isinstance(e, TemboardUIError): if e.code == 302: return HTMLAsyncResult(302, '/login') elif e.code == 401: return HTMLAsyncResult(401, None, {'nav': False}, template_file='unauthorized.html') return HTMLAsyncResult(500, None, { 'nav': False, 'error': e.message }, template_file='error.html')
def get_home(self): try: self.logger.info("Loading home.") self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, 'Current role unknown.') instance_list = get_instances_by_role_name(self.db_session, role.role_name) self.db_session.expunge_all() self.db_session.commit() self.db_session.close() self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_file='home.html', data={ 'nav': True, 'role': role, 'instance_list': instance_list }) except (TemboardUIError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if isinstance(e, TemboardUIError): if e.code == 302: return HTMLAsyncResult(302, '/login') elif e.code == 401: return HTMLAsyncResult(401, None, {'nav': False}, template_file='unauthorized.html') return HTMLAsyncResult(500, None, { 'nav': False, 'error': e.message }, template_file='error.html')
def post_login(self): try: self.logger.info("Login.") p_role_name = self.get_argument('username') p_role_password = self.get_argument('password') role_hash_password = hash_password(p_role_name, p_role_password) self.start_db_session() role = get_role_by_auth(self.db_session, p_role_name, role_hash_password) self.logger.info("Role '%s' authentificated." % (role.role_name)) self.db_session.expunge_all() self.db_session.commit() self.db_session.close() sleep(1) self.logger.info("Done.") redirection = self.get_secure_cookie('referer_uri') \ if self.get_secure_cookie('referer_uri') is not None \ else '/home' return HTMLAsyncResult(http_code=302, redirection=redirection, secure_cookie={ 'name': 'temboard', 'content': gen_cookie(role.role_name, role_hash_password) }) except (TemboardUIError, Exception) as e: try: self.db_session.rollback() self.db_session.close() except Exception: pass self.logger.exception(str(e)) self.logger.info("Failed.") sleep(1) return HTMLAsyncResult(http_code=401, template_file='login.html', data={ 'nav': False, 'error': 'Wrong username/password.' })
def get_index(self, group_kind): try: self.logger.info("Group list.") self.load_auth_cookie() self.start_db_session() self.check_admin() group_list = get_group_list(self.db_session, group_kind) self.logger.debug(group_list) self.db_session.expunge_all() self.db_session.commit() self.db_session.close() self.logger.info("Done.") return HTMLAsyncResult(200, None, { 'nav': True, 'role': self.current_user, 'group_list': group_list, 'group_kind': group_kind }, template_file='settings/group.html') except (TemboardUIError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.rollback() self.db_session.close() except Exception: pass if isinstance(e, TemboardUIError): if e.code == 302: return HTMLAsyncResult(302, '/login') elif e.code == 401: return HTMLAsyncResult(401, None, {'nav': False}, template_file='unauthorized.html') return HTMLAsyncResult(500, None, { 'nav': False, 'error': e.message }, template_file='settings/error.html')
def post_configuration_file(self, agent_address, agent_port): error_code = None error_message = None ret_post = None self.logger.info("Posting configuration (file).") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") try: # Send file content .. ret_post = temboard_post_file_content( self.ssl_ca_cert_file, self.file_type, agent_address, agent_port, xsession, { 'content': self.request.arguments['content'][0], 'new_version': True }) # .. and reload configuration. ret_post = temboard_post_administration_control( self.ssl_ca_cert_file, agent_address, agent_port, xsession, {'action': 'reload'}) except (TemboardError, Exception) as e: self.logger.exception(str(e)) if isinstance(e, TemboardError): error_code = e.code error_message = e.message else: error_code = 500 error_message = "Internale error." # Load file content. file_content = temboard_get_file_content(self.ssl_ca_cert_file, self.file_type, agent_address, agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='edit_file.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'file_type': self.file_type, 'file_content': file_content, 'error_code': error_code, 'error_message': error_message, 'xsession': xsession, 'ret_post': ret_post })
def get_home(self): self.logger.info("Loading home.") self.setUp() role = self.current_user if not role: raise TemboardUIError(302, 'Current role unknown.') instance_list = get_instances_by_role_name(self.db_session, role.role_name) self.tearDown(commit=False) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_file='home.html', data={ 'nav': True, 'role': role, 'instance_list': instance_list })
def post_configuration_file(self, agent_address, agent_port): error_code = None error_message = None ret_post = None try: self.logger.info("Posting configuration (file).") instance = None role = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") if __name__ not in [ plugin.plugin_name for plugin in instance.plugins ]: raise TemboardUIError(408, "Plugin not active.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") try: # Send file content .. ret_post = temboard_post_file_content( self.ssl_ca_cert_file, self.file_type, instance.agent_address, instance.agent_port, xsession, { 'content': self.request.arguments['content'][0], 'new_version': True }) # .. and reload configuration. ret_post = temboard_post_administration_control( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession, {'action': 'reload'}) except (TemboardError, Exception) as e: self.logger.exception(str(e)) if isinstance(TemboardError, e): error_code = e.code error_message = e.message else: error_code = 500 error_message = "Internale error." # Load file content. file_content = temboard_get_file_content(self.ssl_ca_cert_file, self.file_type, instance.agent_address, instance.agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='edit_file.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'pgconf', 'file_type': self.file_type, 'file_content': file_content, 'error_code': error_code, 'error_message': error_message, 'xsession': xsession, 'ret_post': ret_post }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (agent_address, agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })
def post_configuration(self, agent_address, agent_port, category=None): self.logger.info("Posting configuration.") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") query_filter = self.get_argument('filter', None, True) error_code = None error_message = None post_settings = self.request.arguments ret_post = None settings = {'settings': []} for setting_name, setting_value in post_settings.iteritems(): # 'filter' is not a setting, just ignore it. if setting_name == 'filter': continue settings['settings'].append({ 'name': setting_name, 'setting': setting_value[0] }) try: # Try to send settings to the agent. ret_post = temboard_post_configuration(self.ssl_ca_cert_file, agent_address, agent_port, xsession, settings) except TemboardError as e: error_code = e.code error_message = e.message # Get PostgreSQL configuration status: needs restart, reload or is # fine. configuration_status = temboard_get_configuration_status( self.ssl_ca_cert_file, agent_address, agent_port, xsession) # Load settings categories. configuration_cat = temboard_get_configuration_categories( self.ssl_ca_cert_file, agent_address, agent_port, xsession) if category is None: category = tornado.escape.url_escape( configuration_cat['categories'][0]) # Load settings depending on the current category or the filter # value. url = tornado.escape.url_escape(tornado.escape.url_unescape(category)) configuration_data = temboard_get_configuration( self.ssl_ca_cert_file, agent_address, agent_port, xsession, url, query_filter) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='configuration.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'data': configuration_data, 'xsession': xsession, 'current_cat': tornado.escape.url_unescape(category), 'configuration_categories': configuration_cat, 'configuration_status': configuration_status, 'error_code': error_code, 'error_message': error_message, 'ret_post': ret_post, 'query_filter': query_filter })
def get_configuration_file(self, agent_address, agent_port): self.logger.info("Getting configuration (file).") self.setUp(agent_address, agent_port) self.check_active_plugin(__name__) mode = self.get_argument('mode', None) version = self.get_argument('version', None) if mode is None and len(self.available_modes) > 0: mode = self.available_modes[0] if not (mode in self.available_modes): raise TemboardUIError(404, "Editing mode not available.") xsession = self.get_secure_cookie("temboard_%s_%s" % (agent_address, agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") file_versions = temboard_get_conf_file_versions( self.ssl_ca_cert_file, self.file_type, agent_address, agent_port, xsession) if mode == 'raw': # Load file content. conf_file_raw = temboard_get_conf_file_raw(self.ssl_ca_cert_file, self.file_type, version, agent_address, agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='edit_conf_file_raw.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'file_versions': file_versions, 'file_type': self.file_type, 'conf_file_raw': conf_file_raw, 'xsession': xsession }) if mode == 'advanced': hba_options = None if self.file_type == 'hba': hba_options = temboard_get_hba_options(self.ssl_ca_cert_file, agent_address, agent_port, xsession) conf_file = temboard_get_conf_file(self.ssl_ca_cert_file, self.file_type, version, agent_address, agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult( http_code=200, template_path=self.template_path, template_file='edit_conf_file_advanced.html', data={ 'nav': True, 'role': self.current_user, 'instance': self.instance, 'plugin': __name__, 'file_versions': file_versions, 'file_type': self.file_type, 'conf_file': conf_file, 'hba_options': hba_options, 'xsession': xsession })
def post_configuration(self, agent_address, agent_port, category=None): try: self.logger.info("Posting configuration.") instance = None role = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") if __name__ not in [ plugin.plugin_name for plugin in instance.plugins ]: raise TemboardUIError(408, "Plugin not active.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") query_filter = self.get_argument('filter', None, True) error_code = None error_message = None post_settings = self.request.arguments ret_post = None settings = {'settings': []} for setting_name, setting_value in post_settings.iteritems(): # 'filter' is not a setting, just ignore it. if setting_name == 'filter': continue settings['settings'].append({ 'name': setting_name, 'setting': setting_value[0] }) try: # Try to send settings to the agent. ret_post = temboard_post_configuration(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession, settings) except TemboardError as e: error_code = e.code error_message = e.message # Get PostgreSQL configuration status: needs restart, reload or is # fine. configuration_status = temboard_get_configuration_status( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) # Load settings categories. configuration_cat = temboard_get_configuration_categories( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) if category is None: category = tornado.escape.url_escape( configuration_cat['categories'][0]) # Load settings depending on the current category or the filter # value. url = tornado.escape.url_escape( tornado.escape.url_unescape(category)) configuration_data = temboard_get_configuration( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession, url, query_filter) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='configuration.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'pgconf', 'data': configuration_data, 'xsession': xsession, 'current_cat': tornado.escape.url_unescape(category), 'configuration_categories': configuration_cat, 'configuration_status': configuration_status, 'error_code': error_code, 'error_message': error_message, 'ret_post': ret_post, 'query_filter': query_filter }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (agent_address, agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })
def get_notifications(self, agent_address, agent_port): try: self.logger.info("Getting notifications.") instance = None role = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) agent_username = data_profile['username'] # Load notifications. notifications = temboard_get_notifications(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_file='notifications.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'notifications', 'notifications': notifications, 'xsession': xsession, 'agent_username': agent_username, }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (agent_address, agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })
def get_configuration_file(self, agent_address, agent_port): try: self.logger.info("Getting configuration (file).") instance = None role = None self.load_auth_cookie() self.start_db_session() mode = self.get_argument('mode', None) version = self.get_argument('version', None) role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") if mode is None and len(self.available_modes) > 0: mode = self.available_modes[0] if not (mode in self.available_modes): raise TemboardUIError(404, "Editing mode not available.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") if __name__ not in [ plugin.plugin_name for plugin in instance.plugins ]: raise TemboardUIError(408, "Plugin not active.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") file_versions = temboard_get_conf_file_versions( self.ssl_ca_cert_file, self.file_type, instance.agent_address, instance.agent_port, xsession) if mode == 'raw': # Load file content. conf_file_raw = temboard_get_conf_file_raw( self.ssl_ca_cert_file, self.file_type, version, instance.agent_address, instance.agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='edit_conf_file_raw.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'pgconf', 'file_versions': file_versions, 'file_type': self.file_type, 'conf_file_raw': conf_file_raw, 'xsession': xsession }) if mode == 'advanced': hba_options = None if self.file_type == 'hba': hba_options = temboard_get_hba_options( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) conf_file = temboard_get_conf_file(self.ssl_ca_cert_file, self.file_type, version, instance.agent_address, instance.agent_port, xsession) self.logger.info("Done.") return HTMLAsyncResult( http_code=200, template_path=self.template_path, template_file='edit_conf_file_advanced.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'pgconf', 'file_versions': file_versions, 'file_type': self.file_type, 'conf_file': conf_file, 'hba_options': hba_options, 'xsession': xsession }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (agent_address, agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })
def get_index(self, agent_address, agent_port): try: instance = None role = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") if __name__ not in [ plugin.plugin_name for plugin in instance.plugins ]: raise TemboardUIError(408, "Plugin not active.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) # Here we want to get the current agent username if a session # already exists. # Monitoring plugin doesn't require agent authentication since we # already have the data. # Don't fail if there's a session error (for example when the agent # has been restarted) agent_username = None try: if xsession: data_profile = temboard_profile(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) agent_username = data_profile['username'] except TemboardError: pass return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='index.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'monitoring', 'agent_username': agent_username }) except (TemboardUIError, Exception) as e: self.logger.exception(str(e)) try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError)): if e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })
def get_dashboard(self, agent_address, agent_port): try: self.logger.info("Getting dashboard.") instance = None role = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") if __name__ not in [ plugin.plugin_name for plugin in instance.plugins ]: raise TemboardUIError(408, "Plugin not active.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) agent_username = data_profile['username'] dashboard_history = temboard_dashboard_history( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) if dashboard_history and isinstance( dashboard_history, list) and len(dashboard_history) > 0: last_data = dashboard_history[-1] history = json.dumps(dashboard_history) else: # If dashboard history is empty, let's try to get data from the # live data source. last_data = temboard_dashboard_live(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) history = '' self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_file='dashboard.html', template_path=self.template_path, data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'dashboard', 'dashboard': last_data, 'history': history, 'buffers_delta': 0, 'readratio': (100 - last_data['hitratio']), 'xsession': xsession, 'agent_username': agent_username, }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (agent_address, agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })
def get_configuration(self, agent_address, agent_port, category=None): try: self.logger.info("Getting configuration.") instance = None role = None self.load_auth_cookie() self.start_db_session() role = self.current_user if not role: raise TemboardUIError(302, "Current role unknown.") instance = get_instance(self.db_session, agent_address, agent_port) if not instance: raise TemboardUIError(404, "Instance not found.") if __name__ not in [ plugin.plugin_name for plugin in instance.plugins ]: raise TemboardUIError(408, "Plugin not active.") self.db_session.expunge_all() self.db_session.commit() self.db_session.close() xsession = self.get_secure_cookie( "temboard_%s_%s" % (instance.agent_address, instance.agent_port)) if not xsession: raise TemboardUIError(401, "Authentication cookie is missing.") else: data_profile = temboard_profile(self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) agent_username = data_profile['username'] configuration_status = temboard_get_configuration_status( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) configuration_cat = temboard_get_configuration_categories( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession) query_filter = self.get_argument('filter', None, True) if category is None: category = tornado.escape.url_escape( configuration_cat['categories'][0]) url = tornado.escape.url_escape( tornado.escape.url_unescape(category)) configuration_data = temboard_get_configuration( self.ssl_ca_cert_file, instance.agent_address, instance.agent_port, xsession, url, query_filter) self.logger.info("Done.") return HTMLAsyncResult(http_code=200, template_path=self.template_path, template_file='configuration.html', data={ 'nav': True, 'role': role, 'instance': instance, 'plugin': 'pgconf', 'data': configuration_data, 'xsession': xsession, 'agent_username': agent_username, 'current_cat': tornado.escape.url_unescape(category), 'configuration_categories': configuration_cat, 'configuration_status': configuration_status, 'query_filter': query_filter }) except (TemboardUIError, TemboardError, Exception) as e: self.logger.exception(str(e)) self.logger.info("Failed.") try: self.db_session.expunge_all() self.db_session.rollback() self.db_session.close() except Exception: pass if (isinstance(e, TemboardUIError) or isinstance(e, TemboardError)): if e.code == 401: return HTMLAsyncResult(http_code=401, redirection="/server/%s/%s/login" % (agent_address, agent_port)) elif e.code == 302: return HTMLAsyncResult(http_code=401, redirection="/login") code = e.code else: code = 500 return HTMLAsyncResult(http_code=code, template_file='error.html', data={ 'nav': True, 'role': role, 'instance': instance, 'code': e.code, 'error': e.message })