Skip to content

Commit 11796a5

Browse files
committed
feat: add vpn_user_start_time_seconds metric
* added metric giving user session start time in seconds since unix epoch
1 parent 89fe503 commit 11796a5

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

cmd/ocserv-exporter/exporter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func (e *Exporter) updateUsers() {
9898
for _, user := range users {
9999
vpnUserTX.WithLabelValues(user.Username, user.RemoteIP, user.MTU, user.VPNIPv4, user.VPNIPv6, user.Device).Set(float64(user.RawTX))
100100
vpnUserRX.WithLabelValues(user.Username, user.RemoteIP, user.MTU, user.VPNIPv4, user.VPNIPv6, user.Device).Set(float64(user.RawRX))
101+
vpnUserStartTime.WithLabelValues(user.Username, user.RemoteIP, user.MTU, user.VPNIPv4, user.VPNIPv6, user.Device).Set(float64(user.RawConnectedAt))
101102
}
102103
}
103104

cmd/ocserv-exporter/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ var (
9090
Name: "vpn_user_rx_bytes",
9191
Help: "Total RX usage in bytes of a user.",
9292
}, []string{"username", "remote_ip", "mtu", "vpn_ipv4", "vpn_ipv6", "device"})
93+
vpnUserStartTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{
94+
Name: "vpn_user_start_time_seconds",
95+
Help: "Start time of user session since unix epoch in seconds.",
96+
}, []string{"username", "remote_ip", "mtu", "vpn_ipv4", "vpn_ipv6", "device"})
9397
)
9498

9599
func main() {
@@ -120,6 +124,7 @@ func main() {
120124
vpnRX,
121125
vpnUserTX,
122126
vpnUserRX,
127+
vpnUserStartTime,
123128
)
124129

125130
occtlCli, err := occtl.NewClient(&occtl.OcctlCommander{})

lib/occtl/occtl.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ type StatusMessage struct {
3838

3939
// UsersMessage is a structure to decode JSON returned by "occtl --json show users"
4040
type UsersMessage struct {
41-
ID int64 `json:"ID"`
42-
Username string `json:"Username"`
43-
Vhost string `json:"vhost"`
44-
Device string `json:"Device"`
45-
MTU string `json:"MTU"`
46-
RemoteIP string `json:"Remote IP"`
47-
VPNIPv4 string `json:"IPv4"`
48-
VPNIPv6 string `json:"IPv6"`
49-
RawRX int64 `json:"RX,string"`
50-
RawTX int64 `json:"TX,string"`
51-
AverageRX string `json:"Average RX"`
52-
AverageTX string `json:"Average TX"`
41+
ID int64 `json:"ID"`
42+
Username string `json:"Username"`
43+
Vhost string `json:"vhost"`
44+
Device string `json:"Device"`
45+
MTU string `json:"MTU"`
46+
RemoteIP string `json:"Remote IP"`
47+
VPNIPv4 string `json:"IPv4"`
48+
VPNIPv6 string `json:"IPv6"`
49+
RawRX int64 `json:"RX,string"`
50+
RawTX int64 `json:"TX,string"`
51+
AverageRX string `json:"Average RX"`
52+
AverageTX string `json:"Average TX"`
53+
RawConnectedAt int64 `json:"raw_connected_at"`
5354
}
5455

5556
// Commander is an interface implementing exec commands

lib/occtl/occtl_test.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ var (
9797
"Hostname": "laptop",
9898
"Connected at": "2020-07-06 08:08",
9999
"_Connected at": " 8h:17m",
100+
"raw_connected_at": 1594015680,
100101
"Full session": "930gAhoVKQRcCIOf34rnpRj9Pyg=",
101102
"Session": "930gAh",
102103
"TLS ciphersuite": "(TLS1.2)-(ECDHE-ECDSA-SECP384R1)-(AES-256-GCM)",
@@ -141,6 +142,7 @@ var (
141142
"Hostname": "localhost",
142143
"Connected at": "2020-07-06 06:55",
143144
"_Connected at": " 9h:30m",
145+
"raw_connected_at": 1594011300,
144146
"Full session": "8HxOQEoXrjegoTkwioLK41ceims=",
145147
"Session": "8HxOQE",
146148
"TLS ciphersuite": "(TLS1.2)-(ECDHE-ECDSA-SECP384R1)-(AES-256-GCM)",
@@ -164,32 +166,34 @@ var (
164166
`)
165167
validShowUsersMessage = []UsersMessage{
166168
{
167-
ID: 4100,
168-
Username: "alice",
169-
Vhost: "default",
170-
Device: "vpns9",
171-
MTU: "1434",
172-
RemoteIP: "192.0.2.1",
173-
VPNIPv4: "198.51.100.59",
174-
VPNIPv6: "",
175-
RawRX: 9447052,
176-
RawTX: 328664295,
177-
AverageRX: "316 bytes/sec",
178-
AverageTX: "11.0 KB/sec",
169+
ID: 4100,
170+
Username: "alice",
171+
Vhost: "default",
172+
Device: "vpns9",
173+
MTU: "1434",
174+
RemoteIP: "192.0.2.1",
175+
VPNIPv4: "198.51.100.59",
176+
VPNIPv6: "",
177+
RawRX: 9447052,
178+
RawTX: 328664295,
179+
AverageRX: "316 bytes/sec",
180+
AverageTX: "11.0 KB/sec",
181+
RawConnectedAt: 1594015680,
179182
},
180183
{
181-
ID: 15313,
182-
Username: "bob",
183-
Device: "vpns2",
184-
Vhost: "default",
185-
MTU: "1340",
186-
RemoteIP: "192.0.2.2",
187-
VPNIPv4: "198.51.100.41",
188-
VPNIPv6: "",
189-
RawRX: 351042944,
190-
RawTX: 18291460815,
191-
AverageRX: "10.2 KB/sec",
192-
AverageTX: "534.1 KB/sec",
184+
ID: 15313,
185+
Username: "bob",
186+
Device: "vpns2",
187+
Vhost: "default",
188+
MTU: "1340",
189+
RemoteIP: "192.0.2.2",
190+
VPNIPv4: "198.51.100.41",
191+
VPNIPv6: "",
192+
RawRX: 351042944,
193+
RawTX: 18291460815,
194+
AverageRX: "10.2 KB/sec",
195+
AverageTX: "534.1 KB/sec",
196+
RawConnectedAt: 1594011300,
193197
},
194198
}
195199
)

0 commit comments

Comments
 (0)