-
Notifications
You must be signed in to change notification settings - Fork 9
/
router.go
111 lines (98 loc) · 3.36 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* g2z - Zabbix module adapter for Go
* Copyright (C) 2015 - Ryan Armstrong <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package g2z
/*
// zabbix agent headers
#include <stdio.h>
#include <stdint.h>
#include "module.h"
// wrapper for get_rparam macro to ease the burden of indexing a **char in go
static char *g2z_get_rparam(AGENT_REQUEST *request, int i) {
return get_rparam(request, i);
}
*/
import "C"
import (
"fmt"
)
// route_item is the entry point for all registered items.
//
//export route_item
func route_item(request *C.AGENT_REQUEST, result *C.AGENT_RESULT) C.int {
// marshall a C.AGENT_RESULT to g2z.AgentRequest
req := &AgentRequest{
Key: C.GoString(request.key),
Params: make([]string, request.nparam),
}
for i := 0; i < int(request.nparam); i++ {
req.Params[i] = C.GoString(C.g2z_get_rparam(request, C.int(i)))
}
// get the item handler for the requested key
item, ok := itemHandlers[req.Key]
if !ok {
// this should never happen
LogCriticalf("Item appears to be registered but has no go handler: %s", req.Key)
return C.SYSINFO_RET_FAIL
}
// call handler function
switch item.Callback.(type) {
case StringItemHandlerFunc:
LogDebugf("Calling StringItemHandlerFunc for key: %s", req.Key)
if v, err := item.Callback.(StringItemHandlerFunc)(req); err != nil {
setMessageResult(result, err.Error())
return C.SYSINFO_RET_FAIL
} else {
result._type = C.AR_STRING
result.str = C.CString(v) // freed by Zabbix
}
case Uint64ItemHandlerFunc:
LogDebugf("Calling Uint64ItemHandlerFunc for key: %s", req.Key)
if v, err := item.Callback.(Uint64ItemHandlerFunc)(req); err != nil {
setMessageResult(result, err.Error())
return C.SYSINFO_RET_FAIL
} else {
result._type = C.AR_UINT64
result.ui64 = C.uint64_t(v)
}
case DoubleItemHandlerFunc:
LogDebugf("Calling DoubleItemHandlerFunc for key: %s", req.Key)
if v, err := item.Callback.(DoubleItemHandlerFunc)(req); err != nil {
setMessageResult(result, err.Error())
return C.SYSINFO_RET_FAIL
} else {
result._type = C.AR_DOUBLE
result.dbl = C.double(v)
}
case DiscoveryItemHandlerFunc:
LogDebugf("Calling DiscoveryItemHandlerFunc for key: %s", req.Key)
if v, err := item.Callback.(DiscoveryItemHandlerFunc)(req); err != nil {
setMessageResult(result, err.Error())
return C.SYSINFO_RET_FAIL
} else {
result._type = C.AR_STRING
result.str = C.CString(v.Json()) // freed by Zabbix
}
}
return C.SYSINFO_RET_OK
}
// setMessageResult adds an error message to an agent result struct
func setMessageResult(result *C.AGENT_RESULT, format string, a ...interface{}) {
result._type = C.AR_MESSAGE
result.msg = C.CString(fmt.Sprintf(format, a...)) // freed by Zabbix
}