| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nagios |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/logger" |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/ndexec" |
| 11 | outputpkg "github.com/netdata/netdata/go/plugins/plugin/scripts.d/collector/nagios/internal/output" |
| 12 | ) |
| 13 | |
| 14 | type checkRunner interface { |
| 15 | Run(ctx context.Context, req checkRunRequest) (checkRunResult, error) |
| 16 | } |
| 17 | |
| 18 | type checkRunRequest struct { |
| 19 | Job JobConfig |
| 20 | Vnode vnodeInfo |
| 21 | MacroState macroState |
| 22 | Now time.Time |
| 23 | Log *logger.Logger |
| 24 | } |
| 25 | |
| 26 | type checkRunResult struct { |
| 27 | ExitCode int |
| 28 | ServiceState string |
| 29 | JobState string |
| 30 | Parsed outputpkg.ParsedOutput |
| 31 | Duration time.Duration |
| 32 | Usage ndexec.ResourceUsage |
| 33 | } |
| 34 | |
| 35 | type systemCheckRunner struct{} |
| 36 | |
| 37 | func (systemCheckRunner) Run(ctx context.Context, req checkRunRequest) (checkRunResult, error) { |
| 38 | macros := buildMacroSet(req.Job, req.Vnode, req.MacroState, req.Now) |
| 39 | args := macros.CommandArgs |
| 40 | if len(args) == 0 { |
| 41 | args = req.Job.Args |
| 42 | } |
| 43 | |
| 44 | opts := ndexec.RunOptions{ |
| 45 | Env: buildRunEnv(req.Job.WorkingDirectory, req.Job.Environment, macros.Env), |
| 46 | Dir: req.Job.WorkingDirectory, |
| 47 | } |
| 48 | timeout := req.Job.Timeout.Duration() |
| 49 | |
| 50 | execCtx := ctx |
| 51 | cancel := func() {} |
| 52 | if timeout > 0 { |
| 53 | execCtx, cancel = context.WithTimeoutCause(ctx, timeout, errNagiosCheckTimeout) |
| 54 | } |
| 55 | defer cancel() |
| 56 | |
| 57 | startedAt := time.Now() |
| 58 | // Temporary branch-specific direct execution path: |
| 59 | // Nagios checks need job environment values and NAGIOS_* macros to reach the |
| 60 | // real child process, but that does not currently survive the nd-run helper |
| 61 | // boundary. The intended follow-up fix is to restore the helper path once |
| 62 | // nd-run can preserve explicitly forwarded vars while still scrubbing the |
| 63 | // ambient parent environment. |
| 64 | output, _, usage, err := ndexec.RunDirectWithOptionsUsageContext( |
| 65 | execCtx, |
| 66 | req.Log, |
| 67 | 0, |
| 68 | opts, |
| 69 | req.Job.Plugin, |
| 70 | args..., |
| 71 | ) |
| 72 | |
| 73 | result := checkRunResult{ |
| 74 | ExitCode: exitCodeFromError(err), |
| 75 | Duration: time.Since(startedAt), |
| 76 | Usage: usage, |
| 77 | } |
| 78 | result.ServiceState = serviceStateFromExecution(result.ExitCode, err) |
| 79 | result.JobState = jobStateFromExecution(result.ExitCode, err) |
| 80 | result.Parsed = outputpkg.Parse(output) |
| 81 | return result, err |
| 82 | } |
| 83 | |
| 84 | type macroState struct { |
| 85 | ServiceState string |
| 86 | ServiceAttempt int |
| 87 | ServiceMaxAttempts int |
| 88 | } |
| 89 | |
| 90 | type vnodeInfo struct { |
| 91 | Hostname string |
| 92 | Labels map[string]string |
| 93 | } |