pihole_influx icon indicating copy to clipboard operation
pihole_influx copied to clipboard

Although influxdb exists pihole_influx complains

Open pluim003 opened this issue 2 years ago • 1 comments

When starting up pihole_influx it says:

2022-12-02 15:53:06,113 - pihole_influx - INFO - Querying 1 pihole servers: ['https://pihole.xxxxxxx.nl']
2022-12-02 15:53:06,113 - pihole_influx - INFO - Logging to InfluxDB server 10.0.0.10:8086
2022-12-02 15:53:06,114 - pihole_influx - DEBUG - Connecting to 10.0.0.10
2022-12-02 15:53:06,124 - pihole_influx - INFO - Database pihole not found. Will attempt to create it.
2022-12-02 15:53:06,131 - pihole_influx - INFO - Attempting to contact https://pihole.xxxxx.nl with URL https://pihole.xxxxxx.nl/admin/api.php
2022-12-02 15:53:10,303 - pihole_influx - DEBUG - [{'measurement': 'piholestats.https://pihole_xxxxxx_nl', 'tags': {'host': 'https://pihole.dickpluim.nl'}, 'fields': {'domains_being_blocked': 1174496, 'dns_queries_today': 58188, 'ads_percentage_today': 8.561903, 'ads_blocked_today': 4982}}]
2022-12-02 15:53:10,337 - pihole_influx - INFO - Waiting 600

But database pihole does exist, so that is strange.

When I check influx it show series created by pihole_influx.

Connected to http://localhost:8086 version 1.8.10
InfluxDB shell version: 1.8.10
> show databases
name: databases
name
----
_internal
telegraf
hass
pihole
> use pihole
Using database pihole
> show series
key
---
pihole,host=first_one
pihole,host=pihole
pihole,host=second_one
piholestats.https://pihole_xxxxxx_nl,host=https://pihole.xxxxxx.nl
piholestats.pihole_xxxxxxxx_nl,host=pihole.xxxxxxx.nl

What can cause this?

pluim003 avatar Dec 02 '22 16:12 pluim003

Figured it out. Seems that the checking didn't work as expected.

Changed code:

def check_db_status(config, logger):
    """ Check the required DB exists, and create it if necessary """

    logger.debug("Connecting to {}".format(config['INFLUXDB_SERVER']))
    client = InfluxDBClient(
        config['INFLUXDB_SERVER'],
        config['INFLUXDB_PORT'],
        config['INFLUXDB_USERNAME'],
        config['INFLUXDB_PASSWORD']
    )
    for db in client.get_list_database():
        if db['name'] == client:
            logger.info('Found existing database {}.'.format(config['INFLUXDB_DATABASE']))
            return True
    else:
        logger.info('Database {} not found. Will attempt to create it.'.format(config['INFLUXDB_DATABASE']))
        client.create_database(config['INFLUXDB_DATABASE'])
    return True

to

def check_db_status(config, logger):
    """ Check the required DB exists, and create it if necessary """

    logger.debug("Connecting to {}".format(config['INFLUXDB_SERVER']))
    client = InfluxDBClient(
        config['INFLUXDB_SERVER'],
        config['INFLUXDB_PORT'],
        config['INFLUXDB_USERNAME'],
        config['INFLUXDB_PASSWORD']
    )

    if {"name": config['INFLUXDB_DATABASE']} not in client.get_list_database():
        logger.info('Database {} not found. Will attempt to create it.'.format(config['INFLUXDB_DATABASE']))
        client.create_database(config['INFLUXDB_DATABASE'])
        return True
    else:       
        logger.info('Found existing database {}.'.format(config['INFLUXDB_DATABASE']))
        return True

now it seems to be working as expected. I have made other changes as pihole will also change with respect to accessing the api and have a test-branch.

pluim003 avatar Dec 04 '22 15:12 pluim003