| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nagios |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "maps" |
| 8 | "path/filepath" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/scripts.d/pkg/timeperiod" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | defaultCheckInterval = 5 * time.Minute |
| 17 | defaultRetryInterval = 1 * time.Minute |
| 18 | defaultTimeout = 5 * time.Second |
| 19 | defaultMaxCheckAttempts = 3 |
| 20 | maxArgMacros = 32 |
| 21 | ) |
| 22 | |
| 23 | // JobConfig is the user-facing Nagios job configuration surface. |
| 24 | type JobConfig struct { |
| 25 | Name string `yaml:"name" json:"name"` |
| 26 | CheckName string `yaml:"check_name,omitempty" json:"check_name"` |
| 27 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 28 | Plugin string `yaml:"plugin" json:"plugin"` |
| 29 | Args []string `yaml:"args,omitempty" json:"args"` |
| 30 | ArgValues []string `yaml:"arg_values,omitempty" json:"arg_values"` |
| 31 | Environment map[string]string `yaml:"environment,omitempty" json:"environment"` |
| 32 | Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"` |
| 33 | CheckInterval confopt.Duration `yaml:"check_interval,omitempty" json:"check_interval"` |
| 34 | RetryInterval confopt.Duration `yaml:"retry_interval,omitempty" json:"retry_interval"` |
| 35 | MaxCheckAttempts int `yaml:"max_check_attempts,omitempty" json:"max_check_attempts"` |
| 36 | WorkingDirectory string `yaml:"working_directory,omitempty" json:"working_directory"` |
| 37 | CustomVars map[string]string `yaml:"custom_vars,omitempty" json:"custom_vars"` |
| 38 | CheckPeriod string `yaml:"check_period,omitempty" json:"check_period"` |
| 39 | } |
| 40 | |
| 41 | func defaultedJobConfig(cfg JobConfig) JobConfig { |
| 42 | if cfg.Timeout == 0 { |
| 43 | cfg.Timeout = confopt.Duration(defaultTimeout) |
| 44 | } |
| 45 | if cfg.CheckInterval == 0 { |
| 46 | cfg.CheckInterval = confopt.Duration(defaultCheckInterval) |
| 47 | } |
| 48 | if cfg.RetryInterval == 0 { |
| 49 | cfg.RetryInterval = confopt.Duration(defaultRetryInterval) |
| 50 | } |
| 51 | if cfg.MaxCheckAttempts == 0 { |
| 52 | cfg.MaxCheckAttempts = defaultMaxCheckAttempts |
| 53 | } |
| 54 | if cfg.Environment == nil { |
| 55 | cfg.Environment = make(map[string]string) |
| 56 | } |
| 57 | if cfg.CustomVars == nil { |
| 58 | cfg.CustomVars = make(map[string]string) |
| 59 | } |
| 60 | if cfg.CheckPeriod == "" { |
| 61 | cfg.CheckPeriod = timeperiod.DefaultPeriodName |
| 62 | } |
| 63 | return cfg |
| 64 | } |
| 65 | |
| 66 | func (cfg *JobConfig) setDefaults() { |
| 67 | *cfg = defaultedJobConfig(*cfg) |
| 68 | } |
| 69 | |
| 70 | func (cfg JobConfig) validate() error { |
| 71 | if cfg.Name == "" { |
| 72 | return fmt.Errorf("job name is required") |
| 73 | } |
| 74 | if cfg.Plugin == "" { |
| 75 | return fmt.Errorf("job '%s': plugin path is required", cfg.Name) |
| 76 | } |
| 77 | if !filepath.IsAbs(cfg.Plugin) { |
| 78 | return fmt.Errorf("job '%s': plugin path must be absolute", cfg.Name) |
| 79 | } |
| 80 | if len(cfg.ArgValues) > maxArgMacros { |
| 81 | return fmt.Errorf("job '%s': arg_values supports up to %d entries", cfg.Name, maxArgMacros) |
| 82 | } |
| 83 | if cfg.CheckInterval <= 0 { |
| 84 | return fmt.Errorf("job '%s': check_interval must be > 0", cfg.Name) |
| 85 | } |
| 86 | if cfg.RetryInterval <= 0 { |
| 87 | return fmt.Errorf("job '%s': retry_interval must be > 0", cfg.Name) |
| 88 | } |
| 89 | if cfg.Timeout <= 0 { |
| 90 | return fmt.Errorf("job '%s': timeout must be > 0", cfg.Name) |
| 91 | } |
| 92 | if cfg.MaxCheckAttempts < 1 { |
| 93 | return fmt.Errorf("job '%s': max_check_attempts must be >= 1", cfg.Name) |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (cfg JobConfig) normalized() (JobConfig, error) { |
| 99 | cfg.setDefaults() |
| 100 | if err := cfg.validate(); err != nil { |
| 101 | return JobConfig{}, err |
| 102 | } |
| 103 | cfg.CheckName = normalizedCheckName(cfg.CheckName, cfg.Plugin) |
| 104 | |
| 105 | cfg.Args = append([]string{}, cfg.Args...) |
| 106 | cfg.ArgValues = append([]string{}, cfg.ArgValues...) |
| 107 | cfg.Environment = maps.Clone(cfg.Environment) |
| 108 | cfg.CustomVars = maps.Clone(cfg.CustomVars) |
| 109 | return cfg, nil |
| 110 | } |