checker-delegation/checker/evaluate.go

74 lines
2.2 KiB
Go

// This file is part of the happyDomain (R) project.
// Copyright (c) 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 (
"fmt"
sdk "git.happydns.org/checker-sdk-go/checker"
)
// Evaluate folds findings into a single CheckState. The status is the
// highest severity observed: any Crit makes the whole result Crit, any Warn
// makes it Warn, otherwise OK.
func Evaluate(data *DelegationData) sdk.CheckState {
status := sdk.StatusOK
var crit, warn, info int
for _, f := range data.Findings {
switch f.Severity {
case SeverityCrit:
crit++
status = sdk.StatusCrit
case SeverityWarn:
warn++
if status != sdk.StatusCrit {
status = sdk.StatusWarn
}
case SeverityInfo:
info++
if status == sdk.StatusOK {
status = sdk.StatusInfo
}
}
}
var msg string
if len(data.Findings) == 0 {
msg = fmt.Sprintf("Delegation of %s is healthy", data.DelegatedFQDN)
} else {
msg = fmt.Sprintf("Delegation of %s: %d critical, %d warning, %d info", data.DelegatedFQDN, crit, warn, info)
}
return sdk.CheckState{
Status: status,
Message: msg,
Code: "delegation_result",
Meta: map[string]any{
"findings": data.Findings,
"delegated_fqdn": data.DelegatedFQDN,
"parent_zone": data.ParentZone,
"advertised_ns": data.AdvertisedNS,
"parent_ds": data.ParentDS,
"child_serials": data.ChildSerials,
},
}
}