-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (96 loc) · 1.98 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const fetchInterval = 5 * time.Minute
var (
statuses = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ap_status",
Help: "AP connection status",
},
[]string{"ap"},
)
clients2g = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ap_num_clients_2G",
Help: "AP clients 2G",
},
[]string{"ap"},
)
clients5g = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ap_num_clients_5G",
Help: "AP clients 5G",
},
[]string{"ap"},
)
client_count = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "client_count",
Help: "Number of users per SSID",
},
[]string{"ssid"},
)
)
func reset(err error) {
log.Println("failed to fetch, resetting metrics:", err)
statuses.Reset()
clients2g.Reset()
clients5g.Reset()
client_count.Reset()
}
func fetch() {
start := time.Now()
defer func() {
log.Println("fetch: completed in:", time.Since(start))
}()
rc := NewRuckusClient()
err := rc.Login()
if err != nil {
reset(err)
return
}
aps, err := rc.ListAP()
if err != nil {
reset(err)
return
}
for _, ap := range aps.Aps {
status := statuses.WithLabelValues(ap.Name)
if ap.Status == "Online" {
status.Set(1)
} else {
status.Set(0)
}
clients2g.WithLabelValues(ap.Name).Set(float64(ap.Clients24G))
clients5g.WithLabelValues(ap.Name).Set(float64(ap.Clients5G))
}
wlans, err := rc.WLAN()
if err != nil {
reset(err)
return
}
clientBySSID := make(map[string]int)
for _, ssid := range wlans.WLANs {
clientBySSID[ssid.Name] += ssid.Clients
}
for wlanName, count := range clientBySSID {
client_count.WithLabelValues(wlanName).Set(float64(count))
}
}
func main() {
go func() {
for {
fetch()
time.Sleep(fetchInterval)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":9118", nil)
}