Skip to content

Commit f45ce7c

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
check_tasksched: citest fixes
update docs split up the addTasks function since it was getting too long, and had logical parts to be split others are spacing, linting fixes from golangci-lint
1 parent cceafcb commit f45ce7c

5 files changed

Lines changed: 89 additions & 66 deletions

File tree

docs/checks/commands/check_tasksched.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ Naemon Config
5656

5757
| Argument | Description |
5858
| --------- | --------------------------------------------------------------------------------------------------------- |
59-
| folder | The folder where the scheduled task is saved. This is used for exact matches, unless recurisive option is enabled. |
60-
| recursive | Include the subfolders of the specified folder as well when searching for scheduled tasks. |
59+
| folder | The folder where the scheduled task is saved. This is used for exact matches, unless recurisive option is enabled. Default: '\' |
60+
| hidden | Include hidden tasks. Default: 'false' |
61+
| recursive | Include the subfolders of the specified folder as well when searching for scheduled tasks. Default: 'true' |
6162
| timezone | Sets the timezone for time metrics (default is local time) |
62-
| title | Sets the task to check. This corresonds to the title of the scheduled task, called TaskName in Powershell output. |
63+
| title | Sets the task to check. This corresonds to the title of the scheduled task. Default: '\*' |
6364

6465
## Attributes
6566

@@ -89,4 +90,4 @@ these can be used in filters and thresholds (along with the default attributes):
8990
| next_run_time | Time when the registered task is next scheduled to run |
9091
| parameters | Last actions command line parameters |
9192
| execute | Last actions executed program |
92-
| working_dir | Last actions working directory |
93+
| working_directory | Last actions working directory |

pkg/snclient/check_tasksched.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@ func (l *CheckTasksched) Build() *CheckData {
4343
},
4444
args: map[string]CheckArgument{
4545
"timezone": {description: "Sets the timezone for time metrics (default is local time)"},
46-
"title": {value: &l.TaskTitle,
47-
description: fmt.Sprintf("Sets the task to check. This corresonds to the title of the scheduled task. Default: '%s'", CheckTaskschedDefaultTaskTitle)},
48-
"folder": {value: &l.Folder,
46+
"title": {
47+
value: &l.TaskTitle,
48+
description: fmt.Sprintf("Sets the task to check. This corresonds to the title of the scheduled task. Default: '%s'", CheckTaskschedDefaultTaskTitle),
49+
},
50+
"folder": {
51+
value: &l.Folder,
4952
description: fmt.Sprintf("The folder where the scheduled task is saved. This is used for exact matches, unless recurisive option is enabled. Default: '%s'",
50-
CheckTaskschedDefaultFolder)},
51-
"recursive": {value: &l.Recursive,
52-
description: fmt.Sprintf("Include the subfolders of the specified folder as well when searching for scheduled tasks. Default: '%t'", CheckTaskschedDefaultRecursive)},
53-
"hidden": {value: &l.Hidden,
54-
description: fmt.Sprintf("Include hidden tasks. Default: '%t'", CheckTaskschedDefaultHidden)},
53+
CheckTaskschedDefaultFolder),
54+
},
55+
"recursive": {
56+
value: &l.Recursive,
57+
description: fmt.Sprintf("Include the subfolders of the specified folder as well when searching for scheduled tasks. Default: '%t'", CheckTaskschedDefaultRecursive),
58+
},
59+
"hidden": {
60+
value: &l.Hidden,
61+
description: fmt.Sprintf("Include hidden tasks. Default: '%t'", CheckTaskschedDefaultHidden),
62+
},
5563
},
5664
defaultFilter: "enabled = true",
5765
defaultCritical: "exit_code < 0",

pkg/snclient/check_tasksched_windows.go

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
_ "embed"
88
"fmt"
9+
"os/exec"
910
"slices"
1011
"strconv"
1112
"strings"
@@ -18,24 +19,8 @@ import (
1819
//go:embed embed/scripts/windows/scheduled_tasks.ps1
1920
var scheduledTasksPS1 string
2021

21-
//nolint:funlen // function is long, but is simple, should not be dismantled
2222
func (l *CheckTasksched) addTasks(ctx context.Context, snc *Agent, check *CheckData) error {
23-
script := scheduledTasksPS1
24-
25-
// Add backslash to the beginning of the folder path if it does not exist
26-
if l.Folder != CheckTaskschedDefaultFolder {
27-
if !strings.HasPrefix(l.Folder, "\\") {
28-
l.Folder = "\\" + l.Folder
29-
}
30-
}
31-
32-
// Remove backslash at the end of the folder path, if it is not exactly root: "\"
33-
// "\Microsoft\" -> "\Microsoft"
34-
if l.Folder != CheckTaskschedDefaultFolder && l.Folder != "\\" {
35-
if cut, cutOk := strings.CutSuffix(l.Folder, "\\"); cutOk {
36-
l.Folder = cut
37-
}
38-
}
23+
l.cleanupArguments()
3924

4025
titleRuneBlacklist := []rune{'\\', '/', ':', '*', '?', '"', '<', '>', '|'}
4126
if l.TaskTitle != CheckTaskschedDefaultTaskTitle {
@@ -52,41 +37,7 @@ func (l *CheckTasksched) addTasks(ctx context.Context, snc *Agent, check *CheckD
5237
}
5338
}
5439

55-
cmd, err := powerShellCmd(
56-
ctx, script,
57-
PowerShellParameter{
58-
name: "title",
59-
parameterType: "string",
60-
specifyDefaultValue: true,
61-
defaultValue: CheckTaskschedDefaultTaskTitle,
62-
specifyValue: true,
63-
specifiedValue: l.TaskTitle,
64-
},
65-
PowerShellParameter{
66-
name: "folder",
67-
parameterType: "string",
68-
specifyDefaultValue: true,
69-
defaultValue: CheckTaskschedDefaultFolder,
70-
specifyValue: true,
71-
specifiedValue: l.Folder,
72-
},
73-
PowerShellParameter{
74-
name: "recursive",
75-
parameterType: "string",
76-
specifyDefaultValue: true,
77-
defaultValue: strconv.FormatBool(CheckTaskschedDefaultRecursive),
78-
specifyValue: true,
79-
specifiedValue: strconv.FormatBool(l.Recursive),
80-
},
81-
PowerShellParameter{
82-
name: "hidden",
83-
parameterType: "string",
84-
specifyDefaultValue: true,
85-
defaultValue: strconv.FormatBool(CheckTaskschedDefaultHidden),
86-
specifyValue: true,
87-
specifiedValue: strconv.FormatBool(l.Hidden),
88-
},
89-
)
40+
cmd, err := l.buildPowershellCmd(ctx)
9041
if err != nil {
9142
return fmt.Errorf("error when building a powershell command: %s", err.Error())
9243
}
@@ -147,6 +98,63 @@ func (l *CheckTasksched) addTasks(ctx context.Context, snc *Agent, check *CheckD
14798
return nil
14899
}
149100

101+
func (l *CheckTasksched) cleanupArguments() {
102+
// Add backslash to the beginning of the folder path if it does not exist
103+
if l.Folder != CheckTaskschedDefaultFolder {
104+
if !strings.HasPrefix(l.Folder, "\\") {
105+
l.Folder = "\\" + l.Folder
106+
}
107+
}
108+
109+
// Remove backslash at the end of the folder path, if it is not exactly root: "\"
110+
// "\Microsoft\" -> "\Microsoft"
111+
if l.Folder != CheckTaskschedDefaultFolder && l.Folder != "\\" {
112+
if cut, cutOk := strings.CutSuffix(l.Folder, "\\"); cutOk {
113+
l.Folder = cut
114+
}
115+
}
116+
}
117+
118+
func (l *CheckTasksched) buildPowershellCmd(ctx context.Context) (cmd *exec.Cmd, err error) {
119+
cmd, err = powerShellCmd(
120+
ctx, scheduledTasksPS1,
121+
PowerShellParameter{
122+
name: "title",
123+
parameterType: "string",
124+
specifyDefaultValue: true,
125+
defaultValue: CheckTaskschedDefaultTaskTitle,
126+
specifyValue: true,
127+
specifiedValue: l.TaskTitle,
128+
},
129+
PowerShellParameter{
130+
name: "folder",
131+
parameterType: "string",
132+
specifyDefaultValue: true,
133+
defaultValue: CheckTaskschedDefaultFolder,
134+
specifyValue: true,
135+
specifiedValue: l.Folder,
136+
},
137+
PowerShellParameter{
138+
name: "recursive",
139+
parameterType: "string",
140+
specifyDefaultValue: true,
141+
defaultValue: strconv.FormatBool(CheckTaskschedDefaultRecursive),
142+
specifyValue: true,
143+
specifiedValue: strconv.FormatBool(l.Recursive),
144+
},
145+
PowerShellParameter{
146+
name: "hidden",
147+
parameterType: "string",
148+
specifyDefaultValue: true,
149+
defaultValue: strconv.FormatBool(CheckTaskschedDefaultHidden),
150+
specifyValue: true,
151+
specifiedValue: strconv.FormatBool(l.Hidden),
152+
},
153+
)
154+
155+
return cmd, err
156+
}
157+
150158
func parseURIClean(uri string) string {
151159
if strings.Count(uri, "\\") == 1 {
152160
if cut, cutOk := strings.CutPrefix(uri, "\\"); cutOk {

pkg/snclient/condition.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ func (c *Condition) expandDateKeyword(str string) bool {
820820
return false
821821
}
822822

823+
//nolint:funlen // the function is long due to handling all unit types, but it is simple
823824
func (c *Condition) expandUnitByType(str string) error {
824825
// valid units might be "today", "thisweek", "thismonth", "thisyear" and ":utc" variants
825826
unit := c.getUnit(c.keyword)
@@ -841,6 +842,7 @@ func (c *Condition) expandUnitByType(str string) error {
841842
c.unit = ""
842843

843844
return nil
845+
default:
844846
}
845847

846848
match := reConditionValueUnit.FindStringSubmatch(str)
@@ -888,6 +890,8 @@ func (c *Condition) expandUnitByType(str string) error {
888890
return nil
889891
case UPercent:
890892
return nil
893+
case UBool:
894+
return nil // handled in the switch above
891895
case UNone:
892896
// best effort unit expansion
893897
return c.expandUnitByName(str)

pkg/utils/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ func ExpandDuration(val string) (res float64, err error) {
8181
return 0, fmt.Errorf("expandDuration: cannot parse duration, unknown format in %s", val)
8282
}
8383

84-
var truthyValues = []string{"1", "true", "enabled", "yes", "y", "on", "t"}
85-
var falseyValues = []string{"0", "false", "disabled", "no", "n", "off", "f"}
84+
var (
85+
truthyValues = []string{"1", "true", "enabled", "yes", "y", "on", "t"}
86+
falseyValues = []string{"0", "false", "disabled", "no", "n", "off", "f"}
87+
)
8688

8789
var (
8890
truthyRegex, truthyReplacements = replacePatterns(truthyValues, "true")

0 commit comments

Comments
 (0)