Files
Sebastian UnterschützandClaude Opus 5.5 36301ed146
ci / lint (pull_request) Successful in 5m44s
ci / build (pull_request) Successful in 5m8s
ci / image (pull_request) Successful in 4m43s
ci / test (pull_request) Successful in 17m9s
feat: Steam and Minecraft game server checks
Steam servers are asked for A2S_INFO like the server browser does,
answering the challenge newer servers send. Minecraft Java Edition gets
the server list ping, Bedrock Edition a RakNet unconnected ping. A
minimum of free slots can be required. The protocols are implemented
directly; tested against protocol-accurate test servers and once by
hand against public Java and Bedrock servers.

Closes #96

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 17:03:43 +01:00

174 lines
4.6 KiB
Go

// Package check runs a single uptime check and validates check settings.
//
// The API validates monitor settings with Validate before storing them, and
// checkers execute them with Run, so both sides agree on what a setting
// means.
package check
import (
"context"
"encoding/json"
"errors"
"time"
)
// Type names a kind of check. The values are stored in the database.
type Type string
// Check types.
const (
HTTP Type = "http"
TCP Type = "tcp"
ICMP Type = "icmp"
DNS Type = "dns"
TLS Type = "tls"
// JSON checks an HTTP API's JSON response against assertions.
JSON Type = "json"
// Prometheus compares the result of a PromQL query with a threshold.
Prometheus Type = "prometheus"
// Heartbeat monitors are pinged by the monitored job; no checker runs
// them.
Heartbeat Type = "heartbeat"
// Alertmanager monitors follow alerts that Alertmanager sends us.
Alertmanager Type = "alertmanager"
)
// Types lists every check type.
var Types = []Type{HTTP, TCP, ICMP, DNS, TLS, JSON, Prometheus, Postgres, MySQL, MSSQL, MongoDB, Redis, MQTT, Kafka, RabbitMQ, GRPC, SMTP, RADIUS, SNMP, Steam, Minecraft, Heartbeat, Alertmanager}
// Passive reports whether t is reported to us rather than run by checkers.
func Passive(t Type) bool { return t == Heartbeat || t == Alertmanager }
// Spec is everything needed to run one check.
type Spec struct {
Type Type
Target string
Timeout time.Duration
Config json.RawMessage
// AllowPrivate permits targets on private networks. It is false for
// shared checkers in the cloud edition.
AllowPrivate bool
// Capture records Evidence of HTTP exchanges.
Capture bool
}
// Outcome is the result of one check run.
type Outcome struct {
OK bool
Latency time.Duration
// StatusCode is the HTTP status, or 0 for other types.
StatusCode int
// Error explains a failure in a form suitable for showing to customers.
Error string
// Evidence is set when Spec.Capture was and the check made HTTP
// requests.
Evidence *Evidence
}
// ErrInvalid is wrapped by Validate errors. Its message is the code shown to
// API clients, e.g. "invalid_target".
type ErrInvalid struct{ Code string }
func (e ErrInvalid) Error() string { return "check: " + e.Code }
func invalid(code string) error { return ErrInvalid{Code: code} }
type runner interface {
run(ctx context.Context, s Spec) Outcome
}
type validator interface {
// validate checks target and config and returns the config with
// defaults applied.
validate(target string, config json.RawMessage) (json.RawMessage, error)
}
type impl interface {
runner
validator
}
var impls = map[Type]impl{
HTTP: httpCheck{},
TCP: tcpCheck{},
ICMP: icmpCheck{},
DNS: dnsCheck{},
TLS: tlsCheck{},
JSON: jsonCheck{},
Prometheus: prometheusCheck{},
Postgres: dbCheck{Postgres},
MySQL: dbCheck{MySQL},
MSSQL: dbCheck{MSSQL},
MongoDB: dbCheck{MongoDB},
Redis: dbCheck{Redis},
MQTT: brokerCheck{MQTT},
Kafka: brokerCheck{Kafka},
RabbitMQ: brokerCheck{RabbitMQ},
GRPC: grpcCheck{},
SMTP: smtpCheck{},
RADIUS: radiusCheck{},
SNMP: snmpCheck{},
Steam: gameCheck{Steam},
Minecraft: gameCheck{Minecraft},
Heartbeat: heartbeatCheck{},
Alertmanager: alertmanagerCheck{},
}
// Validate checks target and config for type t and returns the normalized
// config. Errors are ErrInvalid.
func Validate(t Type, target string, config json.RawMessage) (json.RawMessage, error) {
im, ok := impls[t]
if !ok {
return nil, invalid("invalid_type")
}
if len(config) == 0 || string(config) == "null" {
config = json.RawMessage("{}")
}
return im.validate(target, config)
}
// Run executes s. It never panics on bad input; problems become a failed
// Outcome.
func Run(ctx context.Context, s Spec) Outcome {
im, ok := impls[s.Type]
if !ok {
return Outcome{Error: "unknown check type"}
}
ctx, cancel := context.WithTimeout(ctx, s.Timeout)
defer cancel()
var rec *recorder
if s.Capture {
rec = newRecorder()
ctx = context.WithValue(ctx, recorderKey{}, rec)
}
start := time.Now()
o := im.run(ctx, s)
if o.Latency == 0 {
o.Latency = time.Since(start)
}
if !o.OK && o.Error == "" {
o.Error = "check failed"
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) && !o.OK {
o.Error = "timeout after " + s.Timeout.String()
}
if rec != nil {
o.Evidence = rec.evidence(o)
}
return o
}
func decode[T any](config json.RawMessage) (T, error) {
var v T
if err := json.Unmarshal(config, &v); err != nil {
return v, invalid("invalid_config")
}
return v, nil
}
func encode(v any) json.RawMessage {
b, _ := json.Marshal(v)
return b
}