Skip to content

Commit

Permalink
Merge pull request #1155 from mackerelio/server-sessions-remove-wmic
Browse files Browse the repository at this point in the history
[mackerel-plugin-windows-server-sessions] Remove the dependency for WMIC command
  • Loading branch information
Arthur1 authored Feb 29, 2024
2 parents 12a8eb5 + 280503b commit c120e56
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/tomasen/fcgi_client v0.0.0-20180423082037-2bb3d819fd19
github.com/urfave/cli v1.22.14
github.com/yusufpapurcu/wmi v1.2.4
golang.org/x/sync v0.6.0
golang.org/x/text v0.14.0
)
Expand All @@ -52,7 +53,7 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF
github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-ldap/ldap/v3 v3.4.4 h1:qPjipEpt+qDa6SI/h1fzuGWoRUY+qqQ9sOZq67/PYUs=
github.com/go-ldap/ldap/v3 v3.4.4/go.mod h1:fe1MsuN5eJJ1FeLT/LEBVdWfNWKh459R7aXgXtJC+aI=
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redismock/v9 v9.0.3 h1:mtHQi2l51lCmXIbTRTqb1EiHYe9tL5Yk5oorlSJJqR0=
Expand Down Expand Up @@ -200,6 +201,8 @@ github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80=
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
2 changes: 1 addition & 1 deletion mackerel-plugin-windows-server-sessions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Windows Server Sessions custom metrics plugin for mackerel-agent.
## Usage

```shell
mackerel-plugin-windows-server-sessions
mackerel-plugin-windows-server-sessions [-legacymetricname]
```

## Example of mackerel-agent.conf
Expand Down
61 changes: 41 additions & 20 deletions mackerel-plugin-windows-server-sessions/lib/sessions.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
//go:build windows

package mpwindowsserversessions

import (
"encoding/csv"
"flag"
"os/exec"
"strconv"
"os"
"strings"

"github.com/yusufpapurcu/wmi"

mp "github.com/mackerelio/go-mackerel-plugin-helper"
)

// WindowsServerSessionsPlugin store the name of servers
type WindowsServerSessionsPlugin struct {
LegacyMetricName bool
}

func getCounts() (map[string]int, error) {
// WMIC PATH Win32_PerfFormattedData_PerfNet_Server GET ServerSessions /FORMAT:CSV
output, err := exec.Command("WMIC", "PATH", "Win32_PerfFormattedData_PerfNet_Server", "GET", "ServerSessions", "/FORMAT:CSV").Output()
if err != nil {
// cf.) https://learn.microsoft.com/en-us/previous-versions/aa394265(v=vs.85)
type Win32_PerfFormattedData_PerfNet_Server struct {
ServerSessions uint32
}

type ServerSessionsCount struct {
Node string
ServerSessions uint32
}

func getCounts() ([]ServerSessionsCount, error) {
var dst []Win32_PerfFormattedData_PerfNet_Server
q := wmi.CreateQuery(&dst, "")
if err := wmi.Query(q, &dst); err != nil {
return nil, err
}
r := csv.NewReader(strings.NewReader(string(output[1:])))
records, err := r.ReadAll()

if len(dst) == 0 {
return []ServerSessionsCount{}, nil
}

node, err := os.Hostname()
if err != nil {
return nil, err
}
counts := make(map[string]int)
for _, record := range records[1:] {
name := strings.TrimSpace(record[0])
n, err := strconv.Atoi(strings.TrimSpace(record[1]))
if err != nil {
continue
}
counts[name] = n
counts := []ServerSessionsCount{
{Node: node, ServerSessions: dst[0].ServerSessions},
}

return counts, nil
}

Expand All @@ -43,9 +55,16 @@ func (m WindowsServerSessionsPlugin) FetchMetrics() (map[string]interface{}, err
if err != nil {
return nil, err
}
stat := make(map[string]interface{})
for k, v := range counts {
stat["windows.server.sessions."+k+".count"] = uint64(v)
stat := make(map[string]interface{}, len(counts))
for _, v := range counts {
var nodeMetricKey string
// node name of Windows can contain ".", which is the metric name delimiter on Mackerel.
if m.LegacyMetricName {
nodeMetricKey = v.Node
} else {
nodeMetricKey = strings.ReplaceAll(v.Node, ".", "_")
}
stat["windows.server.sessions."+nodeMetricKey+".count"] = v.ServerSessions
}
return stat, nil
}
Expand All @@ -66,9 +85,11 @@ func (m WindowsServerSessionsPlugin) GraphDefinition() map[string](mp.Graphs) {
// Do the plugin
func Do() {
optTempfile := flag.String("tempfile", "", "Temp file name")
legacyMetricName := flag.Bool("legacymetricname", false, `Prevent escaping "." in the node name for metric keys`)
flag.Parse()

var plugin WindowsServerSessionsPlugin
plugin.LegacyMetricName = *legacyMetricName

helper := mp.NewMackerelPlugin(plugin)

Expand Down
8 changes: 8 additions & 0 deletions mackerel-plugin-windows-server-sessions/lib/sessions_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows

package mpwindowsserversessions

// Do the plugin
func Do() {
panic("The mackerel-plugin-windows-server-sessions does not work on non Windows environment, of course.")
}

0 comments on commit c120e56

Please sign in to comment.