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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
# File 'lib/morpheus/cli/health_command.rb', line 24
def get(args)
options = {}
params = {}
live_health = false
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[-a] [options]")
opts.on('-a', '--all', "Display all details: CPU, Memory, Database, etc." ) do
options[:details] = true
options[:show_cpu] = true
options[:show_memory] = true
options[:show_database] = true
options[:show_elastic] = true
options[:show_queue] = true
end
opts.on('--details', '--details', "Display all details: CPU, Memory, Database, etc." ) do
options[:details] = true
options[:show_cpu] = true
options[:show_memory] = true
options[:show_database] = true
options[:show_elastic] = true
options[:show_queue] = true
end
opts.add_hidden_option('--details') opts.on('--cpu', "Display CPU details" ) do
options[:show_cpu] = true
end
opts.on('--memory', "Display Memory details" ) do
options[:show_memory] = true
end
opts.on('--database', "Display Database details" ) do
options[:show_database] = true
end
opts.on('--elastic', "Display Elasticsearch details" ) do
options[:show_elastic] = true
end
opts.on('--queue', "Display Queue (Rabbit) details" ) do
options[:show_queue] = true
end
opts.on('--queues', "Display Queue (Rabbit) details" ) do
options[:show_queue] = true
end
opts.on('--rabbit', "Display Queue (Rabbit) details" ) do
options[:show_queue] = true
end
opts.add_hidden_option('--queues')
opts.add_hidden_option('--rabbit')
opts.on('--live', "Fetch Live Health Data. By default the last cached health data is returned. This also retrieves all elastic indices." ) do
live_health = true
end
build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = "Get appliance health information." + "\n" +
"By default, only the health status and levels are displayed." + "\n" +
"Display more details with the options --cpu, --database, --memory, etc." + "\n" +
"Display all details with the -a option."
end
optparse.parse!(args)
if args.count != 0
raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args}\n#{optparse}"
end
connect(options)
begin
@health_interface.setopts(options)
if options[:dry_run]
print_dry_run(live_health ? @health_interface.dry.live(params) : @health_interface.dry.get(params))
return 0
end
json_response = live_health ? @health_interface.live(params) : @health_interface.get(params)
render_result = render_with_format(json_response, options, 'health')
exit_code = json_response['success'] == true ? 0 : 1
return exit_code if render_result
health = json_response['health']
subtitles = []
if options[:details]
subtitles << "Details"
end
if live_health
subtitles << "(Live)"
end
print_h1 "Morpheus Health", subtitles, options
options.merge!({thin:true})
if health.nil?
print yellow,"No health data returned.",reset,"\n"
return 1
end
if health['elastic'] && health['elastic']['noticeMessage'].to_s != ""
print cyan,health['elastic']['noticeMessage'],reset,"\n"
print "\n"
end
print cyan
health_summary_columns = {
"Overall" => lambda {|it| format_health_status(it['cpu']['status']) rescue '' },
"CPU" => lambda {|it| format_health_status(it['cpu']['status']) rescue '' },
"Memory" => lambda {|it| format_health_status(it['memory']['status']) rescue '' },
"Database" => lambda {|it| format_health_status(it['database']['status']) rescue '' },
"Elastic" => lambda {|it| format_health_status(it['elastic']['status']) rescue '' },
"Queue" => lambda {|it| format_health_status(it['rabbit']['status']) rescue '' },
}
print as_pretty_table(health, health_summary_columns, options)
print "\n"
if health['cpu'] && health['cpu']['status'] != 'ok' && health['cpu']['statusMessage']
status_color = health['cpu']['status'] == 'error' ? red : yellow
print status_color,health['cpu']['statusMessage'],reset,"\n"
end
if health['memory'] && health['memory']['status'] != 'ok' && health['memory']['statusMessage']
status_color = health['memory']['status'] == 'error' ? red : yellow
print status_color,health['memory']['statusMessage'],reset,"\n"
end
if health['database'] && health['database']['status'] != 'ok' && health['database']['statusMessage']
status_color = health['database']['status'] == 'error' ? red : yellow
print status_color,health['database']['statusMessage'],reset,"\n"
end
if health['elastic'] && health['elastic']['status'] != 'ok' && health['elastic']['statusMessage']
status_color = health['elastic']['status'] == 'error' ? red : yellow
print status_color,health['elastic']['statusMessage'],reset,"\n"
end
if health['rabbit'] && health['rabbit']['status'] != 'ok' && health['rabbit']['statusMessage']
status_color = health['rabbit']['status'] == 'error' ? red : yellow
print status_color,health['rabbit']['statusMessage'],reset,"\n"
end
print_h2 "Health Levels", options
print cyan
health_levels_columns = {
"Morpheus CPU" => lambda {|it| format_percent(it['cpu']['cpuLoad'].to_f, 0) rescue '' },
"System CPU" => lambda {|it| format_percent(it['cpu']['cpuTotalLoad'].to_f, 0) rescue '' },
"Morpheus Memory" => lambda {|it| format_percent(it['memory']['memoryPercent'].to_f * 100, 0) rescue '' },
"System Memory" => lambda {|it| format_percent(it['memory']['systemMemoryPercent'].to_f * 100, 0) rescue '' },
"Used Swap" => lambda {|it| format_percent(it['memory']['swapPercent'].to_f * 100, 0) rescue '' },
}
print as_pretty_table(health, health_levels_columns, options)
if options[:show_cpu]
if health['cpu'].nil?
print yellow,"No cpu health information returned.",reset,"\n"
else
print_h2 "CPU", options
print cyan
cpu_columns = {
"Processor Count" => lambda {|it| it['processorCount'] rescue '' },
"Process Time" => lambda {|it| format_human_duration(it['processTime'].to_f / 1000) rescue '' },
"Morpheus CPU" => lambda {|it| (it['cpuLoad'].to_f.round(2).to_s + '%') rescue '' },
"System CPU" => lambda {|it| (it['cpuTotalLoad'].to_f.round(2).to_s + '%') rescue '' },
"System Load" => lambda {|it| (it['systemLoad'].to_f.round(3)) rescue '' },
}
print_description_list(cpu_columns, health['cpu'], options)
end
end
if options[:show_memory]
if health['memory'].nil?
print yellow,"No memory health information returned.",reset,"\n"
else
print_h2 "Memory", options
print cyan
memory_columns = {
"Morpheus Memory" => lambda {|it| format_bytes_short(it['totalMemory']) rescue '' },
"Morpheus Used Memory" => lambda {|it| format_bytes_short(it['usedMemory']) rescue '' },
"Morpheus Free Memory" => lambda {|it| format_bytes_short(it['freeMemory']) rescue '' },
"Morpheus Memory Usage" => lambda {|it| format_percent(it['memoryPercent'].to_f * 100) rescue '' },
"System Memory" => lambda {|it| format_bytes_short(it['systemMemory']) rescue '' },
"System Used Memory" => lambda {|it| format_bytes_short(it['committedMemory']) rescue '' },
"System Free Memory" => lambda {|it| format_bytes_short(it['systemFreeMemory']) rescue '' },
"System Memory Usage" => lambda {|it| format_percent(it['systemMemoryPercent'].to_f * 100) rescue '' },
"System Swap" => lambda {|it| format_bytes_short(it['systemSwap']) rescue '' },
"Free Swap" => lambda {|it| format_bytes_short(it['systemFreeSwap']) rescue '' },
}
print_description_list(memory_columns, health['memory'], options)
end
end
if options[:show_database]
if health['database'].nil?
print yellow,"No database health information returned.",reset,"\n"
else
print_h2 "Database", options
print cyan
database_columns = {
"Lifetime Connections" => lambda {|it| it['stats']['Connections'] rescue '' },
"Aborted Connections" => lambda {|it| it['stats']['Aborted_connects'] rescue '' },
"Max Used Connections" => lambda {|it| it['stats']['Max_used_connections'] rescue '' },
"Max Connections" => lambda {|it| it['maxConnections'] rescue '' },
"Threads Running" => lambda {|it| it['stats']['Threads_running'] rescue '' },
"Threads Connected" => lambda {|it| it['stats']['Threads_connected'] rescue '' },
"Slow Queries" => lambda {|it| it['stats']['Slow_queries'] rescue '' },
"Temp Tables" => lambda {|it| it['stats']['Created_tmp_disk_tables'] rescue '' },
"Handler Read First" => lambda {|it| it['stats']['Handler_read_first'] rescue '' },
"Buffer Pool Free" => lambda {|it| it['stats']['Innodb_buffer_pool_wait_free'] rescue '' },
"Open Tables" => lambda {|it| it['stats']['Open_tables'] rescue '' },
"Table Scans" => lambda {|it| it['stats']['Select_scan'] rescue '' },
"Full Joins" => lambda {|it| it['stats']['Select_full_join'] rescue '' },
"Key Read Requests" => lambda {|it| it['stats']['Key_read_requests'] rescue '' },
"Key Reads" => lambda {|it| it['stats']['Key_reads'] rescue '' },
"Engine Waits" => lambda {|it| it['stats']['Innodb_log_waits'] rescue '' },
"Lock Waits" => lambda {|it| it['stats']['Table_locks_waited'] rescue '' },
"Handler Read Rnd" => lambda {|it| it['stats']['Handler_read_rnd'] rescue '' },
"Engine IO Writes" => lambda {|it| it['stats']['Innodb_data_writes'] rescue '' },
"Engine IO Reads" => lambda {|it| it['stats']['Innodb_data_reads'] rescue '' },
"Engine IO Double Writes" => lambda {|it| it['stats']['Innodb_dblwr_writes'] rescue '' },
"Engine Log Writes" => lambda {|it| it['stats']['Innodb_log_writes'] rescue '' },
"Engine Memory" => lambda {|it| format_bytes_short(it['innodbStats']['largeMemory']) rescue '' },
"Dictionary Memory" => lambda {|it| format_bytes_short(it['innodbStats']['dictionaryMemory']) rescue '' },
"Buffer Pool Size" => lambda {|it| it['innodbStats']['bufferPoolSize'] rescue '' },
"Free Buffers" => lambda {|it| it['innodbStats']['freeBuffers'] rescue '' },
"Database Pages" => lambda {|it| it['innodbStats']['databasePages'] rescue '' },
"Old Pages" => lambda {|it| it['innodbStats']['oldPages'] rescue '' },
"Dirty Page Percent" => lambda {|it| format_percent(it['innodbStats']['dirtyPagePercent'] ? it['innodbStats']['dirtyPagePercent'] : '') rescue '' },
"Max Dirty Pages" => lambda {|it| format_percent(it['innodbStats']['maxDirtyPagePercent'].to_f) rescue '' },
"Pending Reads" => lambda {|it| format_number(it['innodbStats']['pendingReads']) rescue '' },
"Insert Rate" => lambda {|it| format_rate(it['innodbStats']['insertsPerSecond'].to_f) rescue '' },
"Update Rate" => lambda {|it| format_rate(it['innodbStats']['updatesPerSecond'].to_f) rescue '' },
"Delete Rate" => lambda {|it| format_rate(it['innodbStats']['deletesPerSecond'].to_f) rescue '' },
"Read Rate" => lambda {|it| format_rate(it['innodbStats']['readsPerSecond']) rescue '' },
"Buffer Hit Rate" => lambda {|it| format_percent(it['innodbStats']['bufferHitRate'].to_f * 100) rescue '' },
"Read Write Ratio" => lambda {|it|
rw_ratio = ""
begin
total_writes = (it['stats']['Com_update'].to_i) + (it['stats']['Com_insert'].to_i) + (it['stats']['Com_delete'].to_f)
total_reads = (it['stats']['Com_select'].to_i)
if total_writes > 0
rw_ratio = (total_reads.to_f / total_writes.to_f).round(2).to_s
end
rescue => ex
puts ex
end
rw_ratio
},
"Uptime" => lambda {|it| (it['stats']['Uptime'] ? format_human_duration(it['stats']['Uptime'].to_i) : '') rescue '' },
}
print_description_list(database_columns, health['database'], options)
end
end
if options[:show_elastic]
if health['elastic'].nil?
print yellow,"No elastic health information returned.",reset,"\n\n"
else
print_h2 "Elastic", options
print cyan
elastic_columns = {
"Status" => 'status',
"Cluster" => lambda {|it| it['stats']['clusterName'] rescue '' },
"Node Count" => lambda {|it| it['stats']['nodeTotal'] rescue '' },
"Data Nodes" => lambda {|it| it['stats']['nodeData'] rescue '' },
"Shards" => lambda {|it| it['stats']['shards'] rescue '' },
"Primary Shards" => lambda {|it| it['stats']['primary'] rescue '' },
"Relocating Shards" => lambda {|it| it['stats']['relocating'] rescue '' },
"Initializing" => lambda {|it| it['stats']['initializing'] rescue '' },
"Unassigned" => lambda {|it| it['stats']['unassigned'] rescue '' },
"Pending Tasks" => lambda {|it| it['stats']['pendingTasks'] rescue '' },
"Active Shards" => lambda {|it| it['stats']['activePercent'] rescue '' },
}
print_description_list(elastic_columns, health['elastic'], options)
elastic_nodes_columns = [
{"NODE" => lambda {|it| it['name'] } },
{"MASTER" => lambda {|it| it['master'] == '*' } },
{"LOCATION" => lambda {|it| it['ip'] } },
{"RAM" => lambda {|it| it['ramPercent'] } },
{"HEAP" => lambda {|it| it['heapPercent'] } },
{"CPU USAGE" => lambda {|it| it['cpuCount'] } },
{"1M LOAD" => lambda {|it| it['loadOne'] } },
{"5M LOAD" => lambda {|it| it['loadFive'] } },
{"15M LOAD" => lambda {|it| it['loadFifteen'] } }
]
print_h2 "Elastic Nodes"
if health['elastic']['nodes'].nil? || health['elastic']['nodes'].empty?
print yellow,"No nodes found.",reset,"\n\n"
else
print as_pretty_table(health['elastic']['nodes'], elastic_nodes_columns, options)
end
elastic_indices_columns = [
{"Health".upcase => lambda {|it| format_index_health(it['health']) } },
{"Index".upcase => lambda {|it| it['index']} },
{"Status".upcase => lambda {|it| it['status'] } },
{"Primary".upcase => lambda {|it| it['primary'] } },
{"Replicas".upcase => lambda {|it| it['replicas'] } },
{"Doc Count".upcase => lambda {|it| format_number(it['count']) } },
{"Primary Size".upcase => lambda {|it| it['primarySize'] } },
{"Total Size".upcase => lambda {|it| it['totalSize'] } },
]
if health['elastic']['indices'] && health['elastic']['indices'].size > 0
print_h2 "Elastic Indices"
if health['elastic']['indices'].nil? || health['elastic']['indices'].empty?
print yellow,"No indices found.",reset,"\n\n"
else
print cyan
print as_pretty_table(health['elastic']['indices'], elastic_indices_columns, options)
end
else
print_h2 "Bad Elastic Indices"
if health['elastic']['badIndices'].nil? || health['elastic']['badIndices'].empty?
else
print cyan
print as_pretty_table(health['elastic']['badIndices'], elastic_indices_columns, options)
end
end
end
end
if options[:show_queue]
print_h2 "Queue (Rabbit)", options
if health['rabbit'].nil?
print yellow,"No rabbit queue health information returned.",reset,"\n\n"
else
print cyan
rabbit_summary_columns = {
"Status" => lambda {|it|
begin
if it['statusMessage'].to_s != ""
format_health_status(it['status']).to_s + " - " + it['statusMessage']
else
format_health_status(it['status'])
end
rescue => ex
''
end
},
"Queues" => lambda {|it| it['queues'].size rescue '' },
"Busy Queues" => lambda {|it| it['busyQueues'].size rescue '' },
"Error Queues" => lambda {|it| it['errorQueues'].size rescue '' }
}
print_description_list(rabbit_summary_columns, health['rabbit'], options)
print_h2 "Queues"
queue_columns = [
{"Status".upcase => lambda {|it|
status_string = it['status'].to_s.downcase if status_string == 'warning'
"#{yellow}WARNING#{cyan}"
elsif status_string == 'error'
"#{red}ERROR#{cyan}"
elsif status_string == 'ok'
"#{green}OK#{cyan}"
else
it['status']
end
} },
{"Name".upcase => lambda {|it| it['name']} },
{"Count".upcase => lambda {|it| format_number(it['count']) } }
]
if health['rabbit'].nil? || health['rabbit']['queues'].nil? || health['rabbit']['queues'].empty?
print yellow,"No queues found.",reset,"\n\n"
else
print cyan
print as_pretty_table(health['rabbit']['queues'], queue_columns, options)
end
if health['rabbit'].nil? || health['rabbit']['busyQueues'].nil? || health['rabbit']['busyQueues'].empty?
else
print_h2 "Busy Queues"
print cyan
print as_pretty_table(health['rabbit']['busyQueues'], queue_columns, options)
end
if health['rabbit'].nil? || health['rabbit']['errorQueues'].nil? || health['rabbit']['errorQueues'].empty?
else
print_h2 "Error Queues"
print cyan
print as_pretty_table(health['rabbit']['errorQueues'], queue_columns, options)
end
end
end
print "\n"
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|