Documentation
¶
Overview ¶
Package probe checks targets from a probe location, the way the Prometheus Blackbox Exporter does and with the same metric names (probe_success, probe_duration_seconds, probe_http_status_code, probe_ssl_earliest_cert_expiry), so that alert rules work with either. It adds what a status service needs: every failure says why (probe_failure_info{reason}), and multi-step checks for a container registry (token, then manifest), a Git repository over HTTP, a domain's expiry (RDAP), the services people run beside their websites: Redis, PostgreSQL, MySQL, MongoDB, MQTT and SMTP, each spoken with the standard library alone (services.go and a file per service), a gRPC server's standard health service (grpc.go), and a container on the probe's own host (docker.go).
The module file is the Blackbox Exporter's format for the http, tcp and icmp probers (the subset this package implements; anything else is an error rather than silently ignored), plus Beacon's dns, registry, git, rdap, service, grpc and docker probers and a few fields of its own (json_query, max_redirects).
Monitors compile to modules (package monitor). A module that uses a prober or a field added after the first probe protocol is of a later protocol (Module.Protocol): 2 for ping, DNS and the HTTP and TCP options, 3 for the service probers, 4 for grpc and docker. The gateway hands it, and the targets that use it, only to a probe that asks with that protocol or a later one (?protocol=4), so that an older probe, which refuses a module file with fields it does not know or skips a module of a prober it does not know, keeps checking everything else and reports no failure for what it cannot check. A probe of this version reads each module on its own (ParseModulesLenient) and skips one it cannot read.
Index ¶
- Constants
- Variables
- func CheckSecretFiles(ms *Modules) error
- func DockerEndpoint(host string) (network, addr string, err error)
- func GRPCReason(code int) string
- func Handler(p *Prober, modules func() *Modules, log func(module, target string, r Result)) http.Handler
- func SecretName(field, name string) error
- func ValidContainer(s string) bool
- func Write(w io.Writer, r Result) error
- func WriteSamples(w io.Writer, samples []Sample) error
- type BasicAuth
- type DNSModule
- type Duration
- type GRPCModule
- type GitModule
- type HTTPModule
- type ICMPModule
- type IPConfig
- type JSONQuery
- type MQTTModule
- type Module
- type Modules
- type MongoDBModule
- type Prober
- type QueryResponse
- type RDAPModule
- type RedisModule
- type RegistryModule
- type Reloadable
- type Result
- type SMTPModule
- type SQLModule
- type Sample
- type ServiceTLS
- type TCPModule
- type TLSConfig
Constants ¶
const ( ReasonDNS = "dns" // the name did not resolve ReasonConnect = "connect" // the connection was refused or reset ReasonTimeout = "timeout" // no answer in time ReasonTLS = "tls" // the TLS handshake or the certificate failed ReasonNotSSL = "not_ssl" // plain HTTP where HTTPS was required ReasonHTTPStatus = "http_status" // an unexpected status code ReasonBody = "body" // the body did not match ReasonBanner = "banner" // a server did not say what was expected (a TCP line, an error, another value) ReasonProtocol = "protocol" // an answer of the wrong kind (content type, JSON, not the service's protocol) ReasonAuth = "auth" // the credentials were refused, or a registry token could not be obtained ReasonManifest = "manifest" // the registry has no such manifest ReasonConfig = "config" // the module or target is wrong ReasonKeyword = "keyword" // a keyword that must be absent is in the body ReasonJSON = "json_query" // the JSON query did not hold ReasonNoReply = "no_reply" // no echo reply to a ping ReasonDNSAnswer = "dns_answer" // the name resolved, but not to what was expected ReasonRedirects = "redirects" // more redirects than allowed ReasonNotServing = "not_serving" // a gRPC health service answered, but not SERVING ReasonNotFound = "not_found" // the Docker Engine has no such container ReasonNotRunning = "not_running" // the container is not running (exited, paused, restarting, …) ReasonUnhealthy = "unhealthy" // the container runs, but its health check does not say healthy )
Failure reasons, the reason label of probe_failure_info.
const DefaultDockerHost = "unix:///var/run/docker.sock"
DefaultDockerHost is the Docker Engine a probe asks unless its operator names another.
const DefaultSecretsDir = "/etc/beacon-probe/secrets" //nolint:gosec // G101: a directory, not a credential
DefaultSecretsDir is where a probe looks for the files its modules name.
const Protocol = 4
Protocol is the newest probe protocol this package speaks: a probe asks the gateway for its modules and targets with ?protocol=Protocol.
Variables ¶
var DNSTypes = []string{"A", "AAAA", "CNAME", "MX", "NS", "TXT", "SRV", "PTR"}
DNSTypes are the record types a DNS probe asks for.
var FourthProbers = []string{"grpc", "docker"}
FourthProbers are the probers of the fourth probe protocol: a gRPC server's health, and a container on the probe's own host.
var JSONOperators = []string{"==", "!=", "<", "<=", ">", ">=", "contains"}
JSONOperators are the comparisons a JSON query may make.
var LegacyProbers = []string{"http", "tcp", "registry", "git", "rdap"}
LegacyProbers are the probers of the first probe protocol, which every probe understands.
var Probers = []string{"http", "tcp", "icmp", "dns", "registry", "git", "rdap", "redis", "postgres", "mysql", "mongodb", "mqtt", "smtp",
"grpc", "docker"}
Probers are the kinds of check.
var Reasons = append([]string{ReasonDNS, ReasonConnect, ReasonTimeout, ReasonTLS, ReasonNotSSL, ReasonHTTPStatus, ReasonBody, ReasonBanner, ReasonProtocol, ReasonAuth, ReasonManifest, ReasonConfig, ReasonKeyword, ReasonJSON, ReasonNoReply, ReasonDNSAnswer, ReasonRedirects, ReasonNotServing, ReasonNotFound, ReasonNotRunning, ReasonUnhealthy}, grpcReasons()...)
Reasons lists every failure reason, for the console's filters and the documentation: a gRPC server's status other than OK is one of its own (GRPCReason).
var ServiceProbers = []string{"redis", "postgres", "mysql", "mongodb", "mqtt", "smtp"}
ServiceProbers are the probers of the third probe protocol: services spoken in their own protocol.
Functions ¶
func CheckSecretFiles ¶
CheckSecretFiles checks every module's file fields with SecretName. The centre refuses a module file that fails it. A probe still parses such a file (an older gateway may hand one out) and refuses each probe that would read the file, so that the other modules keep working.
func DockerEndpoint ¶
DockerEndpoint reads the address of a Docker Engine as an operator gives it: unix:///path (or the path alone) for its Unix socket; tcp://host:port or http://host:port for one that listens on TCP without TLS, such as a socket proxy on the same host. An Engine behind TLS with client certificates is not supported.
func GRPCReason ¶
GRPCReason is the failure reason of a gRPC status code other than OK: grpc_ and its name in lower case, such as grpc_unavailable; a code gRPC does not define is grpc_unknown.
func Handler ¶
func Handler(p *Prober, modules func() *Modules, log func(module, target string, r Result)) http.Handler
Handler serves /probe?module=…&target=…, like the Blackbox Exporter. modules is read on every request, so a reload takes effect. A scrape's own timeout (X-Prometheus-Scrape-Timeout-Seconds) shortens the module's, leaving half a second to answer.
func SecretName ¶
SecretName checks the value of a module's file field: a file name to be found in the secrets directory, without a path. field names the field, for the error; an empty name is no file.
func ValidContainer ¶
ValidContainer reports whether s may name a container, as the Engine allows names and ids: a letter or digit, then letters, digits, dots, dashes and underscores, at most 255 in all (after an optional slash). Checked by hand: a regular expression with such a bound costs the probe memory to compile.
Types ¶
type BasicAuth ¶
type BasicAuth struct {
Username string `yaml:"username"`
PasswordFile string `yaml:"password_file,omitempty"`
}
BasicAuth is HTTP basic authentication with a password from the probe's secrets directory.
type DNSModule ¶
type DNSModule struct {
Server string `yaml:"server,omitempty"`
QueryType string `yaml:"query_type,omitempty"`
Expect []string `yaml:"expect,omitempty"`
}
DNSModule resolves the target, a name, with the probe's resolver or Server (host or host:port), and checks that there is an answer of QueryType (A, AAAA, CNAME, MX, NS, TXT, SRV or PTR; PTR takes an address) and, with Expect, that one answer is one of those listed (compared without case and the final dot).
type Duration ¶
Duration is a time.Duration written as "20s" in YAML.
func (Duration) MarshalYAML ¶
MarshalYAML writes "20s".
type GRPCModule ¶
type GRPCModule struct {
IPConfig `yaml:",inline"`
ServiceTLS `yaml:",inline"`
Service string `yaml:"service,omitempty"`
Metadata map[string]string `yaml:"metadata,omitempty"`
}
GRPCModule asks a gRPC server's standard health service (grpc.health.v1.Health/Check) about Service, the server as a whole when empty, and wants SERVING. With TLS it speaks HTTP/2 over TLS (the certificate's expiry is a result, and TLSConfig can skip its check); without, HTTP/2 in clear text (h2c, with prior knowledge), as the Blackbox Exporter's grpc prober does. Metadata are sent as request headers. The target is host:port.
type GitModule ¶
type GitModule struct {
IPConfig `yaml:",inline"`
Username string `yaml:"username,omitempty"`
PasswordFile string `yaml:"password_file,omitempty"`
}
GitModule asks a repository for its refs over smart HTTP (what git clone does first). The target is the repository URL.
type HTTPModule ¶
type HTTPModule struct {
IPConfig `yaml:",inline"`
Method string `yaml:"method,omitempty"`
ValidStatusCodes []int `yaml:"valid_status_codes,omitempty"`
FollowRedirects *bool `yaml:"follow_redirects,omitempty"`
FailIfNotSSL bool `yaml:"fail_if_not_ssl,omitempty"`
FailIfBodyNotMatchesRegexp []string `yaml:"fail_if_body_not_matches_regexp,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
// Protocol 2. Body is sent with the request; FailIfBodyMatchesRegexp fails a body that matches (a
// keyword that must be absent); BasicAuth and BearerTokenFile authenticate with a secret read from
// the probe's secrets directory; TLSConfig can skip certificate verification; MaxRedirects bounds
// the redirects followed (0: 10); JSONQuery checks one value of a JSON body.
Body string `yaml:"body,omitempty"`
FailIfBodyMatchesRegexp []string `yaml:"fail_if_body_matches_regexp,omitempty"`
BasicAuth *BasicAuth `yaml:"basic_auth,omitempty"`
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
TLSConfig *TLSConfig `yaml:"tls_config,omitempty"`
MaxRedirects int `yaml:"max_redirects,omitempty"`
JSONQuery *JSONQuery `yaml:"json_query,omitempty"`
// contains filtered or unexported fields
}
HTTPModule is an HTTP request and what its answer must look like. Without valid_status_codes any 2xx is success.
type ICMPModule ¶
ICMPModule sends echo requests (ping) to the target, a host name or an address. Packets is how many (default 1, at most 10), a second apart; the probe succeeds when one is answered.
type IPConfig ¶
type IPConfig struct {
PreferredIPProtocol string `yaml:"preferred_ip_protocol,omitempty"`
IPProtocolFallback *bool `yaml:"ip_protocol_fallback,omitempty"`
}
IPConfig chooses the address family: ip4 or ip6 (default ip6, as in the Blackbox Exporter), with or without falling back to the other one.
type JSONQuery ¶
type JSONQuery struct {
Path string `yaml:"path"`
Operator string `yaml:"operator,omitempty"`
Value string `yaml:"value"`
}
JSONQuery reads one value from a JSON body by a path such as data.items[0].status ($ or "" is the whole document) and compares it with Value: ==, !=, <, <=, >, >= (as numbers when both are numbers) or contains.
type MQTTModule ¶
type MQTTModule struct {
IPConfig `yaml:",inline"`
ServiceTLS `yaml:",inline"`
Username string `yaml:"username,omitempty"`
PasswordFile string `yaml:"password_file,omitempty"`
}
MQTTModule connects with MQTT 3.1.1 (as Username, with the password from PasswordFile, when set) and wants the broker to accept the connection. The target is host:port (1883, or 8883 over TLS, when left out).
type Module ¶
type Module struct {
Prober string `yaml:"prober"`
Timeout Duration `yaml:"timeout,omitempty"`
HTTP HTTPModule `yaml:"http,omitempty"`
TCP TCPModule `yaml:"tcp,omitempty"`
ICMP ICMPModule `yaml:"icmp,omitempty"`
DNS DNSModule `yaml:"dns,omitempty"`
Registry RegistryModule `yaml:"registry,omitempty"`
Git GitModule `yaml:"git,omitempty"`
RDAP RDAPModule `yaml:"rdap,omitempty"`
// Protocol 3: the service probers.
Redis RedisModule `yaml:"redis,omitempty"`
Postgres SQLModule `yaml:"postgres,omitempty"`
MySQL SQLModule `yaml:"mysql,omitempty"`
MongoDB MongoDBModule `yaml:"mongodb,omitempty"`
MQTT MQTTModule `yaml:"mqtt,omitempty"`
SMTP SMTPModule `yaml:"smtp,omitempty"`
// Protocol 4: gRPC health. The docker prober has no settings: the target names the container, and
// the probe's operator, never a module, says which Docker Engine it asks (Prober.DockerHost).
GRPC GRPCModule `yaml:"grpc,omitempty"`
}
Module is one way to probe a target.
type Modules ¶
Modules are the named ways to probe a target.
func LoadModules ¶
LoadModules reads and checks a module file.
func ParseModules ¶
ParseModules is LoadModules without the file.
func ParseModulesLenient ¶
ParseModulesLenient reads a module file a module at a time, as a probe does: a module it cannot read (a newer gateway's prober or field) is left out, and said in the errors, while the others are kept. Only a file that is not a module file at all is an error with no modules.
type MongoDBModule ¶
type MongoDBModule struct {
IPConfig `yaml:",inline"`
ServiceTLS `yaml:",inline"`
RequirePrimary bool `yaml:"require_primary,omitempty"`
}
MongoDBModule sends the hello command, which needs no sign-in, and wants ok: 1; with RequirePrimary, also a writable primary. The target is host:port (27017 when left out).
type Prober ¶
type Prober struct {
RootCAs *x509.CertPool
Resolver *net.Resolver
UserAgent string
SecretsDir string
DockerHost string
}
Prober runs probes. RootCAs is nil for the system's roots and Resolver nil for the system's resolver (tests set their own). SecretsDir is the one directory the files modules name (password_file) are read from (secrets.go); empty for DefaultSecretsDir. DockerHost is the Docker Engine the docker prober asks (docker.go), as the probe's operator configured it; empty for DefaultDockerHost.
type QueryResponse ¶
type QueryResponse struct {
Expect string `yaml:"expect,omitempty"`
Send string `yaml:"send,omitempty"`
// contains filtered or unexported fields
}
QueryResponse sends a line, or waits for a line matching Expect.
type RDAPModule ¶
type RDAPModule struct {
IPConfig `yaml:",inline"`
Server string `yaml:"server,omitempty"` // default https://rdap.org, which redirects to the registry's server
}
RDAPModule looks up a domain's expiry. The target is the domain.
type RedisModule ¶
type RedisModule struct {
IPConfig `yaml:",inline"`
ServiceTLS `yaml:",inline"`
Username string `yaml:"username,omitempty"`
PasswordFile string `yaml:"password_file,omitempty"`
}
RedisModule sends PING and wants PONG, first signing in with AUTH when PasswordFile is set (as Username, an ACL user, when set). The target is host:port (6379 when left out).
type RegistryModule ¶
type RegistryModule struct {
IPConfig `yaml:",inline"`
Repository string `yaml:"repository,omitempty"` // such as library/busybox
Reference string `yaml:"reference,omitempty"` // a tag or digest; default latest
Username string `yaml:"username,omitempty"`
PasswordFile string `yaml:"password_file,omitempty"`
}
RegistryModule pulls a manifest the way a container client does: /v2/, then a token from the realm the registry names (anonymously, or with the credentials), then HEAD the manifest. Nothing is downloaded. The target is the registry's base URL.
type Reloadable ¶
type Reloadable struct {
// contains filtered or unexported fields
}
Reloadable holds the module file and swaps it on reload.
type Result ¶
type Result struct {
Success bool
Reason string // why it failed
Detail string // the error, for the log; never in metrics (it may hold anything a server said)
Duration time.Duration
Samples []Sample
ExerciseReceipt string `json:"-"`
}
Result is what a probe found.
type SMTPModule ¶
type SMTPModule struct {
IPConfig `yaml:",inline"`
ServiceTLS `yaml:",inline"`
StartTLS bool `yaml:"starttls,omitempty"`
}
SMTPModule reads a mail server's greeting (220) and says EHLO; with StartTLS it then upgrades the connection and says EHLO again; with TLS it speaks TLS from the start (port 465). The target is host:port (25, or 465 with TLS, when left out).
type SQLModule ¶
type SQLModule struct {
IPConfig `yaml:",inline"`
ServiceTLS `yaml:",inline"`
Username string `yaml:"username,omitempty"`
Database string `yaml:"database,omitempty"`
PasswordFile string `yaml:"password_file,omitempty"`
Query string `yaml:"query,omitempty"`
Expect string `yaml:"expect,omitempty"`
}
SQLModule is a PostgreSQL or MySQL check. Without Username the probe reads the server's greeting only: a PostgreSQL server that asks the probe to authenticate, or a MySQL server's handshake, is up. With Username it signs in (with the password from PasswordFile when the server asks for one) to Database, runs Query (PostgreSQL: SELECT 1 when empty; MySQL pings when it is empty) and, with Expect, wants the first value of the first row to be Expect. The target is host:port (5432 or 3306 when left out).
type Sample ¶
Sample is one line of the result: name{labels} value. Type is gauge unless set (counter).
type ServiceTLS ¶
type ServiceTLS struct {
TLS bool `yaml:"tls,omitempty"`
TLSConfig *TLSConfig `yaml:"tls_config,omitempty"`
}
ServiceTLS is how a service prober uses TLS: with TLS it connects over TLS (PostgreSQL and MySQL ask for it in their own protocol first, as their clients do) and fails when the server does not offer it; TLSConfig can skip the certificate check. The certificate's expiry is a result, as for HTTPS.
type TCPModule ¶
type TCPModule struct {
IPConfig `yaml:",inline"`
TLS bool `yaml:"tls,omitempty"`
QueryResponse []QueryResponse `yaml:"query_response,omitempty"`
// TLSConfig (protocol 2) can skip certificate verification.
TLSConfig *TLSConfig `yaml:"tls_config,omitempty"`
}
TCPModule connects, optionally over TLS, and checks what the server says.