Skip to content

Commit d8b43c6

Browse files
authored
Merge pull request #3 from patrickjahns/disable_per_client_metrics
Disable per client metrics
2 parents 18c48d2 + 5a1ac96 commit d8b43c6

File tree

4 files changed

+74
-30
lines changed

4 files changed

+74
-30
lines changed

pkg/collector/openvpn.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type OpenVPNCollector struct {
1212
logger log.Logger
1313
name string
1414
statusFile string
15+
collectClientMetrics bool
1516
LastUpdated *prometheus.Desc
1617
ConnectedClients *prometheus.Desc
1718
BytesReceived *prometheus.Desc
@@ -21,11 +22,12 @@ type OpenVPNCollector struct {
2122
}
2223

2324
// NewOpenVPNCollector returns a new OpenVPNCollector
24-
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *OpenVPNCollector {
25+
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string, collectClientMetrics bool) *OpenVPNCollector {
2526
return &OpenVPNCollector{
26-
logger: logger,
27-
statusFile: statusFile,
28-
name: name,
27+
logger: logger,
28+
statusFile: statusFile,
29+
name: name,
30+
collectClientMetrics: collectClientMetrics,
2931

3032
LastUpdated: prometheus.NewDesc(
3133
prometheus.BuildFQName(namespace, "", "last_updated"),
@@ -70,10 +72,12 @@ func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *Ope
7072
func (c *OpenVPNCollector) Describe(ch chan<- *prometheus.Desc) {
7173
ch <- c.LastUpdated
7274
ch <- c.ConnectedClients
73-
ch <- c.BytesSent
74-
ch <- c.BytesReceived
75-
ch <- c.ConnectedSince
7675
ch <- c.MaxBcastMcastQueueLen
76+
if c.collectClientMetrics {
77+
ch <- c.BytesSent
78+
ch <- c.BytesReceived
79+
ch <- c.ConnectedSince
80+
}
7781
}
7882

7983
// Collect is called by the Prometheus registry when collecting metrics.
@@ -100,24 +104,26 @@ func (c *OpenVPNCollector) Collect(ch chan<- prometheus.Metric) {
100104
"bytesReceived", client.BytesReceived,
101105
"bytesSent", client.BytesSent,
102106
)
103-
ch <- prometheus.MustNewConstMetric(
104-
c.BytesReceived,
105-
prometheus.GaugeValue,
106-
client.BytesReceived,
107-
c.name, client.CommonName,
108-
)
109-
ch <- prometheus.MustNewConstMetric(
110-
c.BytesSent,
111-
prometheus.GaugeValue,
112-
client.BytesSent,
113-
c.name, client.CommonName,
114-
)
115-
ch <- prometheus.MustNewConstMetric(
116-
c.ConnectedSince,
117-
prometheus.GaugeValue,
118-
float64(client.ConnectedSince.Unix()),
119-
c.name, client.CommonName,
120-
)
107+
if c.collectClientMetrics {
108+
ch <- prometheus.MustNewConstMetric(
109+
c.BytesReceived,
110+
prometheus.GaugeValue,
111+
client.BytesReceived,
112+
c.name, client.CommonName,
113+
)
114+
ch <- prometheus.MustNewConstMetric(
115+
c.BytesSent,
116+
prometheus.GaugeValue,
117+
client.BytesSent,
118+
c.name, client.CommonName,
119+
)
120+
ch <- prometheus.MustNewConstMetric(
121+
c.ConnectedSince,
122+
prometheus.GaugeValue,
123+
float64(client.ConnectedSince.Unix()),
124+
c.name, client.CommonName,
125+
)
126+
}
121127
}
122128
level.Debug(c.logger).Log(
123129
"updatedAt", status.UpdatedAt,

pkg/command/command.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ func Run() error {
6868
Usage: "The OpenVPN status file(s) to export (example test:./example/version1.status )",
6969
Required: true,
7070
},
71+
&cli.BoolFlag{
72+
Name: "disable-client-metrics",
73+
Usage: "Disables per client (bytes_received, bytes_sent, connected_since) metrics",
74+
},
7175
}
7276

7377
app.Before = func(c *cli.Context) error {
74-
cfg.StatusFile = c.StringSlice("status-file")
78+
cfg.StatusCollector.StatusFile = c.StringSlice("status-file")
79+
cfg.StatusCollector.ExportClientMetrics = !c.Bool("disable-client-metrics")
7580
return nil
7681
}
7782

@@ -105,7 +110,7 @@ func run(c *cli.Context, cfg *config.Config) error {
105110
version.GoVersion,
106111
version.Started,
107112
))
108-
for _, statusFile := range cfg.StatusFile {
113+
for _, statusFile := range cfg.StatusCollector.StatusFile {
109114
serverName, statusFile := parseStatusFileSlice(statusFile)
110115

111116
level.Info(logger).Log(
@@ -117,6 +122,7 @@ func run(c *cli.Context, cfg *config.Config) error {
117122
logger,
118123
serverName,
119124
statusFile,
125+
cfg.StatusCollector.ExportClientMetrics,
120126
))
121127
}
122128

pkg/config/config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ type Logs struct {
1414

1515
// Config defines the general configuration object
1616
type Config struct {
17-
Server Server
18-
Logs Logs
19-
StatusFile []string
17+
Server Server
18+
Logs Logs
19+
StatusCollector StatusCollector
20+
}
21+
22+
// StatusCollector contains configuration for the OpenVPN status collector
23+
type StatusCollector struct {
24+
ExportClientMetrics bool
25+
StatusFile []string
2026
}
2127

2228
// Load initializes a default configuration struct.

pkg/openvpn/parser_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,29 @@ func TestConnectedClientsParsedCorrectly(t *testing.T) {
9191
t.Errorf("Clients are not parsed correctly")
9292
}
9393
}
94+
95+
const badFields = `OpenVPN CLIENT LIST
96+
Updated,test
97+
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
98+
user1,1.2.3.4,foo,foo,test
99+
ROUTING TABLE
100+
Virtual Address,Common Name,Real Address,Last Ref
101+
10.240.1.222,user4,1.2.3.7:fooo,test
102+
GLOBAL STATS
103+
Max bcast/mcast queue length,foo
104+
END
105+
`
106+
107+
func TestParsingWrongValuesIsNotAnIssue(t *testing.T) {
108+
status, e := parse(bufio.NewReader(strings.NewReader(badFields)))
109+
if e != nil {
110+
t.Errorf("should have worked")
111+
}
112+
if status.GlobalStats.MaxBcastMcastQueueLen != 0 {
113+
t.Errorf("Parsing wrong MaxBcastMcastQueueLen value lead to unexpected result")
114+
}
115+
expectedTime := time.Time{}
116+
if !expectedTime.Equal(status.UpdatedAt) {
117+
t.Errorf("parsing incorrect time value should have yieleded a default time object")
118+
}
119+
}

0 commit comments

Comments
 (0)