Help us shape the future of Go! Take the 2026 Go Developer Survey

zabbix

package module
v0.0.0-...-7f9baf0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: MIT Imports: 9 Imported by: 0

README

go-zabbix

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

Example:

package main

import (
	"fmt"
	"time"

	"github.com/datadope-io/go-zabbix"
)

const (
	defaultHost  = `localhost`
	defaultPort = 10051
	agentActive = true
	trapper     = false
)

func main() {
	var metrics []*zabbix.Metric
	metrics = append(metrics, zabbix.NewMetric("localhost", "cpu", "1.22", agentActive, time.Now().Unix()))
	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, defaultPort)
	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)
}

CHANGELOG

2020-10-20

  • Now Active/Trapper response is formated in Response,Info strings and could be translated int ResponseInfo struct with Failed/Completed/Processed/Spent time.

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"`
	Active bool   `json:"-"`
}

Metric class.

func NewMetric

func NewMetric(host, key, value string, agentActive bool, clock ...int64) *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"`
	Host         string    `json:"host,omitempty"`
	HostMetadata string    `json:"host_metadata,omitempty"`
}

Packet class.

func NewPacket

func NewPacket(data []*Metric, agentActive bool, clock ...int64) *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
	Port           int
	ConnectTimeout time.Duration
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
}

Sender class.

func NewSender

func NewSender(host string, port int) *Sender

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

func NewSenderTimeout

func NewSenderTimeout(
	host string,
	port int,
	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