Zabbix Agent 2 在按官方说明处理测试插件时发生异常
来源:stackoverflow
时间:2024-02-19 18:15:26 239浏览 收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Zabbix Agent 2 在按官方说明处理测试插件时发生异常》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
我开始探索 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。
我按照说明做了一切:
- 创建了目录 /home/ubuntu/myip
- 创建了一个文件 main.go
- 粘贴了指令中的代码
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())) } }
- go mod init example.test/myip
- 整理模组
- 开始构建
- 我使用插件可执行文件的路径创建了文件 myip.conf 并将其放入目录
/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
进行解析,并添加了协议版本的属性。这就是为什么它试图将协议版本冒充为项目版本。
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 在按官方说明处理测试插件时发生异常》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习