Documentation
¶
Overview ¶
Package g2z provides Go bindings for creating a native Zabbix module.
An example module is provided with the g2z sources (in dummy/dummy.go) which is a Go implementation of the dummy C module published by Zabbix.
To build a shared library compatible with Zabbix v2.2.0+, this project takes advantage of the c-shared build mode introduced in Go v1.5.0.
package main import ( "github.com/cavaliercoder/g2z" "strings" ) // mandatory library entry point, although it is never called. func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } // mandatory initialization function func init() { g2z.RegisterStringItem("go.echo", "Hello world!", Echo) } // handler for 'go.echo' item func Echo(request *g2z.AgentRequest) (string, error) { return strings.Join(request.Params, " "), nil }
Compile your shared library with:
$ go build -buildmode=c-shared
You should see the required Zabbix module functions exported in your shared library
$ nm -g ... 0000000000111830 T zbx_module_api_version 0000000000111860 T zbx_module_init 00000000001118f0 T zbx_module_item_list 00000000001118c0 T zbx_module_item_timeout 0000000000111890 T zbx_module_uninit
Load your `.so` module file into Zabbix as per the Zabbix manual
https://www.zabbix.com/documentation/2.2/manual/config/items/loadablemodules#configuration_parameters
Check that your module items are loaded with:
$ zabbix_agentd -t go.echo[hello,world]
For more thorough performance testing, use zabbix_agent_bench
https://github.com/cavaliercoder/zabbix_agent_bench
Index ¶
- Variables
- func LogCriticalf(format string, a ...interface{})
- func LogDebugf(format string, a ...interface{})
- func LogErrorf(format string, a ...interface{})
- func LogInfof(format string, a ...interface{})
- func LogWarningf(format string, a ...interface{})
- func RegisterDiscoveryItem(key string, testParams string, handler DiscoveryItemHandlerFunc)
- func RegisterDoubleItem(key string, testParams string, handler DoubleItemHandlerFunc)
- func RegisterInitHandler(handler InitHandlerFunc)
- func RegisterStringItem(key string, testParams string, handler StringItemHandlerFunc)
- func RegisterUint64Item(key string, testParams string, handler Uint64ItemHandlerFunc)
- func RegisterUninitHandler(handler UninitHandlerFunc)
- type AgentRequest
- type DiscoveryData
- type DiscoveryItem
- type DiscoveryItemHandlerFunc
- type DoubleItemHandlerFunc
- type InitHandlerFunc
- type StringItemHandlerFunc
- type Uint64ItemHandlerFunc
- type UninitHandlerFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Timeout int
Timeout is set by Zabbix (via zbx_module_item_timeout()) to the number of seconds that all registered item handlers should obey as a strict timeout.
This timeout is not enforced and should be voluntarily implemented.
Functions ¶
func LogCriticalf ¶
func LogCriticalf(format string, a ...interface{})
LogCriticalf formats according to a format specifier and writes to the Zabbix log file with a critical message.
func LogDebugf ¶
func LogDebugf(format string, a ...interface{})
LogDebugf formats according to a format specifier and writes to the Zabbix log file with a debug message.
func LogErrorf ¶
func LogErrorf(format string, a ...interface{})
LogErrorf formats according to a format specifier and writes to the Zabbix log file with an error message.
func LogInfof ¶
func LogInfof(format string, a ...interface{})
LogInfof formats according to a format specifier and writes to the Zabbix log file with an informational message.
func LogWarningf ¶
func LogWarningf(format string, a ...interface{})
LogWarningf formats according to a format specifier and writes to the Zabbix log file with a warning message.
func RegisterDiscoveryItem ¶
func RegisterDiscoveryItem(key string, testParams string, handler DiscoveryItemHandlerFunc)
RegisterDiscoveryItem registers an agent item key, its test parameters and a handler function with Zabbix for a key which returns DiscoveryData.
This function should only be called from `init()`
Example ¶
package main import ( "fmt" "github.com/cavaliercoder/g2z" ) func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } func init() { g2z.RegisterDiscoveryItem("go.discovery", "", Discover) } func Discover(request *g2z.AgentRequest) (g2z.DiscoveryData, error) { d := make(g2z.DiscoveryData, 5) for i := 0; i < 5; i++ { d[i] = g2z.DiscoveryItem{ "index": fmt.Sprintf("%d", i), } } return d, nil }
Output:
func RegisterDoubleItem ¶
func RegisterDoubleItem(key string, testParams string, handler DoubleItemHandlerFunc)
RegisterDoubleItem registers an agent item key, its test parameters and a handler function with Zabbix for a key which returns a double precision floating point integer.
This function should only be called from `init()`
Example ¶
package main import ( "github.com/cavaliercoder/g2z" ) func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } func init() { g2z.RegisterDoubleItem("go.double", "", Double) } func Double(request *g2z.AgentRequest) (float64, error) { return 1.23456, nil }
Output:
func RegisterInitHandler ¶
func RegisterInitHandler(handler InitHandlerFunc)
RegisterInitHandler should be called from init() to register an InitHandlerFunc which will be called when Zabbix calls zbx_module_init().
Example ¶
package main import ( "github.com/cavaliercoder/g2z" ) func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } func init() { g2z.RegisterInitHandler(MyInitFunc) } func MyInitFunc() error { g2z.LogInfof("MyModule loaded") return nil }
Output:
func RegisterStringItem ¶
func RegisterStringItem(key string, testParams string, handler StringItemHandlerFunc)
RegisterStringItem registers an agent item key, its test parameters and a handler function with Zabbix for a key which returns a string.
This function should only be called from `init()`
Example ¶
package main import ( "github.com/cavaliercoder/g2z" "strings" ) func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } func init() { g2z.RegisterStringItem("go.echo", "Hello,world", Echo) } func Echo(request *g2z.AgentRequest) (string, error) { return strings.Join(request.Params, " "), nil }
Output:
func RegisterUint64Item ¶
func RegisterUint64Item(key string, testParams string, handler Uint64ItemHandlerFunc)
RegisterUint64Item registers an agent item key, its test parameters and a handler function with Zabbix for a key which returns an unsigned integer.
This function should only be called from `init()`
Example ¶
package main import ( "github.com/cavaliercoder/g2z" ) func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } func init() { g2z.RegisterUint64Item("go.ping", "", Ping) } func Ping(request *g2z.AgentRequest) (uint64, error) { return 1, nil }
Output:
func RegisterUninitHandler ¶
func RegisterUninitHandler(handler UninitHandlerFunc)
RegisterUninitHandler should be called from init() to register an UninitHandlerFunc which will be called when Zabbix calls zbx_module_uninit().
Example ¶
package main import ( "github.com/cavaliercoder/g2z" ) func main() { panic("THIS_SHOULD_NEVER_HAPPEN") } func init() { g2z.RegisterUninitHandler(MyUninitFunc) } func MyUninitFunc() error { g2z.LogInfof("MyModule unloaded") return nil }
Output:
Types ¶
type AgentRequest ¶
type AgentRequest struct { // Key is the requested agent key (E.g. `agent.version`). Key string // Params is a slice of strings containing each parameter passed in the agent request for the // specified key (E.g. `dummy.echo[<param0>,<param1>]`) Params []string }
A AgentRequest represents an agent item request received from a Zabbix server or perhaps from `zabbix_get`.
type DiscoveryData ¶
type DiscoveryData []DiscoveryItem
DiscoveryData is a slice of DiscoveryItems which is returned for registered discovery rules as a JSON encoded string.
func (DiscoveryData) Json ¶
func (c DiscoveryData) Json() string
Json converts a DiscoveryData struct into a JSON encoded string, compatible with Zabbix Low-Level discovery rules from v2.2.0 and above.
type DiscoveryItem ¶
DiscoveryItem is a map of key/value pairs that represents a single instance of a discovered asset.
type DiscoveryItemHandlerFunc ¶
type DiscoveryItemHandlerFunc func(*AgentRequest) (DiscoveryData, error)
DiscoveryItemHandlerFunc is the function signature for functions which may be registered as an item key handler and return JSON encoded low-level discovery data.
type DoubleItemHandlerFunc ¶
type DoubleItemHandlerFunc func(*AgentRequest) (float64, error)
DoubleItemHandlerFunc is the function signature for functions which may be registered as an item key handler and return a double precision floating point integer value.
type InitHandlerFunc ¶
type InitHandlerFunc func() error
InitHandlerFunc is the function signature for functions which may be registered for callback when a Zabbix agent module is loaded when zbx_module_init() is called.
type StringItemHandlerFunc ¶
type StringItemHandlerFunc func(*AgentRequest) (string, error)
StringItemHandlerFunc is the function signature for functions which may be registered as an item key handler and return a string value.
type Uint64ItemHandlerFunc ¶
type Uint64ItemHandlerFunc func(*AgentRequest) (uint64, error)
Uint64ItemHandlerFunc is the function signature for functions which may be registered as an item key handler and return an unsigned integer value.
type UninitHandlerFunc ¶
type UninitHandlerFunc func() error
UninitHandlerFunc is the function signature for functions which may be registered for callback when a Zabbix agent module is unloaded when zbx_module_uninit() is called.
Directories
¶
Path | Synopsis |
---|---|
Package main is a shared library with sample Zabbix bindings which may be loaded into a Zabbix agent or server using the `LoadModule` directive.
|
Package main is a shared library with sample Zabbix bindings which may be loaded into a Zabbix agent or server using the `LoadModule` directive. |