diff --git a/prometheus/main.go b/prometheus/main.go index 520b1c9..2a887d8 100644 --- a/prometheus/main.go +++ b/prometheus/main.go @@ -169,11 +169,20 @@ func handleMetric(line string, metrics *metrics) { fields := strings.Fields(line) log.Println(fields) + if len(fields) < 2 { + log.Printf("Malformed metric line (need at least 2 fields): %q", line) + return + } + server := fields[0] command := fields[1] switch command { case "connect": + if len(fields) < 3 { + log.Printf("Malformed connect metric (need 3 fields): %q", line) + return + } ip := fields[2] country := geoLookup(ip) lat := CapitalCoordinates[country].Latitude @@ -181,6 +190,10 @@ func handleMetric(line string, metrics *metrics) { // Reduce cardinality by removing ip handleConnect(server, country, lat, lon, metrics) case "disconnect": + if len(fields) < 4 { + log.Printf("Malformed disconnect metric (need 4 fields): %q", line) + return + } // ip := fields[2] parsedTimeTrapped, err := strconv.ParseUint(fields[3], 10, 64) if err != nil { @@ -199,16 +212,28 @@ func handleMetric(line string, metrics *metrics) { if len(fields) >= 4 { url = fields[3] } - + metrics.upnpOtherHttpRequests.WithLabelValues(method, url).Inc() case "M-SEARCH": + if len(fields) < 3 { + log.Printf("Malformed M-SEARCH metric (need 3 fields): %q", line) + return + } ip := fields[2] metrics.upnpMSearchRequests.WithLabelValues(ip).Inc() case "non-M-SEARCH": + if len(fields) < 3 { + log.Printf("Malformed non-M-SEARCH metric (need 3 fields): %q", line) + return + } ip := fields[2] metrics.upnpNonMSearchRequests.WithLabelValues(ip).Inc() // MQTT case "CONNECT": + if len(fields) < 3 { + log.Printf("Malformed MQTT CONNECT metric (need 3 fields): %q", line) + return + } version := fields[2] metrics.mqttConnectVersions.WithLabelValues(version).Inc() @@ -216,6 +241,10 @@ func handleMetric(line string, metrics *metrics) { metrics.mqttMalformedConnect.Inc() case "SUBSCRIBE": + if len(fields) < 4 { + log.Printf("Malformed SUBSCRIBE metric (need 4 fields): %q", line) + return + } topic := fields[2] qos := fields[3] metrics.mqttSubscribeTopics.WithLabelValues(topic, qos).Inc() @@ -229,10 +258,14 @@ func handleMetric(line string, metrics *metrics) { if len(fields) >= 4 { password = fields[3] } - + metrics.mqttCredentials.WithLabelValues(username, password).Inc() case "PUBLISH": + if len(fields) < 4 { + log.Printf("Malformed PUBLISH metric (need 4 fields): %q", line) + return + } topic := fields[2] qos := fields[3] metrics.mqttPublishTopics.WithLabelValues(topic, qos).Inc()