Files
worker/checks/cdns/result.go
Gleb Tv 2c884c5612
Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
refactor: adopt worker module path
2026-07-13 17:56:12 +03:00

110 строки
2.4 KiB
Go

package cdns
import (
"strconv"
"strings"
"time"
"rocketgit.ru/rsmon/worker/app/models"
"rocketgit.ru/rsmon/worker/internal/checkresult"
"rocketgit.ru/rsmon/worker/internal/netaddr"
)
// NSIP holds the result of querying a single nameserver IP.
type NSIP struct {
State string
IP string
ResponseTime time.Duration
Serial uint32
Error error
}
// NSRecord holds a single DNS record from a nameserver response.
type NSRecord struct {
Name string
Kind string
Value netaddr.Inet
}
// NSServer holds the result of querying a single nameserver.
type NSServer struct {
State string
Name string
Error error
NSIPs []NSIP
Response []NSRecord
}
// Result holds the full DNS check result.
type Result struct {
checkresult.CheckResult
NSServers []NSServer
}
// Servers provides functionality.
func (r *Result) Servers() []string {
ret := make([]string, 0, len(r.NSServers))
for _, s := range r.NSServers {
ret = append(ret, s.Name+"-"+s.State)
}
return ret
}
// ServersOK provides functionality.
func (r *Result) ServersOK() int {
ok := 0
for _, s := range r.NSServers {
if s.State == "OK" {
ok++
}
}
return ok
}
// Times provides functionality.
func (r *Result) Times() (time.Duration, time.Duration) {
minTime := time.Hour
maxTime := time.Duration(0)
for _, s := range r.NSServers {
for _, i := range s.NSIPs {
if i.ResponseTime > maxTime {
maxTime = i.ResponseTime
}
if i.ResponseTime < minTime {
minTime = i.ResponseTime
}
}
}
return minTime, maxTime
}
// InfluxFields provides functionality.
func (r *Result) InfluxFields() map[string]interface{} {
ret := make(map[string]interface{}, 0)
_, maxTime := r.Times()
ret["took"] = int64(maxTime / time.Millisecond)
return ret
}
// InfluxTags provides functionality.
func (r *Result) InfluxTags(c models.Check) map[string]string { //nolint:gocritic // hugeParam: accepted for interface compatibility
ret := make(map[string]string, 0)
ret["check"] = strconv.FormatInt(c.ID, 10)
ret["state"] = c.State
minTime, _ := r.Times()
ret["min_took"] = strconv.FormatInt(int64(minTime/time.Millisecond), 10)
ret["state"] = r.State
total := len(r.NSServers)
oks := r.ServersOK()
ret["warnings"] = strings.Join(r.Warnings, ",")
ret["nservers"] = strconv.Itoa(total)
ret["servers"] = strings.Join(r.Servers(), ",")
ret["servers_ok"] = strconv.Itoa(oks)
ret["servers_failed"] = strconv.Itoa(total - oks)
return ret
}