-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (102 loc) · 3.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/natrontech/openvpn-exporter/exporters"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// These variables are set in build step
var Version = "v0.0.0-dev.0"
var Commit = "none"
var BuildTime = "unknown"
func main() {
var (
statusfiles = flag.String("openvpn.status-files", "/var/run/openvpn-status.log",
"Path to OpenVPN status files")
metricsPath = flag.String("openvpn.metrics-path", "/metrics",
"Path under which to expose metrics")
listenAddress = flag.String("openvpn.listen-address", ":9176",
"Address on which to expose metrics")
loglevel = flag.String("openvpn.loglevel", "info",
"Loglevel")
ignoreIndividuals = flag.String("openvpn.ignore-individuals", "false",
"If ignoring metrics for individuals")
showVersion = flag.Bool("version", false, "Show version and exit")
)
flag.Parse()
if *showVersion {
fmt.Printf("OpenVPN Exporter Version: %s, Commit: %s, Build Time: %s\n", Version, Commit, BuildTime)
os.Exit(0)
}
// log build information
log.Printf("INFO: Starting OpenVPN Exporter %s, commit %s, built at %s", Version, Commit, BuildTime)
// if env variable is set, it will overwrite defaults or flags
if os.Getenv("OPENVPN_LOGLEVEL") != "" {
*loglevel = os.Getenv("OPENVPN_LOGLEVEL")
}
if os.Getenv("OPENVPN_STATUS_FILES") != "" {
*statusfiles = os.Getenv("OPENVPN_STATUS_FILES")
}
if os.Getenv("OPENVPN_METRICS_PATH") != "" {
*metricsPath = os.Getenv("OPENVPN_METRICS_PATH")
}
if os.Getenv("OPENVPN_LISTEN_ADDRESS") != "" {
*listenAddress = os.Getenv("OPENVPN_LISTEN_ADDRESS")
}
if os.Getenv("OPENVPN_IGNORE_INDIVIDUALS") != "" {
*ignoreIndividuals = os.Getenv("OPENVPN_IGNORE_INDIVIDUALS")
}
// convert flags
ignoreIndividualsBool, err := strconv.ParseBool(*ignoreIndividuals)
if err != nil {
log.Fatalf("ERROR: Unable to parse ignoreIndividuals: %s", err)
}
files := strings.Split(*statusfiles, ",")
// check if statusfile exists
for _, file := range files {
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Fatalf("ERROR: Status file does not exist: %s", file)
}
}
// debug
if *loglevel == "debug" {
log.Printf("DEBUG: Using status files: %s", *statusfiles)
log.Printf("DEBUG: Using metrics path: %s", *metricsPath)
log.Printf("DEBUG: Using listen address: %s", *listenAddress)
log.Printf("DEBUG: Ignoring individuals: %t", ignoreIndividualsBool)
}
log.Printf("INFO: Listening on: %s", *listenAddress)
log.Printf("INFO: Metrics path: %s", *metricsPath)
exporter, err := exporters.NewOpenVPNExporter(files, ignoreIndividualsBool)
if err != nil {
panic(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`<html>
<head><title>OpenVPN Exporter</title></head>
<body>
<h1>OpenVPN Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Printf("ERROR: Failed to write response: %s", err)
}
})
server := &http.Server{
Addr: *listenAddress,
Handler: nil,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
log.Fatal(server.ListenAndServe())
}