Skip to content

Commit

Permalink
Use ClientID when PeerIDs are repeated
Browse files Browse the repository at this point in the history
  • Loading branch information
AvaxVPN-Team authored and HosseyNJF committed Jun 2, 2021
1 parent d35ddea commit 3188744
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/collector/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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),
)
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/openvpn/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -144,13 +145,15 @@ 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],
RealAddress: parseIP(fields[2]),
BytesReceived: bytesRec,
BytesSent: bytesSent,
ConnectedSince: time.Unix(connectedSinceInt, 0),
ClientID: clientIdInt,
PeerID: peerIdInt,
}
clients = append(clients, client)
Expand Down

0 comments on commit 3188744

Please sign in to comment.