mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-10 06:56:38 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a41b9de37c | ||
|
|
745f0a6f61 | ||
|
|
8ca60af43a | ||
|
|
a66f0b5475 | ||
|
|
25b8dba6df | ||
|
|
f5365c96f6 | ||
|
|
a05febe069 |
@@ -138,18 +138,18 @@ func mangleNetworkName(name string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Win32_PerfRawData_Tcpip_NetworkInterface struct {
|
type Win32_PerfRawData_Tcpip_NetworkInterface struct {
|
||||||
BytesReceivedPerSec uint32
|
BytesReceivedPerSec uint64
|
||||||
BytesSentPerSec uint32
|
BytesSentPerSec uint64
|
||||||
BytesTotalPerSec uint64
|
BytesTotalPerSec uint64
|
||||||
Name string
|
Name string
|
||||||
PacketsOutboundDiscarded uint32
|
PacketsOutboundDiscarded uint64
|
||||||
PacketsOutboundErrors uint32
|
PacketsOutboundErrors uint64
|
||||||
PacketsPerSec uint32
|
PacketsPerSec uint64
|
||||||
PacketsReceivedDiscarded uint32
|
PacketsReceivedDiscarded uint64
|
||||||
PacketsReceivedErrors uint32
|
PacketsReceivedErrors uint64
|
||||||
PacketsReceivedPerSec uint32
|
PacketsReceivedPerSec uint64
|
||||||
PacketsReceivedUnknown uint32
|
PacketsReceivedUnknown uint64
|
||||||
PacketsSentPerSec uint32
|
PacketsSentPerSec uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *NetworkCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
func (c *NetworkCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
||||||
|
|||||||
@@ -55,6 +55,26 @@ type Win32_Service struct {
|
|||||||
StartMode string
|
StartMode string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
allStates = []string{
|
||||||
|
"stopped",
|
||||||
|
"start pending",
|
||||||
|
"stop pending",
|
||||||
|
"running",
|
||||||
|
"continue pending",
|
||||||
|
"pause pending",
|
||||||
|
"paused",
|
||||||
|
"unknown",
|
||||||
|
}
|
||||||
|
allStartModes = []string{
|
||||||
|
"boot",
|
||||||
|
"system",
|
||||||
|
"auto",
|
||||||
|
"manual",
|
||||||
|
"disabled",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
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, "")
|
||||||
@@ -63,21 +83,33 @@ func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Des
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, service := range dst {
|
for _, service := range dst {
|
||||||
ch <- prometheus.MustNewConstMetric(
|
for _, state := range allStates {
|
||||||
c.State,
|
isCurrentState := 0.0
|
||||||
prometheus.GaugeValue,
|
if state == strings.ToLower(service.State) {
|
||||||
1.0,
|
isCurrentState = 1.0
|
||||||
strings.ToLower(service.Name),
|
}
|
||||||
strings.ToLower(service.State),
|
ch <- prometheus.MustNewConstMetric(
|
||||||
)
|
c.State,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
isCurrentState,
|
||||||
|
strings.ToLower(service.Name),
|
||||||
|
state,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
ch <- prometheus.MustNewConstMetric(
|
for _, startMode := range allStartModes {
|
||||||
c.StartMode,
|
isCurrentStartMode := 0.0
|
||||||
prometheus.GaugeValue,
|
if startMode == strings.ToLower(service.StartMode) {
|
||||||
1.0,
|
isCurrentStartMode = 1.0
|
||||||
strings.ToLower(service.Name),
|
}
|
||||||
strings.ToLower(service.StartMode),
|
ch <- prometheus.MustNewConstMetric(
|
||||||
)
|
c.StartMode,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
isCurrentStartMode,
|
||||||
|
strings.ToLower(service.Name),
|
||||||
|
startMode,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
27
exporter.go
27
exporter.go
@@ -25,8 +25,9 @@ type WmiCollector struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system"
|
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system"
|
||||||
serviceName = "wmi_exporter"
|
defaultCollectorsPlaceholder = "[defaults]"
|
||||||
|
serviceName = "wmi_exporter"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -90,9 +91,27 @@ func execute(name string, c collector.Collector, ch chan<- prometheus.Metric) {
|
|||||||
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
|
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
for s, _ := range unique {
|
||||||
|
result = append(result, s)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
func loadCollectors(list string) (map[string]collector.Collector, error) {
|
func loadCollectors(list string) (map[string]collector.Collector, error) {
|
||||||
collectors := map[string]collector.Collector{}
|
collectors := map[string]collector.Collector{}
|
||||||
for _, name := range strings.Split(list, ",") {
|
enabled := expandEnabledCollectors(list)
|
||||||
|
|
||||||
|
for _, name := range enabled {
|
||||||
fn, ok := collector.Factories[name]
|
fn, ok := collector.Factories[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("collector '%s' not available", name)
|
return nil, fmt.Errorf("collector '%s' not available", name)
|
||||||
@@ -115,7 +134,7 @@ func main() {
|
|||||||
showVersion = flag.Bool("version", false, "Print version information.")
|
showVersion = flag.Bool("version", false, "Print version information.")
|
||||||
listenAddress = flag.String("telemetry.addr", ":9182", "host:port for WMI exporter.")
|
listenAddress = flag.String("telemetry.addr", ":9182", "host:port for WMI exporter.")
|
||||||
metricsPath = flag.String("telemetry.path", "/metrics", "URL path for surfacing collected metrics.")
|
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.")
|
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.Parse()
|
flag.Parse()
|
||||||
|
|||||||
49
exporter_test.go
Normal file
49
exporter_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type expansionTestCase struct {
|
||||||
|
input string
|
||||||
|
expectedOutput []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpandEnabled(t *testing.T) {
|
||||||
|
expansionTests := []expansionTestCase{
|
||||||
|
{"", []string{}},
|
||||||
|
// Default case
|
||||||
|
{"cs,os", []string{"cs", "os"}},
|
||||||
|
// Placeholder expansion
|
||||||
|
{defaultCollectorsPlaceholder, strings.Split(defaultCollectors, ",")},
|
||||||
|
// De-duplication
|
||||||
|
{"cs,cs", []string{"cs"}},
|
||||||
|
// De-duplicate placeholder
|
||||||
|
{defaultCollectorsPlaceholder + "," + defaultCollectorsPlaceholder, strings.Split(defaultCollectors, ",")},
|
||||||
|
// Composite case
|
||||||
|
{"foo," + defaultCollectorsPlaceholder + ",bar", append(strings.Split(defaultCollectors, ","), "foo", "bar")},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range expansionTests {
|
||||||
|
output := expandEnabledCollectors(testCase.input)
|
||||||
|
sort.Strings(output)
|
||||||
|
|
||||||
|
success := true
|
||||||
|
if len(output) != len(testCase.expectedOutput) {
|
||||||
|
success = false
|
||||||
|
} else {
|
||||||
|
sort.Strings(testCase.expectedOutput)
|
||||||
|
for idx := range output {
|
||||||
|
if output[idx] != testCase.expectedOutput[idx] {
|
||||||
|
success = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !success {
|
||||||
|
t.Error("For", testCase.input, "expected", testCase.expectedOutput, "got", output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,7 +45,7 @@ mkdir -Force Work,Output | Out-Null
|
|||||||
Write-Verbose "Downloading files"
|
Write-Verbose "Downloading files"
|
||||||
# Somewhat obscure url, points to WiX 3.10 binary release
|
# Somewhat obscure url, points to WiX 3.10 binary release
|
||||||
Write-Verbose "Downloading WiX..."
|
Write-Verbose "Downloading WiX..."
|
||||||
Get-FileIfNotExists "http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=wix&DownloadId=1504735&FileTime=130906491728530000&Build=21031" "$sourceDir\wix-binaries.zip"
|
Get-FileIfNotExists "http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=wix&DownloadId=1587180&FileTime=131118854877130000&Build=21046" "$sourceDir\wix-binaries.zip"
|
||||||
mkdir -Force WiX | Out-Null
|
mkdir -Force WiX | Out-Null
|
||||||
Expand-Archive -Path "${sourceDir}\wix-binaries.zip" -DestinationPath WiX -Force
|
Expand-Archive -Path "${sourceDir}\wix-binaries.zip" -DestinationPath WiX -Force
|
||||||
|
|
||||||
@@ -58,4 +58,4 @@ Invoke-Expression "WiX\candle.exe -nologo -arch $wixArch $wixOpts -out Work\wmi_
|
|||||||
Invoke-Expression "WiX\light.exe -nologo -spdb $wixOpts -out `"Output\wmi_exporter-${Version}-${Arch}.msi`" Work\wmi_exporter.wixobj"
|
Invoke-Expression "WiX\light.exe -nologo -spdb $wixOpts -out `"Output\wmi_exporter-${Version}-${Arch}.msi`" Work\wmi_exporter.wixobj"
|
||||||
|
|
||||||
Write-Verbose "Done!"
|
Write-Verbose "Done!"
|
||||||
Pop-Location
|
Pop-Location
|
||||||
|
|||||||
Reference in New Issue
Block a user