zabbix

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2023 License: MIT Imports: 9 Imported by: 0

README

zabbix-sender

Golang package, implement zabbix sender protocol for send metrics to zabbix.

Example:

package main

import (
    "fmt"
    "time"

    "github.com/spetr/go-zabbix-sender"
)

const (
    defaultHost  = "localhost:10051"
    agentActive = true
    trapper     = false
)

func main() {
    var metrics []*zabbix.Metric
    metrics = append(metrics, zabbix.NewMetric("localhost", "cpu", "1.22", agentActive, time.Now()))
    metrics = append(metrics, zabbix.NewMetric("localhost", "status", "OK", agentActive))
    metrics = append(metrics, zabbix.NewMetric("localhost", "someTrapper", "3.14", trapper))

    // Send metrics to zabbix
    z := zabbix.NewSender(defaultHost)
    resActive, errActive, resTrapper, errTrapper := z.SendMetrics(metrics)

    fmt.Printf("Agent active, response=%s, info=%s, error=%v\n", resActive.Response,resActive.Info, errActive)
    fmt.Printf("Trapper, response=%s, info=%s,error=%v\n", resTrapper.Response, resTrapper.Info,errTrapper)
}

Documentation

Overview

Package zabbix implements the sender protocol to send values to zabbix Taken from github.com/blacked/go-zabbix (discontinued)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 class.

func NewMetric

func NewMetric(host, key, value string, agentActive bool, t ...time.Time) *Metric

NewMetric return a zabbix Metric with the values specified agentActive should be set to true if we are sending to a Zabbix Agent (active) item

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 class.

func NewPacket

func NewPacket(data []*Metric, agentActive bool, t ...time.Time) *Packet

NewPacket return a zabbix packet with a list of metrics

func (*Packet) DataLen

func (p *Packet) DataLen() []byte

DataLen Packet class method, return 8 bytes with packet length in little endian order

type Response

type Response struct {
	Response string
	Info     string
}

Reponse is a response for autoregister method

func (*Response) GetInfo

func (r *Response) GetInfo() (*ResponseInfo, error)

type ResponseInfo

type ResponseInfo struct {
	Processed int
	Failed    int
	Total     int
	Spent     time.Duration
}

type Sender

type Sender struct {
	Host           string
	ConnectTimeout time.Duration
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
}

Sender class

func NewSender

func NewSender(host string) *Sender

NewSender return a sender object to send metrics using default values for timeouts

func NewSenderTimeout

func NewSenderTimeout(
	host string,
	connectTimeout time.Duration,
	readTimeout time.Duration,
	writeTimeout time.Duration,
) *Sender

NewSenderTimeout return a sender object to send metrics defining values for timeouts

func (*Sender) RegisterHost

func (s *Sender) RegisterHost(host, hostmetadata string) error

RegisterHost provides a register a Zabbix's host with Autoregister method.

func (*Sender) Send

func (s *Sender) Send(packet *Packet) (res Response, err error)

Send connects to Zabbix, send the data, return the response and close the connection

func (*Sender) SendMetrics

func (s *Sender) SendMetrics(metrics []*Metric) (resActive Response, errActive error, resTrapper Response, errTrapper error)

SendMetrics send an array of metrics, making different packets for trapper and active items. The response for trapper metrics is in the first element of the res array and err array Response for active metrics is in the second element of the res array and error array

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL