Este es un tutorial paso a paso para crear un complemento cargable simple para Zabbix agent 2.
Durante este tutorial agregarás un nuevo complemento cargable MyIP. El complemento implementará 1 métrica llamada myip que devuelve la dirección IP externa del host donde se ejecuta el agente Zabbix 2.
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.
Keep the file open to add more lines as described in the next steps.
/usr/local/zabbix/go/plugins/myip/main.go
/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:
The third and fourth parameters can be repeated multiple times in a row to register multiple metrics.
/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.")
}
/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()))
}
}
You should receive an output 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, which is the loadable plugin.
Specify the path to the plugin configuration file in Zabbix agent 2 configuration file. The path should be set in the Plugins.<PluginName>.System.Path parameter. Replace <PluginName> with the name of the plugin, defined in the code when calling plugin.RegisterMetrics() function (for this tutorial, Plugins.Myip.System.Path).
echo 'Plugins.Myip.System.Path=/usr/local/zabbix/go/plugins/myip/myip' > /etc/zabbix_agent2.d/plugins.d/myip.conf
The response should contain the external IP address of your host.
In case of an error, first make sure that the user zabbix has permissions to access /usr/local/zabbix/go/plugins/myip
directory.