我开始探索Zabbix agent 2的可能性,并决定按照官方插件创建指南中的描述一步步创建一个测试插件。
在我完成所有步骤后,Zabbix Agent 不想做任何事情(除了 -h 选项)并给出以下错误:
zabbix_agent2 [10046]: ERROR: cannot register plugins: failed to parse agent version strconv.Atoi: parsing "6.0.13": invalid syntax
我在 Ubuntu 22.04 上做所有这些。
Zabbix Agent 2版本:6.0.14.
Go版本:go1.18.1 linux/amd64
我只通过 apt-get 安装了 Zabbix Agent 2。
我按照说明做了一切:
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
而且......它不起作用并抛出错误解析代理版本。
我以为
strconv.Atoi
在 Zabbix Agent 2 本身的代码中以某种方式处理不正确,但是在使用代码编辑器查看整个项目后,我找不到任何值得注意的地方。
另外,奇怪的是Zabbix Agent版本是6.0.14,6.0.13是插件通讯协议版本。我不明白为什么它试图将协议版本作为代理版本。
所以,如果你对这个问题有任何想法,我请你说出来。提前谢谢你。
我找到了解决办法! (嗯,其实我的同事发现了这个,但不是重点)
原因是文件
src/go/plugins/external/broker.go
。在这个文件中,多次更改了请求结构中记录的逻辑。在 22 年夏天,他们改变了 Zabbix Agent 版本属性的方式(它通过 strconv.Atoi
从字符串解析为整数)。
但是在 23 年 1 月,他们删除了代理版本的属性,通过
strconv.Atoi
解析并添加了协议版本的属性。这就是它试图将协议版本作为项目版本的原因。
checkVersion
包的plugin/container/handler.go
文件中的Plugin Support
方法也更改为检查协议版本。
所以,问题出在新的 Zabbix Agent 2 和旧的插件支持包上。
如果您对 Zabbix Agent 2 使用 6.4 版,对
git.zabbix.com/ap/plugin-support/plugin
使用 1.2.2 版,一切正常!