This is a step-by-step tutorial to create a simple loadable plugin for Zabbix agent 2.
During this tutorial, you will add a new loadable plugin MyIP. The plugin will implement 1 metric called myip, which returns the external IP address of the host where Zabbix agent 2 is running.
In this section you will learn how to write the plugin that adds a new metric to Zabbix agent 2.
Create a new directory myip in /usr/local/zabbix/go/plugins/.
Create the file main.go inside myip directory and define the name of your Go package.
/usr/local/zabbix/go/plugins/myip/main.go
Keep the file open to add more lines as described in the next steps.
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error)
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
}
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
resp, err := http.Get("https://api.ipify.org")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return string(body), nil
}
The plugin.RegisterMetrics method parameter description:
To register several metrics, repeat the parameters metric name and description for each metric.
For example: plugin.RegisterMetrics(&impl, "Myip", "metric.one", "Metric one description.", "metric.two", "Metric two description.")
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
resp, err := http.Get("https://api.ipify.org")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return string(body), nil
}
func init() {
plugin.RegisterMetrics(&impl, "Myip", "myip", "Return the external IP address of the host where agent is running.")
}
Defining the main() function is mandatory.
/usr/local/zabbix/go/plugins/myip/main.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
resp, err := http.Get("https://api.ipify.org")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return string(body), nil
}
func init() {
plugin.RegisterMetrics(&impl, "Myip", "myip", "Return the external IP address of the host where agent is running.")
}
func main() {
h, err := container.NewHandler(impl.Name())
if err != nil {
panic(fmt.Sprintf("failed to create plugin handler %s", err.Error()))
}
impl.Logger = &h
err = h.Execute()
if err != nil {
panic(fmt.Sprintf("failed to execute plugin handler %s", err.Error()))
}
}
In this section you will learn how to compile the plugin.
go mod init myip
GOPROXY=direct go get git.zabbix.com/ap/plugin-support/plugin@branchname
go mod tidy
go build
Make sure to specify the correct branch name, i.e. replace branchname
(see Line 2) with one of the following:
release/*
- for the stable release branch, where "*" is the release version (i.e. 6.4
)master
- for the master branch<commit hash>
- for the specific commit version (use the specific commit hash)The output should be similar to this:
go: creating new go.mod: module myip
go: to add module requirements and sums:
go mod tidy
go: finding module for package git.zabbix.com/ap/plugin-support/plugin/container
go: finding module for package git.zabbix.com/ap/plugin-support/plugin
go: found git.zabbix.com/ap/plugin-support/plugin in git.zabbix.com/ap/plugin-support v0.0.0-20220608100211-35b8bffd7ad0
go: found git.zabbix.com/ap/plugin-support/plugin/container in git.zabbix.com/ap/plugin-support v0.0.0-20220608100211-35b8bffd7ad0
Create an executable myip for the loadable plugin.
Specify the path to the plugin configuration file in the Plugins.Myip.System.Path parameter of Zabbix agent 2 configuration file.
Plugin name in the configuration parameter name (Myip in this tutorial) must match the plugin name defined in the plugin.RegisterMetrics() function.
echo 'Plugins.Myip.System.Path=/usr/local/zabbix/go/plugins/myip/myip' > /etc/zabbix_agent2.d/plugins.d/myip.conf
The response should contain an external IP address of your host.
In case of an error, check whether the user zabbix has permissions to access /usr/local/zabbix/go/plugins/myip directory.