Initial commit
This commit is contained in:
commit
7ca2fb60c6
24 changed files with 3098 additions and 0 deletions
120
checker/evaluate.go
Normal file
120
checker/evaluate.go
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
package checker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
sdk "git.happydns.org/checker-sdk-go/checker"
|
||||
)
|
||||
|
||||
// ExtractMetrics turns the stored data into time-series metrics so the host
|
||||
// can feed Prometheus / Grafana dashboards. Implements
|
||||
// sdk.CheckerMetricsReporter.
|
||||
//
|
||||
// Findings-based counters are re-derived from the raw observation using
|
||||
// default option values, since the rule set (and hence severity counts) is
|
||||
// no longer serialized with the observation.
|
||||
func (p *authoritativeConsistencyProvider) ExtractMetrics(ctx sdk.ReportContext, collectedAt time.Time) ([]sdk.CheckMetric, error) {
|
||||
var data ObservationData
|
||||
if err := json.Unmarshal(ctx.Data(), &data); err != nil {
|
||||
return nil, fmt.Errorf("checker: decoding observation: %w", err)
|
||||
}
|
||||
|
||||
var out []sdk.CheckMetric
|
||||
|
||||
// One "reachability" pair per NS, easy to alert on individual servers.
|
||||
for name, r := range data.Results {
|
||||
labels := map[string]string{"zone": data.Zone, "ns": name}
|
||||
|
||||
up := float64(0)
|
||||
if r.UDPReachable && r.Authoritative {
|
||||
up = 1
|
||||
}
|
||||
out = append(out, sdk.CheckMetric{
|
||||
Name: "authoritative_consistency_ns_up",
|
||||
Value: up,
|
||||
Labels: labels,
|
||||
Timestamp: collectedAt,
|
||||
})
|
||||
|
||||
tcp := float64(0)
|
||||
if r.TCPReachable {
|
||||
tcp = 1
|
||||
}
|
||||
out = append(out, sdk.CheckMetric{
|
||||
Name: "authoritative_consistency_ns_tcp",
|
||||
Value: tcp,
|
||||
Labels: labels,
|
||||
Timestamp: collectedAt,
|
||||
})
|
||||
|
||||
if r.LatencyMs > 0 {
|
||||
out = append(out, sdk.CheckMetric{
|
||||
Name: "authoritative_consistency_ns_latency_ms",
|
||||
Value: float64(r.LatencyMs),
|
||||
Unit: "ms",
|
||||
Labels: labels,
|
||||
Timestamp: collectedAt,
|
||||
})
|
||||
}
|
||||
|
||||
if r.Serial > 0 {
|
||||
out = append(out, sdk.CheckMetric{
|
||||
Name: "authoritative_consistency_ns_serial",
|
||||
Value: float64(r.Serial),
|
||||
Labels: labels,
|
||||
Timestamp: collectedAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Zone-wide metrics.
|
||||
uniqueSerials := map[uint32]struct{}{}
|
||||
for _, r := range data.Results {
|
||||
if r.Serial != 0 {
|
||||
uniqueSerials[r.Serial] = struct{}{}
|
||||
}
|
||||
}
|
||||
out = append(out, sdk.CheckMetric{
|
||||
Name: "authoritative_consistency_unique_serials",
|
||||
Value: float64(len(uniqueSerials)),
|
||||
Labels: map[string]string{"zone": data.Zone},
|
||||
Timestamp: collectedAt,
|
||||
})
|
||||
|
||||
// Severity counters sourced from ctx.States(). When the host does not
|
||||
// provide states (e.g. a metrics-only render path), the counters are
|
||||
// simply emitted as zero; we do NOT re-derive from the raw data.
|
||||
states := ctx.States()
|
||||
if states != nil {
|
||||
var crit, warn, info int
|
||||
for _, st := range states {
|
||||
switch st.Status {
|
||||
case sdk.StatusCrit:
|
||||
crit++
|
||||
case sdk.StatusWarn:
|
||||
warn++
|
||||
case sdk.StatusInfo:
|
||||
info++
|
||||
}
|
||||
}
|
||||
for _, x := range []struct {
|
||||
name string
|
||||
val int
|
||||
}{
|
||||
{"authoritative_consistency_findings_critical", crit},
|
||||
{"authoritative_consistency_findings_warning", warn},
|
||||
{"authoritative_consistency_findings_info", info},
|
||||
} {
|
||||
out = append(out, sdk.CheckMetric{
|
||||
Name: x.name,
|
||||
Value: float64(x.val),
|
||||
Labels: map[string]string{"zone": data.Zone},
|
||||
Timestamp: collectedAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue