2021-12-18 18:01:29 +00:00
//go:build windows
2018-11-30 00:51:12 +00:00
// +build windows
2018-02-14 11:24:52 +00:00
package collector
import (
"strings"
2018-03-25 07:50:37 +00:00
2021-01-30 10:16:53 +00:00
"github.com/prometheus-community/windows_exporter/log"
2018-02-14 11:24:52 +00:00
"github.com/prometheus/client_golang/prometheus"
2023-03-12 00:27:31 +00:00
"github.com/yusufpapurcu/wmi"
2018-03-25 07:50:37 +00:00
"gopkg.in/alecthomas/kingpin.v2"
2018-02-14 11:24:52 +00:00
)
func init ( ) {
2020-02-09 20:09:26 +00:00
registerCollector ( "msmq" , NewMSMQCollector )
2018-02-14 11:24:52 +00:00
}
var (
2018-03-25 07:50:37 +00:00
msmqWhereClause = kingpin . Flag ( "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." ) . String ( )
2018-02-14 11:24:52 +00:00
)
// 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
2018-04-05 05:11:36 +00:00
2018-02-14 11:24:52 +00:00
queryWhereClause string
}
// NewWin32_PerfRawData_MSMQ_MSMQQueueCollector ...
func NewMSMQCollector ( ) ( Collector , error ) {
const subsystem = "msmq"
2018-04-05 05:11:36 +00:00
2019-03-14 10:42:34 +00:00
if * msmqWhereClause == "" {
2018-04-05 05:27:26 +00:00
log . Warn ( "No where-clause specified for msmq collector. This will generate a very large number of metrics!" )
2018-02-14 11:24:52 +00:00
}
2018-04-05 05:11:36 +00:00
2018-02-14 11:24:52 +00:00
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 ,
) ,
2018-06-06 08:31:50 +00:00
queryWhereClause : * msmqWhereClause ,
2018-02-14 11:24:52 +00:00
} , nil
}
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
2019-04-05 13:59:40 +00:00
func ( c * Win32_PerfRawData_MSMQ_MSMQQueueCollector ) Collect ( ctx * ScrapeContext , ch chan <- prometheus . Metric ) error {
2018-02-14 11:24:52 +00:00
if desc , err := c . collect ( ch ) ; err != nil {
2018-04-05 05:27:26 +00:00
log . Error ( "failed collecting msmq metrics:" , desc , err )
2018-02-14 11:24:52 +00:00
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
2018-06-06 08:31:50 +00:00
q := queryAllWhere ( & dst , c . queryWhereClause )
2018-02-14 11:24:52 +00:00
if err := wmi . Query ( q , & dst ) ; err != nil {
return nil , err
}
2018-04-05 05:11:36 +00:00
2018-02-14 11:24:52 +00:00
for _ , msmq := range dst {
ch <- prometheus . MustNewConstMetric (
c . BytesinJournalQueue ,
prometheus . GaugeValue ,
float64 ( msmq . BytesinJournalQueue ) ,
strings . ToLower ( msmq . Name ) ,
)
2021-05-16 04:50:55 +00:00
2018-02-14 11:24:52 +00:00
ch <- prometheus . MustNewConstMetric (
c . BytesinQueue ,
prometheus . GaugeValue ,
float64 ( msmq . BytesinQueue ) ,
strings . ToLower ( msmq . Name ) ,
)
2021-05-16 04:50:55 +00:00
2018-02-14 11:24:52 +00:00
ch <- prometheus . MustNewConstMetric (
c . MessagesinJournalQueue ,
prometheus . GaugeValue ,
float64 ( msmq . MessagesinJournalQueue ) ,
strings . ToLower ( msmq . Name ) ,
)
2021-05-16 04:50:55 +00:00
2018-02-14 11:24:52 +00:00
ch <- prometheus . MustNewConstMetric (
c . MessagesinQueue ,
prometheus . GaugeValue ,
float64 ( msmq . MessagesinQueue ) ,
strings . ToLower ( msmq . Name ) ,
)
2018-04-05 05:11:36 +00:00
}
2018-02-14 11:24:52 +00:00
return nil , nil
}