1/*
2** Zabbix
3** Copyright (C) 2001-2021 Zabbix SIA
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18**/
19
20package zbxlib
21
22/* cspell:disable */
23
24/*
25#cgo CFLAGS: -I${SRCDIR}/../../../../include
26
27#include "common.h"
28#include "sysinfo.h"
29#include "module.h"
30typedef int (*zbx_agent_check_t)(AGENT_REQUEST *request, AGENT_RESULT *result);
31
32static int execute_check(const char *key, zbx_agent_check_t check_func, char **value, char **error)
33{
34	int ret = FAIL;
35	char **pvalue;
36	AGENT_RESULT result;
37	AGENT_REQUEST request;
38
39	init_request(&request);
40	init_result(&result);
41	if (SUCCEED != parse_item_key(key, &request))
42	{
43		*value = zbx_strdup(NULL, "Invalid item key format.");
44		goto out;
45	}
46	if (SYSINFO_RET_OK != check_func(&request, &result))
47	{
48		if (0 != ISSET_MSG(&result))
49		{
50			*error = zbx_strdup(NULL, result.msg);
51		}
52		else
53			*error = zbx_strdup(NULL, "Unknown error.");
54		goto out;
55	}
56
57	if (NULL != (pvalue = GET_TEXT_RESULT(&result)))
58		*value = zbx_strdup(NULL, *pvalue);
59
60	ret = SUCCEED;
61out:
62	free_result(&result);
63	free_request(&request);
64	return ret;
65}
66
67*/
68import "C"
69
70import (
71	"errors"
72	"unsafe"
73
74	"zabbix.com/pkg/itemutil"
75)
76
77func ExecuteCheck(key string, params []string) (result *string, err error) {
78	cfunc := resolveMetric(key)
79
80	if cfunc == nil {
81		return nil, errors.New("Unsupported item key.")
82	}
83
84	var cvalue, cerrmsg *C.char
85	ckey := C.CString(itemutil.MakeKey(key, params))
86	if C.execute_check(ckey, C.zbx_agent_check_t(cfunc), &cvalue, &cerrmsg) == Succeed {
87		if cvalue != nil {
88			value := C.GoString(cvalue)
89			result = &value
90		}
91		C.free(unsafe.Pointer(cvalue))
92
93	} else {
94		err = errors.New(C.GoString(cerrmsg))
95		C.free(unsafe.Pointer(cerrmsg))
96	}
97	C.free(unsafe.Pointer(ckey))
98	return
99}
100