From c6803285fe06defe8d6ba5f4039ac5c901a93532 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Fri, 23 Aug 2024 11:06:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E8=AF=95=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + compose.yaml | 16 +++++++++ .../zabbixagent/{plugin => }/handler.go | 2 +- integration/zabbixagent/main.go | 13 ++++--- .../zabbixagent/{plugin => }/plugin.go | 2 +- main.go | 34 ++++++++----------- zabbixagent.dockerfile | 9 +++++ 7 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 compose.yaml rename integration/zabbixagent/{plugin => }/handler.go (98%) rename integration/zabbixagent/{plugin => }/plugin.go (99%) create mode 100644 zabbixagent.dockerfile diff --git a/.gitignore b/.gitignore index a5ee99e..edc87bc 100644 --- a/.gitignore +++ b/.gitignore @@ -171,4 +171,5 @@ Temporary Items # Application config.dev.yaml +compose.dev.yaml diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..648c0ac --- /dev/null +++ b/compose.yaml @@ -0,0 +1,16 @@ +services: + zabbixagent: + build: + context: . + dockerfile: zabbixagent.dockerfile + environment: + ZBX_HOSTNAME: ONVIF plugin for Zabbix agent 2 + ZBX_SERVER_HOST: server + ZBX_SERVER_PORT: 10051 + networks: + - zabbix-network + restart: unless-stopped + +networks: + zabbix-network: + external: true diff --git a/integration/zabbixagent/plugin/handler.go b/integration/zabbixagent/handler.go similarity index 98% rename from integration/zabbixagent/plugin/handler.go rename to integration/zabbixagent/handler.go index 9c56121..bc0d695 100644 --- a/integration/zabbixagent/plugin/handler.go +++ b/integration/zabbixagent/handler.go @@ -1,4 +1,4 @@ -package plugin +package zabbixagent import ( "context" diff --git a/integration/zabbixagent/main.go b/integration/zabbixagent/main.go index b197e9f..471db65 100644 --- a/integration/zabbixagent/main.go +++ b/integration/zabbixagent/main.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "onvif-agent/config" - "onvif-agent/integration/zabbixagent/plugin" "os" "golang.zabbix.com/sdk/plugin/flag" @@ -36,7 +35,7 @@ const ( pluginLicenseYear = 2024 ) -func Run() error { +func Run() { err := flag.HandleFlags( config.Conf.Integrations.ZabbixAgent.Plugin.Name, os.Args[0], @@ -48,16 +47,16 @@ func Run() error { ) if err != nil { if errors.Is(err, zbxerr.ErrorOSExitZero) { - return nil + return } - return err + panic(err) } - err = plugin.Launch() + err = Launch() if err != nil { - return err + return } - return nil + panic(err) } diff --git a/integration/zabbixagent/plugin/plugin.go b/integration/zabbixagent/plugin.go similarity index 99% rename from integration/zabbixagent/plugin/plugin.go rename to integration/zabbixagent/plugin.go index fb3b234..5241ef1 100644 --- a/integration/zabbixagent/plugin/plugin.go +++ b/integration/zabbixagent/plugin.go @@ -1,4 +1,4 @@ -package plugin +package zabbixagent import ( "context" diff --git a/main.go b/main.go index 63ed0f4..40e480a 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,9 @@ package main import ( - "fmt" - "github.com/gin-gonic/gin" "log" "onvif-agent/config" "onvif-agent/integration/zabbixagent" - "onvif-agent/router" ) func main() { @@ -20,26 +17,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.Conf.Server.Host, config.Conf.Server.Port) + // if err := r.Run(addr); err != nil { + // fmt.Println("Failed to start server:", err) + // } + //}() /** * Zabbix agent */ - go func() { - err := zabbixagent.Run() - if err != nil { - fmt.Println("Failed to start Zabbix agent integration:", err) - } - }() + //go func() { + zabbixagent.Run() + //}() - select {} + //select {} } diff --git a/zabbixagent.dockerfile b/zabbixagent.dockerfile new file mode 100644 index 0000000..bf8297d --- /dev/null +++ b/zabbixagent.dockerfile @@ -0,0 +1,9 @@ +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 + +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