Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Add TLS and basic authentication in web interface
Browse files Browse the repository at this point in the history
1) TLS and basic authentication are in flag `--web.config.file`:
https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md#web-configuration
2) All command-line arguments are with two dashes now.

Example:
./ovs-exporter --web.listen-address 10.224.141.48:9134 --database.vswitch.file.pid.path /run/openvswitch/ovsdb-server.pid --database.vswitch.socket.remote unix:/run/openvswitch/db.sock --service.vswitchd.file.pid.path /run/openvswitch/ovs-vswitchd.pid --system.run.dir /run/openvswitch --database.vswitch.file.log.path /var/log/kolla/openvswitch/ovsdb-server.log --service.vswitchd.file.log.path /var/log/kolla/openvswitch/ovs-vswitchd.log --log.level=debug --web.config.file web_config.yml

Prometheus Exporter Toolkit provides TLS and basic authentication in
exporters.
The Toolkit requires newer command-line and log packages.
So packages "flag" and "go-kit/log" were replaced.

https://github.com/greenpau/ovs_exporter/issues
  • Loading branch information
Dmitry-Eremeev committed Dec 5, 2024
1 parent 2ceae24 commit bd6d720
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 379 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ make
make qtest
```

## TLS and basic authentication

The OVS Exporter supports TLS and basic authentication.

To use TLS and/or basic authentication, you need to pass a configuration file
using the `--web.config.file` parameter. The format of the file is described
[in the exporter-toolkit repository](https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md).

## Exported Metrics

| Metric | Meaning | Labels |
Expand Down
142 changes: 54 additions & 88 deletions cmd/ovs_exporter/main.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,41 @@
package main

import (
"flag"
"fmt"
"log/slog"
"net/http"
"os"

"github.com/go-kit/log/level"
"github.com/alecthomas/kingpin/v2"
"github.com/greenpau/ovs_exporter/pkg/ovs_exporter"
ovs "github.com/greenpau/ovs_exporter/pkg/ovs_exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
)

func main() {
var listenAddress string
var metricsPath string
var pollTimeout int
var pollInterval int
var isShowVersion bool
var logLevel string
var systemRunDir string
var databaseVswitchName string
var databaseVswitchSocketRemote string
var databaseVswitchFileDataPath string
var databaseVswitchFileLogPath string
var databaseVswitchFilePidPath string
var databaseVswitchFileSystemIDPath string
var serviceVswitchdFileLogPath string
var serviceVswitchdFilePidPath string
var serviceOvnControllerFileLogPath string
var serviceOvnControllerFilePidPath string

flag.StringVar(&listenAddress, "web.listen-address", ":9475", "Address to listen on for web interface and telemetry.")
flag.StringVar(&metricsPath, "web.telemetry-path", "/metrics", "Path under which to expose metrics.")
flag.IntVar(&pollTimeout, "ovs.timeout", 2, "Timeout on JSON-RPC requests to OVS.")
flag.IntVar(&pollInterval, "ovs.poll-interval", 15, "The minimum interval (in seconds) between collections from OVS server.")
flag.BoolVar(&isShowVersion, "version", false, "version information")
flag.StringVar(&logLevel, "log.level", "info", "logging severity level")

flag.StringVar(&systemRunDir, "system.run.dir", "/var/run/openvswitch", "OVS default run directory.")

flag.StringVar(&databaseVswitchName, "database.vswitch.name", "Open_vSwitch", "The name of OVS db.")
flag.StringVar(&databaseVswitchSocketRemote, "database.vswitch.socket.remote", "unix:/var/run/openvswitch/db.sock", "JSON-RPC unix socket to OVS db.")
flag.StringVar(&databaseVswitchFileDataPath, "database.vswitch.file.data.path", "/etc/openvswitch/conf.db", "OVS db file.")
flag.StringVar(&databaseVswitchFileLogPath, "database.vswitch.file.log.path", "/var/log/openvswitch/ovsdb-server.log", "OVS db log file.")
flag.StringVar(&databaseVswitchFilePidPath, "database.vswitch.file.pid.path", "/var/run/openvswitch/ovsdb-server.pid", "OVS db process id file.")
flag.StringVar(&databaseVswitchFileSystemIDPath, "database.vswitch.file.system.id.path", "/etc/openvswitch/system-id.conf", "OVS system id file.")

flag.StringVar(&serviceVswitchdFileLogPath, "service.vswitchd.file.log.path", "/var/log/openvswitch/ovs-vswitchd.log", "OVS vswitchd daemon log file.")
flag.StringVar(&serviceVswitchdFilePidPath, "service.vswitchd.file.pid.path", "/var/run/openvswitch/ovs-vswitchd.pid", "OVS vswitchd daemon process id file.")

flag.StringVar(&serviceOvnControllerFileLogPath, "service.ovncontroller.file.log.path", "/var/log/openvswitch/ovn-controller.log", "OVN controller daemon log file.")
flag.StringVar(&serviceOvnControllerFilePidPath, "service.ovncontroller.file.pid.path", "/var/run/openvswitch/ovn-controller.pid", "OVN controller daemon process id file.")

var usageHelp = func() {
fmt.Fprintf(os.Stderr, "\n%s - Prometheus Exporter for Open Virtual Switch (OVS)\n\n", ovs.GetExporterName())
fmt.Fprintf(os.Stderr, "Usage: %s [arguments]\n\n", ovs.GetExporterName())
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nDocumentation: https://github.com/greenpau/ovs_exporter/\n\n")
}
flag.Usage = usageHelp
flag.Parse()

if isShowVersion {
var metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.",).Default("/metrics").String()
var pollTimeout = kingpin.Flag("ovs.timeout", "Timeout on JSON-RPC requests to OVS.").Default("2").Int()
var pollInterval = kingpin.Flag("ovs.poll-interval", "The minimum interval (in seconds) between collections from OVS server.").Default("15").Int()
var isShowVersion = kingpin.Flag("version", "version information").Default("false").Bool()
var logLevel = kingpin.Flag("log.level", "logging severity level").Default("info").String()
var systemRunDir = kingpin.Flag("system.run.dir", "OVS default run directory.").Default("/var/run/openvswitch").String()
var databaseVswitchName = kingpin.Flag("database.vswitch.name", "The name of OVS db.").Default("Open_vSwitch").String()
var databaseVswitchSocketRemote = kingpin.Flag("database.vswitch.socket.remote", "JSON-RPC unix socket to OVS db.").Default("unix:/var/run/openvswitch/db.sock").String()
var databaseVswitchFileDataPath = kingpin.Flag("database.vswitch.file.data.path", "OVS db file.").Default("/etc/openvswitch/conf.db").String()
var databaseVswitchFileLogPath = kingpin.Flag("database.vswitch.file.log.path", "OVS db log file.").Default("/var/log/openvswitch/ovsdb-server.log").String()
var databaseVswitchFilePidPath = kingpin.Flag("database.vswitch.file.pid.path", "OVS db process id file.").Default("/var/run/openvswitch/ovsdb-server.pid").String()
var databaseVswitchFileSystemIDPath = kingpin.Flag("database.vswitch.file.system.id.path", "OVS system id file.").Default("/etc/openvswitch/system-id.conf").String()
var serviceVswitchdFileLogPath = kingpin.Flag("service.vswitchd.file.log.path", "OVS vswitchd daemon log file.").Default("/var/log/openvswitch/ovs-vswitchd.log").String()
var serviceVswitchdFilePidPath = kingpin.Flag("service.vswitchd.file.pid.path", "OVS vswitchd daemon process id file.").Default("/var/run/openvswitch/ovs-vswitchd.pid").String()
var serviceOvnControllerFileLogPath = kingpin.Flag("service.ovncontroller.file.log.path", "OVN controller daemon log file.").Default("/var/log/openvswitch/ovn-controller.log").String()
var serviceOvnControllerFilePidPath = kingpin.Flag("service.ovncontroller.file.pid.path", "OVN controller daemon process id file.").Default("/var/run/openvswitch/ovn-controller.pid").String()
var toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9475")
kingpin.Parse()

if *isShowVersion {
fmt.Fprintf(os.Stdout, "%s %s", ovs.GetExporterName(), ovs.GetVersion())
if ovs.GetRevision() != "" {
fmt.Fprintf(os.Stdout, ", commit: %s\n", ovs.GetRevision())
Expand All @@ -71,70 +44,63 @@ func main() {
}
os.Exit(0)
}

logger, err := ovs.NewLogger(logLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "failed initializing logger: %v", err)
os.Exit(1)
logger, error := ovs_exporter.NewLogger(*logLevel)
if error != nil {
panic(error)
}
slog.SetDefault(&logger)

level.Info(logger).Log(
"msg", "Starting exporter",
"exporter", ovs.GetExporterName(),
"version", ovs.GetVersionInfo(),
"build_context", ovs.GetVersionBuildContext(),
slog.Info("Starting exporter",
"exporter", ovs.GetExporterName(),
"version", ovs.GetVersionInfo(),
"build_context", ovs.GetVersionBuildContext(),
)

opts := ovs.Options{
Timeout: pollTimeout,
Logger: logger,
Timeout: *pollTimeout,
Logger: *slog.Default(),
}

exporter := ovs.NewExporter(opts)

exporter.Client.System.RunDir = systemRunDir
exporter.Client.System.RunDir = *systemRunDir

exporter.Client.Database.Vswitch.Name = databaseVswitchName
exporter.Client.Database.Vswitch.Socket.Remote = databaseVswitchSocketRemote
exporter.Client.Database.Vswitch.File.Data.Path = databaseVswitchFileDataPath
exporter.Client.Database.Vswitch.File.Log.Path = databaseVswitchFileLogPath
exporter.Client.Database.Vswitch.File.Pid.Path = databaseVswitchFilePidPath
exporter.Client.Database.Vswitch.File.SystemID.Path = databaseVswitchFileSystemIDPath
exporter.Client.Database.Vswitch.Name = *databaseVswitchName
exporter.Client.Database.Vswitch.Socket.Remote = *databaseVswitchSocketRemote
exporter.Client.Database.Vswitch.File.Data.Path = *databaseVswitchFileDataPath
exporter.Client.Database.Vswitch.File.Log.Path = *databaseVswitchFileLogPath
exporter.Client.Database.Vswitch.File.Pid.Path = *databaseVswitchFilePidPath
exporter.Client.Database.Vswitch.File.SystemID.Path = *databaseVswitchFileSystemIDPath

exporter.Client.Service.Vswitchd.File.Log.Path = serviceVswitchdFileLogPath
exporter.Client.Service.Vswitchd.File.Pid.Path = serviceVswitchdFilePidPath
exporter.Client.Service.Vswitchd.File.Log.Path = *serviceVswitchdFileLogPath
exporter.Client.Service.Vswitchd.File.Pid.Path = *serviceVswitchdFilePidPath

exporter.Client.Service.OvnController.File.Log.Path = serviceOvnControllerFileLogPath
exporter.Client.Service.OvnController.File.Pid.Path = serviceOvnControllerFilePidPath
exporter.Client.Service.OvnController.File.Log.Path = *serviceOvnControllerFileLogPath
exporter.Client.Service.OvnController.File.Pid.Path = *serviceOvnControllerFilePidPath
if err := exporter.Connect(); err != nil {
level.Error(logger).Log(
"msg", "failed to init properly",
"error", err.Error(),
)
slog.Error("failed to init properly", "error", err.Error(),)
os.Exit(1)
}

level.Info(logger).Log("ovs_system_id", exporter.Client.System.ID)
slog.Info("ovs_system_id", "ovs_system_id", exporter.Client.System.ID)

exporter.SetPollInterval(int64(pollInterval))
exporter.SetPollInterval(int64(*pollInterval))
prometheus.MustRegister(exporter)

http.Handle(metricsPath, promhttp.Handler())
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>OVS Exporter</title></head>
<body>
<h1>OVS Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})

level.Info(logger).Log("listen_on ", listenAddress)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
level.Error(logger).Log(
"msg", "listener failed",
"error", err.Error(),
server := &http.Server{}
if err := web.ListenAndServe(server, toolkitFlags, slog.Default()); err != nil {
slog.Error("listener failed", "error", err.Error(),
)
os.Exit(1)
}
Expand Down
39 changes: 27 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
module github.com/greenpau/ovs_exporter

go 1.20
go 1.22

toolchain go1.22.8

require (
github.com/go-kit/log v0.2.1
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/prometheus/client_golang v1.20.4
github.com/prometheus/common v0.60.1
github.com/prometheus/exporter-toolkit v0.13.1
github.com/greenpau/ovsdb v1.0.4
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/common v0.44.0
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
golang.org/x/sys v0.8.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/mdlayher/vsock v1.2.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit bd6d720

Please sign in to comment.