mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-08 05:56:37 +00:00
chore: enable more linter (#1557)
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
$Class,
|
||||
[Parameter(Mandatory=$false)]
|
||||
$Namespace = "root/cimv2",
|
||||
[Parameter(Mandatory=$false)]
|
||||
$CollectorName = ($Class -replace 'Win32_PerfRawData_Perf',''),
|
||||
[Parameter(Mandatory=$false)]
|
||||
$ComputerName = "localhost",
|
||||
[Parameter(Mandatory=$false)]
|
||||
[CimSession] $Session
|
||||
)
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
if($null -ne $Session) {
|
||||
$wmiObject = Get-CimInstance -CimSession $Session -Namespace $Namespace -Class $Class
|
||||
}
|
||||
else {
|
||||
$wmiObject = Get-CimInstance -ComputerName $ComputerName -Namespace $Namespace -Class $Class
|
||||
}
|
||||
|
||||
$members = $wmiObject `
|
||||
| Get-Member -MemberType Properties `
|
||||
| Where-Object { $_.Definition -Match '^u?int' -and $_.Name -NotMatch '_' } `
|
||||
| Select-Object Name, @{Name="Type";Expression={$_.Definition.Split(" ")[0]}}
|
||||
$input = @{
|
||||
"Namespace"=$Namespace;
|
||||
"Class"=$Class;
|
||||
"CollectorName"=$CollectorName;
|
||||
"Members"=$members
|
||||
} | ConvertTo-Json
|
||||
$outFileName = "..\..\collector\$CollectorName.go".ToLower()
|
||||
$input | .\collector-generator.exe | Out-File -NoClobber -Encoding UTF8 $outFileName
|
||||
go fmt $outFileName
|
||||
@@ -1,17 +0,0 @@
|
||||
# Collector generator
|
||||
Generates a collector skeleton implementation from a WMI class.
|
||||
|
||||
## Usage
|
||||
Build the generator:
|
||||
|
||||
```bash
|
||||
go build .
|
||||
```
|
||||
|
||||
Run the script to query the WMI service and send the output to the generator:
|
||||
|
||||
```powershell
|
||||
.\New-Collector.ps1 -Class Win32_PerfRawData_PerfOS_Processor
|
||||
```
|
||||
|
||||
This will generate a collector. The collector name is generated by first removing `Win32_PerfRawData_Perf` and lower-casing, so `Win32_PerfRawData_PerfOS_Processor` will generate `os_processor.go`. This can be overridden by passing `-CollectorName` to the script.
|
||||
@@ -1,56 +0,0 @@
|
||||
package collector
|
||||
import (
|
||||
"github.com/yusufpapurcu/wmi"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus-community/windows_exporter/log"
|
||||
)
|
||||
func init() {
|
||||
registerCollector("{{ .CollectorName | toLower }}", new{{ .CollectorName }}Collector) // TODO: Add any perflib dependencies here
|
||||
}
|
||||
// A {{ .CollectorName }}Collector is a Prometheus collector for WMI {{ .Class }} metrics
|
||||
type {{ .CollectorName }}Collector struct {
|
||||
{{- range $m := .Members }}
|
||||
{{ $m.Name }} *prometheus.Desc
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func new{{ .CollectorName }}Collector() (Collector, error) {
|
||||
const subsystem = "{{ .CollectorName | toLower }}"
|
||||
return &{{ .CollectorName }}Collector{
|
||||
{{- range $m := .Members }}
|
||||
{{ $m.Name }}: prometheus.NewDesc(
|
||||
prometheus.BuildFQName(Namespace, subsystem, "{{ $m.Name | toSnakeCase }}"),
|
||||
"({{ $m.Name }})",
|
||||
nil,
|
||||
nil,
|
||||
),
|
||||
{{- end }}
|
||||
}, nil
|
||||
}
|
||||
|
||||
// {{ .Class }} docs:
|
||||
// - <add link to documentation here>
|
||||
type {{ .Class }} struct {
|
||||
Name string
|
||||
{{ range $m := .Members }}
|
||||
{{ $m.Name }} {{ $m.Type }}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
// Collect sends the metric values for each metric
|
||||
// to the provided prometheus Metric channel.
|
||||
func (c *{{ .CollectorName }}Collector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
|
||||
var dst []{{ .Class }}
|
||||
q := queryAll(&dst)
|
||||
if err := wmi.Query(q, &dst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{ range $m := .Members }}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.{{ $m.Name }},
|
||||
prometheus.GaugeValue,
|
||||
float64(dst[0].{{ $m.Name }}),
|
||||
)
|
||||
{{ end }}
|
||||
return nil, nil
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type TemplateData struct {
|
||||
CollectorName string
|
||||
Class string
|
||||
Members []Member
|
||||
}
|
||||
type Member struct {
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
||||
func main() {
|
||||
bytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var data TemplateData
|
||||
if err = json.Unmarshal(bytes, &data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
funcs := template.FuncMap{
|
||||
"toLower": strings.ToLower,
|
||||
"toSnakeCase": toSnakeCase,
|
||||
}
|
||||
tmpl, err := template.New("template").Funcs(funcs).ParseFiles("collector.template")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = tmpl.ExecuteTemplate(os.Stdout, "collector.template", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// https://gist.github.com/elwinar/14e1e897fdbe4d3432e1
|
||||
func toSnakeCase(in string) string {
|
||||
runes := []rune(in)
|
||||
length := len(runes)
|
||||
|
||||
var out []rune
|
||||
for i := 0; i < length; i++ {
|
||||
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
|
||||
out = append(out, '_')
|
||||
}
|
||||
out = append(out, unicode.ToLower(runes[i]))
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
Reference in New Issue
Block a user