From 6b98771187acca91d2d5cb49bb5fc86a910b5060 Mon Sep 17 00:00:00 2001 From: Teriand Date: Wed, 14 Feb 2018 14:24:52 +0300 Subject: [PATCH] Add MSMQ collector (#159) Add MSMQ collector --- README.md | 1 + collector/msmq.go | 132 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 collector/msmq.go diff --git a/README.md b/README.md index c0cd70e7..c64a34f9 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ dns | [Win32_PerfRawData_DNS_DNS](https://technet.microsoft.com/en-us/library/cc iis | [Win32_PerfRawData_W3SVC_WebService](https://msdn.microsoft.com/en-us/library/aa394345) IIS metrics | logical_disk | [Win32_PerfRawData_PerfDisk_LogicalDisk](https://msdn.microsoft.com/en-us/windows/hardware/aa394307(v=vs.71)) metrics (disk I/O) | ✓ net | [Win32_PerfRawData_Tcpip_NetworkInterface](https://technet.microsoft.com/en-us/security/aa394340(v=vs.80)) metrics (network interface I/O) | ✓ +msmq | [Win32_PerfRawData_MSMQ_MSMQQueue](http://wutils.com/wmi/root/cimv2/win32_perfrawdata_msmq_msmqqueue/) metrics (MSMQ/journal count) | os | [Win32_OperatingSystem](https://msdn.microsoft.com/en-us/library/aa394239) metrics (memory, processes, users) | ✓ process | [Win32_PerfRawData_PerfProc_Process](https://msdn.microsoft.com/en-us/library/aa394323(v=vs.85).aspx) metrics (per-process stats) | service | [Win32_Service](https://msdn.microsoft.com/en-us/library/aa394418(v=vs.85).aspx) metrics (service states) | ✓ diff --git a/collector/msmq.go b/collector/msmq.go new file mode 100644 index 00000000..31b32abb --- /dev/null +++ b/collector/msmq.go @@ -0,0 +1,132 @@ +// returns data points from Win32_PerfRawData_MSMQ_MSMQQueue +// - Win32_PerfRawData_MSMQ_MSMQQueue class +package collector + +import ( + "log" + "flag" + "bytes" + "strings" + "github.com/StackExchange/wmi" + "github.com/prometheus/client_golang/prometheus" +) + +func init() { + Factories["msmq"] = NewMSMQCollector +} + +var ( + msmqWhereClause = flag.String("collector.msmq.msmq-where", "", "WQL 'where' clause to use in WMI metrics query. Limits the response to the msmqs you specify and reduces the size of the response.") +) + +// A Win32_PerfRawData_MSMQ_MSMQQueueCollector is a Prometheus collector for WMI Win32_PerfRawData_MSMQ_MSMQQueue metrics +type Win32_PerfRawData_MSMQ_MSMQQueueCollector struct { + BytesinJournalQueue *prometheus.Desc + BytesinQueue *prometheus.Desc + MessagesinJournalQueue *prometheus.Desc + MessagesinQueue *prometheus.Desc + + queryWhereClause string +} + +// NewWin32_PerfRawData_MSMQ_MSMQQueueCollector ... +func NewMSMQCollector() (Collector, error) { + const subsystem = "msmq" + + var wc bytes.Buffer + if *msmqWhereClause != "" { + wc.WriteString("WHERE ") + wc.WriteString(*msmqWhereClause) + log.Println("warning: No where-clause specified for msmq collector. This will generate a very large number of metrics!") + } +// else { +// log.Println("warning: No where-clause specified for msmq collector. This will generate a very large number of metrics!") +// } + return &Win32_PerfRawData_MSMQ_MSMQQueueCollector{ + BytesinJournalQueue: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "bytes_in_journal_queue"), + "Size of queue journal in bytes", + []string{"name"}, + nil, + ), + BytesinQueue: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "bytes_in_queue"), + "Size of queue in bytes", + []string{"name"}, + nil, + ), + MessagesinJournalQueue: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "messages_in_journal_queue"), + "Count messages in queue journal", + []string{"name"}, + nil, + ), + MessagesinQueue: prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, "messages_in_queue"), + "Count messages in queue", + []string{"name"}, + nil, + ), + queryWhereClause: wc.String(), + }, nil +} + +// Collect sends the metric values for each metric +// to the provided prometheus Metric channel. +func (c *Win32_PerfRawData_MSMQ_MSMQQueueCollector) Collect(ch chan<- prometheus.Metric) error { + if desc, err := c.collect(ch); err != nil { + log.Println("[ERROR] failed collecting msmq metrics:", desc, err) + return err + } + return nil +} + +type Win32_PerfRawData_MSMQ_MSMQQueue struct { + Name string + + BytesinJournalQueue uint64 + BytesinQueue uint64 + MessagesinJournalQueue uint64 + MessagesinQueue uint64 +} + +func (c *Win32_PerfRawData_MSMQ_MSMQQueueCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { + var dst []Win32_PerfRawData_MSMQ_MSMQQueue + q := wmi.CreateQuery(&dst, c.queryWhereClause) + if err := wmi.Query(q, &dst); err != nil { + return nil, err + } + + for _, msmq := range dst { + + if msmq.Name == "Computer Queues" { + continue + } + + ch <- prometheus.MustNewConstMetric( + c.BytesinJournalQueue, + prometheus.GaugeValue, + float64(msmq.BytesinJournalQueue), + strings.ToLower(msmq.Name), + ) + ch <- prometheus.MustNewConstMetric( + c.BytesinQueue, + prometheus.GaugeValue, + float64(msmq.BytesinQueue), + strings.ToLower(msmq.Name), + ) + ch <- prometheus.MustNewConstMetric( + c.MessagesinJournalQueue, + prometheus.GaugeValue, + float64(msmq.MessagesinJournalQueue), + strings.ToLower(msmq.Name), + ) + ch <- prometheus.MustNewConstMetric( + c.MessagesinQueue, + prometheus.GaugeValue, + float64(msmq.MessagesinQueue), + strings.ToLower(msmq.Name), + ) +} + return nil, nil +}