Beispiel #1
0
async def main():

    # Zabbix API
    zapi = ZabbixAPI(zabbix_endpoint)
    zapi.session.verify = False
    zapi.login(zabbix_user, zabbix_pass)

    logger.info(f'{zapi.__class__.__name__} - getting host data')
    zabbix_data = zapi.host.get(output="extend", selectInventory=True)

    o = Oracle()

    await o.delete_geo_points()

    # initialize async task list
    async_tasks = []

    # iterate all hosts and create geo points
    for host in zabbix_data:
        o = Oracle()

        # Create geo point if latitude and logitude data exists.
        if 'location_lat' and 'location_lon' in host['inventory']:
            with suppress(Exception):
                o.location_lat = host['inventory']['location_lat']

            with suppress(Exception):
                o.location_lon = host['inventory']['location_lon']

            with suppress(Exception):
                o.host_name = host['host']

            with suppress(Exception):
                o.hostid = host['hostid']

            with suppress(Exception):
                o.model = host['inventory']['model']

            with suppress(Exception):
                o.vendor = host['inventory']['vendor']

            with suppress(Exception):
                o.host_type = host['inventory']['type']

            # append task to task list
            async_tasks.append(asyncio.create_task(o.create_geo_point()))
            # return control to asyncio event loop
            await asyncio.sleep(0)

    # create geo points from async_tasks list
    await asyncio.gather(*async_tasks)