68 lines
2.6 KiB
Go
68 lines
2.6 KiB
Go
// 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 (
|
|
"context"
|
|
"math/rand/v2"
|
|
"time"
|
|
|
|
"git.happydns.org/happyDomain/model"
|
|
sdk "git.happydns.org/happyDomain/sdk/checker"
|
|
)
|
|
|
|
// Collect gathers observation data. This is called by happyDomain (or the
|
|
// /collect HTTP endpoint) every time a check runs.
|
|
//
|
|
// In a real checker, this is where you would perform the actual monitoring
|
|
// work: sending network requests, querying APIs, measuring latency, etc.
|
|
//
|
|
// This dummy implementation simply reads options and generates a random score
|
|
// so you can focus on the structure rather than external dependencies.
|
|
//
|
|
// Parameters:
|
|
// - ctx: a context for cancellation/timeout — always honour it.
|
|
// - opts: the merged checker options (admin + user + domain + service + run).
|
|
//
|
|
// Return:
|
|
// - any: the observation data (will be JSON-serialised by the SDK).
|
|
// - error: non-nil if collection failed entirely.
|
|
func (p *dummyProvider) Collect(ctx context.Context, opts happydns.CheckerOptions) (any, error) {
|
|
// Read user-configurable options using the SDK helpers.
|
|
// These helpers handle type coercion gracefully — the value may come as
|
|
// a native Go type (in-process plugin) or as a JSON-decoded float64/string
|
|
// (external HTTP mode). The helpers normalise both cases.
|
|
message := "Hello from the dummy checker!"
|
|
if v, ok := sdk.GetOption[string](opts, "message"); ok && v != "" {
|
|
message = v
|
|
}
|
|
|
|
// Generate a random score between 0 and 100 to simulate a measurement.
|
|
// In your real checker, replace this with actual monitoring logic.
|
|
score := rand.Float64() * 100
|
|
|
|
return &DummyData{
|
|
Message: message,
|
|
Score: score,
|
|
CollectedAt: time.Now(),
|
|
}, nil
|
|
}
|