예제 #1
0
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)
    }
예제 #2
0
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_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.")
예제 #3
0
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),
    )
예제 #4
0
def get_info(conn, config):
    dm = DashboardMetrics(conn)
    sysinfo = SysInfo()
    pginfo = PgInfo(conn)
    return dict(
        hostname=sysinfo.hostname(config.temboard.hostname),
        os_version=' '.join([sysinfo.os, sysinfo.os_release]),
        pg_uptime=dm.get_pg_uptime(),
        pg_version=pginfo.version()['full'],
        pg_data=pginfo.setting('data_directory'),
        pg_port=pginfo.setting('port'),
    )
예제 #5
0
def get_info(conn, config, _):
    dm = DashboardMetrics(conn)
    sysinfo = SysInfo()
    pginfo = PgInfo(conn)
    return {
        'hostname': sysinfo.hostname(config.temboard['hostname']),
        'os_version': "%s %s" % (sysinfo.os, sysinfo.os_release),
        'pg_uptime': dm.get_pg_uptime(),
        'pg_version': pginfo.version()['full'],
        'pg_data': pginfo.setting('data_directory'),
        'pg_port': pginfo.setting('port'),
    }
예제 #6
0
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
예제 #7
0
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
예제 #8
0
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.")
예제 #9
0
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
예제 #10
0
def get_pg_version(conn):
    pginfo = PgInfo(conn)
    return dict(pg_version=pginfo.version()['full'])
예제 #11
0
def instance_info(conninfo, hostname):
    """Gather PostgreSQL instance information."""
    instance_info = {
        'hostname': hostname,
        'instance': conninfo['instance'],
        'local_name': conninfo.get('local_name', conninfo['instance']),
        'available': True,
        'host': conninfo['host'],
        'port': conninfo['port'],
        'user': conninfo['user'],
        'database': conninfo['database'],
        'password': conninfo['password']
    }

    # Try the connection
    conn = connector(conninfo['host'], conninfo['port'], conninfo['user'],
                     conninfo['password'], conninfo['database'])
    try:
        conn.connect()
        # Get PostgreSQL informations using PgInfo
        pginfo = PgInfo(conn)
        pgv = pginfo.version()
        # Gather the info while where are connected
        instance_info['version_num'] = pgv['num']
        instance_info['version'] = pgv['server']
        instance_info['data_directory'] = pginfo.setting('data_directory')

        # hot standby is available from 9.0
        instance_info['standby'] = pginfo.is_in_recovery()

        # max_connections
        instance_info['max_connections'] = pginfo.setting('max_connections')

        # Grab the list of tablespaces
        instance_info['tablespaces'] = pginfo.tablespaces(
            instance_info['data_directory'])

        # When the user has not given a dbnames list or '*' in the
        # configuration file, we must get the list of databases. Since
        # we have a working connection, let's do it now.
        dbs = pginfo.databases()
        instance_info['dbnames'] = []
        for db in conninfo['dbnames']:
            if db == '*':
                instance_info['dbnames'] = dbs.values()
                break
            if db in dbs.keys():
                instance_info['dbnames'].append(dbs[db])
        conn.close()

        # Now that we have the data_directory, find the owner
        try:
            statinfo = os.stat(instance_info['data_directory'])
            instance_info['sysuser'] = pwd.getpwuid(statinfo.st_uid).pw_name
        except OSError as e:
            logging.warning("Unable to get the owner of PGDATA: %s", str(e))
            instance_info['sysuser'] = None

    except error as e:
        logging.exception(str(e))
        logging.warning("Unable to gather information for cluster \"%s\"",
                        conninfo['instance'])
        instance_info['available'] = False

    return instance_info
예제 #12
0
def get_pg_data(conn, config, _):
    pginfo = PgInfo(conn)
    return {'pg_data': pginfo.setting('data_directory')}
예제 #13
0
def get_pg_port(conn, config, _):
    pginfo = PgInfo(conn)
    return {'pg_port': pginfo.setting('port')}
예제 #14
0
def get_pg_version(conn, config, _):
    pginfo = PgInfo(conn)
    return {'pg_version': pginfo.version()['full']}