Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Feature/glob support #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ As it is not uncommon to run multiple instances of OpenVPN on a single
system (e.g., multiple servers, multiple clients or a mixture of both),
this exporter can be configured to scrape and export the status of
multiple status files, using the `-openvpn.status_paths` command line
flag. Paths need to be comma separated. Metrics for all status files are
exported over TCP port 9176.
flag. Paths or [globs](https://en.wikipedia.org/wiki/Glob_(programming))
need to be comma separated. Metrics for all status files are exported
over TCP port 9176.

Please refer to this utility's `main()` function for a full list of
supported command line flags.
Expand Down
38 changes: 23 additions & 15 deletions exporters/openvpn_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"io"
"log"
"path/filepath"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -343,21 +344,28 @@ func (e *OpenVPNExporter) Describe(ch chan<- *prometheus.Desc) {
}

func (e *OpenVPNExporter) Collect(ch chan<- prometheus.Metric) {
for _, statusPath := range e.statusPaths {
err := e.collectStatusFromFile(statusPath, ch)
if err == nil {
ch <- prometheus.MustNewConstMetric(
e.openvpnUpDesc,
prometheus.GaugeValue,
1.0,
statusPath)
} else {
log.Printf("Failed to scrape showq socket: %s", err)
ch <- prometheus.MustNewConstMetric(
e.openvpnUpDesc,
prometheus.GaugeValue,
0.0,
statusPath)
for _, statusPathGlob := range e.statusPaths {
matches, err := filepath.Glob(statusPathGlob)
if err != nil {
log.Printf("Glob failed on %v: %v", statusPathGlob, err)
continue
}
for _, statusPath := range matches {
err := e.collectStatusFromFile(statusPath, ch)
if err == nil {
ch <- prometheus.MustNewConstMetric(
e.openvpnUpDesc,
prometheus.GaugeValue,
1.0,
statusPath)
} else {
log.Printf("Failed to scrape showq socket: %s", err)
ch <- prometheus.MustNewConstMetric(
e.openvpnUpDesc,
prometheus.GaugeValue,
0.0,
statusPath)
}
}
}
}