zbx

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 8 Imported by: 0

README

zbx

Go Report Card Godoc Releases LICENSE

Package zbx is a Zabbix Agent implementation in golang that allows your application to act as a zabbix agent and respond to simple requests.

It is compatible with Zabbix version 4 and newer, however it does not support compression and only supports certificate based authentication.

Usage

Basic Agent

This sets up a basic agent with no encryption.

// This function is called for each incoming request from the Zabbix server
getItem := func(itemKey string) (interface{}, error) {
    if itemKey == "agent.ping" {
        return "1", nil
    } else if itemKey == "runtime.version" {
        return runtime.Version, nil
    }

    // Returning nil, nil means the itemKey was unknown
    return nil, nil
}

// This will block
zbx.Start(getItem, "0.0.0.0:10050")
Agent with TLS

This sets up a certificate-based TLS agent. This package doesn't support PSK-based TLS, as crypto/tls does not support this feature, yet.

// This function is called for each incoming request from the Zabbix server
getItem := func(itemKey string) (interface{}, error) {
    if itemKey == "agent.ping" {
        return "1", nil
    } else if itemKey == "runtime.version" {
        return runtime.Version, nil
    }

    // Returning nil, nil means the itemKey was unknown
    return nil, nil
}

// Load the certificate and key that the zabbix agent will use for incoming connections
// from the Zabbix server
cert, err := tls.LoadX509KeyPair("zabbix.crt", "zabbix.key")
if err != nil {
    panic(err)
}

// This will block
zbx.StartTLS(getItem, "0.0.0.0:10050", cert)

Documentation

Overview

Package zbx is a Zabbix Agent implementation in golang that allows your application to act as a zabbix agent and respond to simple requests.

It is compatible with Zabbix version 4 and newer, however it does not support compression or TLS PSK authentication.

Example
package main

import (
	"runtime"

	"github.com/ecnepsnai/zbx"
)

func main() {
	// This function is called for each incoming request from the Zabbix server
	getItem := func(itemKey string) (interface{}, error) {
		if itemKey == "agent.ping" {
			return "1", nil
		} else if itemKey == "runtime.version" {
			return runtime.Version, nil
		}

		// Returning nil, nil means the itemKey was unknown
		return nil, nil
	}

	// This will block
	zbx.Start(getItem, "0.0.0.0:10050")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrorLog io.Writer = os.Stderr

ErrorLog is the writer that error messages are written to. By default this is stderr.

Functions

func Start

func Start(itemFunc ItemFunc, address string) error

Start the Zabbix agent on the specified address. Will block and always return on error. Will panic if itemFunc is nil.

Example
package main

import (
	"runtime"

	"github.com/ecnepsnai/zbx"
)

func main() {
	// This function is called for each incoming request from the Zabbix server
	getItem := func(itemKey string) (interface{}, error) {
		if itemKey == "agent.ping" {
			return "1", nil
		} else if itemKey == "runtime.version" {
			return runtime.Version, nil
		}

		// Returning nil, nil means the itemKey was unknown
		return nil, nil
	}

	// This will block
	zbx.Start(getItem, "0.0.0.0:10050")
}
Output:

func StartListener added in v1.1.4

func StartListener(itemFunc ItemFunc, l net.Listener)

Start the Zabbix agent on the specified listener.

func StartTLS added in v1.1.4

func StartTLS(itemFunc ItemFunc, address string, certificate tls.Certificate) error

StartTLS will start the Zabbix agent on the specified address with TLS. The agent will present the given certificate to the server when connected. Will panic if itemFunc is nil.

Example
package main

import (
	"crypto/tls"
	"runtime"

	"github.com/ecnepsnai/zbx"
)

func main() {
	// This function is called for each incoming request from the Zabbix server
	getItem := func(itemKey string) (interface{}, error) {
		if itemKey == "agent.ping" {
			return "1", nil
		} else if itemKey == "runtime.version" {
			return runtime.Version, nil
		}

		// Returning nil, nil means the itemKey was unknown
		return nil, nil
	}

	// Load the certificate and key that the zabbix agent will use for incoming connections
	// from the Zabbix server
	cert, err := tls.LoadX509KeyPair("zabbix.crt", "zabbix.key")
	if err != nil {
		panic(err)
	}

	// This will block
	zbx.StartTLS(getItem, "0.0.0.0:10050", cert)
}
Output:

Types

type ItemFunc added in v1.1.0

type ItemFunc func(key string) (interface{}, error)

ItemFunc describes the method invoked when the Zabbix Server (or proxy) is requesting an item from this agent. The returned interface be encoded as a string and returned to the server.

If error is not nil, it will be sent back to the server. If (nil, nil) is returned then it is assumed the key is unknown.

Any calls to `panic()` will be recovered from and written to ErrorLog and the server will act as if the key was unknown.

Directories

Path Synopsis
cmd
zabbix-query
Command zabbix-query provides a simply utility to return a item value from a running zabbix agent.
Command zabbix-query provides a simply utility to return a item value from a running zabbix agent.

Jump to

Keyboard shortcuts

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