Пример #1
0
def run():
    """
    Main execution
    """
    args = parse_arguments()
    if args.config is not None:
        config = parse_config(args.config)
    elif args.identifier is not None:
        file_name = args.identifier + '.json'
        # determine if file is /etc or /usr/etc
        config_file = lookup_etc_folder(
            [dirname + file_name for dirname in CONFIG_FILES_DIR])
        config = parse_config(config_file)

    else:
        raise ValueError('configuration file or identifier must be used')

    if config.get('logging', None):
        setup_logging(config)
    else:
        logging.basicConfig(level=args.verbosity or logging.INFO)

    if args.metrics:
        metrics = args.metrics
    else:
        metrics = lookup_etc_folder(METRICS_FILES)

    try:
        hana_config = config['hana']
        dbs = db_manager.DatabaseManager()
        dbs.start(hana_config['host'],
                  hana_config.get('port', 30013),
                  user=hana_config.get('user', ''),
                  password=hana_config.get('password', ''),
                  userkey=hana_config.get('userkey', None),
                  multi_tenant=config.get('multi_tenant', True),
                  timeout=config.get('timeout', 30))
    except KeyError as err:
        raise KeyError(
            'Configuration file {} is malformed: {} not found'.format(
                args.config, err))

    if args.daemon:
        utils.systemd_ready()

    connectors = dbs.get_connectors()
    collector = prometheus_exporter.SapHanaCollectors(connectors=connectors,
                                                      metrics_file=metrics)
    REGISTRY.register(collector)
    LOGGER.info('exporter sucessfully registered')

    LOGGER.info('starting to serve metrics')
    start_http_server(config.get('exposition_port', 9668), '0.0.0.0')
    while True:
        time.sleep(1)
Пример #2
0
def run():
    """
    Main execution
    """
    args = parse_arguments()
    if args.config is not None:
        config = parse_config(args.config)
    elif args.identifier is not None:
        config = parse_config('{}/{}.json'.format(CONFIG_FOLDER,
                                                  args.identifier))
    else:
        raise ValueError('configuration file or identifier must be used')

    if config.get('logging', None):
        setup_logging(config)
    else:
        logging.basicConfig(level=args.verbosity or logging.INFO)

    if args.metrics:
        metrics = args.metrics
    else:
        metrics = find_metrics_file()

    try:
        hana_config = config['hana']
        dbs = db_manager.DatabaseManager()
        dbs.start(hana_config['host'],
                  hana_config.get('port', 30013),
                  user=hana_config.get('user', ''),
                  password=hana_config.get('password', ''),
                  userkey=hana_config.get('userkey', None),
                  multi_tenant=config.get('multi_tenant', True),
                  timeout=config.get('timeout', 600))
    except KeyError as err:
        raise KeyError(
            'Configuration file {} is malformed: {} not found'.format(
                args.config, err))

    connectors = dbs.get_connectors()
    collector = prometheus_exporter.SapHanaCollectors(connectors=connectors,
                                                      metrics_file=metrics)
    REGISTRY.register(collector)
    LOGGER.info('exporter sucessfully registered')

    LOGGER.info('starting to serve metrics')
    start_http_server(config.get('exposition_port', 9668), '0.0.0.0')
    while True:
        time.sleep(1)
    def test_init(self, mock_collector):

        conn1 = mock.Mock()
        conn2 = mock.Mock()

        connectors = [conn1, conn2]

        coll1 = mock.Mock()
        coll2 = mock.Mock()
        mock_collector.side_effect = [coll1, coll2]

        collectors = prometheus_exporter.SapHanaCollectors(
            connectors, 'metrics.json')

        mock_collector.assert_has_calls([
            mock.call(conn1, 'metrics.json'),
            mock.call(conn2, 'metrics.json')
        ])

        assert collectors._collectors == [coll1, coll2]
    def test_collect(self, mock_collector):

        conn1 = mock.Mock()
        conn2 = mock.Mock()

        connectors = [conn1, conn2]

        metrics = ['metric1', 'metric2', 'metric3', 'metric4']
        coll1 = mock.Mock()
        coll1.collect.return_value = [metrics[0], metrics[1]]
        coll2 = mock.Mock()
        coll2.collect.return_value = [metrics[2], metrics[3]]

        mock_collector.side_effect = [coll1, coll2]

        collectors = prometheus_exporter.SapHanaCollectors(
            connectors, 'metrics.json')

        for i, metric in enumerate(collectors.collect()):
            assert metric == metrics[i]

        coll1.collect.assert_called_once_with()
        coll2.collect.assert_called_once_with()