-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathitemkey.go
183 lines (155 loc) · 4.97 KB
/
itemkey.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Zabbix Agent Bench (C) 2014 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 3 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, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"os"
"regexp"
"sort"
"strings"
"time"
)
// An ItemKey is a single Zabbix agent item check key
type ItemKey struct {
Key string
IsDiscoveryRule bool
IsPrototype bool
Prototypes ItemKeys
}
// ItemKeys is an array of pointers to ItemKey structs
type ItemKeys []*ItemKey
// DiscoveryData is a key/val array of values returned embedded in a discovery
// rule response
type DiscoveryData []map[string]string
// DiscoveryResponse is the JSON packet returned by the Zabbix agent for
// discovery rules
type DiscoveryResponse struct {
Data DiscoveryData
}
var (
envVarPattern = regexp.MustCompile(`\{%.*?\}`)
indentPattern = regexp.MustCompile(`^\s+`)
)
// NewItemKey returns a pointer to a new instance of an ItemKey with
// environment variables in the key name expanded.
func NewItemKey(key string) *ItemKey {
return &ItemKey{
Key: ParseItemKey(key),
IsDiscoveryRule: false,
IsPrototype: false,
Prototypes: make(ItemKeys, 0),
}
}
// LongestKeyName returns the length in characters of the longest key name
// in an array of keys.
// Used for formatting output.
func (c ItemKeys) LongestKeyName() int {
longestKeyName := 0
for _, key := range c {
if len(key.Key) > longestKeyName {
longestKeyName = len(key.Key)
}
}
return longestKeyName
}
// SortedKeyNames returns the name of all keys in the this key array sorted
// alphanumerically.
func (c ItemKeys) SortedKeyNames() []string {
keyNames := []string{}
for _, key := range c {
keyNames = append(keyNames, key.Key)
}
sort.Strings(keyNames)
return keyNames
}
// ParseItemKey subsitutes any variables in a key name for runtime
// environment variables and trims any whitespace at the beginning of the key.
//
// Variables in the key name take the form '{%VARNAME}' and are replaced with
// the matching environment variable value (e.g. 'VARNAME').
//
// Variables with no value set in the runtime environment are replaced with a
// zero length string.
func ParseItemKey(key string) string {
// Strip out indentation
key = indentPattern.ReplaceAllString(key, "")
// replace environment variables
vars := envVarPattern.FindAllString(key, -1)
for _, v := range vars {
ev := v[2 : len(v)-1]
key = strings.Replace(key, v, os.Getenv(ev), -1)
}
return key
}
// Discover sends a 'get' request to a Zabbix agent and expand the key's
// discovery prototypes into new standard keys using the response from the
// Zabbix agent
func (c *ItemKey) Discover(host string, timeout time.Duration) (ItemKeys, error) {
if !c.IsDiscoveryRule {
return nil, NewError(nil, "Item is not a discovery rule: %s", c.Key)
}
// get discovery items to expand prototypes
dprintf("Executing discovery rule: %s\n", c.Key)
val, err := Get(host, c.Key, timeout)
if err != nil {
return nil, NewError(err, "Failed to get discovery data for item: %s", c.Key)
}
// check if result is unsupported
if strings.HasPrefix(val, ZBX_NOTSUPPORTED) {
return nil, NewError(nil, "Discovery rule unsupported for item: %s", c.Key)
}
// bind JSON discovery data
response := DiscoveryResponse{}
err = json.Unmarshal([]byte(val), &response)
if err != nil {
return nil, NewError(err, "Failed to parse discovery json data for item: %s\n%s", c.Key, val)
}
// Parse each discovered instance
keys := ItemKeys{}
for _, instance := range response.Data {
// Create prototypes
for _, proto := range c.Prototypes {
// Expand macros
s := proto.Key
for macro, val := range instance {
dprintf("Substituting macro '%s' with value '%s'\n", macro, val)
s = strings.Replace(s, macro, val, -1)
}
// Item discovered item
n := NewItemKey(s)
n.IsPrototype = true
keys = append(keys, n)
dprintf("Added discovered key: %s\n", n.Key)
}
}
return keys, nil
}
// Expand executes a discovery on all discovery rules in a list of keys and
// appends the expanded prototypes to the returned array
func (c ItemKeys) Expand(host string, timeout time.Duration) (ItemKeys, error) {
keys := c
for _, key := range c {
if key.IsDiscoveryRule {
discoveredKeys, err := key.Discover(host, timeout)
if err != nil {
return nil, NewError(err, "Failed to expand prototypes for discovery rule: %s", key.Key)
}
keys = append(keys, discoveredKeys...)
}
}
return keys, nil
}