-
Notifications
You must be signed in to change notification settings - Fork 9
/
init.lua
235 lines (184 loc) · 7.83 KB
/
init.lua
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
-- Copyright 2015 BMC Software, Inc.
-- --
-- -- Licensed under the Apache License, Version 2.0 (the "License");
-- -- you may not use this file except in compliance with the License.
-- -- You may obtain a copy of the License at
-- --
-- -- http://www.apache.org/licenses/LICENSE-2.0
-- --
-- -- Unless required by applicable law or agreed to in writing, software
-- -- distributed under the License is distributed on an "AS IS" BASIS,
-- -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- -- See the License for the specific language governing permissions and
-- -- limitations under the License.
--Framework imports.
local framework = require('framework')
local Plugin = framework.Plugin
local WebRequestDataSource = framework.WebRequestDataSource
local DataSourcePoller = framework.DataSourcePoller
local PollerCollection = framework.PollerCollection
local isHttpSuccess = framework.util.isHttpSuccess
local ipack = framework.util.ipack
local parseJson = framework.util.parseJson
local notEmpty = framework.string.notEmpty
--Changes done for Securing parameters.
--local params = framework.params
local json = require('json')
local env = require('env')
local params = env.get("TSP_PLUGIN_PARAMS")
if(params == nil or params == '') then
params = framework.params
else
params = json.parse(params)
end
-- End changes for Security parameters.
local CLUSTER_STATS = 'cluster_stats'
local CLUSTER_HEALTH = 'cluster_health'
local NODES_STATS = 'nodes_stats'
local function createOptions(item)
local options = {}
options.host = item.host
options.port = item.port
options.wait_for_end = true
return options
end
local function createClusterStats(item)
local options = createOptions(item)
options.path = "/_cluster/stats"
options.meta = {CLUSTER_STATS, item}
return WebRequestDataSource:new(options)
end
local function createClusterHealth(item)
local options = createOptions(item)
options.path = "/_cluster/health"
options.meta = {CLUSTER_HEALTH, item}
return WebRequestDataSource:new(options)
end
local function createNodesStats(item)
local options = createOptions(item)
options.path = "/_nodes/stats"
options.meta = {NODES_STATS, item}
return WebRequestDataSource:new(options)
end
local function createPollers(params)
local pollers = PollerCollection:new()
for _, item in pairs(params.items) do
local cs = createClusterStats(item)
local clusterStatsPoller = DataSourcePoller:new(item.pollInterval, cs)
pollers:add(clusterStatsPoller)
local ch = createClusterHealth(item)
local clusterHealthPoller = DataSourcePoller:new(item.pollInterval, ch)
pollers:add(clusterHealthPoller)
local ns = createNodesStats(item)
local nodesStatsPoller = DataSourcePoller:new(item.pollInterval, ns)
pollers:add(nodesStatsPoller)
end
return pollers
end
local function clusterStatsExtractor (data, item)
local result = {}
local metric = function (...) ipack(result, ...) end
local src = notEmpty(item.source,data.cluster_name)
if data.status ~= nil then
if data.status == 'green' then
metric('ELASTIC_SEARCH_STATUS',1,nil,src)
else
metric('ELASTIC_SEARCH_STATUS',0,nil,src)
end
end
if data.nodes.count.total ~= nil then
metric('ELASTIC_SEARCH_NODE_COUNT', data.nodes.count.total,nil,src)
end
if data.indices.count ~= nil then
metric('ELASTIC_SEARCH_INDEX_COUNT', data.indices.count,nil,src)
end
if data.indices.docs.count ~= nil then
metric('ELASTIC_SEARCH_DOCUMENT_COUNT', data.indices.docs.count,nil,src)
end
if data.indices.store.size_in_bytes ~= nil then
metric('ELASTIC_SEARCH_STORE_SIZE', data.indices.store.size_in_bytes,nil,src)
end
if data.indices.segments.count ~= nil then
metric('ELASTIC_SEARCH_SEGMENT_COUNT', data.indices.segments.count,nil,src)
end
if data.indices.shards ~= nil then
if data.indices.shards.total ~= nil then
metric('ELASTIC_SEARCH_TOTAL_SHARDS', data.indices.shards.total,nil,src)
end
if data.indices.shards.primaries ~= nil then
metric('ELASTIC_SEARCH_PRIMARY_SHARDS',data.indices.shards.primaries, nil, src)
end
end
if data.indices.fielddata ~= nil then
if data.indices.fielddata.evictions ~= nil then
metric('ELASTIC_SEARCH_FIELDDATA_EVICTIONS', data.indices.fielddata.evictions,nil,src)
end
end
if data.indices.filter_cache ~= nil then
metric('ELASTIC_SEARCH_FILTER_CACHE_MEMORY_SIZE', data.indices.filter_cache.memory_size_in_bytes,nil,src)
metric('ELASTIC_SEARCH_FILTER_CACHE_EVICTIONS',data.indices.filter_cache.evictions,nil,src)
else
metric('ELASTIC_SEARCH_QUERY_CACHE_MEMORY_SIZE', data.indices.query_cache.memory_size_in_bytes,nil,src)
metric('ELASTIC_SEARCH_QUERY_CACHE_EVICTIONS', data.indices.query_cache.evictions,nil,src)
end
if data.indices.id_cache ~= nil then
metric('ELASTIC_SEARCH_ID_CACHE_MEMORY_SIZE', data.indices.id_cache.memory_size_in_bytes,nil,src)
end
metric('ELASTIC_SEARCH_COMPLETION_SIZE', data.indices.completion.size_in_bytes,nil,src)
--#######################################################
return result
end
local function clusterHealthExtractor (data, item)
local result = {}
local metric = function (...) ipack(result, ...) end
local src = notEmpty(item.source,data.cluster_name)
metric('ELASTIC_SEARCH_NO_OF_NODES',data.number_of_nodes,nil,src)
metric('ELASTIC_SEARCH_NO_OF_DATA_NODES',data.number_of_data_nodes,nil,src)
metric('ELASTIC_SEARCH_ACTIVE_PRIMARY_SHARDS',data.active_primary_shards,nil,src)
metric('ELASTIC_SEARCH_ACTIVE_SHARDS',data.active_shards,nil,src)
metric('ELASTIC_SEARCH_RELOCATING_SHARDS',data.relocating_shards,nil,src)
metric('ELASTIC_SEARCH_INITIALISING_SHARDS',data.initializing_shards,nil,src)
metric('ELASTIC_SEARCH_UNASSIGNED_SHARDS',data.unassigned_shards,nil,src)
return result
end
local function nodesStatsExtractor (data, item)
local result = {}
local metric = function (...) ipack(result, ...) end
for _,node in pairs(data.nodes) do
local src = notEmpty(item.source,data.cluster_name) .. ".Node-" .. node.name
metric('ELASTIC_SEARCH_JVM_UPTIME_IN_MILLIS',node.jvm.uptime_in_millis,nil,src)
metric('ELASTIC_SEARCH_JVM_MEM_HEAP_USED_PERCENT',(node.jvm.mem.heap_used_percent/100),nil,src)
metric('ELASTIC_SEARCH_PROCESS_OPEN_FILE_DESCRIPTORS',node.process.open_file_descriptors,nil,src)
metric('ELASTIC_SEARCH_PROCESS_MAX_FILE_DESCRIPTORS',node.process.max_file_descriptors,nil,src)
if #node.fs.data>0 and node.fs.data[1].available_in_bytes ~= nil then
metric('ELASTIC_SEARCH_FS_DATA_AVAILABLE_IN_BYTES',node.fs.data[1].available_in_bytes,nil,src)
end
metric('ELASTIC_SEARCH_BREAKERS_FIELDDATA_TRIPPED',node.breakers.fielddata.tripped,nil,src)
if node.indices.fielddata.memory_size_in_bytes ~= nil then
metric('ELASTIC_SEARCH_FIELDDATA_MEMORY_SIZE', node.indices.fielddata.memory_size_in_bytes,nil,src)
end
end
return result
end
local extractors_map = {}
extractors_map[CLUSTER_STATS] = clusterStatsExtractor
extractors_map[CLUSTER_HEALTH] = clusterHealthExtractor
extractors_map[NODES_STATS] = nodesStatsExtractor
local pollers = createPollers(params)
local plugin = Plugin:new(params, pollers)
--Response returned for each of the pollers.
function plugin:onParseValues(data, extra)
if not isHttpSuccess(extra.status_code) then
self:emitEvent('error', ('Http request returned status code %s instead of OK. Please verify configuration.'):format(extra.status_code))
return
end
local success, data = parseJson(data)
if not success then
self:emitEvent('error', 'Cannot parse metrics. Please verify configuration.')
return
end
local key, item = unpack(extra.info)
local extractor = extractors_map[key]
return extractor(data, item)
end
plugin:run()