这是为 Zabbix agent 2 创建一个简单可加载插件的分步教程。
您可以随意使用我们的示例仓库作为创建您自己插件的模板或指南。
在本教程中,您将添加一个新的可加载插件 MyIP。该插件将实现一个名为 myip 的指标, 该指标返回运行 Zabbix agent2 的主机的外部 IP 地址。
在本节中,您将学习如何编写向 Zabbix Agent 2 添加新指标的插件 1.在/usr/local/zabbix/go/plugins/目录新建一个myip 目录。 2. 在 myip 目录中创建文件 main.go 并定义您的 Go 包的名称。 /usr/local/zabbix/go/plugins/myip/main.go
保持文件打开以按照后续步骤添加更多行。
这些是插件所依赖的包 /usr/local/zabbix/go/plugins/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.Base结构以访问标准插件功能。 /usr/local/zabbix/go/plugins/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"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error)
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
}
/usr/local/zabbix/go/plugins/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"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
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
}
要注册多个指标,请为每个指标重复参数 metric name 和 description。例如:plugin.RegisterMetrics(&impl, "Myip", "metric.one", "Metric one description.", "metric.two", "Metric two description.")
*/usr/local/zabbix/go/plugins/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"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
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() {
plugin.RegisterMetrics(&impl, "Myip", "myip", "Return the external IP address of the host where agent is running.")
}
package main
import (
"fmt"
"io/ioutil"
"net/http"
"git.zabbix.com/ap/plugin-support/plugin/container"
"git.zabbix.com/ap/plugin-support/plugin"
)
type Plugin struct {
plugin.Base
}
var impl Plugin
func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error){
p.Infof("received request to handle %s key with %d parameters", key, len(params))
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() {
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()))
}
}
在本节中,您将学习如何编译插件。
go mod init myip
GOPROXY=direct go get git.zabbix.com/ap/plugin-support/plugin@branchname
go mod tidy
go build
确保指定正确的分支名称,即用以下之一替换 branchname
(参见第 2 行):
release/*
- 表示稳定发布分支,其中“*”表示发布版本(即 7.0
)master
- 表示 master 分支<commit hash>
- 表示特定提交版本(使用特定提交哈希)输出应类似于以下内容:
go:创建新的 go.mod:模块 myip
go:添加模块要求和总和:
go mod tidy
go:查找软件包 git.zabbix.com/ap/plugin-support/plugin/container 的模块
go:查找软件包的模块git.zabbix.com/ap/plugin-support/plugin
go: 在 git.zabbix.com/ap/plugin-support v0.0.0-20220608100211-35b8bffd7ad0 中找到 git.zabbix.com/ap/plugin-support/plugin
go: 在 git.zabbix.com/ap/plugin-support v0.0.0-20220608100211-35b8bffd7ad0 中找到 git.zabbix.com/ap/plugin-support/plugin/container
为可加载插件创建可执行文件 myip。
在 Zabbix agent2 配置文件的 Plugins.Myip.System.Path 参数中指定插件配置文件的路径。
配置参数名称中的插件名称(本教程中为 Myip)必须与 plugin.RegisterMetrics() 函数中定义的插件名称匹配。
echo 'Plugins.Myip.System.Path=/usr/local/zabbix/go/plugins/myip/myip' > /etc/zabbix_agent2.d/plugins.d/myip.conf
响应应包含主机的外部 IP 地址。
如果出现错误,请检查用户 zabbix 是否有权访问 /usr/local/zabbix/go/plugins/myip 目录。