Skip to content

Commit

Permalink
Fixed Go pointer return in zbx_module_item_list. Fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
cavaliercoder committed Mar 8, 2016
1 parent fa303eb commit e37acd8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
27 changes: 21 additions & 6 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package g2z

/*
// zabbix agent headers
#include <stdlib.h>
#include <stdint.h>
#include "module.h"
Expand All @@ -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"

Expand Down Expand Up @@ -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
}

0 comments on commit e37acd8

Please sign in to comment.