Skip to content

Commit

Permalink
Add support for device-type filter to starmer.
Browse files Browse the repository at this point in the history
  • Loading branch information
rewbycraft committed May 30, 2024
1 parent 80a5b07 commit fee3bc4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
10 changes: 9 additions & 1 deletion configbuilders/starmer/cmd/access_switch_http_sd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package main

import (
"encoding/json"
"log"
"net/http"
"strings"

"github.com/netbox-community/go-netbox/v3"
"github.com/prometheus/prometheus/discovery/targetgroup"
Expand All @@ -23,9 +25,15 @@ func accessSwitchHttpSD(dcim *netbox.DcimAPIService) *http.HandlerFunc {
var handler http.HandlerFunc
handler = func(resp http.ResponseWriter, req *http.Request) {

dt := req.URL.Query().Get("device-type")
dts := []string{}
if dt != "" {
dts = strings.Split(dt, ",")
}
configs := make([]targetgroup.Group, 0)
sws, err := switchinfo.GetSwitchesWithRoles(req.Context(), dcim, []string{"access-switch", "distribution-switch"})
sws, err := switchinfo.GetSwitchesWithRoles(req.Context(), dcim, []string{"access-switch", "distribution-switch"}, dts)
if err != nil {
log.Printf("Error getting switches: %w", err)
resp.WriteHeader(500)
return
}
Expand Down
2 changes: 1 addition & 1 deletion configbuilders/starmer/cmd/switch_dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const zone string = "net.emf.camp."

func main() {
cl := cmdlib.GetNetboxClient()
switches, err := switchinfo.GetSwitchesWithRoles(context.TODO(), cl.DcimAPI, []string{"access-switch", "distribution-switch"})
switches, err := switchinfo.GetSwitchesWithRoles(context.TODO(), cl.DcimAPI, []string{"access-switch", "distribution-switch"}, []string{})

if err != nil {
log.Fatalf("Error getting access switches: %s", err)
Expand Down
26 changes: 14 additions & 12 deletions configbuilders/starmer/switchinfo/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
)

type Switch struct {
Name string
MgmtIP netip.Addr
Loc Location
Status netbox.DeviceStatus
Name string
MgmtIP netip.Addr
Loc Location
Status netbox.DeviceStatus
DeviceType netbox.NestedDeviceType
}

func GetSwitchesWithRoles(ctx context.Context, dcim *netbox.DcimAPIService, roles []string) ([]Switch, error) {
req := dcim.DcimDevicesList(ctx).Role(roles).Limit(500)
func GetSwitchesWithRoles(ctx context.Context, dcim *netbox.DcimAPIService, roles []string, deviceTypes []string) ([]Switch, error) {
req := dcim.DcimDevicesList(ctx).Role(roles).DeviceType(deviceTypes).Limit(500)

results := make([]Switch, 0)

Expand All @@ -35,14 +36,15 @@ func GetSwitchesWithRoles(ctx context.Context, dcim *netbox.DcimAPIService, role
nloc.Name,
orb.Point{52, 0},
}

results = append(results, Switch{
Name: dev.GetName(),
MgmtIP: netip.MustParsePrefix(dev.GetPrimaryIp4().Address).Addr(),
Loc: loc,
Status: dev.GetStatus(),
Name: dev.GetName(),
MgmtIP: netip.MustParsePrefix(dev.GetPrimaryIp4().Address).Addr(),
Loc: loc,
Status: dev.GetStatus(),
DeviceType: dev.DeviceType,
})

}

return results, nil
Expand Down
5 changes: 3 additions & 2 deletions configbuilders/starmer/switchinfo/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func SwitchPromTarget(s *Switch) *targetgroup.Group {
})

group.Labels = prom.LabelSet{
"device_name": prom.LabelValue(s.Name),
"device_name": prom.LabelValue(s.Name),
"location_name": prom.LabelValue(s.Loc.Name),
"status": prom.LabelValue(*s.Status.Value),
"status": prom.LabelValue(*s.Status.Value),
"device_type": prom.LabelValue(s.DeviceType.Slug),
}

return group
Expand Down

0 comments on commit fee3bc4

Please sign in to comment.