-
Notifications
You must be signed in to change notification settings - Fork 8
/
g2z.go
131 lines (109 loc) · 5.29 KB
/
g2z.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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
// item is a go wrapper for a C.ZBX_METRIC
type item struct {
// Key is the registered item key.
Key string
// TestParams is a comma separated list of parameters passed to the registered key when the
// Zabbix agent executes a test run (via `zabbix_agentd -p`).
TestParams string
// Callback is the function to be called by route_item() when the registered key is queried by
// Zabbix.
Callback interface{}
}
// InitHandlerFunc is the function signature for functions which may be registered for callback
// when a Zabbix agent module is loaded when zbx_module_init() is called.
type InitHandlerFunc func() error
// UninitHandlerFunc is the function signature for functions which may be registered for callback
// when a Zabbix agent module is unloaded when zbx_module_uninit() is called.
type UninitHandlerFunc func() error
// StringItemHandlerFunc is the function signature for functions which may be registered as an
// item key handler and return a string value.
type StringItemHandlerFunc func(*AgentRequest) (string, error)
// Uint64ItemHandlerFunc is the function signature for functions which may be registered as an
// item key handler and return an unsigned integer value.
type Uint64ItemHandlerFunc func(*AgentRequest) (uint64, error)
// DoubleItemHandlerFunc is the function signature for functions which may be registered as an
// item key handler and return a double precision floating point integer value.
type DoubleItemHandlerFunc func(*AgentRequest) (float64, error)
// DiscoveryItemHandlerFunc is the function signature for functions which may be registered as an
// item key handler and return JSON encoded low-level discovery data.
type DiscoveryItemHandlerFunc func(*AgentRequest) (DiscoveryData, error)
// Timeout is set by Zabbix (via zbx_module_item_timeout()) to the number of seconds that all
// registered item handlers should obey as a strict timeout.
//
// This timeout is not enforced and should be voluntarily implemented.
var Timeout int
// initHandlers stores all registered InitHandlerFuncs
var initHandlers = []InitHandlerFunc{}
// uninitHandlers stores all registered UninitHandlerFuncs
var uninitHandlers = []UninitHandlerFunc{}
// itemHandlers stores a map of registered item keys to callback functions
var itemHandlers map[string]item = make(map[string]item, 0)
// RegisterInitHandler should be called from init() to register an InitHandlerFunc which will be
// called when Zabbix calls zbx_module_init().
func RegisterInitHandler(handler InitHandlerFunc) {
LogDebugf("Registering init handler")
initHandlers = append(initHandlers, handler)
}
// RegisterUninitHandler should be called from init() to register an UninitHandlerFunc which will
// be called when Zabbix calls zbx_module_uninit().
func RegisterUninitHandler(handler UninitHandlerFunc) {
LogDebugf("Registering uninit handler")
uninitHandlers = append(uninitHandlers, handler)
}
// registerItems registers an agent item key, its test parameters and a callback function with
// Zabbix.
func registerItem(key string, testParams string, handler interface{}) {
k := item{
Key: key,
TestParams: testParams,
Callback: handler,
}
itemHandlers[key] = k
}
// RegisterStringItem registers an agent item key, its test parameters and a handler function with
// Zabbix for a key which returns a string.
//
// This function should only be called from `init()`
func RegisterStringItem(key string, testParams string, handler StringItemHandlerFunc) {
registerItem(key, testParams, handler)
}
// RegisterUint64Item registers an agent item key, its test parameters and a handler function with
// Zabbix for a key which returns an unsigned integer.
//
// This function should only be called from `init()`
func RegisterUint64Item(key string, testParams string, handler Uint64ItemHandlerFunc) {
registerItem(key, testParams, handler)
}
// RegisterDoubleItem registers an agent item key, its test parameters and a handler function with
// Zabbix for a key which returns a double precision floating point integer.
//
// This function should only be called from `init()`
func RegisterDoubleItem(key string, testParams string, handler DoubleItemHandlerFunc) {
registerItem(key, testParams, handler)
}
// RegisterDiscoveryItem registers an agent item key, its test parameters and a handler function with
// Zabbix for a key which returns DiscoveryData.
//
// This function should only be called from `init()`
func RegisterDiscoveryItem(key string, testParams string, handler DiscoveryItemHandlerFunc) {
registerItem(key, testParams, handler)
}