charlie-haley / omada_exporter

Prometheus Exporter for TP-Link Omada Controller SDN.
MIT License
87 stars 14 forks source link

Switch ports not displayed correctly #44

Closed RaymondMouthaan closed 1 year ago

RaymondMouthaan commented 2 years ago

Hi @charlie-haley,

Thanks for the recent update of the Grafana dashboard! Unfortunately, it didn't work out-of-box and I had to make a modification regarding the ports query:

The query below doesn't return all ports of the switch

omada_port_link_speed_mbps{switch_mac="$Device", vlan_id="", site="$Site"}

After removing vlan_id="":

omada_port_link_speed_mbps{switch_mac="$Device", site="$Site"}

the port section starts displaying all ports.

This works for the TL-SG3428 v2.0 switch as you can see in the screenshot below (renamed Disabled to Not Connected):

image

However the ports of my two other switches TL-SG2008 and TL-SG2218 don't show as expected. After some digging it turns out that the above query returns for each port that has no connection (marked in red) a value of 10 MB/s instead of the expected 0.

Screenshot 2022-05-28 at 12 31 57

After some more digging, I did the rest call for the TL-SG2008 to get the port status. and as you can see the json shows for port 5 a linkStatus of 0, while the query shows a speed of 10 MB/s.

switch TL-SG2008:

"portStatus": {
                "port": 5,
                "linkStatus": 0,
                "linkSpeed": 1,
                "duplex": 2,
                "poe": false,
                "tx": 0,
                "rx": 0,
                "stpDiscarding": false
            },

Compared to a disconnected port on the TL-SG3428 v2.0

 "portStatus": {
                "port": 25,
                "linkStatus": 0,
                "linkSpeed": 0,
                "duplex": 0,
                "poe": false,
                "tx": 0,
                "rx": 0,
                "stpDiscarding": false
            },

The difference is that the TL-SG2008 reports linkSpeed = 1, while TL-SG3428 v2.0 reports linkSpeed = 0 for ports that are not connected. I am not sure why the TL-SG2008 reports linkSpeed = 1, while there is no connection.

In the omada exporter source code, I found these line https://github.com/charlie-haley/omada_exporter/blob/89772bc1aef9f5e3e1720f7242fbd7958a661fe7/pkg/omada/handler.go#L74-L85

And they might be changed to the snippet below, so that in all cases where the LinkStatus is 0 or the LinkSpeed is 0, the returned linkSpeed is 0:

if p.PortStatus.LinkStatus == 0 || p.PortStatus.LinkSpeed == 0 {
    linkSpeed = 0
}
if p.PortStatus.LinkSpeed == 1 {
    linkSpeed = 10
}
if p.PortStatus.LinkSpeed == 2 {
    linkSpeed = 100
}
if p.PortStatus.LinkSpeed == 3 {
    linkSpeed = 1000
}

Hope this helps.

Regards, Raymond

RaymondMouthaan commented 2 years ago

I've created a PR #45

charlie-haley commented 2 years ago

Sorry for the delay at getting around to looking at this @RaymondMouthaan, looks good to me, just left a small comment on the PR