Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/snclient/check_logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
defaultMaxLinesPerFile = 10000

// maxLinesPerFileMax sets the upper limit a user can request by args (can be increased from the config)
maxLinesPerFileMax = 100000
maxLinesPerFileMax = 1000000
)

var numReg = regexp.MustCompile(`\d+`)
Expand Down
67 changes: 49 additions & 18 deletions pkg/snclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,32 @@ const (

var DefaultConfig = map[string]ConfigData{
"/modules": {
"Logrotate": "enabled",
"ProcessMemoryWatcher": "enabled",
"CheckSystem": "enabled",
"CheckSystemUnix": "enabled",
"CheckAlias": "enabled",
"CheckMulti": "enabled",
"CheckExternalScripts": "enabled",
"CheckDisk": "enabled",
"CheckDriveIO": "enabled",
"CheckSwapIO": "enabled",
"CheckWMI": "disabled",
"CheckLogFile": "disabled",
"NRPEServer": "disabled",
"WEBServer": "enabled",
"PrometheusServer": "disabled",
"Updates": "enabled",
"Logrotate": "enabled",
"ProcessMemoryWatcher": "enabled",
"CheckSystem": "enabled",
"CheckSystemUnix": "enabled",
"CheckAlias": "enabled",
"CheckMulti": "enabled",
"CheckExternalScripts": "disabled",
"CheckDisk": "enabled",
"CheckDriveIO": "enabled",
"CheckSwapIO": "enabled",
"CheckWMI": "disabled",
"CheckLogFile": "disabled",
"CheckBuiltinPlugins": "disabled",
"NRPEServer": "disabled",
"WEBServer": "enabled",
"WEBAdminServer": "disabled",
"PrometheusServer": "disabled",
"NodeExporterServer": "disabled",
"WindowsExporterServer": "disabled",
"ExporterExporterServer": "disabled",
"ManagedExporterServer": "disabled",
"Updates": "enabled",
},
"/settings/default": {
"nasty characters": DefaultNastyCharacters,
"nasty characters": DefaultNastyCharacters,
"allow control characters": "false",
},
"/settings/wmi": {
// number of WMI queries that may run in parallel; <= 1 uses the default client
Expand All @@ -74,6 +81,30 @@ var DefaultConfig = map[string]ConfigData{
"stable": "https://api.github.com/repos/ConSol-monitoring/snclient/releases",
"dev": "https://api.github.com/repos/ConSol-monitoring/snclient/actions/artifacts",
},
"/settings/check/files": {
"max files limit": fmt.Sprintf("%d", CheckFilesMaxFilesLimit),
},
"/settings/check/logfile": {
"max lines per file limit": fmt.Sprintf("%d", maxLinesPerFileMax),
},
"/settings/check/multi": {
"max checks": fmt.Sprintf("%d", defaultMaxChecks),
},
"/settings/builtin plugins/default": {
"disabled": "false",
},
"/settings/external scripts/alias/default": {
"ignore perfdata": "false",
"command": "",
},
"/settings/external scripts/scripts/default": {
"ignore perfdata": "false",
"command": "",
},
"/settings/log": {
"level": "info",
},
"/settings/system/default": {},
"/settings/external scripts": {
"timeout": "60",
"script root": "${scripts}", // root path of all scripts
Expand All @@ -84,7 +115,7 @@ var DefaultConfig = map[string]ConfigData{
"ignore perfdata": "false",
},
"/settings/external scripts/wrappings": {
"bat": `${scripts}\%SCRIPT% %ARGS%`,
"bat": `${script root}\%SCRIPT% %ARGS%`,
"ps1": `cmd /c echo ` +
`If (-Not (Test-Path "${script root}\%SCRIPT%") ) ` +
`{ Write-Host "UNKNOWN: Script ` + "`\"%SCRIPT%`\" not found.\"; exit(3) }; " +
Expand Down
95 changes: 87 additions & 8 deletions pkg/snclient/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,100 @@ import (
"testing"
"time"

"github.com/consol-monitoring/snclient/pkg/convert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigPkgDefaults(t *testing.T) {
cfg := NewConfig(true)
err := cfg.ParseINIFile("../../packaging/snclient.ini", nil)
cfg := NewConfig(false)
require.NoError(t, cfg.ParseINIFile("../../packaging/snclient.ini", nil))

// verify default nasty characters
nastyChars, _ := cfg.Section("/settings/WEB/server").GetString("nasty characters")
assert.Equalf(t, DefaultNastyCharacters, nastyChars, "default nasty characters")
assert.Containsf(t, nastyChars, "\\", "nasty characters contains backslash")
assert.Containsf(t, nastyChars, ";", "nasty characters contains semicolon")
defaults := NewConfig(false)
for name, data := range DefaultConfig {
defaults.Section(name).MergeData(data)
}
defaults.Section("/settings/default").MergeData(DefaultListenHTTPConfig)
defaults.Section("/settings/default").MergeData(DefaultHTTPClientConfig)
defaults.Section("/settings/system/default").MergeData(DefaultSystemTaskConfig)
defaults.ApplyMergeDefaultsAll(moduleConfigDefaults)

// Resolve shared path macros equally without testing platform-specific installation paths.
defaults.Section("/paths").MergeData(cfg.Section("/paths").data)

exceptions := map[string]string{
"/paths/exe-path": "Detected from the executable.",
"/paths/shared-path": "Depends on the installation platform.",
"/paths/scripts": "Depends on the installation platform.",
"/paths/certificate-path": "Depends on the installation platform.",
"/paths/cache-path": "Uses the environment or system temporary folder.",
"/settings/log/file name": "The package enables file logging.",
"/settings/check/logfile/allowed pattern": "The package permits specific log paths.",
"/includes/local": "The package loads local configuration files.",
}
for _, name := range cfg.SectionNamesSorted() {
section := cfg.Section(name)
keys := append([]string(nil), section.Keys()...)
for _, key := range defaults.Section(name).Keys() {
if section.HasKey(key) {
continue
}
if _, ok := section.GetString(key); ok {
keys = append(keys, key)
}
}
for _, key := range keys {
t.Run(name+"/"+key, func(t *testing.T) {
if reason, ok := exceptions[name+"/"+key]; ok {
t.Log(reason)

return
}
want := configPkgDefaultValue(t, section, key)
candidate := defaults.Section(name)
found := candidate.HasKey(key)
if key == "password" {
found = candidate.HasKey(key)
}
require.True(t, found, "[%s] %s has no code default or path exception", name, key)
actual := configPkgDefaultValue(t, candidate, key)
assert.Equal(t, want, actual, "[%s] %s: code default differs from packaging/snclient.ini", name, key)
})
}
}
}

require.NoErrorf(t, err, "config parsed")
func configPkgDefaultValue(t *testing.T, section *ConfigSection, key string) string {
t.Helper()

value, _ := section.GetString(key)
boolean := section.name == "/modules"
switch key {
case "password":
return section.data[key]
case "allowed hosts":
hosts, _ := section.GetStringList(key)
for index, host := range hosts {
hosts[index] = strings.Trim(host, "[]")
}

return strings.Join(hosts, ",")
case "bat":
return strings.ReplaceAll(value, `\\`, `\`)
case "cache allowed hosts", "use ssl", "require password", "use hsts header",
"insecure", "allow arguments", "allow nasty characters", "allow control characters",
"ignore perfdata", "disabled", "modules dir watcher", "kill orphaned",
"automatic updates", "automatic restart", "pre release", "ProcessMemoryWatcher":
boolean = true
}
if boolean {
boolean, err := convert.BoolE(value)
require.NoError(t, err)

return fmt.Sprintf("%t", boolean)
}

return value
}

func TestConfigBasic(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/snclient/listen_exporterexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ func init() {
NewHandlerExporterExporter,
ConfigInit{
ConfigData{
"port": "${/settings/WEB/server/port}",
"use ssl": "${/settings/WEB/server/use ssl}",
"url prefix": "/",
"port": "${/settings/WEB/server/port}",
"use ssl": "${/settings/WEB/server/use ssl}",
"url prefix": "/",
"modules dir": "${shared-path}/exporter_modules",
"modules dir watcher": "false",
"default module": "",
"allowed methods": "",
},
"/settings/default",
DefaultListenHTTPConfig,
Expand Down
2 changes: 1 addition & 1 deletion pkg/snclient/listen_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func init() {
ConfigInit{
ConfigData{
"port": "9999",
"use ssl": "0",
"use ssl": "true",
},
"/settings/default",
DefaultListenHTTPConfig,
Expand Down
4 changes: 4 additions & 0 deletions pkg/snclient/snclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ func (snc *Agent) ReadConfiguration(files []string) (initSet *AgentRunSet, err e
section.MergeData(defaults)
}

config.Section("/settings/default").MergeData(DefaultListenHTTPConfig)
config.Section("/settings/default").MergeData(DefaultHTTPClientConfig)
config.Section("/settings/system/default").MergeData(DefaultSystemTaskConfig)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we have the same 3 lines in test and app code and must never forget to keep them in sync. Also not cool :-)

// apply module defaults
config.ApplyMergeDefaultsAll(moduleConfigDefaults)

Expand Down
5 changes: 3 additions & 2 deletions pkg/snclient/task_process_memory_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func init() {
NewProcessMemoryWatcherHandler,
ConfigInit{
ConfigData{
"check interval": "5s",
"memory limit": "512MiB",
"check interval": "5s",
"memory limit": "512MiB",
"ProcessMemoryWatcher": "enabled",
},
},
)
Expand Down
Loading