From aee1e4b1fd1a6bae3b4c6d0c256bd6960b66d97c Mon Sep 17 00:00:00 2001 From: Calle Pettersson Date: Sat, 12 Aug 2017 07:44:59 +0100 Subject: [PATCH] Change to kingpin for flags --- collector/logical_disk.go | 12 +++++++++--- collector/net.go | 12 +++++++++--- collector/process.go | 7 +++++-- collector/service.go | 7 +++++-- exporter.go | 41 ++++++++++++++++++++------------------- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/collector/logical_disk.go b/collector/logical_disk.go index 1e678bcb..d3fc3a26 100644 --- a/collector/logical_disk.go +++ b/collector/logical_disk.go @@ -5,13 +5,13 @@ package collector import ( - "flag" "fmt" "log" "regexp" "github.com/StackExchange/wmi" "github.com/prometheus/client_golang/prometheus" + "gopkg.in/alecthomas/kingpin.v2" ) func init() { @@ -19,8 +19,14 @@ func init() { } var ( - volumeWhitelist = flag.String("collector.logical_disk.volume-whitelist", ".+", "Regexp of volumes to whitelist. Volume name must both match whitelist and not match blacklist to be included.") - volumeBlacklist = flag.String("collector.logical_disk.volume-blacklist", "", "Regexp of volumes to blacklist. Volume name must both match whitelist and not match blacklist to be included.") + volumeWhitelist = kingpin.Flag( + "collector.logical_disk.volume-whitelist", + "Regexp of volumes to whitelist. Volume name must both match whitelist and not match blacklist to be included.", + ).Default(".+").String() + volumeBlacklist = kingpin.Flag( + "collector.logical_disk.volume-blacklist", + "Regexp of volumes to blacklist. Volume name must both match whitelist and not match blacklist to be included.", + ).Default("").String() ) // A LogicalDiskCollector is a Prometheus collector for WMI Win32_PerfRawData_PerfDisk_LogicalDisk metrics diff --git a/collector/net.go b/collector/net.go index b44ea510..51080875 100644 --- a/collector/net.go +++ b/collector/net.go @@ -7,13 +7,13 @@ package collector import ( - "flag" "fmt" "log" "regexp" "github.com/StackExchange/wmi" "github.com/prometheus/client_golang/prometheus" + "gopkg.in/alecthomas/kingpin.v2" ) func init() { @@ -21,8 +21,14 @@ func init() { } var ( - nicWhitelist = flag.String("collector.net.nic-whitelist", ".+", "Regexp of NIC:s to whitelist. NIC name must both match whitelist and not match blacklist to be included.") - nicBlacklist = flag.String("collector.net.nic-blacklist", "", "Regexp of NIC:s to blacklist. NIC name must both match whitelist and not match blacklist to be included.") + nicWhitelist = kingpin.Flag( + "collector.net.nic-whitelist", + "Regexp of NIC:s to whitelist. NIC name must both match whitelist and not match blacklist to be included.", + ).Default(".+").String() + nicBlacklist = kingpin.Flag( + "collector.net.nic-blacklist", + "Regexp of NIC:s to blacklist. NIC name must both match whitelist and not match blacklist to be included.", + ).Default("").String() nicNameToUnderscore = regexp.MustCompile("[^a-zA-Z0-9]") ) diff --git a/collector/process.go b/collector/process.go index 773d2233..e2e363c3 100644 --- a/collector/process.go +++ b/collector/process.go @@ -4,13 +4,13 @@ package collector import ( "bytes" - "flag" "log" "strconv" "strings" "github.com/StackExchange/wmi" "github.com/prometheus/client_golang/prometheus" + "gopkg.in/alecthomas/kingpin.v2" ) func init() { @@ -18,7 +18,10 @@ func init() { } var ( - processWhereClause = flag.String("collector.process.processes-where", "", "WQL 'where' clause to use in WMI metrics query. Limits the response to the processes you specify and reduces the size of the response.") + processWhereClause = kingpin.Flag( + "collector.process.processes-where", + "WQL 'where' clause to use in WMI metrics query. Limits the response to the processes you specify and reduces the size of the response.", + ).Default("").String() ) // A ProcessCollector is a Prometheus collector for WMI Win32_PerfRawData_PerfProc_Process metrics diff --git a/collector/service.go b/collector/service.go index 64c9b91a..fbc91c89 100644 --- a/collector/service.go +++ b/collector/service.go @@ -4,12 +4,12 @@ package collector import ( "bytes" - "flag" "log" "strings" "github.com/StackExchange/wmi" "github.com/prometheus/client_golang/prometheus" + "gopkg.in/alecthomas/kingpin.v2" ) func init() { @@ -17,7 +17,10 @@ func init() { } var ( - serviceWhereClause = flag.String("collector.service.services-where", "", "WQL 'where' clause to use in WMI metrics query. Limits the response to the services you specify and reduces the size of the response.") + serviceWhereClause = kingpin.Flag( + "collector.service.services-where", + "WQL 'where' clause to use in WMI metrics query. Limits the response to the services you specify and reduces the size of the response.", + ).Default("").String() ) // A serviceCollector is a Prometheus collector for WMI Win32_Service metrics diff --git a/exporter.go b/exporter.go index ee97403f..5909c36c 100644 --- a/exporter.go +++ b/exporter.go @@ -1,11 +1,9 @@ package main import ( - "flag" "fmt" "io" "net/http" - "os" "sort" "strings" "sync" @@ -19,6 +17,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/log" "github.com/prometheus/common/version" + "gopkg.in/alecthomas/kingpin.v2" ) // WmiCollector implements the prometheus.Collector interface. @@ -157,28 +156,30 @@ func initWbem() { wmi.DefaultClient.SWbemServicesClient = s } -func usage() { - fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) - flag.PrintDefaults() - fmt.Fprintf(os.Stderr, "\nNote: If executing from Powershell, the flags need to quoted. For example:\n%s\n", - "\twmi_exporter \"-collectors.enabled\" iis") -} - func main() { var ( - showVersion = flag.Bool("version", false, "Print version information.") - listenAddress = flag.String("telemetry.addr", ":9182", "host:port for WMI exporter.") - metricsPath = flag.String("telemetry.path", "/metrics", "URL path for surfacing collected metrics.") - enabledCollectors = flag.String("collectors.enabled", filterAvailableCollectors(defaultCollectors), "Comma-separated list of collectors to use. Use '[defaults]' as a placeholder for all the collectors enabled by default") - printCollectors = flag.Bool("collectors.print", false, "If true, print available collectors and exit.") + listenAddress = kingpin.Flag( + "telemetry.addr", + "host:port for WMI exporter.", + ).Default(":9182").String() + metricsPath = kingpin.Flag( + "telemetry.path", + "URL path for surfacing collected metrics.", + ).Default("/metrics").String() + enabledCollectors = kingpin.Flag( + "collectors.enabled", + "Comma-separated list of collectors to use. Use '[default]' as a placeholder for all the collectors enabled by default."). + Default(filterAvailableCollectors(defaultCollectors)).String() + printCollectors = kingpin.Flag( + "collectors.print", + "If true, print available collectors and exit.", + ).Bool() ) - flag.Usage = usage - flag.Parse() - if *showVersion { - fmt.Fprintln(os.Stdout, version.Print("wmi_exporter")) - os.Exit(0) - } + log.AddFlags(kingpin.CommandLine) + kingpin.Version(version.Print("wmi_exporter")) + kingpin.HelpFlag.Short('h') + kingpin.Parse() if *printCollectors { collectorNames := make(sort.StringSlice, 0, len(collector.Factories))