From 3188744545e010316eab9eb59b948166fd3ef1f7 Mon Sep 17 00:00:00 2001 From: AvaxVPN-Team <69919770+avaxvpn-team@users.noreply.github.com> Date: Thu, 3 Jun 2021 00:07:52 +0430 Subject: [PATCH] Use ClientID when PeerIDs are repeated --- pkg/collector/openvpn.go | 14 +++++++++++--- pkg/openvpn/parser.go | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/collector/openvpn.go b/pkg/collector/openvpn.go index 78c73d3..60b0fe5 100644 --- a/pkg/collector/openvpn.go +++ b/pkg/collector/openvpn.go @@ -130,6 +130,7 @@ func (c *OpenVPNCollector) collect(ovpn OpenVPNServer, ch chan<- prometheus.Metr } connectedClients := 0 + hasFacedZeroPeerId := false var clientCommonNames []string for _, client := range status.ClientList { connectedClients++ @@ -160,23 +161,30 @@ func (c *OpenVPNCollector) collect(ovpn OpenVPNServer, ch chan<- prometheus.Metr } } clientCommonNames = append(clientCommonNames, client.CommonName) + uniqueId := client.PeerID + if uniqueId == 0 { // In TCP mode, PeerID is always 0 + if hasFacedZeroPeerId { // Use ClientID only if a 0 PeerID is matched twice (maybe it's the first item and we are in UDP mode) + uniqueId = -client.ClientID - 1 // ClientID starts at 0; But it may be duplicated with another 0 PeerID + } + hasFacedZeroPeerId = true + } ch <- prometheus.MustNewConstMetric( c.BytesReceived, prometheus.CounterValue, client.BytesReceived, - ovpn.Name, client.CommonName, strconv.FormatInt(client.PeerID, 10), + ovpn.Name, client.CommonName, strconv.FormatInt(uniqueId, 10), ) ch <- prometheus.MustNewConstMetric( c.BytesSent, prometheus.CounterValue, client.BytesSent, - ovpn.Name, client.CommonName, strconv.FormatInt(client.PeerID, 10), + ovpn.Name, client.CommonName, strconv.FormatInt(uniqueId, 10), ) ch <- prometheus.MustNewConstMetric( c.ConnectedSince, prometheus.GaugeValue, float64(client.ConnectedSince.Unix()), - ovpn.Name, client.CommonName, strconv.FormatInt(client.PeerID, 10), + ovpn.Name, client.CommonName, strconv.FormatInt(uniqueId, 10), ) } } diff --git a/pkg/openvpn/parser.go b/pkg/openvpn/parser.go index f7d7d9a..c23e841 100644 --- a/pkg/openvpn/parser.go +++ b/pkg/openvpn/parser.go @@ -23,7 +23,8 @@ type Client struct { BytesReceived float64 BytesSent float64 ConnectedSince time.Time - PeerID int64 + ClientID int64 + PeerID int64 } // ServerInfo reflects information that was collected about the server @@ -144,6 +145,7 @@ func parseStatusV2AndV3(reader io.Reader, separator string) (*Status, error) { bytesRec, _ := strconv.ParseFloat(fields[5], 64) bytesSent, _ := strconv.ParseFloat(fields[6], 64) connectedSinceInt, _ := strconv.ParseInt(fields[8], 10, 64) + clientIdInt, _ := strconv.ParseInt(fields[10], 10, 64) peerIdInt, _ := strconv.ParseInt(fields[11], 10, 64) client := Client{ CommonName: fields[1], @@ -151,6 +153,7 @@ func parseStatusV2AndV3(reader io.Reader, separator string) (*Status, error) { BytesReceived: bytesRec, BytesSent: bytesSent, ConnectedSince: time.Unix(connectedSinceInt, 0), + ClientID: clientIdInt, PeerID: peerIdInt, } clients = append(clients, client)