def get_discover(http_context, app, sessions): logger.info('Starting discovery.') try: sysinfo = SysInfo() with app.postgres.connect() as conn: pginfo = PgInfo(conn) ret = dict(hostname=sysinfo.hostname( app.config.temboard['hostname']), cpu=sysinfo.n_cpu(), memory_size=sysinfo.memory_size(), pg_port=pginfo.setting('port'), pg_version=pginfo.version()['full'], pg_version_summary=pginfo.version()['summary'], pg_data=pginfo.setting('data_directory'), plugins=[ plugin_name for plugin_name in app.config.temboard['plugins'] ]) logger.info('Discovery done.') return ret except (error, Exception, HTTPError) as e: logger.exception(e) logger.info('Discovery failed.') if isinstance(e, HTTPError): raise e else: raise HTTPError(500, "Internal error.")
def get_metrics(conn, config, _=None): dm = DashboardMetrics(conn) sysinfo = SysInfo() pginfo = PgInfo(conn) cpu_models = [cpu['model_name'] for cpu in sysinfo.cpu_info()['cpus']] cpu_models_counter = {} for elem in cpu_models: cpu_models_counter[elem] = cpu_models_counter.get(elem, 0) + 1 return { 'buffers': dm.get_buffers(), 'hitratio': dm.get_hitratio(), 'active_backends': dm.get_active_backends(), 'max_connections': dm.get_max_connections(), 'cpu': dm.get_cpu_usage(), 'loadaverage': dm.get_load_average(), 'memory': dm.get_memory_usage(), 'hostname': sysinfo.hostname(config.temboard['hostname']), 'os_version': sysinfo.os_release, 'linux_distribution': sysinfo.linux_distribution(), 'cpu_models': cpu_models_counter, 'databases': dm.get_stat_db(), 'pg_uptime': dm.get_pg_uptime(), 'n_cpu': sysinfo.n_cpu(), 'pg_version': pginfo.version()['full'], 'pg_data': pginfo.setting('data_directory'), 'pg_port': pginfo.setting('port'), 'notifications': dm.get_notifications(config) }
def get_metrics(conn, config): dm = DashboardMetrics(conn) sysinfo = SysInfo() pginfo = PgInfo(conn) cpu_models = [cpu['model_name'] for cpu in sysinfo.cpu_info()['cpus']] cpu_models_counter = {} for elem in cpu_models: cpu_models_counter[elem] = cpu_models_counter.get(elem, 0) + 1 return dict( buffers=dm.get_buffers(), hitratio=dm.get_hitratio(), active_backends=dm.get_active_backends(), max_connections=dm.get_max_connections(), cpu=dm.get_cpu_usage(), loadaverage=dm.get_load_average(), memory=dm.get_memory_usage(), hostname=sysinfo.hostname(config.temboard.hostname), os_version=sysinfo.os_release, linux_distribution=sysinfo.linux_distribution(), cpu_models=cpu_models_counter, databases=dm.get_stat_db(), pg_uptime=dm.get_pg_uptime(), n_cpu=sysinfo.n_cpu(), pg_version=pginfo.version()['full'], pg_data=pginfo.setting('data_directory'), pg_port=pginfo.setting('port'), notifications=dm.get_notifications(config), )
def get_discover(http_context, app, sessions): logger.info('Starting discovery.') # Optionnal validation of key. For compatibility, we accept unauthenticated # /discover. But for better reliability, we validate a key sent by HTTP # header. temboard-agent-register sends key to prevent configuration # mismatch. request_key = http_context['headers'].get('X-Temboard-Agent-Key') if request_key and request_key != app.config.temboard['key']: raise HTTPError(401, "Invalid key") discover = dict( hostname=None, cpu=None, memory_size=None, pg_port=app.config.postgresql['port'], pg_version=None, pg_version_summary=None, pg_data=None, plugins=[plugin for plugin in app.config.temboard['plugins']], ) try: # Gather system informations sysinfo = SysInfo() hostname = sysinfo.hostname(app.config.temboard['hostname']) cpu = sysinfo.n_cpu() memory_size = sysinfo.memory_size() except (Exception, HTTPError) as e: logger.exception(str(e)) logger.error('System discovery failed.') # We stop here if system information has not been collected if isinstance(e, HTTPError): raise e else: raise HTTPError(500, "Internal error.") discover.update(hostname=hostname, cpu=cpu, memory_size=memory_size) try: with app.postgres.connect() as conn: pginfo = PgInfo(conn) discover.update(pg_block_size=int(pginfo.setting('block_size')), pg_version=pginfo.version()['full'], pg_version_summary=pginfo.version()['summary'], pg_data=pginfo.setting('data_directory')) except Exception as e: logger.exception(str(e)) logger.error('Postgres discovery failed.') # Do not raise HTTPError, just keeping null values for Postgres # informations. logger.info('Discovery done.') logger.debug(discover) return discover
def get_discover(http_context, app, sessions): logger.info('Starting discovery.') discover = dict( hostname=None, cpu=None, memory_size=None, pg_port=app.config.postgresql['port'], pg_version=None, pg_version_summary=None, pg_data=None, plugins=[plugin for plugin in app.config.temboard['plugins']], ) try: # Gather system informations sysinfo = SysInfo() hostname = sysinfo.hostname(app.config.temboard['hostname']) cpu = sysinfo.n_cpu() memory_size = sysinfo.memory_size() except (Exception, HTTPError) as e: logger.exception(str(e)) logger.error('System discovery failed.') # We stop here if system information has not been collected if isinstance(e, HTTPError): raise e else: raise HTTPError(500, "Internal error.") discover.update( hostname=hostname, cpu=cpu, memory_size=memory_size ) try: with app.postgres.connect() as conn: pginfo = PgInfo(conn) discover.update( pg_block_size=int(pginfo.setting('block_size')), pg_version=pginfo.version()['full'], pg_version_summary=pginfo.version()['summary'], pg_data=pginfo.setting('data_directory') ) except Exception as e: logger.exception(str(e)) logger.error('Postgres discovery failed.') # Do not raise HTTPError, just keeping null values for Postgres # informations. logger.info('Discovery done.') logger.debug(discover) return discover
def get_metrics(app): res = dict() try: with app.postgres.connect() as conn: dm = DashboardMetrics(conn) pginfo = PgInfo(conn) res.update( dict( buffers=dm.get_buffers(), hitratio=dm.get_hitratio(), active_backends=dm.get_active_backends(), max_connections=dm.get_max_connections(), databases=dm.get_stat_db(), pg_uptime=dm.get_pg_uptime(), pg_version=pginfo.version()['full'], pg_data=pginfo.setting('data_directory'), pg_port=pginfo.setting('port'), )) except UserError: pass dm = DashboardMetrics() res.update( dict( cpu=dm.get_cpu_usage(), loadaverage=dm.get_load_average(), memory=dm.get_memory_usage(), notifications=dm.get_notifications(app.config), )) sysinfo = SysInfo() cpu_models = [cpu['model_name'] for cpu in sysinfo.cpu_info()['cpus']] cpu_models_counter = {} for elem in cpu_models: cpu_models_counter[elem] = cpu_models_counter.get(elem, 0) + 1 res.update( dict(hostname=sysinfo.hostname(app.config.temboard.hostname), os_version=sysinfo.os_release, linux_distribution=sysinfo.linux_distribution(), cpu_models=cpu_models_counter, n_cpu=sysinfo.n_cpu(), timestamp=time.time())) return res
def get_discover(http_context, config=None, sessions=None): conn = connector(host=config.postgresql['host'], port=config.postgresql['port'], user=config.postgresql['user'], password=config.postgresql['password'], database=config.postgresql['dbname']) logger.info('Starting discovery.') try: conn.connect() sysinfo = SysInfo() pginfo = PgInfo(conn) ret = { 'hostname': sysinfo.hostname(config.temboard['hostname']), 'cpu': sysinfo.n_cpu(), 'memory_size': sysinfo.memory_size(), 'pg_port': pginfo.setting('port'), 'pg_version': pginfo.version()['full'], 'pg_data': pginfo.setting('data_directory'), 'plugins': [plugin_name for plugin_name in config.temboard['plugins']] } conn.close() logger.info('Discovery done.') return ret except (error, Exception, HTTPError) as e: logger.exception(str(e)) logger.info('Discovery failed.') try: conn.close() except Exception: pass if isinstance(e, HTTPError): raise e else: raise HTTPError(500, "Internal error.")
def get_n_cpu(): sysinfo = SysInfo() return dict(n_cpu=sysinfo.n_cpu())
def get_n_cpu(config, _): sysinfo = SysInfo() return {'n_cpu': sysinfo.n_cpu()}