Documentation
¶
Overview ¶
Package zabbix_sender implements Zabbix sender protocol with proxy group redirects and multi-host HA support.
Index ¶
- Constants
- func TLSConfigFromFiles(caFile, certFile, keyFile string) (*tls.Config, error)
- type AgentConfig
- type ClusterResult
- type Metric
- type MultiSender
- type Packet
- type RedirectInfo
- type Response
- type ResponseError
- type ResponseInfo
- type Sender
- func (s *Sender) PrimaryHost() string
- func (s *Sender) RegisterHost(host, hostmetadata string) error
- func (s *Sender) RegisterHostContext(ctx context.Context, host, hostmetadata string) error
- func (s *Sender) Send(packet *Packet) (Response, error)
- func (s *Sender) SendContext(ctx context.Context, packet *Packet) (Response, error)
- func (s *Sender) SendMetrics(metrics []*Metric) (resActive Response, errActive error, resTrapper Response, errTrapper error)
- func (s *Sender) SendMetricsContext(ctx context.Context, metrics []*Metric) (resActive Response, errActive error, resTrapper Response, errTrapper error)
- func (s *Sender) SendValue(host, key string, value interface{}) (Response, error)
- func (s *Sender) SendValueContext(ctx context.Context, host, key string, value interface{}) (Response, error)
- func (s *Sender) SetPrimaryHost(host string)
Constants ¶
const DefaultChunkSize = 250
DefaultChunkSize is the number of metrics sent per packet when Sender.ChunkSize is 0 (same default as python-zabbix-utils).
const DefaultMaxResponseSize = 16 << 20 // 16 MiB
DefaultMaxResponseSize caps how large a response body the sender accepts when Sender.MaxResponseSize is 0. Real Zabbix answers are tiny; the cap only guards against a broken or malicious peer.
Variables ¶
This section is empty.
Functions ¶
func TLSConfigFromFiles ¶
TLSConfigFromFiles builds a certificate-based tls.Config the way zabbix_sender does for TLSConnect=cert:
- caFile (TLSCAFile) is the CA bundle used to verify the server; empty means the system CA pool.
- certFile/keyFile (TLSCertFile/TLSKeyFile) are the client certificate and key; both empty means no client certificate is presented.
The result can be assigned to Sender.TLSConfig, possibly after further adjustment (e.g. ServerName when connecting by IP).
Types ¶
type AgentConfig ¶
type AgentConfig struct {
// ServerActive holds the parsed ServerActive value: one entry per
// independent destination (comma-separated in the config file), each
// entry a list of HA nodes (semicolon-separated) with ports
// normalized. Every destination is meant to receive a full copy of
// the data; within a destination only the first reachable node is.
ServerActive [][]string
// Hostname is the agent's Hostname, useful as the default Metric host.
Hostname string
// SourceIP is the local address to bind outgoing connections to.
SourceIP string
// TLS holds every TLS* key of the config file verbatim, with
// lowercased key names ("tlsconnect", "tlscafile", "tlscertfile",
// "tlskeyfile", "tlspskfile", "tlspskidentity", ...).
TLS map[string]string
}
AgentConfig holds the sender-relevant values of a zabbix_agentd.conf / zabbix_agent2.conf file.
func ParseAgentConfig ¶
func ParseAgentConfig(path string) (*AgentConfig, error)
ParseAgentConfig reads a Zabbix agent configuration file (flat Key=Value lines, # comments). It extracts ServerActive (falling back to Server, then to 127.0.0.1:10051), Hostname, SourceIP, and all TLS* keys. Keys are matched case-insensitively; Include directives are not followed.
func (*AgentConfig) TLSClientConfig ¶
func (c *AgentConfig) TLSClientConfig() (*tls.Config, error)
TLSClientConfig builds a tls.Config from the config file's TLS* keys. It returns (nil, nil) when TLSConnect is absent or "unencrypted", a certificate-based config for TLSConnect=cert, and an error for TLSConnect=psk: Go's crypto/tls has no TLS-PSK support, use Sender.DialFunc with a PSK-capable transport instead.
type ClusterResult ¶
type ClusterResult struct {
Sender *Sender
ResActive Response
ErrActive error
ResTrapper Response
ErrTrapper error
}
ClusterResult is the outcome of one destination's send.
func (*ClusterResult) Err ¶
func (r *ClusterResult) Err() error
Err returns the first non-nil error of the result.
type Metric ¶
type Metric struct {
Host string `json:"host"`
Key string `json:"key"`
Value string `json:"value"`
Clock int64 `json:"clock,omitempty"`
NS int `json:"ns,omitempty"`
Active bool `json:"-"`
}
Metric represents a Zabbix metric.
func NewMetric ¶
NewMetric creates a Zabbix metric.
agentActive=true for active agent items ("agent data"), agentActive=false for trapper items ("sender data"). t optionally sets custom timestamp.
func NewMetricValue ¶
NewMetricValue is NewMetric for values of any type: numbers, booleans, fmt.Stringer implementations, etc. are formatted with fmt.Sprint (the Zabbix protocol carries all values as strings).
type MultiSender ¶
type MultiSender struct {
Senders []*Sender
}
MultiSender fans metrics out to multiple independent destinations, each a Sender with its own HA node list — the Zabbix agent semantics for comma-separated ServerActive entries: every destination receives a full copy of the data.
func NewMultiSender ¶
func NewMultiSender(clusters [][]string) *MultiSender
NewMultiSender creates a MultiSender from destination clusters, each a list of HA nodes (ports normalized, IPv6 supported).
func NewMultiSenderFromConfig ¶
func NewMultiSenderFromConfig(path string) (*MultiSender, error)
NewMultiSenderFromConfig creates a MultiSender from a Zabbix agent configuration file (see NewSenderFromConfig).
func (*MultiSender) SendMetrics ¶
func (m *MultiSender) SendMetrics(metrics []*Metric) []ClusterResult
SendMetrics sends the metrics to every destination concurrently and returns one result per destination, in Senders order.
func (*MultiSender) SendMetricsContext ¶
func (m *MultiSender) SendMetricsContext(ctx context.Context, metrics []*Metric) []ClusterResult
SendMetricsContext is SendMetrics honoring the context's deadline and cancellation on top of each Sender's own timeouts.
type Packet ¶
type Packet struct {
Request string `json:"request"`
Data []*Metric `json:"data,omitempty"`
Clock int64 `json:"clock,omitempty"`
NS int `json:"ns,omitempty"`
Host string `json:"host,omitempty"`
HostMetadata string `json:"host_metadata,omitempty"`
}
Packet struct.
type RedirectInfo ¶
RedirectInfo struct.
type Response ¶
type Response struct {
Response string `json:"response"`
Info string `json:"info"`
Redirect *RedirectInfo `json:"redirect,omitempty"`
}
Response is the response struct from Zabbix server/proxy.
func (*Response) GetInfo ¶
func (r *Response) GetInfo() (*ResponseInfo, error)
GetInfo parses success response "info" field into statistics.
type ResponseError ¶
ResponseError is returned when a server/proxy was reached and answered with a well-formed non-success response. It lets callers distinguish application-level failures from transport errors; Send does not fail over to another host when it occurs.
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
type ResponseInfo ¶
ResponseInfo struct holds parsed statistics from response "info" field.
type Sender ¶
type Sender struct {
Hosts []string // ordered list of proxies/servers; first successful cached as primary host
MaxRedirects int // max redirect attempts before error; default is 3
UpdateHost bool // if true, cache the final redirect target instead of the starting host
ConnectTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
// MaxResponseSize caps the response body length the sender accepts.
// 0 means DefaultMaxResponseSize.
MaxResponseSize uint64
// ChunkSize splits packets with more than this many metrics into
// multiple sequential packets; the returned Response carries the
// summed statistics. 0 means DefaultChunkSize, negative disables
// chunking.
ChunkSize int
// Compression zlib-compresses outgoing packets (protocol flag 0x02).
// Compressed responses are always accepted, regardless of this flag.
Compression bool
// TLSConfig enables certificate-based TLS (Zabbix TLSConnect=cert)
// when non-nil. Ignored when DialFunc is set.
TLSConfig *tls.Config
// SourceIP optionally binds outgoing connections to a local address
// (Zabbix SourceIP). Ignored when DialFunc is set.
SourceIP string
// DialFunc, when non-nil, replaces the built-in dialer entirely and
// its net.Conn is used as the packet transport. This is the hook for
// TLS-PSK (not supported by crypto/tls) or any custom transport; the
// implementation should honor ctx and ConnectTimeout itself.
DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
// contains filtered or unexported fields
}
Sender sends packets to a Zabbix server/proxy.
A Sender is safe for concurrent use by multiple goroutines: sends run in parallel, and the working-host cache is internally synchronized. The configuration fields (Hosts, MaxRedirects, timeouts, ...) are read without locking and must be set before the Sender is shared between goroutines.
func NewSenderFromConfig ¶
NewSenderFromConfig creates one Sender per ServerActive destination of a Zabbix agent configuration file, with SourceIP and certificate-based TLS applied. Every returned Sender is meant to receive a full copy of the data (Zabbix agent semantics for comma-separated ServerActive entries).
func NewSenderHosts ¶
NewSenderHosts creates sender for multiple hosts (HA or Proxy Group).
func NewSenderTimeout ¶
func NewSenderTimeout( host string, connectTimeout time.Duration, readTimeout time.Duration, writeTimeout time.Duration, ) *Sender
NewSenderTimeout creates Sender with custom timeouts.
func (*Sender) PrimaryHost ¶
PrimaryHost returns the cached working host (empty = none cached yet).
func (*Sender) RegisterHost ¶
RegisterHost sends host autoregistration request ("active checks"). A first "failed" answer is expected for unknown hosts (it is what triggers the server-side autoregistration), so it retries once to confirm.
func (*Sender) RegisterHostContext ¶
RegisterHostContext is RegisterHost honoring the context's deadline and cancellation on top of the Sender's own timeouts.
func (*Sender) Send ¶
Send sends single packet with redirect/HA handling. Caches working PrimaryHost for future calls. Fails over to the next host only on transport errors; a host that answers (even with "failed") is considered reachable and its answer final (see ResponseError).
func (*Sender) SendContext ¶
SendContext is Send honoring the context's deadline and cancellation on top of the Sender's own timeouts.
func (*Sender) SendMetrics ¶
func (s *Sender) SendMetrics(metrics []*Metric) (resActive Response, errActive error, resTrapper Response, errTrapper error)
SendMetrics sends mixed active+trapper metrics. Automatically separates into "agent data" and "sender data" packets. Returns 4 values: (activeRes, activeErr, trapperRes, trapperErr)
func (*Sender) SendMetricsContext ¶
func (s *Sender) SendMetricsContext(ctx context.Context, metrics []*Metric) (resActive Response, errActive error, resTrapper Response, errTrapper error)
SendMetricsContext is SendMetrics honoring the context's deadline and cancellation on top of the Sender's own timeouts.
func (*Sender) SendValue ¶
SendValue sends one trapper value in a single call, formatting value with fmt.Sprint (like python-zabbix-utils' send_value).
func (*Sender) SendValueContext ¶
func (s *Sender) SendValueContext(ctx context.Context, host, key string, value interface{}) (Response, error)
SendValueContext is SendValue honoring the context's deadline and cancellation on top of the Sender's own timeouts.
func (*Sender) SetPrimaryHost ¶
SetPrimaryHost pre-sets (or, with "", clears) the cached working host.