Zabbix Agent 2在尝试处理官方指南中的测试插件时会报错。

huangapple go评论174阅读模式
英文:

Zabbix Agent 2 throws an error when it tries to process the test plugin from the official instructions

问题

我开始探索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。


我按照以下说明进行了操作:
1)创建了一个目录/home/ubuntu/myip
2)创建了一个文件main.go
3)将指导中的代码粘贴到文件中

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "git.zabbix.com/ap/plugin-support/plugin/container"
    "git.zabbix.com/ap/plugin-support/plugin"
)

// 插件必须定义结构并嵌入plugin.Base结构。
type Plugin struct {
    plugin.Base
}

// 创建定义的插件结构的新实例
var impl Plugin

// 插件必须实现一个或多个插件接口。
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
    // 您可以使用Critf、Errf、Infof、Warningf、Debugf、Tracef函数之一进行日志记录。
    p.Infof("received request to handle %s key with %d parameters", key, len(params))

    // 从指定的URL获取响应,它应该只是IP地址。
    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() {
    // 注册我们的指标,指定插件和指标详细信息。
    // 1 - 指向插件实现的指针
    // 2 - 插件名称
    // 3 - 指标名称(项键)
    // 4 - 指标描述
    //
    // 注意!指标描述必须以句号结尾,否则Zabbix Agent 2将返回错误并且无法启动!
    // 指标名称(项键)和指标描述可以在循环中重复以注册其他指标。
    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()))
    }
}

4)go mod init example.test/myip
5)go mod tidy
6)go build
7)我创建了一个名为myip.conf的文件,其中包含插件可执行文件的路径,并将其放在目录/etc/zabbix/zabbix_agent2.d/plugins.d
8)运行命令zabbix_agent2 -t myip

然后...它不起作用,并抛出关于错误解析代理版本的错误。

我认为strconv.Atoi在Zabbix Agent 2的代码中可能被错误处理了,但是在使用代码编辑器查看整个项目后,我找不到任何值得注意的地方。

另外,奇怪的是Zabbix Agent的版本是6.0.14,而6.0.13是插件通信协议的版本。我不明白为什么它试图将协议版本作为代理版本。

所以,如果你对这个问题有任何想法,请告诉我。提前谢谢你。

英文:

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:

  1. created a directory /home/ubuntu/myip
  2. created a file main.go
  3. pasted the code from the instruction
       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()))
}
}
  1. go mod init example.test/myip
  2. go mod tidy
  3. go build
  4. I created the file myip.conf with the path to the plugin executable file and put it in the directory /etc/zabbix/zabbix_agent2.d/plugins.d
  5. and launch command 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.

答案1

得分: 0

我找到了一个解决方案!(实际上是我的同事找到的,但不是重点)

问题出在文件src/go/plugins/external/broker.go上。在这个文件中,多次更改了请求结构中的记录逻辑。在'22年夏天,他们改变了Zabbix Agent版本属性的方式(通过strconv.Atoi将其从字符串解析为整数)。

但在'23年1月,他们删除了代理版本的属性,通过strconv.Atoi进行解析,并添加了协议版本的属性。这就是为什么它试图将协议版本作为项目版本传递的原因。

Plugin Support包中的plugin/container/handler.go文件中的checkVersion方法也已更改为检查协议版本。

所以,问题出在新的Zabbix Agent 2和旧的Plugin Support包上。

如果你使用Zabbix Agent 2的6.4版本和git.zabbix.com/ap/plugin-support/plugin的1.2.2版本,一切都正常工作!

英文:

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!

huangapple
  • 本文由 发表于 2023年3月10日 21:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75696538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定