refactor: 重写 zabbix agent 集成为 http 请求
This commit is contained in:
parent
dc53b3aaf8
commit
bdc56bbce5
@ -1,43 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// HandlerFunc describes the signature all metric handler functions must have.
|
|
||||||
type HandlerFunc func(
|
|
||||||
ctx context.Context,
|
|
||||||
metricParams map[string]string,
|
|
||||||
extraParams ...string,
|
|
||||||
) (any, error)
|
|
||||||
|
|
||||||
// Handler hold client and syscall implementation for request functions.
|
|
||||||
type Handler struct {
|
|
||||||
client *http.Client
|
|
||||||
sysCalls systemCalls
|
|
||||||
}
|
|
||||||
|
|
||||||
type systemCalls interface {
|
|
||||||
environ() []string
|
|
||||||
lookupEnv(key string) (string, bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
type osWrapper struct{}
|
|
||||||
|
|
||||||
// New creates a new handler with initialized clients for system and tcp calls.
|
|
||||||
func New() *Handler {
|
|
||||||
return &Handler{
|
|
||||||
client: http.DefaultClient,
|
|
||||||
sysCalls: osWrapper{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (osWrapper) environ() []string {
|
|
||||||
return os.Environ()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (osWrapper) lookupEnv(key string) (string, bool) {
|
|
||||||
return os.LookupEnv(key)
|
|
||||||
}
|
|
@ -1,102 +0,0 @@
|
|||||||
package plugin
|
|
||||||
|
|
||||||
import (
|
|
||||||
"golang.zabbix.com/sdk/errs"
|
|
||||||
"golang.zabbix.com/sdk/metric"
|
|
||||||
"golang.zabbix.com/sdk/plugin"
|
|
||||||
"golang.zabbix.com/sdk/plugin/container"
|
|
||||||
"onvif-agent/config"
|
|
||||||
"onvif-agent/integration/zabbixagent/plugin/handler"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
Name = config.Conf.Integrations.ZabbixAgent.Plugin.Name
|
|
||||||
)
|
|
||||||
|
|
||||||
type onvifMetricKey string
|
|
||||||
|
|
||||||
type onvifMetric struct {
|
|
||||||
metric *metric.Metric
|
|
||||||
handler handler.HandlerFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
type onvifPlugin struct {
|
|
||||||
plugin.Base
|
|
||||||
metrics map[onvifMetricKey]*onvifMetric
|
|
||||||
}
|
|
||||||
|
|
||||||
// Launch launches the plugin. Blocks until plugin execution has finished.
|
|
||||||
func Launch() error {
|
|
||||||
p := &onvifPlugin{}
|
|
||||||
|
|
||||||
err := p.registerMetrics()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
h, err := container.NewHandler(Name)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err, "failed to create new handler")
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Logger = &h
|
|
||||||
|
|
||||||
err = h.Execute()
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err, "failed to execute plugin handler")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start starts the example plugin. Is required for plugin to match runner interface.
|
|
||||||
func (p *onvifPlugin) Start() {
|
|
||||||
p.Logger.Infof("Start called")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop stops the example plugin. Is required for plugin to match runner interface.
|
|
||||||
func (p *onvifPlugin) Stop() {
|
|
||||||
p.Logger.Infof("Stop called")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *onvifPlugin) registerMetrics() error {
|
|
||||||
//h := handler.New()
|
|
||||||
|
|
||||||
p.metrics = map[onvifMetricKey]*onvifMetric{
|
|
||||||
//myIPMetric: {
|
|
||||||
// metric: metric.New(
|
|
||||||
// "Returns the availability groups.",
|
|
||||||
// params.Params,
|
|
||||||
// false,
|
|
||||||
// ),
|
|
||||||
// handler: handler.WithJSONResponse(
|
|
||||||
// handler.WithCredentialValidation(
|
|
||||||
// h.MyIP,
|
|
||||||
// ),
|
|
||||||
// ),
|
|
||||||
//},
|
|
||||||
//goEnvMetric: {
|
|
||||||
// metric: metric.New(
|
|
||||||
// "Returns the result rows of a custom query.",
|
|
||||||
// params.Params,
|
|
||||||
// true,
|
|
||||||
// ),
|
|
||||||
// handler: handler.WithJSONResponse(
|
|
||||||
// handler.WithCredentialValidation(handler.GoEnvironment),
|
|
||||||
// ),
|
|
||||||
//},
|
|
||||||
}
|
|
||||||
|
|
||||||
metricSet := metric.MetricSet{}
|
|
||||||
|
|
||||||
for k, m := range p.metrics {
|
|
||||||
metricSet[string(k)] = m.metric
|
|
||||||
}
|
|
||||||
|
|
||||||
err := plugin.RegisterMetrics(p, Name, metricSet.List()...)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err, "failed to register metrics")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
62
integration/zabbixagent/plugin/plugin.go
Normal file
62
integration/zabbixagent/plugin/plugin.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.zabbix.com/sdk/errs"
|
||||||
|
"golang.zabbix.com/sdk/metric"
|
||||||
|
"golang.zabbix.com/sdk/plugin"
|
||||||
|
"golang.zabbix.com/sdk/plugin/container"
|
||||||
|
"onvif-agent/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
PluginName = config.Conf.Integrations.ZabbixAgent.Plugin.Name
|
||||||
|
)
|
||||||
|
|
||||||
|
type zabbixAgentPlugin struct {
|
||||||
|
plugin.Base
|
||||||
|
}
|
||||||
|
|
||||||
|
// Launch launches the plugin. Blocks until plugin execution has finished.
|
||||||
|
func Launch() error {
|
||||||
|
p := &zabbixAgentPlugin{}
|
||||||
|
|
||||||
|
err := p.registerMetrics()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
h, err := container.NewHandler(PluginName)
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err, "failed to create new handler")
|
||||||
|
}
|
||||||
|
|
||||||
|
p.Logger = &h
|
||||||
|
|
||||||
|
err = h.Execute()
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err, "failed to execute plugin handler")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *zabbixAgentPlugin) registerMetrics() error {
|
||||||
|
metricSet := metric.MetricSet{
|
||||||
|
"onvif.client": metric.New(
|
||||||
|
"ONVIF client",
|
||||||
|
[]*metric.Param{
|
||||||
|
metric.NewParam("method", "HTTP method"),
|
||||||
|
metric.NewParam("url", "URL"),
|
||||||
|
metric.NewParam("data", "Request data"),
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := plugin.RegisterMetrics(p, PluginName, metricSet.List()...)
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err, "failed to register metrics")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user