Skip to content

Commit

Permalink
mssql: add counter based on server version
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke committed Dec 1, 2024
1 parent f5ff75e commit 19b1507
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 172 deletions.
6 changes: 1 addition & 5 deletions docs/collector.mssql.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@ Comma-separated list of MSSQL WMI classes to use. Supported values are `accessme

If true, print available mssql WMI classes and exit. Only displays if the mssql collector is enabled.fman`, `databases`, `dbreplica`, `genstats`, `locks`, `memmgr`, `sqlstats`, `sqlerrors`, `transactions`, and `waitstats`.

### `--collector.mssql.port`

Port of MSSQL server used for `windows_mssql_info` metric. Default is `1433`.

## Metrics

| Name | Description | Type | Labels |
|--------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|-------------------------------|
| `windows_mssql_info` | Returns information about the MSSQL server running on port 1433 | gauge | `version` |
| `windows_mssql_collector_duration_seconds` | The time taken for each sub-collector to return | gauge | `collector`, `mssql_instance` |
| `windows_mssql_collector_success` | 1 if sub-collector succeeded, 0 otherwise | gauge | `collector`, `mssql_instance` |
| `windows_mssql_accessmethods_au_batch_cleanups` | The total number of batches that were completed successfully by the background task that cleans up deferred dropped allocation units | counter | `mssql_instance` |
Expand Down Expand Up @@ -197,6 +192,7 @@ Port of MSSQL server used for `windows_mssql_info` metric. Default is `1433`.
| `windows_mssql_genstats_trace_event_notification_queue_size` | Number of trace event notification instances waiting in the internal queue to be sent through Service Broker | gauge | `mssql_instance` |
| `windows_mssql_genstats_transactions` | Number of transaction enlistments (local, DTC, bound all combined) | gauge | `mssql_instance` |
| `windows_mssql_genstats_user_connections` | Counts the number of users currently connected to SQL Server | gauge | `mssql_instance` |
| `windows_mssql_instance_info ` | Returns information about the MSSQL server running on port 1433 | gauge | `version` |
| `windows_mssql_locks_average_wait_seconds` | Average amount of wait time (in milliseconds) for each lock request that resulted in a wait | gauge | `mssql_instance`, `resource` |
| `windows_mssql_locks_lock_requests` | Number of new locks and lock conversions per second requested from the lock manager | counter | `mssql_instance`, `resource` |
| `windows_mssql_locks_lock_timeouts` | Number of lock requests per second that timed out, including requests for NOWAIT locks | counter | `mssql_instance`, `resource` |
Expand Down
152 changes: 28 additions & 124 deletions internal/collector/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@ import (
"fmt"
"log/slog"
"sort"
"strconv"
"strings"
"sync"
"time"
"unsafe"

"github.com/Microsoft/go-winio/pkg/process"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/headers/iphlpapi"
"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/client_golang/prometheus"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)

Expand All @@ -46,6 +41,7 @@ const (
subCollectorDatabases = "databases"
subCollectorDatabaseReplica = "dbreplica"
subCollectorGeneralStatistics = "genstats"
subCollectorInfo = "info"
subCollectorLocks = "locks"
subCollectorMemoryManager = "memmgr"
subCollectorSQLErrors = "sqlerrors"
Expand All @@ -56,7 +52,6 @@ const (

type Config struct {
CollectorsEnabled []string `yaml:"collectors_enabled"`
Port uint16 `yaml:"port"`
}

//nolint:gochecknoglobals
Expand All @@ -68,14 +63,14 @@ var ConfigDefaults = Config{
subCollectorDatabases,
subCollectorDatabaseReplica,
subCollectorGeneralStatistics,
subCollectorInfo,
subCollectorLocks,
subCollectorMemoryManager,
subCollectorSQLErrors,
subCollectorSQLStats,
subCollectorTransactions,
subCollectorWaitStats,
},
Port: 1433,
}

// A Collector is a Prometheus Collector for various WMI Win32_PerfRawData_MSSQLSERVER_* metrics.
Expand All @@ -84,24 +79,21 @@ type Collector struct {

logger *slog.Logger

mssqlInstances mssqlInstancesType
mssqlInstances []mssqlInstance
collectorFns []func(ch chan<- prometheus.Metric) error
closeFns []func()

fileVersion string
productVersion string

// meta
mssqlScrapeDurationDesc *prometheus.Desc
mssqlScrapeSuccessDesc *prometheus.Desc
mssqlInfoDesc *prometheus.Desc

collectorAccessMethods
collectorAvailabilityReplica
collectorBufferManager
collectorDatabaseReplica
collectorDatabases
collectorGeneralStatistics
collectorInstance
collectorLocks
collectorMemoryManager
collectorSQLErrors
Expand All @@ -110,8 +102,6 @@ type Collector struct {
collectorWaitStats
}

type mssqlInstancesType map[string]string

func New(config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
Expand All @@ -121,10 +111,6 @@ func New(config *Config) *Collector {
config.CollectorsEnabled = ConfigDefaults.CollectorsEnabled
}

if config.Port == 0 {
config.Port = ConfigDefaults.Port
}

c := &Collector{
config: *config,
}
Expand All @@ -144,11 +130,6 @@ func NewWithFlags(app *kingpin.Application) *Collector {
"Comma-separated list of collectors to use.",
).Default(strings.Join(c.config.CollectorsEnabled, ",")).StringVar(&collectorsEnabled)

app.Flag(
"collector.mssql.port",
"Port of MSSQL server used for windows_mssql_info metric.",
).Default(strconv.FormatUint(uint64(c.config.Port), 10)).Uint16Var(&c.config.Port)

app.Action(func(*kingpin.ParseContext) error {
c.config.CollectorsEnabled = strings.Split(collectorsEnabled, ",")

Expand All @@ -172,18 +153,13 @@ func (c *Collector) Close() error {

func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
c.logger = logger.With(slog.String("collector", Name))
c.mssqlInstances = c.getMSSQLInstances()

fileVersion, productVersion, err := c.getMSSQLServerVersion(c.config.Port)
instances, err := c.getMSSQLInstances()
if err != nil {
logger.Warn("failed to get MSSQL server version",
slog.Any("err", err),
slog.String("collector", Name),
)
return fmt.Errorf("couldn't get SQL instances: %w", err)
}

c.fileVersion = fileVersion
c.productVersion = productVersion
c.mssqlInstances = instances

subCollectors := map[string]struct {
build func() error
Expand Down Expand Up @@ -220,6 +196,11 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
collect: c.collectGeneralStatistics,
close: c.closeGeneralStatistics,
},
subCollectorInfo: {
build: c.buildInstance,
collect: c.collectInstance,
close: c.closeInstance,
},
subCollectorLocks: {
build: c.buildLocks,
collect: c.collectLocks,
Expand Down Expand Up @@ -272,14 +253,6 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
c.closeFns = append(c.closeFns, subCollectors[name].close)
}

// meta
c.mssqlInfoDesc = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "info"),
"mssql server information",
[]string{"file_version", "version"},
nil,
)

c.mssqlScrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "collector_duration_seconds"),
"windows_exporter: Duration of an mssql child collection.",
Expand Down Expand Up @@ -326,22 +299,12 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
return errors.Join(errs...)
}

func (c *Collector) getMSSQLInstances() mssqlInstancesType {
sqlInstances := make(mssqlInstancesType)

// in case querying the registry fails, return the default instance
sqlDefaultInstance := make(mssqlInstancesType)
sqlDefaultInstance["MSSQLSERVER"] = ""

func (c *Collector) getMSSQLInstances() ([]mssqlInstance, error) {
regKey := `Software\Microsoft\Microsoft SQL Server\Instance Names\SQL`

k, err := registry.OpenKey(registry.LOCAL_MACHINE, regKey, registry.QUERY_VALUE)
if err != nil {
c.logger.Warn("couldn't open registry to determine SQL instances",
slog.Any("err", err),
)

return sqlDefaultInstance
return nil, fmt.Errorf("couldn't open registry to determine SQL instances: %w", err)
}

defer func(key registry.Key) {
Expand All @@ -354,22 +317,28 @@ func (c *Collector) getMSSQLInstances() mssqlInstancesType {

instanceNames, err := k.ReadValueNames(0)
if err != nil {
c.logger.Warn("can't ReadSubKeyNames",
slog.Any("err", err),
)

return sqlDefaultInstance
return nil, fmt.Errorf("couldn't read subkey names: %w", err)
}

sqlInstances := make([]mssqlInstance, 0, len(instanceNames))

for _, instanceName := range instanceNames {
if instanceVersion, _, err := k.GetStringValue(instanceName); err == nil {
sqlInstances[instanceName] = instanceVersion
instanceVersion, _, err := k.GetStringValue(instanceName)
if err != nil {
return nil, fmt.Errorf("couldn't get instance info: %w", err)
}

instance, err := newMssqlInstance(instanceVersion)
if err != nil {
return nil, err
}

sqlInstances = append(sqlInstances, instance)
}

c.logger.Debug(fmt.Sprintf("detected MSSQL Instances: %#v\n", sqlInstances))

return sqlInstances
return sqlInstances, nil
}

// mssqlGetPerfObjectName returns the name of the Windows Performance
Expand Down Expand Up @@ -433,68 +402,3 @@ func (c *Collector) collect(

return errors.Join(errs...)
}

// getMSSQLServerVersion get the version of the SQL Server instance by
// reading the version information from the process running the SQL Server instance port.
func (c *Collector) getMSSQLServerVersion(port uint16) (string, string, error) {
pid, err := iphlpapi.GetOwnerPIDOfTCPPort(windows.AF_INET, port)
if err != nil {
return "", "", fmt.Errorf("failed to get the PID of the process running on port 1433: %w", err)
}

hProcess, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pid)
if err != nil {
return "", "", fmt.Errorf("failed to open the process with PID %d: %w", pid, err)
}

defer windows.CloseHandle(hProcess) //nolint:errcheck

processFilePath, err := process.QueryFullProcessImageName(hProcess, process.ImageNameFormatWin32Path)
if err != nil {
return "", "", fmt.Errorf("failed to query the full path of the process with PID %d: %w", pid, err)
}

// Load the file version information
size, err := windows.GetFileVersionInfoSize(processFilePath, nil)
if err != nil {
return "", "", fmt.Errorf("failed to get the size of the file version information: %w", err)
}

fileVersionInfo := make([]byte, size)

err = windows.GetFileVersionInfo(processFilePath, 0, size, unsafe.Pointer(&fileVersionInfo[0]))
if err != nil {
return "", "", fmt.Errorf("failed to get the file version information: %w", err)
}

var (
verData *byte
verSize uint32
)

err = windows.VerQueryValue(
unsafe.Pointer(&fileVersionInfo[0]),
`\StringFileInfo\040904b0\ProductVersion`,
unsafe.Pointer(&verData),
&verSize,
)
if err != nil {
return "", "", fmt.Errorf("failed to query the product version: %w", err)
}

productVersion := windows.UTF16ToString((*[1 << 16]uint16)(unsafe.Pointer(verData))[:verSize])

err = windows.VerQueryValue(
unsafe.Pointer(&fileVersionInfo[0]),
`\StringFileInfo\040904b0\FileVersion`,
unsafe.Pointer(&verData),
&verSize,
)
if err != nil {
return "", "", fmt.Errorf("failed to query the file version: %w", err)
}

fileVersion := windows.UTF16ToString((*[1 << 16]uint16)(unsafe.Pointer(verData))[:verSize])

return fileVersion, productVersion, nil
}
6 changes: 3 additions & 3 deletions internal/collector/mssql/mssql_access_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func (c *Collector) buildAccessMethods() error {
accessMethodsWorktablesFromCacheRatioBase,
}

for sqlInstance := range c.mssqlInstances {
c.accessMethodsPerfDataCollectors[sqlInstance], err = perfdata.NewCollector(c.mssqlGetPerfObjectName(sqlInstance, "Access Methods"), nil, counters)
for _, sqlInstance := range c.mssqlInstances {
c.accessMethodsPerfDataCollectors[sqlInstance.name], err = perfdata.NewCollector(c.mssqlGetPerfObjectName(sqlInstance.name, "Access Methods"), nil, counters)
if err != nil {
errs = append(errs, fmt.Errorf("failed to create AccessMethods collector for instance %s: %w", sqlInstance, err))
errs = append(errs, fmt.Errorf("failed to create AccessMethods collector for instance %s: %w", sqlInstance.name, err))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/collector/mssql/mssql_availability_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func (c *Collector) buildAvailabilityReplica() error {
availReplicaSendsToTransportPerSec,
}

for sqlInstance := range c.mssqlInstances {
c.availabilityReplicaPerfDataCollectors[sqlInstance], err = perfdata.NewCollector(c.mssqlGetPerfObjectName(sqlInstance, "Availability Replica"), perfdata.InstancesAll, counters)
for _, sqlInstance := range c.mssqlInstances {
c.availabilityReplicaPerfDataCollectors[sqlInstance.name], err = perfdata.NewCollector(c.mssqlGetPerfObjectName(sqlInstance.name, "Availability Replica"), perfdata.InstancesAll, counters)
if err != nil {
errs = append(errs, fmt.Errorf("failed to create Availability Replica collector for instance %s: %w", sqlInstance, err))
errs = append(errs, fmt.Errorf("failed to create Availability Replica collector for instance %s: %w", sqlInstance.name, err))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/collector/mssql/mssql_buffer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func (c *Collector) buildBufferManager() error {
bufManTargetPages,
}

for sqlInstance := range c.mssqlInstances {
c.bufManPerfDataCollectors[sqlInstance], err = perfdata.NewCollector(c.mssqlGetPerfObjectName(sqlInstance, "Buffer Manager"), nil, counters)
for _, sqlInstance := range c.mssqlInstances {
c.bufManPerfDataCollectors[sqlInstance.name], err = perfdata.NewCollector(c.mssqlGetPerfObjectName(sqlInstance.name, "Buffer Manager"), nil, counters)
if err != nil {
errs = append(errs, fmt.Errorf("failed to create Buffer Manager collector for instance %s: %w", sqlInstance, err))
errs = append(errs, fmt.Errorf("failed to create Buffer Manager collector for instance %s: %w", sqlInstance.name, err))
}
}

Expand Down
Loading

0 comments on commit 19b1507

Please sign in to comment.