feat: 初步集成 zabbix agent

This commit is contained in:
2024-08-22 16:48:04 +08:00
parent e148d8aeda
commit dc53b3aaf8
12 changed files with 289 additions and 19 deletions

View File

@ -0,0 +1,43 @@
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)
}

View File

@ -0,0 +1,102 @@
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
}