Skip to content

Commit

Permalink
Merge pull request #3 from patrickjahns/disable_per_client_metrics
Browse files Browse the repository at this point in the history
Disable per client metrics
  • Loading branch information
patrickjahns authored Apr 26, 2020
2 parents 18c48d2 + 5a1ac96 commit d8b43c6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
56 changes: 31 additions & 25 deletions pkg/collector/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type OpenVPNCollector struct {
logger log.Logger
name string
statusFile string
collectClientMetrics bool
LastUpdated *prometheus.Desc
ConnectedClients *prometheus.Desc
BytesReceived *prometheus.Desc
Expand All @@ -21,11 +22,12 @@ type OpenVPNCollector struct {
}

// NewOpenVPNCollector returns a new OpenVPNCollector
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *OpenVPNCollector {
func NewOpenVPNCollector(logger log.Logger, name string, statusFile string, collectClientMetrics bool) *OpenVPNCollector {
return &OpenVPNCollector{
logger: logger,
statusFile: statusFile,
name: name,
logger: logger,
statusFile: statusFile,
name: name,
collectClientMetrics: collectClientMetrics,

LastUpdated: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "last_updated"),
Expand Down Expand Up @@ -70,10 +72,12 @@ func NewOpenVPNCollector(logger log.Logger, name string, statusFile string) *Ope
func (c *OpenVPNCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.LastUpdated
ch <- c.ConnectedClients
ch <- c.BytesSent
ch <- c.BytesReceived
ch <- c.ConnectedSince
ch <- c.MaxBcastMcastQueueLen
if c.collectClientMetrics {
ch <- c.BytesSent
ch <- c.BytesReceived
ch <- c.ConnectedSince
}
}

// Collect is called by the Prometheus registry when collecting metrics.
Expand All @@ -100,24 +104,26 @@ func (c *OpenVPNCollector) Collect(ch chan<- prometheus.Metric) {
"bytesReceived", client.BytesReceived,
"bytesSent", client.BytesSent,
)
ch <- prometheus.MustNewConstMetric(
c.BytesReceived,
prometheus.GaugeValue,
client.BytesReceived,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.BytesSent,
prometheus.GaugeValue,
client.BytesSent,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.ConnectedSince,
prometheus.GaugeValue,
float64(client.ConnectedSince.Unix()),
c.name, client.CommonName,
)
if c.collectClientMetrics {
ch <- prometheus.MustNewConstMetric(
c.BytesReceived,
prometheus.GaugeValue,
client.BytesReceived,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.BytesSent,
prometheus.GaugeValue,
client.BytesSent,
c.name, client.CommonName,
)
ch <- prometheus.MustNewConstMetric(
c.ConnectedSince,
prometheus.GaugeValue,
float64(client.ConnectedSince.Unix()),
c.name, client.CommonName,
)
}
}
level.Debug(c.logger).Log(
"updatedAt", status.UpdatedAt,
Expand Down
10 changes: 8 additions & 2 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ func Run() error {
Usage: "The OpenVPN status file(s) to export (example test:./example/version1.status )",
Required: true,
},
&cli.BoolFlag{
Name: "disable-client-metrics",
Usage: "Disables per client (bytes_received, bytes_sent, connected_since) metrics",
},
}

app.Before = func(c *cli.Context) error {
cfg.StatusFile = c.StringSlice("status-file")
cfg.StatusCollector.StatusFile = c.StringSlice("status-file")
cfg.StatusCollector.ExportClientMetrics = !c.Bool("disable-client-metrics")
return nil
}

Expand Down Expand Up @@ -105,7 +110,7 @@ func run(c *cli.Context, cfg *config.Config) error {
version.GoVersion,
version.Started,
))
for _, statusFile := range cfg.StatusFile {
for _, statusFile := range cfg.StatusCollector.StatusFile {
serverName, statusFile := parseStatusFileSlice(statusFile)

level.Info(logger).Log(
Expand All @@ -117,6 +122,7 @@ func run(c *cli.Context, cfg *config.Config) error {
logger,
serverName,
statusFile,
cfg.StatusCollector.ExportClientMetrics,
))
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ type Logs struct {

// Config defines the general configuration object
type Config struct {
Server Server
Logs Logs
StatusFile []string
Server Server
Logs Logs
StatusCollector StatusCollector
}

// StatusCollector contains configuration for the OpenVPN status collector
type StatusCollector struct {
ExportClientMetrics bool
StatusFile []string
}

// Load initializes a default configuration struct.
Expand Down
26 changes: 26 additions & 0 deletions pkg/openvpn/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,29 @@ func TestConnectedClientsParsedCorrectly(t *testing.T) {
t.Errorf("Clients are not parsed correctly")
}
}

const badFields = `OpenVPN CLIENT LIST
Updated,test
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
user1,1.2.3.4,foo,foo,test
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.240.1.222,user4,1.2.3.7:fooo,test
GLOBAL STATS
Max bcast/mcast queue length,foo
END
`

func TestParsingWrongValuesIsNotAnIssue(t *testing.T) {
status, e := parse(bufio.NewReader(strings.NewReader(badFields)))
if e != nil {
t.Errorf("should have worked")
}
if status.GlobalStats.MaxBcastMcastQueueLen != 0 {
t.Errorf("Parsing wrong MaxBcastMcastQueueLen value lead to unexpected result")
}
expectedTime := time.Time{}
if !expectedTime.Equal(status.UpdatedAt) {
t.Errorf("parsing incorrect time value should have yieleded a default time object")
}
}

0 comments on commit d8b43c6

Please sign in to comment.