github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-windows-server-sessions/lib/sessions.go (about)

     1  //go:build windows
     2  
     3  package mpwindowsserversessions
     4  
     5  import (
     6  	"flag"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/yusufpapurcu/wmi"
    11  
    12  	mp "github.com/mackerelio/go-mackerel-plugin-helper"
    13  )
    14  
    15  // WindowsServerSessionsPlugin store the name of servers
    16  type WindowsServerSessionsPlugin struct {
    17  	LegacyMetricName bool
    18  }
    19  
    20  // cf.) https://learn.microsoft.com/en-us/previous-versions/aa394265(v=vs.85)
    21  type Win32_PerfFormattedData_PerfNet_Server struct {
    22  	ServerSessions uint32
    23  }
    24  
    25  type ServerSessionsCount struct {
    26  	Node           string
    27  	ServerSessions uint32
    28  }
    29  
    30  func getCounts() ([]ServerSessionsCount, error) {
    31  	var dst []Win32_PerfFormattedData_PerfNet_Server
    32  	q := wmi.CreateQuery(&dst, "")
    33  	if err := wmi.Query(q, &dst); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	if len(dst) == 0 {
    38  		return []ServerSessionsCount{}, nil
    39  	}
    40  
    41  	node, err := os.Hostname()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	counts := []ServerSessionsCount{
    46  		{Node: node, ServerSessions: dst[0].ServerSessions},
    47  	}
    48  
    49  	return counts, nil
    50  }
    51  
    52  // FetchMetrics interface for mackerelplugin
    53  func (m WindowsServerSessionsPlugin) FetchMetrics() (map[string]interface{}, error) {
    54  	counts, err := getCounts()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	stat := make(map[string]interface{}, len(counts))
    59  	for _, v := range counts {
    60  		var nodeMetricKey string
    61  		// node name of Windows can contain ".", which is the metric name delimiter on Mackerel.
    62  		if m.LegacyMetricName {
    63  			nodeMetricKey = v.Node
    64  		} else {
    65  			nodeMetricKey = strings.ReplaceAll(v.Node, ".", "_")
    66  		}
    67  		stat["windows.server.sessions."+nodeMetricKey+".count"] = v.ServerSessions
    68  	}
    69  	return stat, nil
    70  }
    71  
    72  // GraphDefinition interface for mackerelplugin
    73  func (m WindowsServerSessionsPlugin) GraphDefinition() map[string](mp.Graphs) {
    74  	return map[string](mp.Graphs){
    75  		"windows.server.sessions.#": mp.Graphs{
    76  			Label: "Windows Server Sessions",
    77  			Unit:  "integer",
    78  			Metrics: []mp.Metrics{
    79  				{Name: "count", Label: "count", Diff: false, Stacked: false},
    80  			},
    81  		},
    82  	}
    83  }
    84  
    85  // Do the plugin
    86  func Do() {
    87  	optTempfile := flag.String("tempfile", "", "Temp file name")
    88  	legacyMetricName := flag.Bool("legacymetricname", false, `Prevent escaping "." in the node name for metric keys`)
    89  	flag.Parse()
    90  
    91  	var plugin WindowsServerSessionsPlugin
    92  	plugin.LegacyMetricName = *legacyMetricName
    93  
    94  	helper := mp.NewMackerelPlugin(plugin)
    95  
    96  	helper.Tempfile = *optTempfile
    97  	helper.Run()
    98  }