Пример #1
0
def test_configuration_kafka(tmpdir):
    "Test the configuration functions"
    createTempConfig(tmpdir, False)
    conf = m.Configuration(str(tmpdir)+'/config/testconfig.ini', 'test')
    assert conf.kafka_broker == 'kafka:12345'
    assert conf.kafka_SSL_CA == 'ca.pem'
    assert conf.kafka_SSL_cert == 'service.cert'
    assert conf.kafka_SSL_key == 'service.key'
    assert conf.kafka_topic == 'monitor_topic'
Пример #2
0
def test_configuration_postgres(tmpdir):
    "Test the configuration functions"
    createTempConfig(tmpdir, False)
    conf = m.Configuration(str(tmpdir)+'/config/testconfig.ini', 'test')
    assert conf.db_host == 'hostname'
    assert conf.db_port == '12345'
    assert conf.db_user == 'username'
    assert conf.db_passwd == 'secret'
    assert conf.db_schema == 'monitor'
Пример #3
0
def test_kafka_connection(tmpdir):
    # we do the real config here
    conf = m.Configuration('configx.ini', "test")

    # in case the field is empty
    if conf.kafka_broker == '':
        pytest.skip("no broker configured in config.ini")

    kafka_handle = con.connect_kafka(conf, 'TESTCON')
    # function will fail if cannot connect
    assert kafka_handle
Пример #4
0
def test_db_connection(tmpdir):
    "test postgres connection"

    conf = m.Configuration('configx.ini', "test")

    # in case the field is empty
    if conf.db_host == '':
        pytest.skip("no broker configured in config.ini")

    db_handle = con.connect_db(conf)
    # function will fail if cannot connect
    assert db_handle
Пример #5
0
def main(argv):
    "main"

    client_num = 1
    config_file = ''

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:c:")
    except getopt.GetoptError:
        print('consumntodb.py -i <clinet_num>')
        sys.exit(2)
    for opt, args in opts:
        if opt == '-c':
            config_file = args
        elif opt == "-i":
            client_num = args

    conf = m.Configuration(config_file, "PostgresConsumer")

    # connect to kafka and DB
    db_connection = connect_db(conf)
    kafka_handle = connect_kafka(conf, client_num)

    # wait loop for messages on our topic
    for message in kafka_handle:
        target = json.loads(message.value)
        db_cursor = db_connection.cursor()

        sql = """INSERT INTO {}.checks (name, host, checktime, regex_hits, regex, return_code,response_time) VALUES ('{}', '{}', to_timestamp({}) AT TIME ZONE 'UTC', {}, '{}', {}, {});""".format(
            conf.db_schema,
            target['name'],
            target['host'],
            target['checktime'],
            (target['regex_hits'] if 'regex_hits' in target else 'NULL'),
            (target['regex'] if ('regex' in target and target['regex'] != 'None') else 'NULL'),
            target['return_code'],
            target['response_time']
        )

        # prepare and commit
        db_cursor.execute(sql)
        db_connection.commit()

        m.if_debug_print("received and wrote result for {}".format(target['name']), conf)
Пример #6
0
def main(argv=None):
    "main"

    config_file = ''

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:c:")
    except getopt.GetoptError:
        print('consumntodb.py -i <clinet_num>')
        sys.exit(2)
    for opt, args in opts:
        if opt == '-c':
            config_file = args

    # read config
    conf = m.Configuration(config_file, "checker")

    # prepare the array for all the Target objects
    targets = []

    # parse targets.yaml
    targets = fill_targets(targets, conf)
    target_num = len(targets)

    if target_num == 0:
        m.error_print("no targets found, exiting", conf)
        sys.exit(1)

    m.if_debug_print(str(target_num) + " targets", conf)

    # get connected to Kafka
    KafkaHandle = connect_kafka(conf)

    # iterate through the target array and check each and write out the result
    for entry in targets:
        check_host(entry, conf)
        write_results(entry, KafkaHandle, conf)
Пример #7
0
def test_configuration_debugt(tmpdir):
    "Test the configuration functions"
    createTempConfig(tmpdir, True)
    conf = m.Configuration(str(tmpdir)+'/config/testconfig.ini', 'test')
    assert conf.debug
Пример #8
0
def test_configuration_targets(tmpdir):
    "Test the configuration functions"
    createTempConfig(tmpdir, False)
    conf = m.Configuration(str(tmpdir)+'/config/testconfig.ini', 'test')
    assert conf.path_target_list == 'targets.yaml'
Пример #9
0
def setupconf(tmpdir):
    createTempConfig(tmpdir, False, Path(tmpdir / 'targets/targets.yaml'))
    createTempTargets(tmpdir)
    return m.Configuration(str(tmpdir)+'/config/testconfig.ini', 'test')