I started to explore the possibilities of Zabbix agent 2 and decided to create a test plugin step by step as described in the official plugin creation guide.
After all the steps I have gone through, Zabbix Agent does not want to do anything (except for the -h option) and gives the following error:
zabbix_agent2 [10046]: ERROR: cannot register plugins: failed to parse agent version strconv.Atoi: parsing "6.0.13": invalid syntax
I do all this on Ubuntu 22.04.
Version of Zabbix Agent 2: 6.0.14.
Go version: go1.18.1 linux/amd64
I only have Zabbix Agent 2 installed via apt-get.
I did everything according to the instructions:
package main
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.")
}
// This is the main function, it is required to compile the plugin.
// By default the function implements our packages to handle the plugin creation and execution.
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()))
}
}
/etc/zabbix/zabbix_agent2.d/plugins.d
zabbix_agent2 -t myip
And... It dosn't work and throwing error about bad parsing agent version.
I thought that strconv.Atoi
was somehow handled incorrectly in the code of the Zabbix Agent 2 itself, but after looking through the whole project with the code editor I couldn't find anything remarkable.
Also, the strange thing is that the Zabbix Agent version is 6.0.14 and 6.0.13 is the plugin communication protocol version. I don't understand why it tries to pass off the protocol version as the agent version.
So, if you have any thoughts on this problem, I ask you to voice them. Thank you in advance.
I found a solution! (Well, actually my work colleague found this, but not the point)
The reason was the file src/go/plugins/external/broker.go
. In this file several times changed the logic of the record in the structure of the request. In the summer of '22 they changed the way the Zabbix Agent version property (it became parsed from string to integer via strconv.Atoi
).
But in January '23 they removed the property for the agent version, parsing by strconv.Atoi
and added a property for the protocol version. That's why it tried to pass off the protocol version as the project version.
The checkVersion
method in the plugin/container/handler.go
file of the Plugin Support
package has also been changed to check the protocol version.
So, the problem was the new Zabbix Agent 2 and the old Plugin Support package.
If you use version 6.4 for Zabbix Agent 2 and version 1.2.2 for git.zabbix.com/ap/plugin-support/plugin
, everything works fine!