mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-08 22:16:38 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a41b9de37c | ||
|
|
745f0a6f61 | ||
|
|
8ca60af43a | ||
|
|
a66f0b5475 | ||
|
|
25b8dba6df | ||
|
|
f5365c96f6 |
@@ -138,18 +138,18 @@ func mangleNetworkName(name string) string {
|
||||
}
|
||||
|
||||
type Win32_PerfRawData_Tcpip_NetworkInterface struct {
|
||||
BytesReceivedPerSec uint32
|
||||
BytesSentPerSec uint32
|
||||
BytesReceivedPerSec uint64
|
||||
BytesSentPerSec uint64
|
||||
BytesTotalPerSec uint64
|
||||
Name string
|
||||
PacketsOutboundDiscarded uint32
|
||||
PacketsOutboundErrors uint32
|
||||
PacketsPerSec uint32
|
||||
PacketsReceivedDiscarded uint32
|
||||
PacketsReceivedErrors uint32
|
||||
PacketsReceivedPerSec uint32
|
||||
PacketsReceivedUnknown uint32
|
||||
PacketsSentPerSec uint32
|
||||
PacketsOutboundDiscarded uint64
|
||||
PacketsOutboundErrors uint64
|
||||
PacketsPerSec uint64
|
||||
PacketsReceivedDiscarded uint64
|
||||
PacketsReceivedErrors uint64
|
||||
PacketsReceivedPerSec uint64
|
||||
PacketsReceivedUnknown uint64
|
||||
PacketsSentPerSec uint64
|
||||
}
|
||||
|
||||
func (c *NetworkCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
||||
|
||||
@@ -55,6 +55,26 @@ type Win32_Service struct {
|
||||
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) {
|
||||
var dst []Win32_Service
|
||||
q := wmi.CreateQuery(&dst, "")
|
||||
@@ -63,21 +83,33 @@ func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Des
|
||||
}
|
||||
|
||||
for _, service := range dst {
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.State,
|
||||
prometheus.GaugeValue,
|
||||
1.0,
|
||||
strings.ToLower(service.Name),
|
||||
strings.ToLower(service.State),
|
||||
)
|
||||
for _, state := range allStates {
|
||||
isCurrentState := 0.0
|
||||
if state == strings.ToLower(service.State) {
|
||||
isCurrentState = 1.0
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.State,
|
||||
prometheus.GaugeValue,
|
||||
isCurrentState,
|
||||
strings.ToLower(service.Name),
|
||||
state,
|
||||
)
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.StartMode,
|
||||
prometheus.GaugeValue,
|
||||
1.0,
|
||||
strings.ToLower(service.Name),
|
||||
strings.ToLower(service.StartMode),
|
||||
)
|
||||
for _, startMode := range allStartModes {
|
||||
isCurrentStartMode := 0.0
|
||||
if startMode == strings.ToLower(service.StartMode) {
|
||||
isCurrentStartMode = 1.0
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.StartMode,
|
||||
prometheus.GaugeValue,
|
||||
isCurrentStartMode,
|
||||
strings.ToLower(service.Name),
|
||||
startMode,
|
||||
)
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
27
exporter.go
27
exporter.go
@@ -25,8 +25,9 @@ type WmiCollector struct {
|
||||
}
|
||||
|
||||
const (
|
||||
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system"
|
||||
serviceName = "wmi_exporter"
|
||||
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system"
|
||||
defaultCollectorsPlaceholder = "[defaults]"
|
||||
serviceName = "wmi_exporter"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -90,9 +91,27 @@ func execute(name string, c collector.Collector, ch chan<- prometheus.Metric) {
|
||||
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) {
|
||||
collectors := map[string]collector.Collector{}
|
||||
for _, name := range strings.Split(list, ",") {
|
||||
enabled := expandEnabledCollectors(list)
|
||||
|
||||
for _, name := range enabled {
|
||||
fn, ok := collector.Factories[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("collector '%s' not available", name)
|
||||
@@ -115,7 +134,7 @@ func main() {
|
||||
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.")
|
||||
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.")
|
||||
)
|
||||
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"
|
||||
# Somewhat obscure url, points to WiX 3.10 binary release
|
||||
Write-Verbose "Downloading WiX..."
|
||||
Get-FileIfNotExists "http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=wix&DownloadId=1504735&FileTime=130906491728530000&Build=21040" "$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
|
||||
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"
|
||||
|
||||
Write-Verbose "Done!"
|
||||
Pop-Location
|
||||
Pop-Location
|
||||
|
||||
Reference in New Issue
Block a user