| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nagios |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/scripts.d/pkg/timeperiod" |
| 11 | ) |
| 12 | |
| 13 | const defaultCollectorUpdateEvery = 10 |
| 14 | |
| 15 | type compiledJob struct { |
| 16 | config JobConfig |
| 17 | period *timeperiod.Period |
| 18 | cadenceWarning string |
| 19 | } |
| 20 | |
| 21 | func (j compiledJob) configured() bool { |
| 22 | return j.config.Plugin != "" |
| 23 | } |
| 24 | |
| 25 | func compileCollectorConfig(cfg Config) (compiledJob, error) { |
| 26 | job, err := cfg.JobConfig.normalized() |
| 27 | if err != nil { |
| 28 | return compiledJob{}, err |
| 29 | } |
| 30 | |
| 31 | periodCfgs := timeperiod.EnsureDefault(append([]timeperiod.Config(nil), cfg.TimePeriods...)) |
| 32 | periodSet, err := timeperiod.Compile(periodCfgs) |
| 33 | if err != nil { |
| 34 | return compiledJob{}, err |
| 35 | } |
| 36 | |
| 37 | period, err := periodSet.Resolve(job.CheckPeriod) |
| 38 | if err != nil { |
| 39 | return compiledJob{}, err |
| 40 | } |
| 41 | |
| 42 | updateEvery := resolveUpdateEvery(cfg.UpdateEvery) |
| 43 | |
| 44 | return compiledJob{ |
| 45 | config: job, |
| 46 | period: period, |
| 47 | cadenceWarning: cadenceResolutionWarning(job.Name, updateEvery, job.CheckInterval.Duration(), job.RetryInterval.Duration()), |
| 48 | }, nil |
| 49 | } |
| 50 | |
| 51 | func resolveUpdateEvery(seconds int) time.Duration { |
| 52 | if seconds <= 0 { |
| 53 | seconds = defaultCollectorUpdateEvery |
| 54 | } |
| 55 | return time.Duration(seconds) * time.Second |
| 56 | } |
| 57 | |
| 58 | func cadenceResolutionWarning(jobName string, updateEvery, checkInterval, retryInterval time.Duration) string { |
| 59 | if updateEvery <= 0 { |
| 60 | return "" |
| 61 | } |
| 62 | var requested []string |
| 63 | if checkInterval > 0 && updateEvery > checkInterval { |
| 64 | requested = append(requested, fmt.Sprintf("check_interval=%s", checkInterval)) |
| 65 | } |
| 66 | if retryInterval > 0 && updateEvery > retryInterval { |
| 67 | requested = append(requested, fmt.Sprintf("retry_interval=%s", retryInterval)) |
| 68 | } |
| 69 | if len(requested) == 0 { |
| 70 | return "" |
| 71 | } |
| 72 | return fmt.Sprintf( |
| 73 | "job '%s': update_every (%s) is slower than requested cadence (%s); checks and retries execute on collector ticks, so the effective cadence is limited by update_every", |
| 74 | jobName, |
| 75 | updateEvery, |
| 76 | strings.Join(requested, ", "), |
| 77 | ) |
| 78 | } |