|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/DRuggeri/netgear_client" |
| 5 | + "github.com/prometheus/client_golang/prometheus" |
| 6 | + "github.com/prometheus/common/log" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type ClientCollector struct { |
| 12 | + namespace string |
| 13 | + client *netgear_client.NetgearClient |
| 14 | + clientsMetric *prometheus.GaugeVec |
| 15 | + wirelessSpeedMetric *prometheus.GaugeVec |
| 16 | + wirelessStrengthMetric *prometheus.GaugeVec |
| 17 | + |
| 18 | + scrapesTotalMetric prometheus.Counter |
| 19 | + scrapeErrorsTotalMetric prometheus.Counter |
| 20 | + lastScrapeErrorMetric prometheus.Gauge |
| 21 | + lastScrapeTimestampMetric prometheus.Gauge |
| 22 | + lastScrapeDurationSecondsMetric prometheus.Gauge |
| 23 | +} |
| 24 | + |
| 25 | +func NewClientCollector(namespace string, client *netgear_client.NetgearClient) *ClientCollector { |
| 26 | + clientsMetric := prometheus.NewGaugeVec( |
| 27 | + prometheus.GaugeOpts{ |
| 28 | + Namespace: namespace, |
| 29 | + Subsystem: "client", |
| 30 | + Name: "info", |
| 31 | + Help: "Client information with ip, name, MAC address and connection type labels", |
| 32 | + }, |
| 33 | + []string{"ip", "name", "mac", "connection_type"}, |
| 34 | + ) |
| 35 | + |
| 36 | + wirelessSpeedMetric := prometheus.NewGaugeVec( |
| 37 | + prometheus.GaugeOpts{ |
| 38 | + Namespace: namespace, |
| 39 | + Subsystem: "client", |
| 40 | + Name: "wireless_speed", |
| 41 | + Help: "Wireless speed of clients connected to the network", |
| 42 | + }, |
| 43 | + []string{"mac"}, |
| 44 | + ) |
| 45 | + |
| 46 | + wirelessStrengthMetric := prometheus.NewGaugeVec( |
| 47 | + prometheus.GaugeOpts{ |
| 48 | + Namespace: namespace, |
| 49 | + Subsystem: "client", |
| 50 | + Name: "wireless_strength", |
| 51 | + Help: "Wireless strength of clients connected to the network", |
| 52 | + }, |
| 53 | + []string{"mac"}, |
| 54 | + ) |
| 55 | + |
| 56 | + scrapesTotalMetric := prometheus.NewCounter( |
| 57 | + prometheus.CounterOpts{ |
| 58 | + Namespace: namespace, |
| 59 | + Subsystem: "client_scrapes", |
| 60 | + Name: "total", |
| 61 | + Help: "Total number of scrapes for Netgear client stats.", |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + scrapeErrorsTotalMetric := prometheus.NewCounter( |
| 66 | + prometheus.CounterOpts{ |
| 67 | + Namespace: namespace, |
| 68 | + Subsystem: "client_scrape_errors", |
| 69 | + Name: "total", |
| 70 | + Help: "Total number of scrapes errors for Netgear client stats.", |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + lastScrapeErrorMetric := prometheus.NewGauge( |
| 75 | + prometheus.GaugeOpts{ |
| 76 | + Namespace: namespace, |
| 77 | + Subsystem: "", |
| 78 | + Name: "last_client_scrape_error", |
| 79 | + Help: "Whether the last scrape of Netgear client stats resulted in an error (1 for error, 0 for success).", |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + lastScrapeTimestampMetric := prometheus.NewGauge( |
| 84 | + prometheus.GaugeOpts{ |
| 85 | + Namespace: namespace, |
| 86 | + Subsystem: "", |
| 87 | + Name: "last_client_scrape_timestamp", |
| 88 | + Help: "Number of seconds since 1970 since last scrape of Netgear client metrics.", |
| 89 | + }, |
| 90 | + ) |
| 91 | + |
| 92 | + lastScrapeDurationSecondsMetric := prometheus.NewGauge( |
| 93 | + prometheus.GaugeOpts{ |
| 94 | + Namespace: namespace, |
| 95 | + Subsystem: "", |
| 96 | + Name: "last_client_scrape_duration_seconds", |
| 97 | + Help: "Duration of the last scrape of Netgear client stats.", |
| 98 | + }, |
| 99 | + ) |
| 100 | + |
| 101 | + return &ClientCollector{ |
| 102 | + namespace: namespace, |
| 103 | + client: client, |
| 104 | + clientsMetric: clientsMetric, |
| 105 | + wirelessSpeedMetric: wirelessSpeedMetric, |
| 106 | + wirelessStrengthMetric: wirelessStrengthMetric, |
| 107 | + |
| 108 | + scrapesTotalMetric: scrapesTotalMetric, |
| 109 | + scrapeErrorsTotalMetric: scrapeErrorsTotalMetric, |
| 110 | + lastScrapeErrorMetric: lastScrapeErrorMetric, |
| 111 | + lastScrapeTimestampMetric: lastScrapeTimestampMetric, |
| 112 | + lastScrapeDurationSecondsMetric: lastScrapeDurationSecondsMetric, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (c *ClientCollector) Collect(ch chan<- prometheus.Metric) { |
| 117 | + var begun = time.Now() |
| 118 | + |
| 119 | + errorMetric := float64(0) |
| 120 | + clients, err := c.client.GetAttachDevice() |
| 121 | + if err != nil { |
| 122 | + log.Errorf("Error while collecting client statistics: %v", err) |
| 123 | + errorMetric = float64(1) |
| 124 | + c.scrapeErrorsTotalMetric.Inc() |
| 125 | + } else { |
| 126 | + for _, client := range clients { |
| 127 | + c.clientsMetric.WithLabelValues( |
| 128 | + client["IPAddress"], |
| 129 | + client["Name"], |
| 130 | + client["MACAddress"], |
| 131 | + client["ConnectionType"], |
| 132 | + ).Set(float64(1)) |
| 133 | + |
| 134 | + if client["ConnectionType"] != "wired" { |
| 135 | + tmp, _ := strconv.ParseFloat(client["WirelessLinkSpeed"], 64) |
| 136 | + c.wirelessSpeedMetric.WithLabelValues(client["MACAddress"]).Set(tmp) |
| 137 | + |
| 138 | + tmp, _ = strconv.ParseFloat(client["WirelessSignalStrength"], 64) |
| 139 | + c.wirelessStrengthMetric.WithLabelValues(client["MACAddress"]).Set(tmp) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + c.clientsMetric.Collect(ch) |
| 144 | + c.wirelessSpeedMetric.Collect(ch) |
| 145 | + c.wirelessStrengthMetric.Collect(ch) |
| 146 | + |
| 147 | + c.scrapeErrorsTotalMetric.Collect(ch) |
| 148 | + |
| 149 | + c.scrapesTotalMetric.Inc() |
| 150 | + c.scrapesTotalMetric.Collect(ch) |
| 151 | + |
| 152 | + c.lastScrapeErrorMetric.Set(errorMetric) |
| 153 | + c.lastScrapeErrorMetric.Collect(ch) |
| 154 | + |
| 155 | + c.lastScrapeTimestampMetric.Set(float64(time.Now().Unix())) |
| 156 | + c.lastScrapeTimestampMetric.Collect(ch) |
| 157 | + |
| 158 | + c.lastScrapeDurationSecondsMetric.Set(time.Since(begun).Seconds()) |
| 159 | + c.lastScrapeDurationSecondsMetric.Collect(ch) |
| 160 | +} |
| 161 | + |
| 162 | +func (c *ClientCollector) Describe(ch chan<- *prometheus.Desc) { |
| 163 | + c.clientsMetric.Describe(ch) |
| 164 | + c.wirelessSpeedMetric.Describe(ch) |
| 165 | + c.wirelessStrengthMetric.Describe(ch) |
| 166 | + c.scrapesTotalMetric.Describe(ch) |
| 167 | + c.scrapeErrorsTotalMetric.Describe(ch) |
| 168 | + c.lastScrapeErrorMetric.Describe(ch) |
| 169 | + c.lastScrapeTimestampMetric.Describe(ch) |
| 170 | + c.lastScrapeDurationSecondsMetric.Describe(ch) |
| 171 | +} |
0 commit comments