feat: 初步集成 zabbix agent
This commit is contained in:
63
integration/zabbixagent/main.go
Normal file
63
integration/zabbixagent/main.go
Normal file
@ -0,0 +1,63 @@
|
||||
package zabbixagent
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"onvif-agent/config"
|
||||
"onvif-agent/integration/zabbixagent/plugin"
|
||||
"os"
|
||||
|
||||
"golang.zabbix.com/sdk/plugin/flag"
|
||||
"golang.zabbix.com/sdk/zbxerr"
|
||||
)
|
||||
|
||||
const copyrightMessage = //
|
||||
`Copyright 2001-%d imbytecat
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
||||
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
`
|
||||
const (
|
||||
pluginVersionMajor = 1
|
||||
pluginVersionMinor = 0
|
||||
pluginVersionPatch = 0
|
||||
pluginVersionRC = "alpha1"
|
||||
pluginLicenseYear = 2024
|
||||
)
|
||||
|
||||
func Run() error {
|
||||
err := flag.HandleFlags(
|
||||
config.Conf.Integrations.ZabbixAgent.Plugin.Name,
|
||||
os.Args[0],
|
||||
fmt.Sprintf(copyrightMessage, pluginLicenseYear),
|
||||
pluginVersionRC,
|
||||
pluginVersionMajor,
|
||||
pluginVersionMinor,
|
||||
pluginVersionPatch,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, zbxerr.ErrorOSExitZero) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
err = plugin.Launch()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
43
integration/zabbixagent/plugin/handler/handler.go
Normal file
43
integration/zabbixagent/plugin/handler/handler.go
Normal 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)
|
||||
}
|
102
integration/zabbixagent/plugin/plguin.go
Normal file
102
integration/zabbixagent/plugin/plguin.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user