diff --git a/.run/all.run.xml b/.run/all.run.xml index 15a33922..e5f581bf 100644 --- a/.run/all.run.xml +++ b/.run/all.run.xml @@ -19,7 +19,7 @@ - + diff --git a/README.md b/README.md index cca4f68b..beb89e7a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ A Prometheus exporter for Windows machines. | [dhcp](docs/collector.dhcp.md) | DHCP Server | | | [dns](docs/collector.dns.md) | DNS Server | | | [exchange](docs/collector.exchange.md) | Exchange metrics | | -| [filetime](docs/collector.filetime.md) | FileTime metrics | | +| [file](docs/collector.file.md) | File metrics | | | [fsrmquota](docs/collector.fsrmquota.md) | Microsoft File Server Resource Manager (FSRM) Quotas collector | | | [gpu](docs/collector.gpu.md) | GPU metrics | | | [hyperv](docs/collector.hyperv.md) | Hyper-V hosts | | diff --git a/docs/README.md b/docs/README.md index dc69b196..849181d5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -14,6 +14,7 @@ This directory contains documentation of the collectors in the windows_exporter, - [`diskdrive`](collector.diskdrive.md) - [`dns`](collector.dns.md) - [`exchange`](collector.exchange.md) +- [`file`](collector.file.md) - [`fsrmquota`](collector.fsrmquota.md) - [`hyperv`](collector.hyperv.md) - [`iis`](collector.iis.md) diff --git a/docs/collector.filetime.md b/docs/collector.filetime.md deleted file mode 100644 index 17b8369e..00000000 --- a/docs/collector.filetime.md +++ /dev/null @@ -1,36 +0,0 @@ -# filetime collector - -The filetime collector exposes modified timestamps of files in the filesystem. - -The collector - -||| --|- -Metric name prefix | `filetime` -Enabled by default? | No - -## Flags - -### `--collectors.filetime.file-patterns` -Comma-separated list of file patterns. Each pattern is a glob pattern that can contain `*`, `?`, and `**` (recursive). -See https://github.com/bmatcuk/doublestar#patterns for an extended description of the pattern syntax. - -## Metrics - -Name | Description | Type | Labels ------|-------------|------|------- -`windows_filetime_mtime_timestamp_seconds` | File modification time | gauge | `file` - -### Example metric - -``` -# HELP windows_filetime_mtime_timestamp_seconds File modification time -# TYPE windows_filetime_mtime_timestamp_seconds gauge -windows_filetime_mtime_timestamp_seconds{file="C:\\Users\\admin\\Desktop\\Dashboard.lnk"} 1.726434517e+09 -``` - -## Useful queries -_This collector does not yet have any useful queries added, we would appreciate your help adding them!_ - -## Alerting examples -_This collector does not yet have alerting examples, we would appreciate your help adding them!_ diff --git a/internal/collector/filetime/filetime.go b/internal/collector/filetime/filetime.go deleted file mode 100644 index e80b6ed1..00000000 --- a/internal/collector/filetime/filetime.go +++ /dev/null @@ -1,173 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// Copyright 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. - -//go:build windows - -package filetime - -import ( - "fmt" - "io/fs" - "log/slog" - "os" - "path/filepath" - "strings" - "sync" - - "github.com/alecthomas/kingpin/v2" - "github.com/bmatcuk/doublestar/v4" - "github.com/prometheus-community/windows_exporter/internal/mi" - "github.com/prometheus-community/windows_exporter/internal/types" - "github.com/prometheus/client_golang/prometheus" -) - -const Name = "filetime" - -type Config struct { - FilePatterns []string `yaml:"file-patterns"` -} - -//nolint:gochecknoglobals -var ConfigDefaults = Config{ - FilePatterns: []string{}, -} - -// A Collector is a Prometheus Collector for collecting file times. -type Collector struct { - config Config - - logger *slog.Logger - fileMTime *prometheus.Desc -} - -func New(config *Config) *Collector { - if config == nil { - config = &ConfigDefaults - } - - if config.FilePatterns == nil { - config.FilePatterns = ConfigDefaults.FilePatterns - } - - c := &Collector{ - config: *config, - } - - return c -} - -func NewWithFlags(app *kingpin.Application) *Collector { - c := &Collector{ - config: ConfigDefaults, - } - c.config.FilePatterns = make([]string, 0) - - app.Flag( - "collector.filetime.file-patterns", - "Comma-separated list of file patterns. Each pattern is a glob pattern that can contain `*`, `?`, and `**` (recursive). See https://github.com/bmatcuk/doublestar#patterns", - ).Default(strings.Join(ConfigDefaults.FilePatterns, ",")).StringsVar(&c.config.FilePatterns) - - return c -} - -func (c *Collector) GetName() string { - return Name -} - -func (c *Collector) Close() error { - return nil -} - -func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error { - c.logger = logger.With(slog.String("collector", Name)) - - c.logger.Info("filetime collector is in an experimental state! It may subject to change.") - - c.fileMTime = prometheus.NewDesc( - prometheus.BuildFQName(types.Namespace, Name, "mtime_timestamp_seconds"), - "File modification time", - []string{"file"}, - nil, - ) - - for _, filePattern := range c.config.FilePatterns { - basePath, pattern := doublestar.SplitPattern(filePattern) - - _, err := doublestar.Glob(os.DirFS(basePath), pattern, doublestar.WithFilesOnly()) - if err != nil { - return fmt.Errorf("invalid glob pattern: %w", err) - } - } - - return nil -} - -// Collect sends the metric values for each metric -// to the provided prometheus Metric channel. -func (c *Collector) Collect(ch chan<- prometheus.Metric) error { - wg := sync.WaitGroup{} - - for _, filePattern := range c.config.FilePatterns { - wg.Add(1) - - go func(filePattern string) { - defer wg.Done() - - if err := c.collectGlobFilePath(ch, filePattern); err != nil { - c.logger.Error("failed collecting metrics for filepath", - slog.String("filepath", filePattern), - slog.Any("err", err), - ) - } - }(filePattern) - } - - wg.Wait() - - return nil -} - -func (c *Collector) collectGlobFilePath(ch chan<- prometheus.Metric, filePattern string) error { - basePath, pattern := doublestar.SplitPattern(filepath.ToSlash(filePattern)) - basePathFS := os.DirFS(basePath) - - err := doublestar.GlobWalk(basePathFS, pattern, func(path string, d fs.DirEntry) error { - filePath := filepath.Join(basePath, path) - - fileInfo, err := os.Stat(filePath) - if err != nil { - c.logger.Warn("failed to state file", - slog.String("file", filePath), - slog.Any("err", err), - ) - - return nil - } - - ch <- prometheus.MustNewConstMetric( - c.fileMTime, - prometheus.GaugeValue, - float64(fileInfo.ModTime().UTC().UnixMicro())/1e6, - filePath, - ) - - return nil - }, doublestar.WithFilesOnly(), doublestar.WithCaseInsensitive()) - if err != nil { - return fmt.Errorf("failed to glob: %w", err) - } - - return nil -} diff --git a/internal/collector/filetime/filetime_test.go b/internal/collector/filetime/filetime_test.go deleted file mode 100644 index a8f65bc4..00000000 --- a/internal/collector/filetime/filetime_test.go +++ /dev/null @@ -1,35 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// Copyright 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. - -//go:build windows - -package filetime_test - -import ( - "testing" - - "github.com/prometheus-community/windows_exporter/internal/collector/filetime" - "github.com/prometheus-community/windows_exporter/internal/utils/testutils" -) - -func BenchmarkCollector(b *testing.B) { - testutils.FuncBenchmarkCollector(b, filetime.Name, filetime.NewWithFlags) -} - -func TestCollector(t *testing.T) { - testutils.TestCollector(t, filetime.New, &filetime.Config{ - FilePatterns: []string{"*.*"}, - }) -} diff --git a/pkg/collector/collection.go b/pkg/collector/collection.go index 52882644..e1eaeede 100644 --- a/pkg/collector/collection.go +++ b/pkg/collector/collection.go @@ -41,7 +41,6 @@ import ( "github.com/prometheus-community/windows_exporter/internal/collector/dns" "github.com/prometheus-community/windows_exporter/internal/collector/exchange" "github.com/prometheus-community/windows_exporter/internal/collector/file" - "github.com/prometheus-community/windows_exporter/internal/collector/filetime" "github.com/prometheus-community/windows_exporter/internal/collector/fsrmquota" "github.com/prometheus-community/windows_exporter/internal/collector/gpu" "github.com/prometheus-community/windows_exporter/internal/collector/hyperv" @@ -111,7 +110,6 @@ func NewWithConfig(config Config) *Collection { collectors[diskdrive.Name] = diskdrive.New(&config.DiskDrive) collectors[dns.Name] = dns.New(&config.DNS) collectors[exchange.Name] = exchange.New(&config.Exchange) - collectors[filetime.Name] = filetime.New(&config.Filetime) collectors[file.Name] = file.New(&config.File) collectors[fsrmquota.Name] = fsrmquota.New(&config.Fsrmquota) collectors[gpu.Name] = gpu.New(&config.GPU) diff --git a/pkg/collector/config.go b/pkg/collector/config.go index 6eb316bc..a148b515 100644 --- a/pkg/collector/config.go +++ b/pkg/collector/config.go @@ -31,7 +31,6 @@ import ( "github.com/prometheus-community/windows_exporter/internal/collector/dns" "github.com/prometheus-community/windows_exporter/internal/collector/exchange" "github.com/prometheus-community/windows_exporter/internal/collector/file" - "github.com/prometheus-community/windows_exporter/internal/collector/filetime" "github.com/prometheus-community/windows_exporter/internal/collector/fsrmquota" "github.com/prometheus-community/windows_exporter/internal/collector/gpu" "github.com/prometheus-community/windows_exporter/internal/collector/hyperv" @@ -81,7 +80,6 @@ type Config struct { DiskDrive diskdrive.Config `yaml:"diskdrive"` DNS dns.Config `yaml:"dns"` Exchange exchange.Config `yaml:"exchange"` - Filetime filetime.Config `yaml:"filetime"` File file.Config `yaml:"file"` Fsrmquota fsrmquota.Config `yaml:"fsrmquota"` GPU gpu.Config `yaml:"gpu"` @@ -136,7 +134,6 @@ var ConfigDefaults = Config{ DiskDrive: diskdrive.ConfigDefaults, DNS: dns.ConfigDefaults, Exchange: exchange.ConfigDefaults, - Filetime: filetime.ConfigDefaults, Fsrmquota: fsrmquota.ConfigDefaults, GPU: gpu.ConfigDefaults, HyperV: hyperv.ConfigDefaults, diff --git a/pkg/collector/map.go b/pkg/collector/map.go index da22aafd..2c5aa382 100644 --- a/pkg/collector/map.go +++ b/pkg/collector/map.go @@ -35,7 +35,6 @@ import ( "github.com/prometheus-community/windows_exporter/internal/collector/dns" "github.com/prometheus-community/windows_exporter/internal/collector/exchange" "github.com/prometheus-community/windows_exporter/internal/collector/file" - "github.com/prometheus-community/windows_exporter/internal/collector/filetime" "github.com/prometheus-community/windows_exporter/internal/collector/fsrmquota" "github.com/prometheus-community/windows_exporter/internal/collector/gpu" "github.com/prometheus-community/windows_exporter/internal/collector/hyperv" @@ -92,7 +91,6 @@ var BuildersWithFlags = map[string]BuilderWithFlags[Collector]{ diskdrive.Name: NewBuilderWithFlags(diskdrive.NewWithFlags), dns.Name: NewBuilderWithFlags(dns.NewWithFlags), exchange.Name: NewBuilderWithFlags(exchange.NewWithFlags), - filetime.Name: NewBuilderWithFlags(filetime.NewWithFlags), file.Name: NewBuilderWithFlags(file.NewWithFlags), fsrmquota.Name: NewBuilderWithFlags(fsrmquota.NewWithFlags), gpu.Name: NewBuilderWithFlags(gpu.NewWithFlags),