refactor(zabbix): 使 zabbix agent 插件集成独立化
This commit is contained in:
parent
c6803285fe
commit
a5f6c3b1d9
1
.gitignore
vendored
1
.gitignore
vendored
@ -170,6 +170,7 @@ Temporary Items
|
||||
.apdisk
|
||||
|
||||
# Application
|
||||
log/
|
||||
config.dev.yaml
|
||||
compose.dev.yaml
|
||||
|
||||
|
10
config.yaml
10
config.yaml
@ -1,12 +1,4 @@
|
||||
server:
|
||||
app:
|
||||
port: 8080
|
||||
host: "localhost"
|
||||
|
||||
app:
|
||||
url: "http://localhost:8080"
|
||||
|
||||
integrations:
|
||||
zabbix_agent:
|
||||
plugin:
|
||||
name: "Onvif"
|
||||
version: "1.0.0"
|
||||
|
@ -4,29 +4,21 @@ import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"onvif-agent/constant"
|
||||
"os"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
Server serverConfig `mapstructure:"server"`
|
||||
App appConfig `mapstructure:"app"`
|
||||
Integrations integrationConfig `mapstructure:"integrations"`
|
||||
}
|
||||
|
||||
type serverConfig struct {
|
||||
Port int `mapstructure:"port"`
|
||||
Host string `mapstructure:"host"`
|
||||
}
|
||||
|
||||
type appConfig struct {
|
||||
URL string `mapstructure:"url"`
|
||||
Port int `mapstructure:"port"`
|
||||
Host string `mapstructure:"host"`
|
||||
URL string `mapstructure:"url"` // web server url
|
||||
}
|
||||
|
||||
type integrationConfig struct {
|
||||
ZabbixAgent IntegrationConfig `mapstructure:"zabbix_agent"`
|
||||
}
|
||||
|
||||
var Conf config
|
||||
var Config config
|
||||
|
||||
func LoadConfig() error {
|
||||
env := os.Getenv("PROFILE")
|
||||
@ -37,17 +29,18 @@ func LoadConfig() error {
|
||||
configName = fmt.Sprint("config.", env)
|
||||
}
|
||||
|
||||
log.Println("Loading config: ", configName)
|
||||
|
||||
viper.SetConfigName(configName)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath(fmt.Sprintf("/etc/%s", constant.AppName))
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&Conf); err != nil {
|
||||
log.Println("Loading config file:", viper.ConfigFileUsed())
|
||||
|
||||
if err := viper.Unmarshal(&Config); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -1,10 +0,0 @@
|
||||
package config
|
||||
|
||||
type IntegrationConfig struct {
|
||||
Plugin PluginConfig `mapstructure:"plugin"`
|
||||
}
|
||||
|
||||
type PluginConfig struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Version string `mapstructure:"version"`
|
||||
}
|
6
constant/app.go
Normal file
6
constant/app.go
Normal file
@ -0,0 +1,6 @@
|
||||
package constant
|
||||
|
||||
const (
|
||||
AppName = "onvif-agent"
|
||||
AppVersion = "1.0.0"
|
||||
)
|
5
integration/zabbixagent/constant.go
Normal file
5
integration/zabbixagent/constant.go
Normal file
@ -0,0 +1,5 @@
|
||||
package zabbixagent
|
||||
|
||||
const (
|
||||
PluginName = "OnvifAgent"
|
||||
)
|
@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"onvif-agent/config"
|
||||
"onvif-agent/constant"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -22,8 +23,8 @@ type Handler struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func (h *Handler) GetPluginVersion(_ context.Context, _ map[string]string, _ ...string) (any, error) {
|
||||
return config.Conf.Integrations.ZabbixAgent.Plugin.Version, nil
|
||||
func (h *Handler) GetAppVersion(_ context.Context, _ map[string]string, _ ...string) (any, error) {
|
||||
return constant.AppVersion, nil
|
||||
}
|
||||
|
||||
func (h *Handler) HTTPClient(ctx context.Context, params map[string]string, _ ...string) (any, error) {
|
||||
@ -34,13 +35,12 @@ func (h *Handler) HTTPClient(ctx context.Context, params map[string]string, _ ..
|
||||
|
||||
url := params["url"]
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
url = fmt.Sprintf("http://localhost:%d/%s", config.Conf.Server.Port, url)
|
||||
url = fmt.Sprintf("http://localhost:%d/%s", config.Config.App.Port, url)
|
||||
}
|
||||
|
||||
body := params["body"]
|
||||
|
||||
//req, err := http.NewRequestWithContext(ctx, method, "https://api.imbytecat.com/ip", http.NoBody)
|
||||
req, err := http.NewRequestWithContext(ctx, method, "https://api.imbytecat.com/ip", strings.NewReader(body))
|
||||
req, err := http.NewRequestWithContext(ctx, method, "https://api.imbytecat.com/ip", strings.NewReader(body)) // TODO: if empty use http.NoBody
|
||||
if err != nil {
|
||||
return nil, errs.Wrapf(err, "failed to create request")
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package zabbixagent
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"onvif-agent/config"
|
||||
"os"
|
||||
|
||||
"golang.zabbix.com/sdk/plugin/flag"
|
||||
@ -37,7 +36,7 @@ const (
|
||||
|
||||
func Run() {
|
||||
err := flag.HandleFlags(
|
||||
config.Conf.Integrations.ZabbixAgent.Plugin.Name,
|
||||
PluginName,
|
||||
os.Args[0],
|
||||
fmt.Sprintf(copyrightMessage, pluginLicenseYear),
|
||||
pluginVersionRC,
|
||||
|
@ -7,12 +7,9 @@ import (
|
||||
"golang.zabbix.com/sdk/plugin"
|
||||
"golang.zabbix.com/sdk/plugin/container"
|
||||
"golang.zabbix.com/sdk/zbxerr"
|
||||
"onvif-agent/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Name = config.Conf.Integrations.ZabbixAgent.Plugin.Name
|
||||
|
||||
type metricKey string
|
||||
|
||||
type metricBinding struct {
|
||||
@ -34,7 +31,7 @@ func Launch() error {
|
||||
return err
|
||||
}
|
||||
|
||||
h, err := container.NewHandler(Name)
|
||||
h, err := container.NewHandler(PluginName)
|
||||
if err != nil {
|
||||
return errs.Wrap(err, "failed to create new handler")
|
||||
}
|
||||
@ -81,11 +78,11 @@ func (p *zabbixAgentPlugin) registerMetrics() error {
|
||||
p.metrics = map[metricKey]*metricBinding{
|
||||
"onvif.version": {
|
||||
metric: metric.New(
|
||||
"ONVIF plugin version",
|
||||
"ONVIF app version",
|
||||
nil,
|
||||
false,
|
||||
),
|
||||
handler: h.GetPluginVersion,
|
||||
handler: h.GetAppVersion,
|
||||
},
|
||||
"onvif.client": {
|
||||
metric: metric.New(
|
||||
@ -107,7 +104,7 @@ func (p *zabbixAgentPlugin) registerMetrics() error {
|
||||
metricSet[string(k)] = m.metric
|
||||
}
|
||||
|
||||
err := plugin.RegisterMetrics(p, Name, metricSet.List()...)
|
||||
err := plugin.RegisterMetrics(p, PluginName, metricSet.List()...)
|
||||
if err != nil {
|
||||
return errs.Wrap(err, "failed to register metrics")
|
||||
}
|
||||
|
53
main.go
53
main.go
@ -1,12 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"log"
|
||||
"onvif-agent/config"
|
||||
"onvif-agent/constant"
|
||||
"onvif-agent/integration/zabbixagent"
|
||||
"onvif-agent/router"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
/**
|
||||
* Logging
|
||||
*/
|
||||
date := time.Now().Format("2006-01-02")
|
||||
f, err := os.OpenFile(fmt.Sprintf("/var/log/%s/%s.log", constant.AppName, date), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening file: %v", err)
|
||||
}
|
||||
|
||||
defer func(f *os.File) {
|
||||
err := f.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Error closing file: %v", err)
|
||||
}
|
||||
}(f)
|
||||
|
||||
writer := io.MultiWriter(os.Stdout, f)
|
||||
log.SetOutput(writer)
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||
|
||||
/**
|
||||
* Load config
|
||||
*/
|
||||
@ -17,23 +44,23 @@ func main() {
|
||||
/**
|
||||
* Web server
|
||||
*/
|
||||
//go func() {
|
||||
// r := gin.Default()
|
||||
//
|
||||
// router.SetupRoutes(r)
|
||||
//
|
||||
// addr := fmt.Sprintf("%s:%d", config.Conf.Server.Host, config.Conf.Server.Port)
|
||||
// if err := r.Run(addr); err != nil {
|
||||
// fmt.Println("Failed to start server:", err)
|
||||
// }
|
||||
//}()
|
||||
go func() {
|
||||
r := gin.Default()
|
||||
|
||||
router.SetupRoutes(r)
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", config.Config.App.Host, config.Config.App.Port)
|
||||
if err := r.Run(addr); err != nil {
|
||||
fmt.Println("Failed to start server:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
/**
|
||||
* Zabbix agent
|
||||
*/
|
||||
//go func() {
|
||||
go func() {
|
||||
zabbixagent.Run()
|
||||
//}()
|
||||
}()
|
||||
|
||||
//select {}
|
||||
select {}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
|
||||
func CreateEventSubscription(c *gin.Context) {
|
||||
xaddr := c.Param("xaddr")
|
||||
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Conf.App.URL, xaddr))
|
||||
callbackURL := event.AttributedURIType(fmt.Sprintf("%s/onvif/subscriptions/%s/callback", config.Config.App.URL, xaddr))
|
||||
log.Printf("CreateEventSubscription callback URL: %s", callbackURL)
|
||||
|
||||
conn := conns[xaddr]
|
||||
|
@ -2,8 +2,8 @@ FROM golang:1.23 AS builder
|
||||
WORKDIR /build
|
||||
COPY . ./
|
||||
RUN GOPROXY=https://goproxy.cn go mod download
|
||||
RUN PROFILE=dev CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o /app
|
||||
RUN PROFILE=dev CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o out/app
|
||||
|
||||
FROM zabbix/zabbix-agent2:ubuntu-7.0-latest
|
||||
COPY --from=builder /app /usr/sbin/zabbix-agent2-plugin/onvif
|
||||
RUN echo "Plugins.Onvif.System.Path=/usr/sbin/zabbix-agent2-plugin/onvif" >> /etc/zabbix/zabbix_agent2.d/plugins.d/onvif.conf
|
||||
COPY --from=builder /build/out/app /usr/sbin/onvif-agent
|
||||
RUN echo "Plugins.Onvif.System.Path=/usr/sbin/onvif-agent" >> /etc/zabbix/zabbix_agent2.d/plugins.d/onvif.conf
|
||||
|
Loading…
x
Reference in New Issue
Block a user