From d1130264ec6139724746b435bf17dfe50c547385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sun, 24 Nov 2024 13:18:22 +0100 Subject: [PATCH 1/3] msmq: Use Performance Counter instead WMI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- docs/collector.msmq.md | 26 ++++------ internal/collector/msmq/const.go | 21 ++++++++ internal/collector/msmq/msmq.go | 88 ++++++++++---------------------- 3 files changed, 59 insertions(+), 76 deletions(-) create mode 100644 internal/collector/msmq/const.go diff --git a/docs/collector.msmq.md b/docs/collector.msmq.md index c973219e3..8042c5361 100644 --- a/docs/collector.msmq.md +++ b/docs/collector.msmq.md @@ -2,26 +2,22 @@ The msmq collector exposes metrics about the queues on a MSMQ server -||| --|- -Metric name prefix | `msmq` -Classes | `Win32_PerfRawData_MSMQ_MSMQQueue` -Enabled by default? | No +| | | +|---------------------|----------------------| +| Metric name prefix | `msmq` | +| Spource | Performance Counters | +| Enabled by default? | No | ## Flags -### `--collector.msmq.msmq-where` - -A WMI filter on which queues to include. `%` is a wildcard, and can be used to match on substrings. - ## Metrics -Name | Description | Type | Labels ------|-------------|------|------- -`windows_msmq_bytes_in_journal_queue` | Size of queue journal in bytes | gauge | `name` -`windows_msmq_bytes_in_queue` | Size of queue in bytes | gauge | `name` -`windows_msmq_messages_in_journal_queue` | Count messages in queue journal | gauge | `name` -`windows_msmq_messages_in_queue` | Count messages in queue | gauge | `name` +| Name | Description | Type | Labels | +|------------------------------------------|---------------------------------|-------|--------| +| `windows_msmq_bytes_in_journal_queue` | Size of queue journal in bytes | gauge | `name` | +| `windows_msmq_bytes_in_queue` | Size of queue in bytes | gauge | `name` | +| `windows_msmq_messages_in_journal_queue` | Count messages in queue journal | gauge | `name` | +| `windows_msmq_messages_in_queue` | Count messages in queue | gauge | `name` | ### Example metric _This collector does not yet have explained examples, we would appreciate your help adding them!_ diff --git a/internal/collector/msmq/const.go b/internal/collector/msmq/const.go new file mode 100644 index 000000000..87cff529f --- /dev/null +++ b/internal/collector/msmq/const.go @@ -0,0 +1,21 @@ +// Copyright 2024 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package msmq + +const ( + BytesInJournalQueue = "Bytes in Journal Queue" + BytesInQueue = "Bytes in Queue" + MessagesInJournalQueue = "Messages in Journal Queue" + MessagesInQueue = "Messages in Queue" +) diff --git a/internal/collector/msmq/msmq.go b/internal/collector/msmq/msmq.go index bdc7e114e..b72723ff4 100644 --- a/internal/collector/msmq/msmq.go +++ b/internal/collector/msmq/msmq.go @@ -3,32 +3,26 @@ package msmq import ( - "errors" "fmt" "log/slog" - "strings" "github.com/alecthomas/kingpin/v2" "github.com/prometheus-community/windows_exporter/internal/mi" + "github.com/prometheus-community/windows_exporter/internal/perfdata" "github.com/prometheus-community/windows_exporter/internal/types" - "github.com/prometheus-community/windows_exporter/internal/utils" "github.com/prometheus/client_golang/prometheus" ) const Name = "msmq" -type Config struct { - QueryWhereClause *string `yaml:"query_where_clause"` -} +type Config struct{} -var ConfigDefaults = Config{ - QueryWhereClause: utils.ToPTR(""), -} +var ConfigDefaults = Config{} // A Collector is a Prometheus Collector for WMI Win32_PerfRawData_MSMQ_MSMQQueue metrics. type Collector struct { - config Config - miSession *mi.Session + config Config + perfDataCollector *perfdata.Collector bytesInJournalQueue *prometheus.Desc bytesInQueue *prometheus.Desc @@ -41,10 +35,6 @@ func New(config *Config) *Collector { config = &ConfigDefaults } - if config.QueryWhereClause == nil { - config.QueryWhereClause = ConfigDefaults.QueryWhereClause - } - c := &Collector{ config: *config, } @@ -52,15 +42,11 @@ func New(config *Config) *Collector { return c } -func NewWithFlags(app *kingpin.Application) *Collector { +func NewWithFlags(_ *kingpin.Application) *Collector { c := &Collector{ config: ConfigDefaults, } - app.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."). - Default(*c.config.QueryWhereClause).StringVar(c.config.QueryWhereClause) - return c } @@ -72,17 +58,17 @@ func (c *Collector) Close() error { return nil } -func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error { - logger = logger.With(slog.String("collector", Name)) +func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { + var err error - if miSession == nil { - return errors.New("miSession is nil") - } - - c.miSession = miSession - - if *c.config.QueryWhereClause == "" { - logger.Warn("No where-clause specified for msmq collector. This will generate a very large number of metrics!") + c.perfDataCollector, err = perfdata.NewCollector("MSMQ Queue", perfdata.InstanceAll, []string{ + BytesInJournalQueue, + BytesInQueue, + MessagesInJournalQueue, + MessagesInQueue, + }) + if err != nil { + return fmt.Errorf("failed to create MSMQ Queue collector: %w", err) } c.bytesInJournalQueue = prometheus.NewDesc( @@ -113,61 +99,41 @@ func (c *Collector) Build(logger *slog.Logger, miSession *mi.Session) error { return nil } -type msmqQueue struct { - Name string `mi:"Name"` - - BytesInJournalQueue uint64 `mi:"BytesInJournalQueue"` - BytesInQueue uint64 `mi:"BytesInQueue"` - MessagesInJournalQueue uint64 `mi:"MessagesInJournalQueue"` - MessagesInQueue uint64 `mi:"MessagesInQueue"` -} - // Collect sends the metric values for each metric // to the provided prometheus Metric channel. func (c *Collector) Collect(ch chan<- prometheus.Metric) error { - var dst []msmqQueue - - query := "SELECT * FROM Win32_PerfRawData_MSMQ_MSMQQueue" - if *c.config.QueryWhereClause != "" { - query += " WHERE " + *c.config.QueryWhereClause - } - - queryExpression, err := mi.NewQuery(query) + perfData, err := c.perfDataCollector.Collect() if err != nil { - return fmt.Errorf("failed to create WMI query: %w", err) - } - - if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, queryExpression); err != nil { - return fmt.Errorf("WMI query failed: %w", err) + return fmt.Errorf("failed to collect MSMQ Queue metrics: %w", err) } - for _, msmq := range dst { + for name, data := range perfData { ch <- prometheus.MustNewConstMetric( c.bytesInJournalQueue, prometheus.GaugeValue, - float64(msmq.BytesInJournalQueue), - strings.ToLower(msmq.Name), + data[BytesInJournalQueue].FirstValue, + name, ) ch <- prometheus.MustNewConstMetric( c.bytesInQueue, prometheus.GaugeValue, - float64(msmq.BytesInQueue), - strings.ToLower(msmq.Name), + data[BytesInQueue].FirstValue, + name, ) ch <- prometheus.MustNewConstMetric( c.messagesInJournalQueue, prometheus.GaugeValue, - float64(msmq.MessagesInJournalQueue), - strings.ToLower(msmq.Name), + data[MessagesInJournalQueue].FirstValue, + name, ) ch <- prometheus.MustNewConstMetric( c.messagesInQueue, prometheus.GaugeValue, - float64(msmq.MessagesInQueue), - strings.ToLower(msmq.Name), + data[MessagesInQueue].FirstValue, + name, ) } From bbcb207d05fc84e63499a3ee46febc081e8f4bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sun, 24 Nov 2024 13:23:46 +0100 Subject: [PATCH 2/3] msmq: Use Performance Counter instead WMI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- internal/collector/msmq/msmq.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/collector/msmq/msmq.go b/internal/collector/msmq/msmq.go index b72723ff4..d68eaf3e7 100644 --- a/internal/collector/msmq/msmq.go +++ b/internal/collector/msmq/msmq.go @@ -61,7 +61,7 @@ func (c *Collector) Close() error { func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { var err error - c.perfDataCollector, err = perfdata.NewCollector("MSMQ Queue", perfdata.InstanceAll, []string{ + c.perfDataCollector, err = perfdata.NewCollector("MSMQ Queue", perfdata.InstancesAll, []string{ BytesInJournalQueue, BytesInQueue, MessagesInJournalQueue, From 83f923c9a26409bbd94892dfc5a63f96fec517db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sun, 24 Nov 2024 13:25:02 +0100 Subject: [PATCH 3/3] msmq: Use Performance Counter instead WMI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- internal/collector/smb/smb.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/collector/smb/smb.go b/internal/collector/smb/smb.go index e4f247cc4..28d707594 100644 --- a/internal/collector/smb/smb.go +++ b/internal/collector/smb/smb.go @@ -96,19 +96,19 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { nil, ) c.writeRequests = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "server_shares_write_requests_count"), + prometheus.BuildFQName(types.Namespace, Name, "server_shares_write_requests_count_total"), "Writes requests on the SMB Server Share", []string{"share"}, nil, ) c.readRequests = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "server_shares_read_requests_count"), + prometheus.BuildFQName(types.Namespace, Name, "server_shares_read_requests_count_total"), "Read requests on the SMB Server Share", []string{"share"}, nil, ) c.metadataRequests = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "server_shares_metadata_requests_count"), + prometheus.BuildFQName(types.Namespace, Name, "server_shares_metadata_requests_count_total"), "Metadata requests on the SMB Server Share", []string{"share"}, nil, @@ -120,7 +120,7 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { nil, ) c.filesOpened = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "server_shares_filed_opened_count"), + prometheus.BuildFQName(types.Namespace, Name, "server_shares_filed_opened_count_total"), "Files opened on the SMB Server Share", []string{"share"}, nil, @@ -160,21 +160,21 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric( c.writeRequests, - prometheus.GaugeValue, + prometheus.CounterValue, data[writeRequests].FirstValue, share, ) ch <- prometheus.MustNewConstMetric( c.readRequests, - prometheus.GaugeValue, + prometheus.CounterValue, data[readRequests].FirstValue, share, ) ch <- prometheus.MustNewConstMetric( c.metadataRequests, - prometheus.GaugeValue, + prometheus.CounterValue, data[metadataRequests].FirstValue, share, ) @@ -188,7 +188,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric( c.filesOpened, - prometheus.GaugeValue, + prometheus.CounterValue, data[filesOpened].FirstValue, share, )