-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_jmx.lua
197 lines (160 loc) · 5.09 KB
/
check_jmx.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
--
-- Copyright (c) 2010, Cloudkick, Inc.
-- All right reserved.
--
module(..., package.seeall);
local util = require 'util'
local Check = util.Check
local log = util.log
local http = require("socket.http")
local io = require 'io'
local os = require 'os'
local jmxquery_older_versions = { '1.0' }
local jmxquery_current_version = '1.1'
local jmxquery_url = equus.equus_url().. 'jmxquery-' .. jmxquery_current_version .. '.jar'
local jmxquery_sig_url = equus.equus_url().. 'jmxquery-' .. jmxquery_current_version .. '.sig'
local function equus_exec_call_win32(args)
local escaped = {}
local i
local v
for i,v in ipairs(args) do
escaped[i] = '"' .. string.gsub(v, '"', '\\"') .. '"'
end
cmd = table.concat(escaped, " ")
return equus_exec_call_win32_core(cmd)
end
local function get_metrics(jmxquery_path, hostname, port, username, password,
object_name, attribute_name, attribute_keys)
local cmd = {'java',
'-classpath',
jmxquery_path,
'jmxquery.JMXQuery',
'-U',
'service:jmx:rmi:///jndi/rmi://'.. hostname ..':'.. port ..'/jmxrmi',
'-O',
object_name,
'-A',
attribute_name}
if username then
table.insert(cmd, '-username')
table.insert(cmd, username)
end
if password then
table.insert(cmd, '-password')
table.insert(cmd, password)
end
if attribute_keys then
table.insert(cmd, '-K')
table.insert(cmd, attribute_keys)
end
log.dbg('jmx command: %s', table.concat(cmd, " "))
local p,rc
if equus.p_is_windows() == 1 then
p,rc = equus_exec_call_win32(cmd)
else
p,rc = equus_exec_call_unix_core(cmd)
end
return p,rc
end
local function get_jmxquery_path(version)
local version = version or jmxquery_current_version
local filename = 'jmxquery-' .. version .. '.jar'
local path = nil
if equus.p_is_windows() == 1 then
path = os.getenv("ProgramFiles") ..'\\Cloudkick Agent\\'
else
path = '/usr/lib/cloudkick-agent/'
end
return path .. filename
end
local function download_jmxquery(delete_older_versions)
local delete_older_versions = delete_older_versions or true
local file_path = get_jmxquery_path()
local path, rv
local sig,sigc,h = http.request(jmxquery_sig_url)
assert(sig ~= nil, "jmxquery: Signature is null")
assert(sigc ~= "200", "jmxquery: Signature http code is not 200")
local data,datac,h = http.request(jmxquery_url)
assert(data ~= nil, "jmxquery: Data is null")
assert(datac ~= "200", "jmxquery: Data http code is not 200")
local rc = equus.equus_verify(data, data:len(), sig, sig:len(), "", 0)
assert(rc == 0, "jmxquery from "..jmxquery_url.." signature validation failed")
--log.info('jmxquery: signature verify result: '.. rc)
--log.info('jmxquery: downloaded bytes: '.. data:len())
local fp = assert(io.open(file_path, 'wb'))
fp:write(data)
fp:close()
if delete_older_versions then
for _, version in ipairs(jmxquery_older_versions) do
path = get_jmxquery_path(version)
if util.file_exists(path) then
rv = os.remove(path)
-- log.info('jmxquery: deleted old version ' .. version .. ' - ' .. tostring(rv))
end
end
end
return true
end
function run(rcheck, args)
if args.hostname == nil then
args.hostname = 'localhost'
else
args.hostname = args.hostname[1]
end
if args.username == nil then
args.username = nil
else
args.username = args.username[1]
end
if args.password == nil then
args.password = nil
else
args.password = args.password[1]
end
if args.port == nil then
rcheck:set_error('port is required for JMX check')
return rcheck
else
args.port = args.port[1]
end
if args.object_name == nil then
rcheck:set_error('object_name is required for JMX check')
return rcheck
else
args.object_name = args.object_name[1]
end
if args.attribute_name == nil then
rcheck:set_error('attribute_name is required for JMX check')
return rcheck
else
args.attribute_name = args.attribute_name[1]
end
if args.attribute_keys == nil then
args.attribute_keys = nil
else
args.attribute_keys = args.attribute_keys[1]
end
-- Download the jmxquery.jar file if it's not already present
local jmxquery_path = get_jmxquery_path()
if not util.file_exists(jmxquery_path) then
log.info('jmxquery.jar not found, trying to download it from %s', jmxquery_url)
if not download_jmxquery() then
log.crit('downloading jmxquery.jar failed')
rcheck:set_error('JMX check failed: failed to download jmxquery.jar')
return rchek
end
end
local rv, r, rc = pcall(get_metrics, jmxquery_path, args.hostname, args.port,
args.username, args.password, args.object_name,
args.attribute_name, args.attribute_keys)
if rv and rc == 0 then
rcheck.output = r
else
if rc ~= 0 and rc ~= nil then
r = string.format("exit code: %d output: %s", rc, tostring(r))
end
log.err("err from jmx plugin: %s", tostring(r))
rcheck:set_error("%s", tostring(r))
end
return rcheck
end