Built-in plugins are located in the plugin directory tree, grouped by meaning, for example plugins/system/uptime/uptime.go
.
A built-in plugin for Zabbix agent 2 is a plugin that is compiled directly into the agent. In order to use it, it is required to have Zabbix sources and to recompile Zabbix agent 2.
The entire plugin building process includes four stages. During the first stage Go code is installed, then Zabbix sources are installed and Go environment is configured, after that plugin support packages are installed, and finally the environment for the installation is prepared, which includes agent recompiling.
See the instructions given below.
Stage 1
At first, create a built-in plugin that implements one metric called myip, which returns the external IP address of the host where the agent is running. It will be done by performing an HTTP request to https://api.ipify.org, which returns the IP address of the client connected.
One more Go package is required: "git.zabbix.com/ap/plugin-support/plugin", which is already available in Zabbix source code, and can be downloaded together with Zabbix agent 2 compilation. All the other required package imports come along with Go.
Stage 2
git clone https://git.zabbix.com/scm/zbx/zabbix.git --branch 6.2.2 --depth 1 --single-branch zabbix-6.2.2
Any Zabbix version starting from 5.0.0 and higher can be used to extract the sources for Zabbix agent 2. However, it should be noted that the different versions may require different steps of implementation.
At this step, it is important to make sure that Zabbix agent 2 is compiled as it is (without any changes implemented), because later it will be recompiled again (already with the new changes implemented). Thus, if some errors arose during the further steps, this would assure they were due to the errors in the code, and not due to some missing dependencies.
// This is the source code of plugin Myip. It implements 1 metric
// called myip, which returns the external IP address of the host where
// Zabbix agent 2 is running.
package myip
// Packages we will use.
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
// Plugin must define structure and embed plugin.Base structure.
type Plugin struct {
plugin.Base
}
// Create a new instance of the defined plugin structure
var impl Plugin
// Plugin must implement one or several plugin interfaces.
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
// You may use one of Critf, Errf, Infof, Warningf, Debugf, Tracef functions for logging.
p.Infof("received request to handle %s key with %d parameters", key, len(params))
// Fetch response from the specified URL, it should be just the IP address.
resp, err := http.Get("https://api.ipify.org")
if err != nil {
// Plugin will return an error response if the request failed
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// Plugin will return an error response if it failed to read the response
return nil, err
}
return string(body), nil
}
func init() {
// Register our metric, specifying the plugin and metric details.
// 1 - a pointer to plugin implementation
// 2 - plugin name
// 3 - metric name (item key)
// 4 - metric description
//
// NB! The metric description must end with a period, otherwise the Zabbix agent 2 will return an error and won't start!
// Metric name (item key) and metric description can be repeated in a loop to register additional metrics.
plugin.RegisterMetrics(&impl, "Myip", "myip", "Return the external IP address of the host where agent is running.")
}
Stage 3
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
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 third and fourth parameters can be input multiple times in a row to register multiple metrics.
func init() {
plugin.RegisterMetrics(&impl, "Myip", "myip", "Return the external IP address of the host where agent is running.")
}
The metric description must end with a period, otherwise the agent will not start!
Stage 4
src/go/plugins/plugins_linux.go
, and add your plugin "zabbix.com/plugins/myip"
to the end of the existing import list:import (
_ "zabbix.com/plugins/log"
_ "zabbix.com/plugins/systemrun"
_ "zabbix.com/plugins/zabbix/async"
_ "zabbix.com/plugins/zabbix/stats"
_ "zabbix.com/plugins/zabbix/sync"
_ "zabbix.com/plugins/myip"
)
Two other suitable plugins are also available:plugins_windows.go and plugins_darwin.go. However, note that Zabbix agent 2 is not currently officially supported on darwin.
As a result, the external IP address of your host should be displayed.