diff --git a/services/runner/monitor/http.go b/services/runner/monitor/http.go index 83c8914..6f2292e 100644 --- a/services/runner/monitor/http.go +++ b/services/runner/monitor/http.go @@ -12,7 +12,7 @@ import ( "time" ) -func (m *Monitor) runHTTP() (*types.Result, map[string]any) { +func (m *Monitor) runHTTP() *types.Result { r := types.NewResult(m.Name, m.Target, m.ID) var err error @@ -30,7 +30,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { if timeoutProp != "" { timeout, err = time.ParseDuration(timeoutProp) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } } @@ -38,13 +38,13 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { if validateTLSProp != "" { validateTLS, err = strconv.ParseBool(validateTLSProp) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } } req, err := http.NewRequest(method, m.Target, nil) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } if m.Properties["body"] != "" { @@ -56,7 +56,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { err = json.Unmarshal([]byte(m.Properties["headers"]), &headers) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } for k, v := range headers { @@ -74,7 +74,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { resp, err := client.Do(req) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } defer resp.Body.Close() @@ -83,7 +83,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { // Read response body body, err := io.ReadAll(resp.Body) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } bodyStr := string(body) @@ -92,7 +92,7 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { if m.Properties["bodyRegex"] != "" { re, err := regexp.Compile(m.Properties["bodyRegex"]) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } match := re.FindStringSubmatch(bodyStr) @@ -126,9 +126,8 @@ func (m *Monitor) runHTTP() (*types.Result, map[string]any) { } } - // Save all outputs to the result but omit the body + // Save all outputs r.Outputs = outputs - r.Outputs["body"] = "" - return r, outputs + return r } diff --git a/services/runner/monitor/monitor.go b/services/runner/monitor/monitor.go index 6ebc6e8..6982b00 100644 --- a/services/runner/monitor/monitor.go +++ b/services/runner/monitor/monitor.go @@ -91,19 +91,17 @@ func (m *Monitor) run() (bool, *types.Result) { var result *types.Result - var outputs map[string]interface{} - log.Printf("### Running monitor '%s' at '%s'", m.Name, m.Target) switch m.Type { case typeHTTP: - result, outputs = m.runHTTP() + result = m.runHTTP() case typePing: - result, outputs = m.runPing() + result = m.runPing() case typeTCP: - result, outputs = m.runTCP() + result = m.runTCP() default: log.Printf("### Unknown monitor type '%s', will be skipped", m.Type) @@ -111,10 +109,10 @@ func (m *Monitor) run() (bool, *types.Result) { } if os.Getenv("DEBUG") == "true" { - log.Printf("### DEBUG '%s' outputs: %+v", m.Name, outputs) + log.Printf("### DEBUG '%s' outputs: %+v", m.Name, result.Outputs) } - if m.Rule != "" && outputs != nil { + if m.Rule != "" && result.Outputs != nil { //log.Printf("### Running rule '%s' for monitor '%s'", m.Rule, m.Name) ruleExp, err := govaluate.NewEvaluableExpression(m.Rule) if err != nil { @@ -124,7 +122,7 @@ func (m *Monitor) run() (bool, *types.Result) { return false, result } - res, err := ruleExp.Evaluate(outputs) + res, err := ruleExp.Evaluate(result.Outputs) if err != nil { result = types.NewFailedResult(m.Name, m.Target, m.ID, fmt.Errorf("rule eval error: "+err.Error())) _ = storeResult(m, *result) @@ -146,6 +144,12 @@ func (m *Monitor) run() (bool, *types.Result) { } } + // Remove the body from the outputs after rules are checked + // TODO: Horrible leakiness from HTTP monitor here, should be fixed + if result.Outputs["body"] != nil { + result.Outputs["body"] = "*** Removed ***" + } + err := storeResult(m, *result) if err != nil { log.Printf("### Error storing result: %s", err.Error()) diff --git a/services/runner/monitor/monitor_test.go b/services/runner/monitor/monitor_test.go index 724a2e4..377c844 100644 --- a/services/runner/monitor/monitor_test.go +++ b/services/runner/monitor/monitor_test.go @@ -1,15 +1,13 @@ package monitor import ( - "io" - "log" "testing" "time" ) func init() { // Comment out this line to see debug output - log.SetOutput(io.Discard) + //log.SetOutput(io.Discard) } func TestMonitorDisabledStart(t *testing.T) { diff --git a/services/runner/monitor/ping.go b/services/runner/monitor/ping.go index d65b642..581e590 100644 --- a/services/runner/monitor/ping.go +++ b/services/runner/monitor/ping.go @@ -8,7 +8,7 @@ import ( ping "github.com/prometheus-community/pro-bing" ) -func (m *Monitor) runPing() (*types.Result, map[string]any) { +func (m *Monitor) runPing() *types.Result { r := types.NewResult(m.Name, m.Target, m.ID) var err error @@ -21,7 +21,7 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) { if countProp != "" { count, err = strconv.Atoi(countProp) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } } @@ -29,7 +29,7 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) { if intervalProp != "" { interval, err = time.ParseDuration(intervalProp) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } } @@ -37,13 +37,13 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) { if timeoutProp != "" { timeout, err = time.ParseDuration(timeoutProp) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } } pinger, err := ping.NewPinger(m.Target) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } pinger.SetPrivileged(true) @@ -53,7 +53,7 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) { err = pinger.Run() // NOTE: Blocks if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } stats := pinger.Statistics() @@ -69,5 +69,5 @@ func (m *Monitor) runPing() (*types.Result, map[string]any) { r.Value = int(stats.AvgRtt.Milliseconds()) r.Outputs = outputs - return r, outputs + return r } diff --git a/services/runner/monitor/tcp.go b/services/runner/monitor/tcp.go index b4f2d4e..cb613a9 100644 --- a/services/runner/monitor/tcp.go +++ b/services/runner/monitor/tcp.go @@ -7,7 +7,7 @@ import ( "time" ) -func (m *Monitor) runTCP() (*types.Result, map[string]any) { +func (m *Monitor) runTCP() *types.Result { r := types.NewResult(m.Name, m.Target, m.ID) var err error @@ -18,7 +18,7 @@ func (m *Monitor) runTCP() (*types.Result, map[string]any) { if timeoutProp != "" { timeout, err = time.ParseDuration(timeoutProp) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } } @@ -27,7 +27,7 @@ func (m *Monitor) runTCP() (*types.Result, map[string]any) { conn, err := dialer.Dial("tcp", m.Target) if err != nil { - return types.NewFailedResult(m.Name, m.Target, m.ID, err), nil + return types.NewFailedResult(m.Name, m.Target, m.ID, err) } r.Value = int(time.Since(start).Milliseconds()) @@ -41,5 +41,5 @@ func (m *Monitor) runTCP() (*types.Result, map[string]any) { r.Outputs = outputs - return r, outputs + return r }