New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] - Add iregex filter for devices model and return custom_fields as labels #65
Comments
Here is what I tried : What works, but hard-codedclass DeviceViewSet(NetBoxModelViewSet): # pylint: disable=too-many-ancestors
queryset = Device.objects.prefetch_related(
"device_type__manufacturer",
"device_role",
"tenant",
"platform",
"site",
"location",
"rack",
"parent_bay",
"virtual_chassis__master",
"primary_ip4__nat_outside",
"primary_ip6__nat_outside",
"tags",
).filter(
Q(device_type__model__iregex=r'^Dlink.*$')
)
filterset_class = DeviceFilterSet
serializer_class = PrometheusDeviceSerializer
pagination_class = None What does not work, calling the url
|
I'm having trouble displaying the Here is what I tried but without success : # netbox_prometheus_sd/api/serializers.py
if hasattr(obj, "custom_fields") and obj.custom_fields is not None and len(obj.custom_fields.all()):
labels["custom_fields"] = ",".join(
[
f"{cf.name}={cf.value}"
for cf in obj.custom_fields.all()
if cf.value is not None
]
) EDIT:By persevering, I found the solution that I share below: # netbox_prometheus_sd/api/serializers.py
if hasattr(obj, "custom_field_data") and obj.custom_field_data is not None:
for key, value in obj.custom_field_data.items():
if value is not None:
labels["custom_field_" + key.lower()] = value @FlxPeters are you interested by a PR ? |
Custom fields have been merged to main branch. Please provide feedback if the solution meets your requirements. |
Hey! |
I just released a pre-release on PyPi: https://pypi.org/project/netbox-plugin-prometheus-sd/0.6.0rc1/ |
Awesome, thank you :) I'll tinker around with it for a couple days and report back! |
I just wanted to make a issue/PR to include the custom fields and checked-out the pre-release which you mentioned above. Seems to work at-least for my use-case. |
Works great for me. So far I did not encounter any limitations |
I did a quick test with the git repo and it worked pretty good. Thanks I'd been wanting to do this for a while. |
It gets a bit awkward where custom fields have data type "multiselect". They are currently returned as a not-quite JSON list:
("not-quite JSON" because it uses single quotes rather than double quotes). This is rather inconvenient when it comes to the new multi-module support in SNMP exporter (v0.24.0+), which requires a plain comma-separated list like AFAICT there's no global search-replace in Prometheus relabelling, so this was the best I could come up with:
(which works for a maximum of 4 selected values). That's pretty ugly. I'm not sure of the best solution here. Automatically converting a list value into a comma-joined string would be the easy thing for this particular case, but I guess there might be other cases where people would want something different. (I considered that a plain value could contain a comma, but this doesn't apply in a Netbox custom field select or multiselect, because the list of allowed values in the custom field definition is itself comma-separated) EDIT: Since Netbox 3.6 has added Custom Field choice sets, comma is no longer a special field (although colon is) For fields with a comma you could do backslash escaping ( |
Hi, below is the Netbox API return from
/api/dcim/devices/?name=router-01.example.com
and I would like to know if you could implement in your API a filter on themodel
ormodel slug
(using the model__iregex) that will return all devices matching this model as well as an option to return allcustom_fields
as labels, please.Thank you in advance.
The text was updated successfully, but these errors were encountered: