Compare commits
3 commits
704bd87c71
...
a74c8d83aa
| Author | SHA1 | Date | |
|---|---|---|---|
| a74c8d83aa | |||
| 73f5782ee3 | |||
| 48785e2896 |
6 changed files with 176 additions and 55 deletions
|
|
@ -23,7 +23,6 @@ package checker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -53,48 +52,43 @@ type Metric struct {
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// EvaluateResult holds the evaluation outcome.
|
// EvaluateResult holds the evaluation outcome for a single target.
|
||||||
type EvaluateResult struct {
|
type EvaluateResult struct {
|
||||||
|
Address string `json:"address"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
StatusOK = 1
|
StatusUnknown = 0
|
||||||
StatusWarn = 3
|
StatusOK = 1
|
||||||
StatusCrit = 4
|
StatusWarn = 3
|
||||||
|
StatusCrit = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
// Evaluate checks the ping data against the given thresholds.
|
// Evaluate checks the ping data against the given thresholds and returns one
|
||||||
// StatusUnknown indicates the check could not be performed.
|
// result per target.
|
||||||
const StatusUnknown = 0
|
func Evaluate(data *PingData, warningRTT, criticalRTT, warningPacketLoss, criticalPacketLoss float64) []EvaluateResult {
|
||||||
|
|
||||||
func Evaluate(data *PingData, warningRTT, criticalRTT, warningPacketLoss, criticalPacketLoss float64) EvaluateResult {
|
|
||||||
if len(data.Targets) == 0 {
|
if len(data.Targets) == 0 {
|
||||||
return EvaluateResult{
|
return nil
|
||||||
Status: StatusUnknown,
|
|
||||||
Message: "No targets to ping",
|
|
||||||
Code: "ping_no_targets",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
overallStatus := StatusOK
|
results := make([]EvaluateResult, 0, len(data.Targets))
|
||||||
var summaryParts []string
|
|
||||||
|
|
||||||
for _, target := range data.Targets {
|
for _, target := range data.Targets {
|
||||||
|
status := StatusOK
|
||||||
if target.PacketLoss >= criticalPacketLoss || target.RTTAvg >= criticalRTT {
|
if target.PacketLoss >= criticalPacketLoss || target.RTTAvg >= criticalRTT {
|
||||||
overallStatus = StatusCrit
|
status = StatusCrit
|
||||||
} else if (target.PacketLoss >= warningPacketLoss || target.RTTAvg >= warningRTT) && overallStatus < StatusWarn {
|
} else if target.PacketLoss >= warningPacketLoss || target.RTTAvg >= warningRTT {
|
||||||
overallStatus = StatusWarn
|
status = StatusWarn
|
||||||
}
|
}
|
||||||
|
|
||||||
summaryParts = append(summaryParts, fmt.Sprintf("%s: %.1fms avg, %.0f%% loss", target.Address, target.RTTAvg, target.PacketLoss))
|
results = append(results, EvaluateResult{
|
||||||
}
|
Address: target.Address,
|
||||||
|
Status: status,
|
||||||
return EvaluateResult{
|
Message: fmt.Sprintf("%.1fms avg, %.0f%% loss", target.RTTAvg, target.PacketLoss),
|
||||||
Status: overallStatus,
|
Code: "ping_result",
|
||||||
Message: strings.Join(summaryParts, " | "),
|
})
|
||||||
Code: "ping_result",
|
|
||||||
}
|
}
|
||||||
|
return results
|
||||||
}
|
}
|
||||||
|
|
|
||||||
109
checker/interactive.go
Normal file
109
checker/interactive.go
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
// This file is part of the happyDomain (R) project.
|
||||||
|
// Copyright (c) 2020-2026 happyDomain
|
||||||
|
// Authors: Pierre-Olivier Mercier, et al.
|
||||||
|
//
|
||||||
|
// This program is offered under a commercial and under the AGPL license.
|
||||||
|
// For commercial licensing, contact us at <contact@happydomain.org>.
|
||||||
|
//
|
||||||
|
// For AGPL licensing:
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package checker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RenderForm implements sdk.CheckerInteractive. It exposes a minimal form
|
||||||
|
// letting a human submit one or more ping targets (hostnames or IPs) along
|
||||||
|
// with the usual threshold knobs.
|
||||||
|
func (p *pingProvider) RenderForm() []sdk.CheckerOptionField {
|
||||||
|
return []sdk.CheckerOptionField{
|
||||||
|
{
|
||||||
|
Id: "addresses",
|
||||||
|
Type: "string",
|
||||||
|
Label: "Targets",
|
||||||
|
Placeholder: "example.com, 192.0.2.1",
|
||||||
|
Description: "Comma- or newline-separated list of hostnames or IP addresses.",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "count",
|
||||||
|
Type: "uint",
|
||||||
|
Label: "Number of pings to send",
|
||||||
|
Default: float64(5),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "warningRTT",
|
||||||
|
Type: "number",
|
||||||
|
Label: "Warning RTT threshold (ms)",
|
||||||
|
Default: float64(100),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "criticalRTT",
|
||||||
|
Type: "number",
|
||||||
|
Label: "Critical RTT threshold (ms)",
|
||||||
|
Default: float64(500),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "warningPacketLoss",
|
||||||
|
Type: "number",
|
||||||
|
Label: "Warning packet loss threshold (%)",
|
||||||
|
Default: float64(10),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "criticalPacketLoss",
|
||||||
|
Type: "number",
|
||||||
|
Label: "Critical packet loss threshold (%)",
|
||||||
|
Default: float64(50),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseForm implements sdk.CheckerInteractive. It converts the HTML form
|
||||||
|
// inputs into a CheckerOptions that Collect can consume directly — pinging
|
||||||
|
// resolves hostnames on its own, so no extra lookups are needed here.
|
||||||
|
func (p *pingProvider) ParseForm(r *http.Request) (sdk.CheckerOptions, error) {
|
||||||
|
raw := strings.TrimSpace(r.FormValue("addresses"))
|
||||||
|
if raw == "" {
|
||||||
|
return nil, errors.New("at least one target is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
var addresses []string
|
||||||
|
for _, part := range strings.FieldsFunc(raw, func(c rune) bool {
|
||||||
|
return c == ',' || c == '\n' || c == '\r' || c == ' ' || c == '\t' || c == ';'
|
||||||
|
}) {
|
||||||
|
if part = strings.TrimSpace(part); part != "" {
|
||||||
|
addresses = append(addresses, part)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(addresses) == 0 {
|
||||||
|
return nil, errors.New("at least one target is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := sdk.CheckerOptions{"addresses": addresses}
|
||||||
|
for _, k := range []string{"count", "warningRTT", "criticalRTT", "warningPacketLoss", "criticalPacketLoss"} {
|
||||||
|
if v := strings.TrimSpace(r.FormValue(k)); v != "" {
|
||||||
|
if n, err := strconv.ParseFloat(v, 64); err == nil {
|
||||||
|
opts[k] = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return opts, nil
|
||||||
|
}
|
||||||
|
|
@ -53,9 +53,9 @@ func (p *pingProvider) Definition() *happydns.CheckerDefinition {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractMetrics implements happydns.CheckerMetricsReporter.
|
// ExtractMetrics implements happydns.CheckerMetricsReporter.
|
||||||
func (p *pingProvider) ExtractMetrics(raw json.RawMessage, collectedAt time.Time) ([]happydns.CheckMetric, error) {
|
func (p *pingProvider) ExtractMetrics(ctx happydns.ReportContext, collectedAt time.Time) ([]happydns.CheckMetric, error) {
|
||||||
var data PingData
|
var data PingData
|
||||||
if err := json.Unmarshal(raw, &data); err != nil {
|
if err := json.Unmarshal(ctx.Data(), &data); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -106,14 +106,14 @@ func (r *pingRule) ValidateOptions(opts sdk.CheckerOptions) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *pingRule) Evaluate(ctx context.Context, obs sdk.ObservationGetter, opts sdk.CheckerOptions) sdk.CheckState {
|
func (r *pingRule) Evaluate(ctx context.Context, obs sdk.ObservationGetter, opts sdk.CheckerOptions) []sdk.CheckState {
|
||||||
var data PingData
|
var data PingData
|
||||||
if err := obs.Get(ctx, ObservationKeyPing, &data); err != nil {
|
if err := obs.Get(ctx, ObservationKeyPing, &data); err != nil {
|
||||||
return sdk.CheckState{
|
return []sdk.CheckState{{
|
||||||
Status: sdk.StatusError,
|
Status: sdk.StatusError,
|
||||||
Message: fmt.Sprintf("Failed to get ping data: %v", err),
|
Message: fmt.Sprintf("Failed to get ping data: %v", err),
|
||||||
Code: "ping_error",
|
Code: "ping_error",
|
||||||
}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
warningRTT := sdk.GetFloatOption(opts, "warningRTT", 100)
|
warningRTT := sdk.GetFloatOption(opts, "warningRTT", 100)
|
||||||
|
|
@ -121,26 +121,44 @@ func (r *pingRule) Evaluate(ctx context.Context, obs sdk.ObservationGetter, opts
|
||||||
warningPacketLoss := sdk.GetFloatOption(opts, "warningPacketLoss", 10)
|
warningPacketLoss := sdk.GetFloatOption(opts, "warningPacketLoss", 10)
|
||||||
criticalPacketLoss := sdk.GetFloatOption(opts, "criticalPacketLoss", 50)
|
criticalPacketLoss := sdk.GetFloatOption(opts, "criticalPacketLoss", 50)
|
||||||
|
|
||||||
result := Evaluate(&data, warningRTT, criticalRTT, warningPacketLoss, criticalPacketLoss)
|
results := Evaluate(&data, warningRTT, criticalRTT, warningPacketLoss, criticalPacketLoss)
|
||||||
|
if len(results) == 0 {
|
||||||
var status sdk.Status
|
return []sdk.CheckState{{
|
||||||
switch result.Status {
|
Status: sdk.StatusInfo,
|
||||||
case StatusOK:
|
Message: "No targets to ping",
|
||||||
status = sdk.StatusOK
|
Code: "ping_no_targets",
|
||||||
case StatusWarn:
|
}}
|
||||||
status = sdk.StatusWarn
|
|
||||||
case StatusCrit:
|
|
||||||
status = sdk.StatusCrit
|
|
||||||
default:
|
|
||||||
status = sdk.StatusUnknown
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sdk.CheckState{
|
targetByAddr := make(map[string]PingTargetResult, len(data.Targets))
|
||||||
Status: status,
|
for _, t := range data.Targets {
|
||||||
Message: result.Message,
|
targetByAddr[t.Address] = t
|
||||||
Code: result.Code,
|
|
||||||
Meta: map[string]any{
|
|
||||||
"targets": data.Targets,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out := make([]sdk.CheckState, 0, len(results))
|
||||||
|
for _, r := range results {
|
||||||
|
var status sdk.Status
|
||||||
|
switch r.Status {
|
||||||
|
case StatusOK:
|
||||||
|
status = sdk.StatusOK
|
||||||
|
case StatusWarn:
|
||||||
|
status = sdk.StatusWarn
|
||||||
|
case StatusCrit:
|
||||||
|
status = sdk.StatusCrit
|
||||||
|
default:
|
||||||
|
status = sdk.StatusUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
state := sdk.CheckState{
|
||||||
|
Status: status,
|
||||||
|
Subject: r.Address,
|
||||||
|
Message: r.Message,
|
||||||
|
Code: r.Code,
|
||||||
|
}
|
||||||
|
if t, ok := targetByAddr[r.Address]; ok {
|
||||||
|
state.Meta = map[string]any{"target": t}
|
||||||
|
}
|
||||||
|
out = append(out, state)
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -3,12 +3,12 @@ module git.happydns.org/checker-ping
|
||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.happydns.org/checker-sdk-go v1.2.0
|
||||||
git.happydns.org/happyDomain v0.7.0
|
git.happydns.org/happyDomain v0.7.0
|
||||||
github.com/prometheus-community/pro-bing v0.8.0
|
github.com/prometheus-community/pro-bing v0.8.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.happydns.org/checker-sdk-go v0.0.1
|
|
||||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||||
github.com/bytedance/sonic v1.15.0 // indirect
|
github.com/bytedance/sonic v1.15.0 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||||
|
|
|
||||||
4
go.sum
4
go.sum
|
|
@ -1,5 +1,5 @@
|
||||||
git.happydns.org/checker-sdk-go v0.0.1 h1:4RxCJr73HWKxjOyU/6NJMO8lXJmH0gMLA68EzTqLbQI=
|
git.happydns.org/checker-sdk-go v1.2.0 h1:v4MpKAz0W3PwP+bxx3pya8w893sVH5xTD1of1cc0TV8=
|
||||||
git.happydns.org/checker-sdk-go v0.0.1/go.mod h1:aNAcfYFfbhvH9kJhE0Njp5GX0dQbxdRB0rJ0KvSC5nI=
|
git.happydns.org/checker-sdk-go v1.2.0/go.mod h1:aNAcfYFfbhvH9kJhE0Njp5GX0dQbxdRB0rJ0KvSC5nI=
|
||||||
git.happydns.org/happyDomain v0.7.0 h1:NV82/NbcSeRm0+IUZqaK3Vu9Ovl5+vv4AigUJZMdwws=
|
git.happydns.org/happyDomain v0.7.0 h1:NV82/NbcSeRm0+IUZqaK3Vu9Ovl5+vv4AigUJZMdwws=
|
||||||
git.happydns.org/happyDomain v0.7.0/go.mod h1:5tgkmqFE65kK359rY49V++49wgZ0gco+Gh9X6tbL+bY=
|
git.happydns.org/happyDomain v0.7.0/go.mod h1:5tgkmqFE65kK359rY49V++49wgZ0gco+Gh9X6tbL+bY=
|
||||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue