2018-11-30 00:51:12 +00:00
|
|
|
// +build windows
|
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-09-01 14:04:43 +00:00
|
|
|
"fmt"
|
2016-08-26 06:59:27 +00:00
|
|
|
"net/http"
|
2016-09-01 14:04:43 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2016-08-26 06:59:27 +00:00
|
|
|
"sync"
|
2016-09-01 14:04:43 +00:00
|
|
|
"time"
|
2016-08-26 06:59:27 +00:00
|
|
|
|
2016-09-16 06:36:58 +00:00
|
|
|
"golang.org/x/sys/windows/svc"
|
|
|
|
|
2017-06-26 19:03:17 +00:00
|
|
|
"github.com/StackExchange/wmi"
|
2016-09-01 12:55:35 +00:00
|
|
|
"github.com/martinlindhe/wmi_exporter/collector"
|
2016-08-26 06:59:27 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-04-30 22:12:05 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2016-09-01 14:04:43 +00:00
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
"github.com/prometheus/common/version"
|
2017-08-12 06:44:59 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
// WmiCollector implements the prometheus.Collector interface.
|
|
|
|
type WmiCollector struct {
|
2019-05-15 19:22:29 +00:00
|
|
|
maxScrapeDuration time.Duration
|
|
|
|
collectors map[string]collector.Collector
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
const (
|
2018-04-03 19:37:10 +00:00
|
|
|
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system,textfile"
|
2017-03-04 11:44:47 +00:00
|
|
|
defaultCollectorsPlaceholder = "[defaults]"
|
|
|
|
serviceName = "wmi_exporter"
|
2016-09-01 14:04:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-07-05 11:12:42 +00:00
|
|
|
scrapeDurationDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_duration_seconds"),
|
|
|
|
"wmi_exporter: Duration of a collection.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
scrapeSuccessDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_success"),
|
|
|
|
"wmi_exporter: Whether the collector was successful.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
2016-09-01 14:04:43 +00:00
|
|
|
)
|
2019-05-15 19:22:29 +00:00
|
|
|
scrapeTimeoutDesc = prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(collector.Namespace, "exporter", "collector_timeout"),
|
|
|
|
"wmi_exporter: Whether the collector timed out.",
|
|
|
|
[]string{"collector"},
|
|
|
|
nil,
|
|
|
|
)
|
2018-04-29 14:53:34 +00:00
|
|
|
|
|
|
|
// This can be removed when client_golang exposes this on Windows
|
|
|
|
// (See https://github.com/prometheus/client_golang/issues/376)
|
2018-06-06 08:31:50 +00:00
|
|
|
startTime = float64(time.Now().Unix())
|
2018-04-29 14:53:34 +00:00
|
|
|
startTimeDesc = prometheus.NewDesc(
|
|
|
|
"process_start_time_seconds",
|
|
|
|
"Start time of the process since unix epoch in seconds.",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2016-09-01 14:04:43 +00:00
|
|
|
)
|
2016-08-26 06:59:27 +00:00
|
|
|
|
|
|
|
// Describe sends all the descriptors of the collectors included to
|
|
|
|
// the provided channel.
|
2016-09-01 14:04:43 +00:00
|
|
|
func (coll WmiCollector) Describe(ch chan<- *prometheus.Desc) {
|
2017-07-05 11:12:42 +00:00
|
|
|
ch <- scrapeDurationDesc
|
|
|
|
ch <- scrapeSuccessDesc
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the collected metrics from each of the collectors to
|
2019-05-15 19:22:29 +00:00
|
|
|
// prometheus.
|
2016-09-01 14:04:43 +00:00
|
|
|
func (coll WmiCollector) Collect(ch chan<- prometheus.Metric) {
|
2019-04-05 13:59:40 +00:00
|
|
|
scrapeContext, err := collector.PrepareScrapeContext()
|
|
|
|
if err != nil {
|
|
|
|
ch <- prometheus.NewInvalidMetric(scrapeSuccessDesc, fmt.Errorf("failed to prepare scrape: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-15 19:22:29 +00:00
|
|
|
remainingCollectors := make(map[string]bool)
|
|
|
|
for name := range coll.collectors {
|
|
|
|
remainingCollectors[name] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
metricsBuffer := make(chan prometheus.Metric)
|
|
|
|
allDone := make(chan struct{})
|
|
|
|
stopped := false
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-metricsBuffer:
|
|
|
|
if !stopped {
|
|
|
|
ch <- m
|
|
|
|
}
|
|
|
|
case <-allDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(coll.collectors))
|
2019-05-15 19:22:29 +00:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(allDone)
|
|
|
|
close(metricsBuffer)
|
|
|
|
}()
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
for name, c := range coll.collectors {
|
|
|
|
go func(name string, c collector.Collector) {
|
2019-05-15 19:22:29 +00:00
|
|
|
execute(name, c, scrapeContext, metricsBuffer)
|
2016-09-01 14:04:43 +00:00
|
|
|
wg.Done()
|
2019-05-15 19:22:29 +00:00
|
|
|
delete(remainingCollectors, name)
|
2016-09-01 14:04:43 +00:00
|
|
|
}(name, c)
|
|
|
|
}
|
2018-04-29 14:53:34 +00:00
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
startTimeDesc,
|
|
|
|
prometheus.CounterValue,
|
|
|
|
startTime,
|
|
|
|
)
|
2019-05-15 19:22:29 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-allDone:
|
|
|
|
stopped = true
|
|
|
|
return
|
|
|
|
case <-time.After(coll.maxScrapeDuration):
|
|
|
|
stopped = true
|
|
|
|
remainingCollectorNames := make([]string, 0, len(remainingCollectors))
|
|
|
|
for rc := range remainingCollectors {
|
|
|
|
remainingCollectorNames = append(remainingCollectorNames, rc)
|
|
|
|
}
|
|
|
|
log.Warn("Collection timed out, still waiting for ", remainingCollectorNames)
|
|
|
|
for name := range remainingCollectors {
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
scrapeSuccessDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
0.0,
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
scrapeTimeoutDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
1.0,
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2016-09-01 14:04:43 +00:00
|
|
|
}
|
2016-08-26 06:59:27 +00:00
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
func filterAvailableCollectors(collectors string) string {
|
|
|
|
var availableCollectors []string
|
|
|
|
for _, c := range strings.Split(collectors, ",") {
|
|
|
|
_, ok := collector.Factories[c]
|
|
|
|
if ok {
|
|
|
|
availableCollectors = append(availableCollectors, c)
|
|
|
|
}
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
2016-09-01 14:04:43 +00:00
|
|
|
return strings.Join(availableCollectors, ",")
|
|
|
|
}
|
|
|
|
|
2019-04-05 13:59:40 +00:00
|
|
|
func execute(name string, c collector.Collector, ctx *collector.ScrapeContext, ch chan<- prometheus.Metric) {
|
2016-09-01 14:04:43 +00:00
|
|
|
begin := time.Now()
|
2019-04-05 13:59:40 +00:00
|
|
|
err := c.Collect(ctx, ch)
|
2016-09-01 14:04:43 +00:00
|
|
|
duration := time.Since(begin)
|
2017-07-05 11:12:42 +00:00
|
|
|
var success float64
|
2016-09-01 14:04:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Errorf("collector %s failed after %fs: %s", name, duration.Seconds(), err)
|
2017-07-05 11:12:42 +00:00
|
|
|
success = 0
|
2016-09-01 14:04:43 +00:00
|
|
|
} else {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Debugf("collector %s succeeded after %fs.", name, duration.Seconds())
|
2017-07-05 11:12:42 +00:00
|
|
|
success = 1
|
2016-09-01 14:04:43 +00:00
|
|
|
}
|
2017-07-05 11:12:42 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
scrapeDurationDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
duration.Seconds(),
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
scrapeSuccessDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
success,
|
|
|
|
name,
|
|
|
|
)
|
2019-05-15 19:22:29 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
scrapeTimeoutDesc,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
0.0,
|
|
|
|
name,
|
|
|
|
)
|
2016-09-01 14:04:43 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 11:44:47 +00:00
|
|
|
func expandEnabledCollectors(enabled string) []string {
|
|
|
|
expanded := strings.Replace(enabled, defaultCollectorsPlaceholder, defaultCollectors, -1)
|
|
|
|
separated := strings.Split(expanded, ",")
|
|
|
|
unique := map[string]bool{}
|
|
|
|
for _, s := range separated {
|
|
|
|
if s != "" {
|
|
|
|
unique[s] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result := make([]string, 0, len(unique))
|
2018-04-05 05:11:36 +00:00
|
|
|
for s := range unique {
|
2017-03-04 11:44:47 +00:00
|
|
|
result = append(result, s)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
func loadCollectors(list string) (map[string]collector.Collector, error) {
|
|
|
|
collectors := map[string]collector.Collector{}
|
2017-03-04 11:44:47 +00:00
|
|
|
enabled := expandEnabledCollectors(list)
|
|
|
|
|
|
|
|
for _, name := range enabled {
|
2016-09-01 14:04:43 +00:00
|
|
|
fn, ok := collector.Factories[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("collector '%s' not available", name)
|
|
|
|
}
|
|
|
|
c, err := fn()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
collectors[name] = c
|
|
|
|
}
|
|
|
|
return collectors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(version.NewCollector("wmi_exporter"))
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 19:03:17 +00:00
|
|
|
func initWbem() {
|
|
|
|
// This initialization prevents a memory leak on WMF 5+. See
|
|
|
|
// https://github.com/martinlindhe/wmi_exporter/issues/77 and linked issues
|
|
|
|
// for details.
|
|
|
|
log.Debugf("Initializing SWbemServices")
|
|
|
|
s, err := wmi.InitializeSWbemServices(wmi.DefaultClient)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-06-06 08:31:50 +00:00
|
|
|
wmi.DefaultClient.AllowMissingFields = true
|
2017-06-26 19:03:17 +00:00
|
|
|
wmi.DefaultClient.SWbemServicesClient = s
|
|
|
|
}
|
|
|
|
|
2016-08-26 06:59:27 +00:00
|
|
|
func main() {
|
|
|
|
var (
|
2017-08-12 06:44:59 +00:00
|
|
|
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",
|
2018-08-06 01:39:43 +00:00
|
|
|
"Comma-separated list of collectors to use. Use '[defaults]' as a placeholder for all the collectors enabled by default.").
|
2017-08-12 06:44:59 +00:00
|
|
|
Default(filterAvailableCollectors(defaultCollectors)).String()
|
|
|
|
printCollectors = kingpin.Flag(
|
|
|
|
"collectors.print",
|
|
|
|
"If true, print available collectors and exit.",
|
|
|
|
).Bool()
|
2019-05-15 19:22:29 +00:00
|
|
|
maxScrapeDuration = kingpin.Flag(
|
|
|
|
"scrape.max-duration",
|
|
|
|
"Time after which collectors are aborted during a scrape",
|
|
|
|
).Default("30s").Duration()
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
|
|
|
|
2017-08-12 06:44:59 +00:00
|
|
|
log.AddFlags(kingpin.CommandLine)
|
|
|
|
kingpin.Version(version.Print("wmi_exporter"))
|
|
|
|
kingpin.HelpFlag.Short('h')
|
|
|
|
kingpin.Parse()
|
2016-09-01 14:04:43 +00:00
|
|
|
|
|
|
|
if *printCollectors {
|
|
|
|
collectorNames := make(sort.StringSlice, 0, len(collector.Factories))
|
|
|
|
for n := range collector.Factories {
|
|
|
|
collectorNames = append(collectorNames, n)
|
|
|
|
}
|
|
|
|
collectorNames.Sort()
|
|
|
|
fmt.Printf("Available collectors:\n")
|
|
|
|
for _, n := range collectorNames {
|
|
|
|
fmt.Printf(" - %s\n", n)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-26 19:03:17 +00:00
|
|
|
initWbem()
|
|
|
|
|
2016-09-16 06:36:58 +00:00
|
|
|
isInteractive, err := svc.IsAnInteractiveSession()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stopCh := make(chan bool)
|
|
|
|
if !isInteractive {
|
2018-10-05 05:29:56 +00:00
|
|
|
go func() {
|
|
|
|
err = svc.Run(serviceName, &wmiExporterService{stopCh: stopCh})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to start service: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2016-09-16 06:36:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
collectors, err := loadCollectors(*enabledCollectors)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't load collectors: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:36:58 +00:00
|
|
|
log.Infof("Enabled collectors: %v", strings.Join(keys(collectors), ", "))
|
2016-09-01 14:04:43 +00:00
|
|
|
|
2019-05-15 19:22:29 +00:00
|
|
|
exporter := WmiCollector{
|
|
|
|
collectors: collectors,
|
|
|
|
maxScrapeDuration: *maxScrapeDuration,
|
|
|
|
}
|
|
|
|
prometheus.MustRegister(exporter)
|
2016-08-26 06:59:27 +00:00
|
|
|
|
2017-04-30 22:12:05 +00:00
|
|
|
http.Handle(*metricsPath, promhttp.Handler())
|
2016-09-27 12:37:12 +00:00
|
|
|
http.HandleFunc("/health", healthCheck)
|
2016-08-26 06:59:27 +00:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently)
|
|
|
|
})
|
|
|
|
|
2016-09-01 14:04:43 +00:00
|
|
|
log.Infoln("Starting WMI exporter", version.Info())
|
|
|
|
log.Infoln("Build context", version.BuildContext())
|
|
|
|
|
2016-09-16 06:36:58 +00:00
|
|
|
go func() {
|
|
|
|
log.Infoln("Starting server on", *listenAddress)
|
2017-04-30 22:12:05 +00:00
|
|
|
log.Fatalf("cannot start WMI exporter: %s", http.ListenAndServe(*listenAddress, nil))
|
2016-09-16 06:36:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if <-stopCh {
|
|
|
|
log.Info("Shutting down WMI exporter")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 12:37:12 +00:00
|
|
|
func healthCheck(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-10-05 06:01:02 +00:00
|
|
|
_, err := fmt.Fprintln(w, `{"status":"ok"}`)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Failed to write to stream: %v", err)
|
|
|
|
}
|
2016-09-27 12:37:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 06:36:58 +00:00
|
|
|
func keys(m map[string]collector.Collector) []string {
|
|
|
|
ret := make([]string, 0, len(m))
|
2016-09-27 12:37:12 +00:00
|
|
|
for key := range m {
|
2016-09-16 06:36:58 +00:00
|
|
|
ret = append(ret, key)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type wmiExporterService struct {
|
|
|
|
stopCh chan<- bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *wmiExporterService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
|
|
|
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
|
|
|
changes <- svc.Status{State: svc.StartPending}
|
|
|
|
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-r:
|
|
|
|
switch c.Cmd {
|
|
|
|
case svc.Interrogate:
|
|
|
|
changes <- c.CurrentStatus
|
|
|
|
case svc.Stop, svc.Shutdown:
|
|
|
|
s.stopCh <- true
|
|
|
|
break loop
|
|
|
|
default:
|
|
|
|
log.Error(fmt.Sprintf("unexpected control request #%d", c))
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|
2016-09-16 06:36:58 +00:00
|
|
|
changes <- svc.Status{State: svc.StopPending}
|
|
|
|
return
|
2016-08-26 06:59:27 +00:00
|
|
|
}
|