diff --git a/integration/zabbixagent/plugin/handler/handler.go b/integration/zabbixagent/plugin/handler/handler.go deleted file mode 100644 index 8bdd124..0000000 --- a/integration/zabbixagent/plugin/handler/handler.go +++ /dev/null @@ -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) -} diff --git a/integration/zabbixagent/plugin/plguin.go b/integration/zabbixagent/plugin/plguin.go deleted file mode 100644 index 119a8fd..0000000 --- a/integration/zabbixagent/plugin/plguin.go +++ /dev/null @@ -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 -} diff --git a/integration/zabbixagent/plugin/plugin.go b/integration/zabbixagent/plugin/plugin.go new file mode 100644 index 0000000..4779fb0 --- /dev/null +++ b/integration/zabbixagent/plugin/plugin.go @@ -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 +} diff --git a/main.go b/main.go index 7121d4e..63ed0f4 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,9 @@ import ( ) func main() { + /** + * Load config + */ if err := config.LoadConfig(); err != nil { log.Fatalf("Error loading config: %v", err) }