-
Notifications
You must be signed in to change notification settings - Fork 1
/
webrockit-poller.rb
executable file
·293 lines (277 loc) · 8.92 KB
/
webrockit-poller.rb
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/opt/sensu/embedded/bin/ruby
require 'json'
require 'uri'
require 'time'
require 'optparse'
require 'timeout'
options = {}
options[:phantomas_bin] = "/opt/phantomjs/collectoids/phantomas/bin/phantomas.js"
options[:phantomas_opts] = "--format=json "
options[:ghost_bin] = "/opt/phantomjs/collectoids/webrockit-poller/ghost"
options[:phantomas_extra_ops] = [ ]
options[:phantomas_external_opts] = ""
options[:critical] = 30
options[:limitexternal] = ""
options[:debug] = false
options[:jsonreports] = false
options[:format] = "plain"
options[:metricdetail] = "standard"
def bail(msg,format="plain")
nowstamp = Time.now.to_i
statusdata = JSON.parse(msg)
if format.to_s == "plain"
puts "pollerstatus\t" + statusdata['pollstatus'].to_s + "\t#{nowstamp}\n"
if statusdata['pollstatus'].to_s != "0"
puts "errormsg\t\"" + statusdata['errormsg'].to_s + "\"\t#{nowstamp}\n"
end
elsif format.to_s == "json" && statusdata['pollstatus'].to_s != "0"
puts msg
end
exit statusdata['status'].to_i
end
OptionParser.new do |opts|
nowstamp = Time.now.to_i
opts.banner = "Usage: #{$0} [options]"
opts.on("-d", "--debug", "Enable debug output") do
options[:debug] = true
end
opts.on("-e", "--external none,limit", "Define external asset fetching") do |e|
begin
if e.to_s.empty?
raise
end
rescue
bail("{\"pollstatus\":1,\"errormsg\":\"No exteral asset parameter provided, please use --external [none,limit]\"}",options[:limitexternal])
end
options[:limitexternal] = e
end
opts.on("-f", "--format json,plain", "Output data and status as plain(text/tsv) or json (default: plain)") do |f|
begin
if f.to_s.empty?
raise
end
rescue
bail("{\"pollstatus\":1,\"errormsg\":\"No format provided, please use --format [plain,json]\"}",options[:format])
end
options[:format] = f
end
opts.on("-i", "--ip x.x.x.x", "Override DNS or provide IP for request (default: use dns)") do |i|
begin
if i =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/
options[:ip_address] = i
else
raise
end
rescue
bail("{\"pollstatus\":1,\"errormsg\":\"No ip provided after ip option, please use --ip x.x.x.x\"}",options[:format])
end
end
opts.on("-l", "--ps-extra-opts [STRING]", "Extra Phantomas Options (default: no options) [eg -l 'debug' -l 'proxy=localhost']") do |l|
options[:phantomas_extra_ops] << "--" + l.to_s
end
opts.on("-m", "--metricdetail minimal,standard,verbose", "Level of data to output (default: standard)") do |m|
begin
if m.to_s.empty?
raise
end
rescue
bail("{\"pollstatus\":1,\"errormsg\":\"No metriclevel provided, please use -m [minimal, standard, verbose]\"}",options[:format])
end
options[:metricdetail] = m
end
opts.on("-p", "--phantomas [PATH]", "Path to Phantomas binary (default: #{options[:phantomas_bin]})") do |p|
options[:phantomas_bin] = p
end
opts.on("-u", "--url [STRING]", "URL to query" ) do |u|
begin
if u.to_s.empty?
raise
end
rescue
bail("{\"pollstatus\":1,\"errormsg\":\"Empty url provided, please use --url http://example.tld\"}",options[:format])
end
options[:url] = u
options[:domain] = u.sub(/^https?\:\/\//, '').split("/")[0]
end
end.parse!
unless File.executable?(options[:phantomas_bin])
bail("{\"pollstatus\":3,\"errormsg\":\"Could not find Phantomas binary (#{options[:phantomas_bin]})\"}",options[:format])
end
if !options[:ip_address].to_s.empty?
cmd = Array.new
cmd << "sudo env GEM_PATH=/opt/sensu/embedded/lib/ruby/gems/2.0.0 "+options[:ghost_bin]+" modify "+options[:domain]+" "+options[:ip_address]
cmd << "2> /dev/null"
warn "Ghost cmd is: " + cmd.join(" ") if options[:debug]
@pipe = IO.popen(cmd.join(" "))
output = @pipe.read
Process.wait(@pipe.pid)
end
if options[:url].to_s.empty?
bail("{\"pollstatus\":1,\"errormsg\":\"No url provided, please use --url http://example.tld\"}",options[:format])
end
if options[:format].to_s.empty?
bail("{\"pollstatus\":1,\"errormsg\":\"Missing or bad format provided, please use --format [plain,json]\"}",options[:format])
end
# --no-externals block requests to 3rd party domains
# --allow-domain=[domain],[domain] allow requests to given domain(s) - aka whitelist
# --block-domain=[domain],[domain] disallow requests to given domain(s) - aka blacklist
if options[:limitexternal].to_s == "none"
options[:phantomas_external_opts] = "--no-externals"
elsif options[:limitexternal].to_s == "limit"
options[:phantomas_external_opts] = "--no-externals --allow-domain ."+options[:domain]
end
website_url = URI(options[:url])
website_load_time = 0.0
# Run Phantomas
output = ""
nowstamp = Time.now.to_i
begin
Timeout::timeout(options[:critical].to_i + 3) do
cmd = Array.new
cmd << options[:phantomas_bin]
cmd << options[:phantomas_opts]
cmd << options[:phantomas_extra_ops]
cmd << options[:phantomas_external_opts]
cmd << " --url " + website_url.to_s
cmd << "2> /dev/null"
warn "Phantomas cmd is: " + cmd.join(" ") if options[:debug]
@pipe = IO.popen(cmd.join(" "))
output = @pipe.read
Process.wait(@pipe.pid)
end
rescue Timeout::Error => e
critical_time_ms = options[:critical].to_i * 1000
Process.kill(9, @pipe.pid)
Process.wait(@pipe.pid)
bail("{\"pollstatus\":2,\"errormsg\":\"Critical: #{website_url.to_s}: Timeout after: #{options[:critical]} | load_time=#{critical_time_ms.to_s}\"}",options[:format])
end
begin
warn "JSON Output:\n" + output if options[:debug]
hash = JSON.parse(output)
rescue
bail("{\"pollstatus\":3,\"errormsg\":\"Poller returned nil output\"}",options[:format])
end
if options[:metricdetail].to_s == "verbose"
metrics = ['requests',
'gzipRequests',
'postRequests',
'httpsRequests',
'redirects',
'notFound',
'timeToFirstByte',
'timeToLastByte',
'bodySize',
'contentLength',
'ajaxRequests',
'htmlCount',
'htmlSize',
'cssCount',
'cssSize',
'jsCount',
'jsSize',
'jsonCount',
'jsonSize',
'imageCount',
'imageSize',
'webfontCount',
'webfontSize',
'base64Count',
'base64Size',
'otherCount',
'otherSize',
'cacheHits',
'cacheMisses',
'cachingNotSpecified',
'cachingTooShort',
'cachingDisabled',
'domains',
'maxRequestsPerDomain',
'medianRequestsPerDomain',
'DOMqueries',
'DOMqueriesById',
'DOMqueriesByClassName',
'DOMqueriesByTagName',
'DOMqueriesByQuerySelectorAll',
'DOMinserts',
'DOMqueriesDuplicated',
'eventsBound',
'headersCount',
'headersSentCount',
'headersRecvCount',
'headersSize',
'headersSentSize',
'headersRecvSize',
'documentWriteCalls',
'evalCalls',
'jQueryOnDOMReadyFunctions',
'jQuerySizzleCalls',
'assetsNotGzipped',
'assetsWithQueryString',
'smallImages',
'multipleRequests',
'timeToFirstCss',
'timeToFirstJs',
'timeToFirstImage',
'onDOMReadyTime',
'onDOMReadyTimeEnd',
'windowOnLoadTime',
'windowOnLoadTimeEnd',
'httpTrafficCompleted',
'windowAlerts',
'windowConfirms',
'windowPrompts',
'consoleMessages',
'cookiesSent',
'cookiesRecv',
'domainsWithCookies',
'documentCookiesLength',
'documentCookiesCount',
'bodyHTMLSize',
'iframesCount',
'imagesWithoutDimensions',
'commentsSize',
'hiddenContentSize',
'whiteSpacesSize',
'DOMelementsCount',
'DOMelementMaxDepth',
'nodesWithInlineCSS',
'globalVariables',
'jsErrors',
'localStorageEntries',
'smallestResponse',
'biggestResponse',
'fastestResponse',
'slowestResponse',
'medianResponse']
elsif options[:metricdetail].to_s == "minimal"
metrics = ['timeToFirstByte',
'httpTrafficCompleted']
else
metrics = ['timeToFirstByte',
'httpTrafficCompleted',
'contentLength',
'bodySize',
'domains',
'requests',
'redirects',
'notFound']
end
metriccount = 0
if options[:format].to_s == "plain"
metrics.each { |metric|
metricvalue = hash['metrics'][metric]
puts metric.downcase + "\t#{metricvalue}\t#{nowstamp}\n"
}
elsif options[:format].to_s == "json"
print "[{"
metrics.each { |metric|
metricvalue = hash['metrics'][metric]
print "\"" + metric.downcase + "\":[ #{metricvalue}, #{nowstamp}]"
metriccount += 1
if metriccount != metrics.length
print ","
end
}
print "}]"
end
bail("{\"pollstatus\":0,\"errormsg\":\"Great success!\"}",options[:format])