mirror of
https://github.com/unpoller/unpoller.git
synced 2026-10-19 07:41:32 +02:00
Lint and format cleanup
This commit is contained in:
@@ -12,8 +12,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
history_seconds = 86400
|
||||
poll_duration = time.Second * history_seconds
|
||||
historySeconds = 86400
|
||||
pollDuration = time.Second * historySeconds
|
||||
)
|
||||
|
||||
var ErrScrapeFilterMatchFailed = fmt.Errorf("scrape filter match failed, and filter is not http URL")
|
||||
@@ -105,13 +105,14 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
|
||||
defer updateWeb(c, m)
|
||||
|
||||
// FIXME needs to be last poll time maybe
|
||||
st := m.TS.Add(-1 * poll_duration)
|
||||
st := m.TS.Add(-1 * pollDuration)
|
||||
tp := unifi.EpochMillisTimePeriod{StartEpochMillis: st.UnixMilli(), EndEpochMillis: m.TS.UnixMilli()}
|
||||
|
||||
if c.SaveRogue != nil && *c.SaveRogue {
|
||||
if m.RogueAPs, err = c.Unifi.GetRogueAPs(sites); err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetRogueAPs(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d RogueAPs entries", len(m.RogueAPs))
|
||||
}
|
||||
|
||||
@@ -119,11 +120,13 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
|
||||
if m.SitesDPI, err = c.Unifi.GetSiteDPI(sites); err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetSiteDPI(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d SitesDPI entries", len(m.SitesDPI))
|
||||
|
||||
if m.ClientsDPI, err = c.Unifi.GetClientsDPI(sites); err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetClientsDPI(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d ClientsDPI entries", len(m.ClientsDPI))
|
||||
}
|
||||
|
||||
@@ -131,6 +134,7 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
|
||||
if m.CountryTraffic, err = c.Unifi.GetCountryTraffic(sites, &tp); err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetCountryTraffic(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d CountryTraffic entries", len(m.CountryTraffic))
|
||||
}
|
||||
|
||||
@@ -139,6 +143,7 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetClientTraffic(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d ClientUsageByApp entries", len(clientUsageByApp))
|
||||
b4 := len(m.ClientsDPI)
|
||||
u.convertToClientDPI(clientUsageByApp, m)
|
||||
@@ -149,11 +154,13 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
|
||||
if m.Clients, err = c.Unifi.GetClients(sites); err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetClients(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d Clients entries", len(m.Clients))
|
||||
|
||||
if m.Devices, err = c.Unifi.GetDevices(sites); err != nil {
|
||||
return nil, fmt.Errorf("unifi.GetDevices(%s): %w", c.URL, err)
|
||||
}
|
||||
|
||||
u.LogDebugf("Found %d UBB, %d UXG, %d PDU, %d UCI, %d UAP %d USG %d USW %d UDM devices",
|
||||
len(m.Devices.UBBs), len(m.Devices.UXGs),
|
||||
len(m.Devices.PDUs), len(m.Devices.UCIs),
|
||||
@@ -161,7 +168,7 @@ func (u *InputUnifi) pollController(c *Controller) (*poller.Metrics, error) {
|
||||
len(m.Devices.USWs), len(m.Devices.UDMs))
|
||||
|
||||
// Get speed test results for all WANs
|
||||
if m.SpeedTests, err = c.Unifi.GetSpeedTests(sites, history_seconds); err != nil {
|
||||
if m.SpeedTests, err = c.Unifi.GetSpeedTests(sites, historySeconds); err != nil {
|
||||
// Don't fail collection if speed tests fail - older controllers may not have this endpoint
|
||||
u.LogDebugf("unifi.GetSpeedTests(%s): %v (continuing)", c.URL, err)
|
||||
} else {
|
||||
@@ -191,10 +198,12 @@ func (u *InputUnifi) convertToClientDPI(clientUsageByApp []*unifi.ClientUsageByA
|
||||
for _, client := range clientUsageByApp {
|
||||
byApp := make([]unifi.DPIData, 0)
|
||||
byCat := make([]unifi.DPIData, 0)
|
||||
|
||||
type catCount struct {
|
||||
BytesReceived int64
|
||||
BytesTransmitted int64
|
||||
}
|
||||
|
||||
byCatMap := make(map[int]catCount)
|
||||
dpiClients := make([]*unifi.DPIClient, 0)
|
||||
// TODO create cat table
|
||||
@@ -209,6 +218,7 @@ func (u *InputUnifi) convertToClientDPI(clientUsageByApp []*unifi.ClientUsageByA
|
||||
TxBytes: u.int64ToFlexInt(app.BytesTransmitted),
|
||||
TxPackets: u.int64ToFlexInt(0), // We don't have packets from Unifi Controller
|
||||
}
|
||||
|
||||
cat, ok := byCatMap[app.Category]
|
||||
if ok {
|
||||
cat.BytesReceived += app.BytesReceived
|
||||
@@ -220,8 +230,10 @@ func (u *InputUnifi) convertToClientDPI(clientUsageByApp []*unifi.ClientUsageByA
|
||||
}
|
||||
byCatMap[app.Category] = cat
|
||||
}
|
||||
|
||||
byApp = append(byApp, dpiData)
|
||||
}
|
||||
|
||||
if len(byApp) <= 1 {
|
||||
byCat = byApp
|
||||
} else {
|
||||
@@ -239,6 +251,7 @@ func (u *InputUnifi) convertToClientDPI(clientUsageByApp []*unifi.ClientUsageByA
|
||||
byCat = append(byCat, dpiData)
|
||||
}
|
||||
}
|
||||
|
||||
dpiTable := unifi.DPITable{
|
||||
ByApp: byApp,
|
||||
ByCat: byCat,
|
||||
|
||||
@@ -166,6 +166,7 @@ func (u *UnifiPoller) HealthCheck() error {
|
||||
for _, output := range outputs {
|
||||
if output.Enabled() {
|
||||
hasEnabledOutput = true
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,6 +269,7 @@ func AppendMetrics(existing *Metrics, m *Metrics) *Metrics {
|
||||
existing.Clients = append(existing.Clients, m.Clients...)
|
||||
existing.Devices = append(existing.Devices, m.Devices...)
|
||||
existing.CountryTraffic = append(existing.CountryTraffic, m.CountryTraffic...)
|
||||
|
||||
return existing
|
||||
}
|
||||
|
||||
|
||||
@@ -31,20 +31,25 @@ func (u *promUnifi) exportCountryTraffic(r report, v any) {
|
||||
s, ok := v.(*unifi.UsageByCountry)
|
||||
if !ok {
|
||||
u.LogErrorf("invalid type given to CountryTraffic: %T", v)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
country, ok := countrycodes.GetByAlpha2(s.Country)
|
||||
name := "Unknown"
|
||||
region := "Unknown"
|
||||
subRegion := "Unknown"
|
||||
|
||||
if ok {
|
||||
name = country.Name
|
||||
region = country.Region
|
||||
subRegion = country.SubRegion
|
||||
}
|
||||
|
||||
if s.Country == "GB" || s.Country == "UK" {
|
||||
name = "United Kingdom" // Because the name is so long otherwise
|
||||
}
|
||||
|
||||
labels := []string{s.Country, name, region, subRegion, s.TrafficSite.SiteName, s.TrafficSite.SourceName}
|
||||
r.send([]*metric{
|
||||
{u.CountryTraffic.RxBytes, counter, s.BytesReceived, labels},
|
||||
|
||||
Reference in New Issue
Block a user