首頁 > 後端開發 > Golang > 主體

Zabbix Agent 2 在嘗試根據官方說明處理測試插件時拋出錯誤

PHPz
發布: 2024-02-11 08:54:17
轉載
860 人瀏覽過

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 在嘗試根據官方說明處理測試插件時拋出錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!