Skip to content

Commit

Permalink
Update serializers.py to add Interface Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrichbaum authored Aug 11, 2024
1 parent 2c9c49a commit 6fc88a7
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion netbox_prometheus_sd/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rest_framework import serializers
from django.db import models
from virtualization.models import VirtualMachine
from dcim.models import Device
from dcim.models import Device, Interface
from ipam.models import IPAddress, Service

from netaddr import IPNetwork
Expand Down Expand Up @@ -206,3 +206,41 @@ def get_labels(self, obj):
utils.extract_custom_fields(obj, labels)

return labels.get_labels()

class PrometheusInterfaceSerializer(serializers.ModelSerializer, PrometheusTargetsMixin):
"""Serialize an interface to Prometheus target representation"""

class Meta:
model = Interface
fields = ["targets", "labels"]

targets = serializers.SerializerMethodField()
labels = serializers.SerializerMethodField()

def get_labels(self, obj):
firstip = ""
firstipmask = ""
if obj.count_ipaddresses > 0:
ipsplit = str(obj.ip_addresses.first()).split("/", 1)
firstip = ipsplit[0]
firstipmask = ipsplit[1]

labels = LabelDict(
{
"id": str(obj.id),
"devicename": str(obj.device.name),
"deviceid": str(obj.device.id),
"ip_address": firstip,
"ip_mask": firstipmask
}
)

utils.extract_tags(obj, labels)
utils.extract_custom_fields(obj, labels)

labels = labels.get_labels()

# Those shouldn't have the netbox prefix
utils.extract_prometheus_sd_config(obj, labels)

return labels

0 comments on commit 6fc88a7

Please sign in to comment.