iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Zabbix Agent 2 在尝试根据官方说明处理测试插件时抛出错误
  • 848
分享到

Zabbix Agent 2 在尝试根据官方说明处理测试插件时抛出错误

字符串解析 2024-02-11 08:02:45 848人浏览 八月长安
摘要

PHP小编柚子今天要和大家分享关于Zabbix Agent 2的一则问题。在尝试根据官方说明处理测试插件时,一位用户遇到了错误的抛出问题。这个问题可能会影响插件的正常运行,因此我们将会

PHP小编柚子今天要和大家分享关于Zabbix Agent 2的一则问题。在尝试根据官方说明处理测试插件时,一位用户遇到了错误的抛出问题。这个问题可能会影响插件的正常运行,因此我们将会探讨解决这个问题的方法。让我们一起来看看吧!

问题内容

我开始探索 zabbix agent 2 的可能性,并决定按照官方插件创建指南中的描述逐步创建一个测试插件。

在我完成所有步骤之后,zabbix agent 不想执行任何操作(除了 -h 选项)并给出以下错误:

zabbix_agent2 [10046]:错误:无法注册插件:无法解析代理版本strconv.atoi:解析“6.0.13”:语法无效

我在 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 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. 整理模组
  3. 开始构建
  4. 我使用插件可执行文件的路径创建了文件 myip.conf 并将其放入目录 /etc/zabbix/zabbix_agent2.d/plugins.d
  5. 并启动命令 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 进行解析,并添加了协议版本的属性。这就是为什么它试图将协议版本冒充为项目版本。

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

所以,问题出在新的 Zabbix Agent 2 和旧的插件支持包上。

如果您对 Zabbix Agent 2 使用版本 6.4,对 git.zabbix.com/ap/plugin-support/plugin 使用版本 1.2.2,则一切正常!

以上就是Zabbix Agent 2 在尝试根据官方说明处理测试插件时抛出错误的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: Zabbix Agent 2 在尝试根据官方说明处理测试插件时抛出错误

本文链接: https://www.lsjlt.com/news/563379.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作