Skip to content

Commit

Permalink
feat: add site_id label to metrics (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-haley authored Apr 7, 2022
1 parent 1ca3944 commit a9cc0c9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 54 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ omada:
## 📊 Metrics
Name|Description|Labels
|--|--|--|
omada_device_uptime_seconds | Uptime of the device. | device, model, version, ip, mac, site, device_type
omada_device_cpu_percentage | Percentage of device CPU used. | device, model, version, ip, mac, site, device_type
omada_device_mem_percentage | Percentage of device Memory used. | device, model, version, ip, mac, site, device_type
omada_device_need_upgrade | A boolean on whether the device needs an upgrade. | device, model, version, ip, mac, site, device_type
omada_device_tx_rate | The tx rate of the device. | device, model, version, ip, mac, site, device_type
omada_device_rx_rate | The rx rate of the device. | device, model, version, ip, mac, site, device_type
omada_device_poe_remain_watts | The remaining amount of PoE power for the device in watts. | device, model, version, ip, mac, site, device_type
omada_client_download_activity_bytes | The current download activity for the client in bytes. | client, vendor, switch_port, vlan_id, ip, mac, site, ap_name, ssid, wifi_mode
omada_client_signal_dbm | The signal level for the wireless client in dBm. | client, vendor, ip, mac, ap_name, site, ssid, wifi_mode
omada_port_power_watts | The current PoE usage of the port in watts. | client, vendor, switch_port, switch_mac, switch_id, vlan_id, profile, site
omada_port_link_status | A boolean representing the link status of the port. | client, vendor, switch_port, switch_mac, switch_id, vlan_id, profile, site
omada_port_link_speed_mbps | Port link speed in mbps. This is the capability of the connection, not the active throughput. | client, vendor, switch_port, switch_mac, switch_id, vlan_id, profile, site
omada_device_uptime_seconds | Uptime of the device. | device, model, version, ip, mac, site, site_id, device_type
omada_device_cpu_percentage | Percentage of device CPU used. | device, model, version, ip, mac, site, site_id, device_type
omada_device_mem_percentage | Percentage of device Memory used. | device, model, version, ip, mac, site, site_id, device_type
omada_device_need_upgrade | A boolean on whether the device needs an upgrade. | device, model, version, ip, mac, site, site_id, device_type
omada_device_tx_rate | The tx rate of the device. | device, model, version, ip, mac, site, site_id, device_type
omada_device_rx_rate | The rx rate of the device. | device, model, version, ip, mac, site, site_id, device_type
omada_device_poe_remain_watts | The remaining amount of PoE power for the device in watts. | device, model, version, ip, mac, site, site_id, device_type
omada_client_download_activity_bytes | The current download activity for the client in bytes. | client, vendor, switch_port, vlan_id, ip, mac, site, site_id, ap_name, ssid, wifi_mode
omada_client_signal_dbm | The signal level for the wireless client in dBm. | client, vendor, ip, mac, ap_name, site, site_id, ssid, wifi_mode
omada_port_power_watts | The current PoE usage of the port in watts. | client, vendor, switch_port, switch_mac, switch_id, vlan_id, profile, site, site_id
omada_port_link_status | A boolean representing the link status of the port. | client, vendor, switch_port, switch_mac, switch_id, vlan_id, profile, site, site_id
omada_port_link_speed_mbps | Port link speed in mbps. This is the capability of the connection, not the active throughput. | client, vendor, switch_port, switch_mac, switch_id, vlan_id, profile, site, site_id
omada_controller_uptime_seconds | Uptime of the controller. | controller_name, model, controller_version, firmware_version, mac
omada_controller_storage_used_bytes | Storage used on the controller. | storage_name, controller_name, model, controller_version, firmware_version, mac
omada_controller_storage_available_bytes | Total storage available for the controller. | storage_name, controller_name, model, controller_version, firmware_version, mac
omada_client_connected_total | Total number of connected clients. | site
omada_client_connected_total | Total number of connected clients. | site, site_id
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Client struct {
httpClient *http.Client
token string
omadaCID string
siteId string
SiteId string
}

func setuphttpClient(insecure bool) (*http.Client, error) {
Expand Down Expand Up @@ -56,7 +56,7 @@ func Configure(c *cli.Context) (*Client, error) {
if err != nil {
return nil, err
}
client.siteId = *sid
client.SiteId = *sid

return client, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Client) getClientsWithFilters(filtersEnabled bool, mac string) ([]Netwo
}
}

url := fmt.Sprintf("%s/%s/api/v2/sites/%s/clients", c.Config.String("host"), c.omadaCID, c.siteId)
url := fmt.Sprintf("%s/%s/api/v2/sites/%s/clients", c.Config.String("host"), c.omadaCID, c.SiteId)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *Client) GetDevices() ([]Device, error) {
}
}

url := fmt.Sprintf("%s/%s/api/v2/sites/%s/devices", c.Config.String("host"), c.omadaCID, c.siteId)
url := fmt.Sprintf("%s/%s/api/v2/sites/%s/devices", c.Config.String("host"), c.omadaCID, c.SiteId)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *Client) GetPorts(switchMac string) ([]Port, error) {
}
}

url := fmt.Sprintf("%s/%s/api/v2/sites/%s/switches/%s/ports", c.Config.String("host"), c.omadaCID, c.siteId, switchMac)
url := fmt.Sprintf("%s/%s/api/v2/sites/%s/switches/%s/ports", c.Config.String("host"), c.omadaCID, c.SiteId, switchMac)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand Down
46 changes: 23 additions & 23 deletions pkg/omada/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ func Scrape(c *api.Client) error {
if err != nil {
return fmt.Errorf("failed to get devices: %s", err)
}
setDeviceMetrics(devices, site)
setDeviceMetrics(devices, site, c.SiteId)

clients, err := c.GetClients()
if err != nil {
return fmt.Errorf("failed to get clients: %s", err)
}
setClientMetrics(clients, site)
setClientMetrics(clients, site, c.SiteId)

for _, device := range devices {
err := setPortMetricsByDevice(c, device, site)
err := setPortMetricsByDevice(c, device, site, c.SiteId)
if err != nil {
return fmt.Errorf("failed to set port metrics: %s", err)
}
Expand All @@ -47,28 +47,28 @@ func setControllerMetrics(controller *api.Controller) {
}

// set prometheus metrics for devices
func setDeviceMetrics(devices []api.Device, site string) {
func setDeviceMetrics(devices []api.Device, site string, siteId string) {
for _, item := range devices {
needUpgrade := float64(0)
if item.NeedUpgrade {
needUpgrade = 1
}
omada_device_uptime_seconds.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(item.Uptime)
omada_device_cpu_percentage.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(item.CpuUtil)
omada_device_mem_percentage.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(item.MemUtil)
omada_device_need_upgrade.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(needUpgrade)
omada_device_uptime_seconds.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(item.Uptime)
omada_device_cpu_percentage.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(item.CpuUtil)
omada_device_mem_percentage.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(item.MemUtil)
omada_device_need_upgrade.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(needUpgrade)
if item.Type == "ap" {
omada_device_tx_rate.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(item.TxRate)
omada_device_rx_rate.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(item.RxRate)
omada_device_tx_rate.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(item.TxRate)
omada_device_rx_rate.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(item.RxRate)
}
if item.Type == "switch" {
omada_device_poe_remain_watts.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, item.Type).Set(item.PoeRemain)
omada_device_poe_remain_watts.WithLabelValues(item.Name, item.Model, item.Version, item.Ip, item.Mac, site, siteId, item.Type).Set(item.PoeRemain)
}
}
}

// set prometheus metrics for ports
func setPortMetricsByDevice(c *api.Client, device api.Device, site string) error {
func setPortMetricsByDevice(c *api.Client, device api.Device, site string, siteId string) error {
for _, p := range device.Ports {
linkSpeed := float64(0)
if p.PortStatus.LinkSpeed == 0 {
Expand All @@ -93,34 +93,34 @@ func setPortMetricsByDevice(c *api.Client, device api.Device, site string) error
if client != nil {
vlanId := fmt.Sprintf("%.0f", client.VlanId)

omada_port_power_watts.WithLabelValues(client.HostName, client.Vendor, port, p.SwitchMac, p.SwitchId, vlanId, p.ProfileName, site).Set(p.PortStatus.PoePower)
omada_port_link_status.WithLabelValues(client.HostName, client.Vendor, port, p.SwitchMac, p.SwitchId, vlanId, p.ProfileName, site).Set(p.PortStatus.LinkStatus)
omada_port_link_speed_mbps.WithLabelValues(client.HostName, client.Vendor, port, p.SwitchMac, p.SwitchId, vlanId, p.ProfileName, site).Set(linkSpeed)
omada_port_power_watts.WithLabelValues(client.HostName, client.Vendor, port, p.SwitchMac, p.SwitchId, vlanId, p.ProfileName, site, siteId).Set(p.PortStatus.PoePower)
omada_port_link_status.WithLabelValues(client.HostName, client.Vendor, port, p.SwitchMac, p.SwitchId, vlanId, p.ProfileName, site, siteId).Set(p.PortStatus.LinkStatus)
omada_port_link_speed_mbps.WithLabelValues(client.HostName, client.Vendor, port, p.SwitchMac, p.SwitchId, vlanId, p.ProfileName, site, siteId).Set(linkSpeed)
} else {
omada_port_power_watts.WithLabelValues("", "", port, p.SwitchMac, p.SwitchId, "", p.ProfileName, site).Set(p.PortStatus.PoePower)
omada_port_link_status.WithLabelValues("", "", port, p.SwitchMac, p.SwitchId, "", p.ProfileName, site).Set(p.PortStatus.LinkStatus)
omada_port_link_speed_mbps.WithLabelValues("", "", port, p.SwitchMac, p.SwitchId, "", p.ProfileName, site).Set(linkSpeed)
omada_port_power_watts.WithLabelValues("", "", port, p.SwitchMac, p.SwitchId, "", p.ProfileName, site, siteId).Set(p.PortStatus.PoePower)
omada_port_link_status.WithLabelValues("", "", port, p.SwitchMac, p.SwitchId, "", p.ProfileName, site, siteId).Set(p.PortStatus.LinkStatus)
omada_port_link_speed_mbps.WithLabelValues("", "", port, p.SwitchMac, p.SwitchId, "", p.ProfileName, site, siteId).Set(linkSpeed)
}

}
return nil
}

// set prometheus metrics for clients
func setClientMetrics(clients []api.NetworkClient, site string) {
omada_client_connected_total.WithLabelValues(site).Set(float64(len(clients)))
func setClientMetrics(clients []api.NetworkClient, site string, siteId string) {
omada_client_connected_total.WithLabelValues(site, siteId).Set(float64(len(clients)))

for _, item := range clients {
vlanId := fmt.Sprintf("%.0f", item.VlanId)
port := fmt.Sprintf("%.0f", item.Port)
if item.Wireless {
wifiMode := fmt.Sprintf("%.0f", item.WifiMode)
omada_client_download_activity_bytes.WithLabelValues(item.HostName, item.Vendor, "", "", item.Ip, item.Mac, site, item.ApName, item.Ssid, wifiMode).Set(item.Activity)
omada_client_signal_dbm.WithLabelValues(item.HostName, item.Vendor, item.Ip, item.Mac, item.ApName, site, item.Ssid, wifiMode).Set(item.SignalLevel)
omada_client_download_activity_bytes.WithLabelValues(item.HostName, item.Vendor, "", "", item.Ip, item.Mac, site, siteId, item.ApName, item.Ssid, wifiMode).Set(item.Activity)
omada_client_signal_dbm.WithLabelValues(item.HostName, item.Vendor, item.Ip, item.Mac, item.ApName, site, siteId, item.Ssid, wifiMode).Set(item.SignalLevel)
}
//
if !item.Wireless {
omada_client_download_activity_bytes.WithLabelValues(item.HostName, item.Vendor, port, vlanId, item.Ip, item.Mac, site, "", "", "").Set(item.Activity)
omada_client_download_activity_bytes.WithLabelValues(item.HostName, item.Vendor, port, vlanId, item.Ip, item.Mac, site, siteId, "", "", "").Set(item.Activity)
}
}
}
26 changes: 13 additions & 13 deletions pkg/omada/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,73 +10,73 @@ var (
Name: "omada_device_uptime_seconds",
Help: "Uptime of the device.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_device_cpu_percentage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_device_cpu_percentage",
Help: "Percentage of device CPU used.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_device_mem_percentage = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_device_mem_percentage",
Help: "Percentage of device Memory used.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_device_need_upgrade = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_device_need_upgrade",
Help: "A boolean on whether the device needs an upgrade.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_device_tx_rate = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_device_tx_rate",
Help: "The tx rate of the device.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_device_rx_rate = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_device_rx_rate",
Help: "The rx rate of the device.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_device_poe_remain_watts = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_device_poe_remain_watts",
Help: "The remaining amount of PoE power for the device in watts.",
},
[]string{"device", "model", "version", "ip", "mac", "site", "device_type"})
[]string{"device", "model", "version", "ip", "mac", "site", "site_id", "device_type"})

omada_client_download_activity_bytes = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_client_download_activity_bytes",
Help: "The current download activity for the client in bytes.",
},
[]string{"client", "vendor", "switch_port", "vlan_id", "ip", "mac", "site", "ap_name", "ssid", "wifi_mode"})
[]string{"client", "vendor", "switch_port", "vlan_id", "ip", "mac", "site", "site_id", "ap_name", "ssid", "wifi_mode"})

omada_client_signal_dbm = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_client_signal_dbm",
Help: "The signal level for the wireless client in dBm.",
},
[]string{"client", "vendor", "ip", "mac", "ap_name", "site", "ssid", "wifi_mode"})
[]string{"client", "vendor", "ip", "mac", "ap_name", "site", "site_id", "ssid", "wifi_mode"})

omada_port_power_watts = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_port_power_watts",
Help: "The current PoE usage of the port in watts.",
},
[]string{"client", "vendor", "switch_port", "switch_mac", "switch_id", "vlan_id", "profile", "site"})
[]string{"client", "vendor", "switch_port", "switch_mac", "switch_id", "vlan_id", "profile", "site", "site_id"})

omada_port_link_status = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_port_link_status",
Help: "A boolean representing the link status of the port.",
},
[]string{"client", "vendor", "switch_port", "switch_mac", "switch_id", "vlan_id", "profile", "site"})
[]string{"client", "vendor", "switch_port", "switch_mac", "switch_id", "vlan_id", "profile", "site", "site_id"})

omada_port_link_speed_mbps = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_port_link_speed_mbps",
Help: "Port link speed in mbps. This is the capability of the connection, not the active throughput.",
},
[]string{"client", "vendor", "switch_port", "switch_mac", "switch_id", "vlan_id", "profile", "site"})
[]string{"client", "vendor", "switch_port", "switch_mac", "switch_id", "vlan_id", "profile", "site", "site_id"})

omada_controller_uptime_seconds = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "omada_controller_uptime_seconds",
Expand All @@ -100,5 +100,5 @@ var (
Name: "omada_client_connected_total",
Help: "Total number of connected clients.",
},
[]string{"site"})
[]string{"site", "site_id"})
)

0 comments on commit a9cc0c9

Please sign in to comment.