scheduled_task: do not report windows_scheduled_task_last_result for task that never run before (#1562)

This commit is contained in:
Jan-Otto Kröpke 2024-08-11 15:47:59 +02:00 committed by GitHub
parent dd956c986b
commit ca4ad46e2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 42 additions and 24 deletions

View File

@ -51,7 +51,11 @@ const (
TASK_STATE_QUEUED
TASK_STATE_READY
TASK_STATE_RUNNING
TASK_RESULT_SUCCESS TaskResult = 0x0
)
const (
SCHED_S_SUCCESS TaskResult = 0x0
SCHED_S_TASK_HAS_NOT_RUN TaskResult = 0x00041303
)
type ScheduledTask struct {
@ -102,19 +106,19 @@ func NewWithFlags(app *kingpin.Application) *Collector {
app.Flag(
"collector.scheduled_task.include",
"Regexp of tasks to include. Task path must both match include and not match exclude to be included.",
).Default(c.config.TaskExclude.String()).StringVar(&taskInclude)
).Default(c.config.TaskInclude.String()).StringVar(&taskInclude)
app.Action(func(*kingpin.ParseContext) error {
var err error
c.config.TaskExclude, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", taskExclude))
if err != nil {
return fmt.Errorf("collector.physical_disk.disk-exclude: %w", err)
return fmt.Errorf("collector.scheduled_task.exclude: %w", err)
}
c.config.TaskInclude, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", taskInclude))
if err != nil {
return fmt.Errorf("collector.physical_disk.disk-include: %w", err)
return fmt.Errorf("collector.scheduled_task.include: %w", err)
}
return nil
@ -187,8 +191,28 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
continue
}
for _, state := range TASK_STATES {
var stateValue float64
if strings.ToLower(task.State.String()) == state {
stateValue = 1.0
}
ch <- prometheus.MustNewConstMetric(
c.state,
prometheus.GaugeValue,
stateValue,
task.Path,
state,
)
}
if task.LastTaskResult == SCHED_S_TASK_HAS_NOT_RUN {
continue
}
lastResult := 0.0
if task.LastTaskResult == TASK_RESULT_SUCCESS {
if task.LastTaskResult == SCHED_S_SUCCESS {
lastResult = 1.0
}
@ -205,22 +229,6 @@ func (c *Collector) collect(ch chan<- prometheus.Metric) error {
task.MissedRunsCount,
task.Path,
)
for _, state := range TASK_STATES {
var stateValue float64
if strings.ToLower(task.State.String()) == state {
stateValue = 1.0
}
ch <- prometheus.MustNewConstMetric(
c.state,
prometheus.GaugeValue,
stateValue,
task.Path,
state,
)
}
}
return nil

View File

@ -4,5 +4,5 @@ import "regexp"
var (
RegExpAny = regexp.MustCompile(".+")
RegExpEmpty = regexp.MustCompile("^$")
RegExpEmpty = regexp.MustCompile("")
)

View File

@ -43,6 +43,7 @@ windows_exporter_collector_success{collector="logical_disk"} 1
windows_exporter_collector_success{collector="physical_disk"} 1
windows_exporter_collector_success{collector="net"} 1
windows_exporter_collector_success{collector="os"} 1
windows_exporter_collector_success{collector="scheduled_task"} 1
windows_exporter_collector_success{collector="service"} 1
windows_exporter_collector_success{collector="system"} 1
windows_exporter_collector_success{collector="textfile"} 1
@ -54,6 +55,7 @@ windows_exporter_collector_timeout{collector="logical_disk"} 0
windows_exporter_collector_timeout{collector="physical_disk"} 0
windows_exporter_collector_timeout{collector="net"} 0
windows_exporter_collector_timeout{collector="os"} 0
windows_exporter_collector_timeout{collector="scheduled_task"} 0
windows_exporter_collector_timeout{collector="service"} 0
windows_exporter_collector_timeout{collector="system"} 0
windows_exporter_collector_timeout{collector="textfile"} 0
@ -169,6 +171,13 @@ windows_exporter_collector_timeout{collector="textfile"} 0
# TYPE windows_os_virtual_memory_free_bytes gauge
# HELP windows_os_visible_memory_bytes OperatingSystem.TotalVisibleMemorySize
# TYPE windows_os_visible_memory_bytes gauge
# HELP windows_scheduled_task_state The current state of a scheduled task
# TYPE windows_scheduled_task_state gauge
windows_scheduled_task_state{state="disabled",task="/Microsoft/Windows/Maintenance/WinSAT"} 1
windows_scheduled_task_state{state="queued",task="/Microsoft/Windows/Maintenance/WinSAT"} 0
windows_scheduled_task_state{state="ready",task="/Microsoft/Windows/Maintenance/WinSAT"} 0
windows_scheduled_task_state{state="running",task="/Microsoft/Windows/Maintenance/WinSAT"} 0
windows_scheduled_task_state{state="unknown",task="/Microsoft/Windows/Maintenance/WinSAT"} 0
# HELP windows_service_info A metric with a constant '1' value labeled with service information
# TYPE windows_service_info gauge
# HELP windows_service_start_mode The start mode of the service (StartMode)

View File

@ -25,7 +25,7 @@ $skip_re = "^(go_|windows_exporter_build_info|windows_exporter_collector_duratio
$exporter_proc = Start-Process `
-PassThru `
-FilePath ..\windows_exporter.exe `
-ArgumentList "--log.level=debug --web.disable-exporter-metrics --collectors.enabled=[defaults],textfile --collector.textfile.directories=$($textfile_dir)" `
-ArgumentList "--log.level=debug --web.disable-exporter-metrics --collectors.enabled=[defaults],textfile,scheduled_task --collector.scheduled_task.include=.*WinSAT --collector.textfile.directories=$($textfile_dir)" `
-WindowStyle Hidden `
-RedirectStandardOutput "$($temp_dir)/windows_exporter.log" `
-RedirectStandardError "$($temp_dir)/windows_exporter_error.log"
@ -36,7 +36,7 @@ for ($i=1; $i -le 5; $i++) {
$netstat_output = netstat -anp tcp | Select-String 'listening'
if ($netstat_output -like '*:9182*') {
break
break
}
Write-Host "Waiting for exporter to start"
}
@ -64,5 +64,6 @@ if (-not ($null -eq $output_diff)) {
Get-Content "$($temp_dir)/windows_exporter.log"
Write-Host "STDERR"
Get-Content "$($temp_dir)/windows_exporter_error.log"
exit 1
}