mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-04-03 00:06:36 +00:00
file: fix collector producing no metrics, startup hang, and duplicate metric errors (#2371)
Signed-off-by: EisenbergD <dominik.eisenberg@beiersdorf.com> Co-authored-by: EisenbergD <dominik.eisenberg@beiersdorf.com>
This commit is contained in:
committed by
GitHub
parent
43f83573ef
commit
612b29cfc9
@@ -18,19 +18,19 @@ See https://github.com/bmatcuk/doublestar#patterns for an extended description o
|
||||
## Metrics
|
||||
|
||||
| Name | Description | Type | Labels |
|
||||
|----------------------------------------|------------------------|-------|--------|
|
||||
| `windows_file_mtime_timestamp_seconds` | File modification time | gauge | `file` |
|
||||
| `windows_file_size_bytes` | File size | gauge | `file` |
|
||||
|----------------------------------------|------------------------|-------|--------------------|
|
||||
| `windows_file_mtime_timestamp_seconds` | File modification time | gauge | `file`, `pattern` |
|
||||
| `windows_file_size_bytes` | File size | gauge | `file`, `pattern` |
|
||||
|
||||
### Example metric
|
||||
|
||||
```
|
||||
# HELP windows_file_mtime_timestamp_seconds File modification time
|
||||
# TYPE windows_file_mtime_timestamp_seconds gauge
|
||||
windows_file_mtime_timestamp_seconds{file="C:\\Users\\admin\\Desktop\\Dashboard.lnk"} 1.726434517e+09
|
||||
windows_file_mtime_timestamp_seconds{file="C:\\Users\\admin\\Desktop\\Dashboard.lnk",pattern="C:\\Users\\admin\\Desktop\\*.lnk"} 1.726434517e+09
|
||||
# HELP windows_file_size_bytes File size
|
||||
# TYPE windows_file_size_bytes gauge
|
||||
windows_file_size_bytes{file="C:\\Users\\admin\\Desktop\\Dashboard.lnk"} 123
|
||||
windows_file_size_bytes{file="C:\\Users\\admin\\Desktop\\Dashboard.lnk",pattern="C:\\Users\\admin\\Desktop\\*.lnk"} 123
|
||||
```
|
||||
|
||||
## Useful queries
|
||||
|
||||
@@ -74,12 +74,23 @@ func NewWithFlags(app *kingpin.Application) *Collector {
|
||||
c := &Collector{
|
||||
config: ConfigDefaults,
|
||||
}
|
||||
c.config.FilePatterns = make([]string, 0)
|
||||
|
||||
var filePatterns string
|
||||
|
||||
app.Flag(
|
||||
"collector.file.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)
|
||||
).Default(strings.Join(ConfigDefaults.FilePatterns, ",")).StringVar(&filePatterns)
|
||||
|
||||
app.Action(func(*kingpin.ParseContext) error {
|
||||
for p := range strings.SplitSeq(filePatterns, ",") {
|
||||
if p != "" {
|
||||
c.config.FilePatterns = append(c.config.FilePatterns, p)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return c
|
||||
}
|
||||
@@ -100,23 +111,24 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
|
||||
c.fileMTime = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "mtime_timestamp_seconds"),
|
||||
"File modification time",
|
||||
[]string{"file"},
|
||||
[]string{"file", "pattern"},
|
||||
nil,
|
||||
)
|
||||
|
||||
c.fileSize = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, Name, "size_bytes"),
|
||||
"File size",
|
||||
[]string{"file"},
|
||||
[]string{"file", "pattern"},
|
||||
nil,
|
||||
)
|
||||
|
||||
for _, filePattern := range c.config.FilePatterns {
|
||||
basePath, pattern := doublestar.SplitPattern(filePattern)
|
||||
if filePattern == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := doublestar.Glob(os.DirFS(basePath), pattern, doublestar.WithFilesOnly())
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid glob pattern: %w", err)
|
||||
if !doublestar.ValidatePattern(filepath.ToSlash(filePattern)) {
|
||||
return fmt.Errorf("invalid glob pattern: %s", filePattern)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,6 +182,7 @@ func (c *Collector) collectGlobFilePath(ch chan<- prometheus.Metric, filePattern
|
||||
prometheus.GaugeValue,
|
||||
float64(fileInfo.ModTime().UTC().UnixMicro())/1e6,
|
||||
filePath,
|
||||
filePattern,
|
||||
)
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
@@ -177,6 +190,7 @@ func (c *Collector) collectGlobFilePath(ch chan<- prometheus.Metric, filePattern
|
||||
prometheus.GaugeValue,
|
||||
float64(fileInfo.Size()),
|
||||
filePath,
|
||||
filePattern,
|
||||
)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -134,6 +134,7 @@ var ConfigDefaults = Config{
|
||||
DiskDrive: diskdrive.ConfigDefaults,
|
||||
DNS: dns.ConfigDefaults,
|
||||
Exchange: exchange.ConfigDefaults,
|
||||
File: file.ConfigDefaults,
|
||||
Fsrmquota: fsrmquota.ConfigDefaults,
|
||||
GPU: gpu.ConfigDefaults,
|
||||
HyperV: hyperv.ConfigDefaults,
|
||||
|
||||
Reference in New Issue
Block a user