From e37acd8c1a42782df86045128a376878d2b0dac6 Mon Sep 17 00:00:00 2001 From: Ryan Armstrong Date: Tue, 8 Mar 2016 13:55:02 +0800 Subject: [PATCH] Fixed Go pointer return in zbx_module_item_list. Fixes #7 --- Dockerfile | 2 +- Makefile | 12 +++++++++--- module.go | 27 +++++++++++++++++++++------ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index bf33798..0c7cde8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -FROM golang:1.5 +FROM golang:1.6 # install Zabbix agent RUN \ diff --git a/Makefile b/Makefile index d67aad9..a27c5bc 100644 --- a/Makefile +++ b/Makefile @@ -27,11 +27,17 @@ clean: go clean -i -x rm -vf g2z.h -clean-all: clean +clean-dummy: cd dummy && $(MAKE) clean +clean-all: clean clean-dummy + docker-build: - docker build -t cavaliercoder/g2z . + docker build \ + --build-arg "http_proxy=$(http_proxy)" \ + --build-arg "https_proxy=$(https_proxy)" \ + --build-arg "no_proxy=$(no_proxy)" \ + -t cavaliercoder/g2z . docker-run: docker-build docker run --rm -it \ @@ -40,4 +46,4 @@ docker-run: docker-build --privileged \ cavaliercoder/g2z -.PHONY: all g2z dummy clean clean-all docker-build docker-run +.PHONY: all g2z dummy clean clean-dummy clean-all docker-build docker-run diff --git a/module.go b/module.go index 5190eb5..31c5793 100644 --- a/module.go +++ b/module.go @@ -21,6 +21,7 @@ package g2z /* // zabbix agent headers +#include #include #include "module.h" @@ -29,6 +30,20 @@ typedef int (*agent_item_handler)(AGENT_REQUEST*, AGENT_RESULT*); // item callback router function defined in router.go int route_item(AGENT_REQUEST *request, AGENT_RESULT *result); + +// create new metric list +static ZBX_METRIC *new_metric_list(const size_t n) { + return (ZBX_METRIC*)calloc(sizeof(ZBX_METRIC), n + 1); +} + +// append a metric to a metric list +static void append_metric(ZBX_METRIC *list, const ZBX_METRIC *n) { + while(NULL != list->key) + list++; + + memcpy(list, n, sizeof(ZBX_METRIC)); +} + */ import "C" @@ -101,17 +116,17 @@ func zbx_module_item_list() *C.ZBX_METRIC { router := C.agent_item_handler(unsafe.Pointer(C.route_item)) // create null-terminated array of C.ZBX_METRICS - i := 0 - metrics := make([]C.ZBX_METRIC, len(itemHandlers)+1) + metrics := C.new_metric_list(C.size_t(len(itemHandlers))) // never freed for _, item := range itemHandlers { - metrics[i] = C.ZBX_METRIC{ + m := C.ZBX_METRIC{ key: C.CString(item.Key), // freed by Zabbix flags: C.CF_HAVEPARAMS, function: router, test_param: C.CString(item.TestParams), // freed by Zabbix - } - i++ + } // freed by Go GC + + C.append_metric(metrics, &m) } - return &metrics[0] + return metrics }