mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-10 15:06:36 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44c39405c7 | ||
|
|
96faedf481 | ||
|
|
3cfc11c6d2 | ||
|
|
80f1cf0546 | ||
|
|
691b672a1e | ||
|
|
2e0842573d | ||
|
|
7537c9896e | ||
|
|
88271ddf14 | ||
|
|
f4195aa435 | ||
|
|
e8cfeef26c |
3
Makefile
3
Makefile
@@ -1,2 +1,5 @@
|
|||||||
fmt:
|
fmt:
|
||||||
gofmt -l -w -s .
|
gofmt -l -w -s .
|
||||||
|
|
||||||
|
build:
|
||||||
|
promu build -v .
|
||||||
|
|||||||
11
README.md
11
README.md
@@ -52,13 +52,22 @@ See [open issues](https://github.com/martinlindhe/wmi_exporter/issues)
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
go get -u github.com/kardianos/govendor
|
go get -u github.com/kardianos/govendor
|
||||||
|
go get -u github.com/prometheus/promu
|
||||||
go get -u github.com/martinlindhe/wmi_exporter
|
go get -u github.com/martinlindhe/wmi_exporter
|
||||||
cd $env:GOPATH/src/github.com/martinlindhe/wmi_exporter
|
cd $env:GOPATH/src/github.com/martinlindhe/wmi_exporter
|
||||||
govendor build +local
|
promu build -v .
|
||||||
.\wmi_exporter.exe
|
.\wmi_exporter.exe
|
||||||
|
|
||||||
The prometheus metrics will be exposed on [localhost:9182](http://localhost:9182)
|
The prometheus metrics will be exposed on [localhost:9182](http://localhost:9182)
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Please note: The quotes in the parameter names are required because of how Powershell parses command line arguments.
|
||||||
|
|
||||||
|
### Enable only service collector and specify a custom query
|
||||||
|
|
||||||
|
.\wmi_exporter.exe "-collectors.enabled" "service" "-collector.service.services-where" "Name='wmi_exporter'"
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,10 @@
|
|||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/StackExchange/wmi"
|
"github.com/StackExchange/wmi"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -454,7 +455,7 @@ func NewADCollector() (Collector, error) {
|
|||||||
// to the provided prometheus Metric channel.
|
// to the provided prometheus Metric channel.
|
||||||
func (c *ADCollector) Collect(ch chan<- prometheus.Metric) error {
|
func (c *ADCollector) Collect(ch chan<- prometheus.Metric) error {
|
||||||
if desc, err := c.collect(ch); err != nil {
|
if desc, err := c.collect(ch); err != nil {
|
||||||
log.Errorf("[ERROR] failed collecting ad metrics:", desc, err)
|
log.Println("[ERROR] failed collecting ad metrics:", desc, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
1167
collector/iis.go
1167
collector/iis.go
File diff suppressed because it is too large
Load Diff
@@ -5,12 +5,12 @@ package collector
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/StackExchange/wmi"
|
"github.com/StackExchange/wmi"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/common/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -49,7 +49,7 @@ func NewProcessCollector() (Collector, error) {
|
|||||||
wc.WriteString("WHERE ")
|
wc.WriteString("WHERE ")
|
||||||
wc.WriteString(*processWhereClause)
|
wc.WriteString(*processWhereClause)
|
||||||
} else {
|
} else {
|
||||||
log.Warn("No where-clause specified for process collector. This will generate a very large number of metrics!")
|
log.Println("warning: No where-clause specified for process collector. This will generate a very large number of metrics!")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ProcessCollector{
|
return &ProcessCollector{
|
||||||
@@ -139,7 +139,7 @@ func NewProcessCollector() (Collector, error) {
|
|||||||
// to the provided prometheus Metric channel.
|
// to the provided prometheus Metric channel.
|
||||||
func (c *ProcessCollector) Collect(ch chan<- prometheus.Metric) error {
|
func (c *ProcessCollector) Collect(ch chan<- prometheus.Metric) error {
|
||||||
if desc, err := c.collect(ch); err != nil {
|
if desc, err := c.collect(ch); err != nil {
|
||||||
log.Errorln("[ERROR] failed collecting process metrics:", desc, err)
|
log.Println("[ERROR] failed collecting process metrics:", desc, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -14,15 +16,30 @@ func init() {
|
|||||||
Factories["service"] = NewserviceCollector
|
Factories["service"] = NewserviceCollector
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.")
|
||||||
|
)
|
||||||
|
|
||||||
// A serviceCollector is a Prometheus collector for WMI Win32_Service metrics
|
// A serviceCollector is a Prometheus collector for WMI Win32_Service metrics
|
||||||
type serviceCollector struct {
|
type serviceCollector struct {
|
||||||
State *prometheus.Desc
|
State *prometheus.Desc
|
||||||
StartMode *prometheus.Desc
|
StartMode *prometheus.Desc
|
||||||
|
|
||||||
|
queryWhereClause string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewserviceCollector ...
|
// NewserviceCollector ...
|
||||||
func NewserviceCollector() (Collector, error) {
|
func NewserviceCollector() (Collector, error) {
|
||||||
const subsystem = "service"
|
const subsystem = "service"
|
||||||
|
|
||||||
|
var wc bytes.Buffer
|
||||||
|
if *serviceWhereClause != "" {
|
||||||
|
wc.WriteString("WHERE ")
|
||||||
|
wc.WriteString(*serviceWhereClause)
|
||||||
|
} else {
|
||||||
|
log.Println("warning: No where-clause specified for process collector. This will generate a very large number of metrics!")
|
||||||
|
}
|
||||||
|
|
||||||
return &serviceCollector{
|
return &serviceCollector{
|
||||||
State: prometheus.NewDesc(
|
State: prometheus.NewDesc(
|
||||||
prometheus.BuildFQName(Namespace, subsystem, "state"),
|
prometheus.BuildFQName(Namespace, subsystem, "state"),
|
||||||
@@ -36,6 +53,7 @@ func NewserviceCollector() (Collector, error) {
|
|||||||
[]string{"name", "start_mode"},
|
[]string{"name", "start_mode"},
|
||||||
nil,
|
nil,
|
||||||
),
|
),
|
||||||
|
queryWhereClause: wc.String(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +95,7 @@ var (
|
|||||||
|
|
||||||
func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
||||||
var dst []Win32_Service
|
var dst []Win32_Service
|
||||||
q := wmi.CreateQuery(&dst, "")
|
q := wmi.CreateQuery(&dst, c.queryWhereClause)
|
||||||
if err := wmi.Query(q, &dst); err != nil {
|
if err := wmi.Query(q, &dst); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package collector
|
package collector
|
||||||
|
|
||||||
import "github.com/prometheus/client_golang/prometheus"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
const (
|
const (
|
||||||
@@ -18,3 +24,30 @@ type Collector interface {
|
|||||||
// Get new metrics and expose them via prometheus registry.
|
// Get new metrics and expose them via prometheus registry.
|
||||||
Collect(ch chan<- prometheus.Metric) (err error)
|
Collect(ch chan<- prometheus.Metric) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is adapted from StackExchange/wmi/wmi.go, and lets us change the class
|
||||||
|
// name being queried for:
|
||||||
|
// CreateQuery returns a WQL query string that queries all columns of src. where
|
||||||
|
// is an optional string that is appended to the query, to be used with WHERE
|
||||||
|
// clauses. In such a case, the "WHERE" string should appear at the beginning.
|
||||||
|
func createQuery(src interface{}, class, where string) string {
|
||||||
|
var b bytes.Buffer
|
||||||
|
b.WriteString("SELECT ")
|
||||||
|
s := reflect.Indirect(reflect.ValueOf(src))
|
||||||
|
t := s.Type()
|
||||||
|
if s.Kind() == reflect.Slice {
|
||||||
|
t = t.Elem()
|
||||||
|
}
|
||||||
|
if t.Kind() != reflect.Struct {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var fields []string
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
fields = append(fields, t.Field(i).Name)
|
||||||
|
}
|
||||||
|
b.WriteString(strings.Join(fields, ", "))
|
||||||
|
b.WriteString(" FROM ")
|
||||||
|
b.WriteString(class)
|
||||||
|
b.WriteString(" " + where)
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|||||||
@@ -157,6 +157,13 @@ func initWbem() {
|
|||||||
wmi.DefaultClient.SWbemServicesClient = s
|
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() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
showVersion = flag.Bool("version", false, "Print version information.")
|
showVersion = flag.Bool("version", false, "Print version information.")
|
||||||
@@ -165,6 +172,7 @@ func main() {
|
|||||||
enabledCollectors = flag.String("collectors.enabled", filterAvailableCollectors(defaultCollectors), "Comma-separated list of collectors to use. Use '[default]' as a placeholder for all the collectors enabled by default")
|
enabledCollectors = flag.String("collectors.enabled", filterAvailableCollectors(defaultCollectors), "Comma-separated list of collectors to use. Use '[default]' as a placeholder for all the collectors enabled by default")
|
||||||
printCollectors = flag.Bool("collectors.print", false, "If true, print available collectors and exit.")
|
printCollectors = flag.Bool("collectors.print", false, "If true, print available collectors and exit.")
|
||||||
)
|
)
|
||||||
|
flag.Usage = usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showVersion {
|
if *showVersion {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
<File Id="wmi_exporter.exe" Name="wmi_exporter.exe" Source="Work\wmi_exporter.exe" KeyPath="yes">
|
<File Id="wmi_exporter.exe" Name="wmi_exporter.exe" Source="Work\wmi_exporter.exe" KeyPath="yes">
|
||||||
<fw:FirewallException Id="MetricsEndpoint" Name="WMI Exporter (HTTP [LISTEN_PORT])" Description="WMI Exporter HTTP endpoint" Port="[LISTEN_PORT]" Protocol="tcp" Scope="any" IgnoreFailure="yes" />
|
<fw:FirewallException Id="MetricsEndpoint" Name="WMI Exporter (HTTP [LISTEN_PORT])" Description="WMI Exporter HTTP endpoint" Port="[LISTEN_PORT]" Protocol="tcp" Scope="any" IgnoreFailure="yes" />
|
||||||
</File>
|
</File>
|
||||||
<ServiceInstall Id="InstallExporterService" Name="wmi_exporter" ErrorControl="normal" Start="auto" Type="ownProcess" Arguments="-log.format logger:eventlog?name=wmi_exporter [CollectorsFlag] [ListenFlag] [MetricsPathFlag]" />
|
<ServiceInstall Id="InstallExporterService" Name="wmi_exporter" DisplayName="WMI exporter" Description="Exports Prometheus metrics from WMI queries" ErrorControl="normal" Start="auto" Type="ownProcess" Arguments="-log.format logger:eventlog?name=wmi_exporter [CollectorsFlag] [ListenFlag] [MetricsPathFlag]" />
|
||||||
<ServiceControl Id="ServiceStateControl" Name="wmi_exporter" Remove="uninstall" Start="install" Stop="both" />
|
<ServiceControl Id="ServiceStateControl" Name="wmi_exporter" Remove="uninstall" Start="install" Stop="both" />
|
||||||
</Component>
|
</Component>
|
||||||
</ComponentGroup>
|
</ComponentGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user