msmq: Use Performance Counter instead WMI (#1766)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke 2024-11-24 13:38:24 +01:00 committed by GitHub
parent e812584f27
commit 86ace68978
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 84 deletions

View File

@ -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!_

View File

@ -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"
)

View File

@ -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.InstancesAll, []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)
return fmt.Errorf("failed to collect MSMQ Queue metrics: %w", err)
}
if err := c.miSession.Query(&dst, mi.NamespaceRootCIMv2, queryExpression); err != nil {
return fmt.Errorf("WMI query failed: %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,
)
}

View File

@ -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,
)