wvandeun / nornir_netbox

NetBox plugin for Nornir
Apache License 2.0
69 stars 17 forks source link

a few improvement ideas #15

Closed sjtarik closed 3 years ago

sjtarik commented 4 years ago

@wvandeun would you be open to two PRs

justification for these two is the frequent api failure we see on netbox that resolves with pagination (limit/offset) for dcim/devices apis. we can add a flag to turn on this behavior and leave pagination off by default.

wvandeun commented 4 years ago

I'm not so sure about caching, how are you going to decide when the current cache should be invalidated and we should fetch the data from NetBox again?

The plugin is already using pagination. The plugin sets the limit parameter to 0, NetBox will then return up to the amount of objects that is defined by the MAX_PAGE_SIZE configuration setting. If that is giving you issues you could try to investigate why those issues happen and see if you can remediate that on NetBox side, or reduce the MAX_PAGE_SIZE setting.

sjtarik commented 4 years ago

I'm not so sure about caching, how are you going to decide when the current cache should be invalidated and we should fetch the data from NetBox again?

I am still digging ansible plugin cache implementation. I think it is either from filesystem from file attributes or a metadata they injected into cached json file indicating the last time data is pulled from netbox. by default it is an hour for them.

The plugin is already using pagination. The plugin sets the limit parameter to 0, NetBox will then return up to the amount of objects that is defined by the MAX_PAGE_SIZE configuration setting. If that is giving you issues you could try to investigate why those issues happen and see if you can remediate that on NetBox side, or reduce the MAX_PAGE_SIZE setting.

We just started using netbox and my observation was with limit set to zero it fails almost once in every 10 requests. With using non zero limit and offset I see none. We don’t have a huge inventory <1000 nodes and this runs on a well provisioned VM. I see other people complaining as well. Have you seen anything similar? I will apply the max pagination advice as well.

wvandeun commented 4 years ago

If you are not using config_context then you could also try disabling rendering config_context. This can be done by passing a filter parameters:

nr = InitNornir(
    inventory={
        "plugin":"NetBoxInventory2",
        "options": {
            "nb_url": "https://netbox",
            "nb_token": "1234",
            "filter_parameters": {"exclude":"config_context"}
        }
    }
)

Disabling config_context rendering speeds it up drastically.

There is an open issue on the NetBox project for this: https://github.com/netbox-community/netbox/issues/4559 And a fix is available in https://github.com/netbox-community/netbox/pull/5266, which is part of the 2.9.8 release

sjtarik commented 3 years ago

Hey, as I suspected Ansible cache modules check the file attributes from os.stats to check cache timeouts, link here. I am planning to implement this over the weekend and send a PR similar to this Ansible netbox plugin implementation.

wvandeun commented 3 years ago

Did you test excluding config_context rendering? Did it resolve the issue of failing requests?

sjtarik commented 3 years ago

Did you test excluding config_context rendering? Did it resolve the issue of failing requests?

Actually I followed your first advice and set the max page limit to 500 instead of 5000 default and failing requests disappeared. But it was still slow, for <1000 device it takes 12-17 seconds to pull the device list

After applying your second advice and remove the config_context rendering, response time is much shorter. It is now around 4 seconds to get device list as compared to 12-17 seconds.

Thanks for both.

wvandeun commented 3 years ago

Ok great! I will close this issue, as the described problem has been solved. We can further discuss on the implementation of caching in a PR (you can open one already even if you don't have code finished and tag it as work in progress).

sjtarik commented 3 years ago

thank you so much again.