diff --git a/pkg/snclient/check_logfile.go b/pkg/snclient/check_logfile.go index 87355673..1792d8c3 100644 --- a/pkg/snclient/check_logfile.go +++ b/pkg/snclient/check_logfile.go @@ -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+`) diff --git a/pkg/snclient/config.go b/pkg/snclient/config.go index fa1f5225..f4f07665 100644 --- a/pkg/snclient/config.go +++ b/pkg/snclient/config.go @@ -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 @@ -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 @@ -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) }; " + diff --git a/pkg/snclient/config_test.go b/pkg/snclient/config_test.go index 1a103608..62d107a4 100644 --- a/pkg/snclient/config_test.go +++ b/pkg/snclient/config_test.go @@ -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) { diff --git a/pkg/snclient/listen_exporterexporter.go b/pkg/snclient/listen_exporterexporter.go index 6d3fd5cc..98be6a3b 100644 --- a/pkg/snclient/listen_exporterexporter.go +++ b/pkg/snclient/listen_exporterexporter.go @@ -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, diff --git a/pkg/snclient/listen_prometheus.go b/pkg/snclient/listen_prometheus.go index e361ff0b..a94519a4 100644 --- a/pkg/snclient/listen_prometheus.go +++ b/pkg/snclient/listen_prometheus.go @@ -17,7 +17,7 @@ func init() { ConfigInit{ ConfigData{ "port": "9999", - "use ssl": "0", + "use ssl": "true", }, "/settings/default", DefaultListenHTTPConfig, diff --git a/pkg/snclient/snclient.go b/pkg/snclient/snclient.go index 70809745..52cf6579 100644 --- a/pkg/snclient/snclient.go +++ b/pkg/snclient/snclient.go @@ -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) + // apply module defaults config.ApplyMergeDefaultsAll(moduleConfigDefaults) diff --git a/pkg/snclient/task_process_memory_watcher.go b/pkg/snclient/task_process_memory_watcher.go index ed1c7a38..f41ffb6b 100644 --- a/pkg/snclient/task_process_memory_watcher.go +++ b/pkg/snclient/task_process_memory_watcher.go @@ -20,8 +20,9 @@ func init() { NewProcessMemoryWatcherHandler, ConfigInit{ ConfigData{ - "check interval": "5s", - "memory limit": "512MiB", + "check interval": "5s", + "memory limit": "512MiB", + "ProcessMemoryWatcher": "enabled", }, }, )