Jmx4Perl/J4psh/Command/MBean.pm untested

filter: Statementstmt 0.0% 0 / 395 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 32 totaltotal 0.0% 0 / 674 CC 63 SCAR 37.2
CC
63
Cov
0%
cmd_show_mbean
182.0
cmd_show_attributes
56.0
cmd_list_domains
42.0
CRAP 41.5
1 #!/usr/bin/perl
2  
3 package JMX::Jmx4Perl::J4psh::Command::MBean;
4 use strict;
5 use base qw(JMX::Jmx4Perl::J4psh::Command);
6 use JMX::Jmx4Perl::Util;
7 use JMX::Jmx4Perl::Request;
8 use Data::Dumper;
9 use JSON;
10  
11 =head1 NAME
12  
13 JMX::Jmx4Perl::J4psh::Command::MBean - MBean commands
14  
15 =head1 DESCRIPTION
16  
17 =head1 COMMANDS
18  
19 =over
20  
21 =cut
22  
23  
24 # Name of this command
25 sub name { "mbean" }
26  
27 # We hook into as top-level commands
28 sub top_commands {
29 my $self = shift;
30 return $self->agent ? $self->domain_commands : {};
31 }
32  
33 # The 'real' commands
34 sub domain_commands {
35 my $self = shift;
36 return {
37 "ls" => {
38 desc => "List MBean Domains",
39 proc => $self->cmd_list_domains,
40 args => $self->complete->mbeans(all => 1),
41 },
42 "cd" => {
43 desc => "Enter a domain",
44 proc => sub {
45 my $domain = join '',@_;
46 $self->_cd_absolute($domain);
47 },
48 args => $self->complete->mbeans(all => 1),
49 },
50 };
51 }
52  
53  
54 sub property_commands {
55 my $self = shift;
56 my $domain = shift;
57 my $prop_cmds = $self->mbean_commands;
58 return {
59 "ls" => {
60 desc => "List MBeans for a domain",
61 proc => $self->cmd_list_domains($domain),
62 args => $self->complete->mbeans(domain => $domain),
63 },
64 "cd" => {
65 desc => "Enter a MBean",
66 proc => sub {
67 #print Dumper([@_]);
68 #print Devel::StackTrace->new->as_string;
69 my $input = join '',@_;
70 if (!$self->_handle_navigation($input)) {
71 if ($input =~ /:/) {
72 $self->_cd_absolute($input);
73 } else {
74 $self->_cd_mbean($domain,$input);
75 }
76 }
77 },
78 args => $self->complete->mbeans(domain => $domain)
79 },
80 "pwd" => {
81 desc => "Print currently selected domain",
82 proc => sub {
83 my ($s,$r) = $self->color("domain_name","reset");
84 print $s . $domain . $r,":\n";
85 }
86 }
87 };
88 }
89  
90 sub mbean_commands {
91 my $self = shift;
92 my $mbean_props = shift;
93 return {
94 "ls" => {
95 desc => "List MBeans for a domain.",
96 doc => <<EOT,
97 List all MBeans within a domain.
98  
99 The following options are supported:
100  
101 -a: Attributes only
102 -o: Operations only
103  
104 Wildcards are supported for filtering
105 EOT
106 proc => $self->cmd_show_mbean($mbean_props),
107 #args => $self->complete->mbean_attribs($mbean_props),
108 },
109 "cd" => {
110 desc => "Navigate up (..), to the top (/) or directly to another MBean",
111 proc => sub {
112 my $input = join '',@_;
113 if (!$self->_handle_navigation($input)) {
114 if ($input =~ /:/) {
115 # "Absolute path"
116 $self->_cd_absolute($input);
117 } else {
118 die "No MBean '",$input,"' known\n";
119 }
120 };
121 },
122 },
123 "cat" => {
124 desc => "Show value of an attribute",
125 proc => $self->cmd_show_attributes($mbean_props),
126 args => $self->complete->mbean_attributes($mbean_props),
127 },
128 "set" => {
129 desc => "Set value of an attribute",
130 proc => $self->cmd_set_attribute($mbean_props),
131 args => $self->complete->mbean_attributes($mbean_props),
132 },
133
134 "exec" => {
135 desc => "Execute an operation",
136 proc => $self->cmd_execute_operation($mbean_props),
137 args => $self->complete->mbean_operations($mbean_props),
138 },
139 "pwd" => {
140 desc => "Show the currently selected MBean",
141 proc => sub {
142 my ($d,$k,$v,$r) = $self->color("domain_name","property_key","property_value","reset");
143 print $d . $mbean_props->{domain} . $r . ":" . $self->_color_props($mbean_props) . "\n";
144 }
145 }
146 };
147 }
148  
149 sub cmd_show_attributes {
150 my $self = shift;
151 my $m_info = shift;
152 return sub {
153 my $attributes = @_;
154 my $info = $m_info->{info};
155 my $mbean = $m_info->{full};
156 my $context = $self->context;
157 my $agent = $context->agent;
158 my @attrs = ();
159 for my $a (@_) {
160 if ($a =~ /[\*\?]/) {
161 my $regexp = $self->convert_wildcard_pattern_to_regexp($a);
162 push @attrs, grep { $_ =~ /^$regexp$/ } keys %{$m_info->{info}->{attr}};
163 } else {
164 push @attrs,$a;
165 }
166 }
167 # Use only unique values
168 my %attrM = map { $_ => 1 } @attrs;
169 @attrs = keys %attrM;
170 if (@attrs == 0) {
171 die "No attribute given\n";
172 }
173 my $request = JMX::Jmx4Perl::Request->new(READ,$mbean,\@attrs,{ignoreErrors => 1});
174 my $response = $agent->request($request);
175 if ($response->is_error) {
176 die "Error: " . $response->error_text;
177 }
178 my $values = $response->value;
179 my $p = "";
180 my ($c_a,$c_r) = $self->color("attribute_name","reset");
181 if (@attrs > 1) {
182 # Print as list
183 for my $attr (@attrs) {
184 my $value = $values->{$attr};
185 if ($self->_is_object($value)) {
186 $p .= sprintf(" $c_a%-31.31s$c_r\n",$attr);
187 $p .= $self->_dump($value);
188 } else {
189 $p .= sprintf(" $c_a%-31.31s$c_r %s\n",$attr,$self->_dump_scalar($value));
190 }
191 }
192 } else {
193 # Print single attribute
194 my $value = $values->{$attrs[0]};
195 if ($self->_is_object($value)) {
196 $p .= $self->_dump($value);
197 } else {
198 $p .= $self->_dump_scalar($value)."\n";
199 }
200 }
201 $self->print_paged($p);
202 };
203  
204 }
205  
206 sub cmd_set_attribute {
207 my $self = shift;
208 my $m_info = shift;
209 return sub {
210 my @args = @_;
211 die "Usage: set <attribute-name> <value> [<path>]\n" if (@args != 2 && @args != 3);
212 my $mbean = $m_info->{full};
213 my $agent = $self->context->agent;
214 my $req = new JMX::Jmx4Perl::Request(WRITE,$mbean,$args[0],$args[1],$args[2]);
215 my $resp = $agent->request($req);
216 if ($resp->is_error) {
217 die $resp->error_text . "\n";
218 }
219 my $old_value = $resp->value;
220 my ($c_l,$c_r) = $self->color("label","reset");
221  
222 my $p = "";
223 if ($self->_is_object($old_value)) {
224 $p .= sprintf(" $c_l%-5.5ss$c_r\n","Old:");
225 $p .= $self->_dump($old_value);
226 } else {
227 $p .= sprintf(" $c_l%-5.5s$c_r %s\n","Old:",$self->_dump_scalar($old_value));
228 }
229 $p .= sprintf(" $c_l%-5.5s$c_r %s\n","New:",$args[1]);;
230 $self->print_paged($p);
231 }
232 }
233  
234 sub cmd_execute_operation {
235 my $self = shift;
236 my $m_info = shift;
237 return sub {
238 my @args = @_;
239 die "Usage: exec <attribute-name> <value> [<path>]\n" if (!@args);
240 my $mbean = $m_info->{full};
241 my $agent = $self->context->agent;
242 my $req = new JMX::Jmx4Perl::Request(EXEC,$mbean,@args,{ignoreErrors => 1});
243 my $resp = $agent->request($req);
244 if ($resp->is_error) {
245 die $resp->error_text . "\n";
246 }
247 my $value = $resp->value;
248 my ($c_l,$c_r) = $self->color("label","reset");
249  
250 my $p = "";
251 if ($self->_is_object($value)) {
252 $p .= sprintf(" $c_l%-7.7s$c_r\n","Return:");
253 $p .= $self->_dump($value);
254 } else {
255 $p .= sprintf(" $c_l%-7.7s$c_r %s\n","Return:",$self->_dump_scalar($value));
256 }
257 $self->print_paged($p);
258 }
259 }
260  
261 sub _is_object {
262 my $self = shift;
263 my $value = shift;
264 return JMX::Jmx4Perl::Util->is_object_to_dump($value);
265 }
266  
267 sub _dump {
268 my $self = shift;
269 my $value = shift;
270 return JMX::Jmx4Perl::Util->dump_value($value,{format => $self->_get_opt_or_config("format"),
271 booleans => $self->_get_opt_or_config("booleans"),
272 indent => $self->_get_opt_or_config("indent")});
273 }
274  
275 sub _dump_scalar {
276 my $self = shift;
277 return JMX::Jmx4Perl::Util->dump_scalar(shift,$self->_get_opt_or_config("booleans"));
278 }
279  
280  
281 sub _get_opt_or_config {
282 my $self = shift;
283 my $key = shift;
284  
285 my $args = $self->context->args || {};
286 my $config = $self->context->config || {};
287 if (defined($args->{option}) && defined($args->{option}->{lc $key})) {
288 return $args->{option}->{lc $key};
289 } else {
290 my $shell_config = $config->{shell} || {};
291 return $shell_config->{lc $key};
292 }
293 }
294  
295  
296 # ===================================================================================================
297  
298 =item cmd_list
299  
300 List commands which can filter mbean by wildcard and knows about the
301 following options:
302  
303 =over
304  
305 =item -l
306  
307 Show attributes and operations
308  
309 =back
310  
311 If a single mbean is given as argument its details are shown.
312  
313 =cut
314  
315 sub cmd_list_domains {
316 my $self = shift;
317 my $domain = shift;
318 return sub {
319 my $context = $self->context;
320 my $agent = $context->agent;
321 print "Not connected to a server\n" and return unless $agent;
322 my ($opts,@filters) = $self->extract_command_options(["l!"],@_);
323 if ($domain) {
324 if (@filters) {
325 @filters = map { $domain . ":" .$_ } @filters
326 } else {
327 @filters = "$domain:*";
328 }
329 }
330 # Show all
331 if (@filters) {
332 for my $filter (@filters) {
333 my $regexp = $self->convert_wildcard_pattern_to_regexp($filter);
334 my $mbean_filter;
335 ($filter,$mbean_filter) = ($1,$2) if ($filter && $filter =~ /(.*?):(.*)/) ;
336 # It's a domain (pattern)
337 $self->show_domain($opts,$self->_filter($context->mbeans_by_domain,$filter),$mbean_filter);
338 }
339 } else {
340 $self->show_domain($opts,$self->_filter($context->mbeans_by_domain));
341 }
342 }
343 }
344  
345 sub cmd_show_mbean {
346 my $self = shift;
347 my $m_info = shift;
348 return sub {
349 my $info = $m_info->{info};
350 my ($c_m,$c_a,$c_o,$c_r) = $self->color("mbean_name","attribute_name","operation_name","reset");
351 my $op_len = 50 + length($c_o) + length($c_r);
352 my ($do_show_attrs,$do_show_ops,$filters) = $self->_show_what_from_mbean($info,@_);
353  
354 my $p = "";
355
356 my $name = $m_info->{full};
357 $p .= $c_m . $name . $c_r;
358 $p .= "\n\n";
359
360 #print Dumper($m_info);
361 my $attrs = $info->{attr};
362 if ($do_show_attrs) {
363 my @lines = ();
364 for my $attr (keys %$attrs) {
365 my $line = "";
366 if ($self->_pass_filter($attr,$filters)) {
367 if (length($attr) > 31) {
368 $line .= sprintf(" $c_a%s$c_r\n",$attr);
369 $line .= sprintf(" %-31.31s %-13.13s %-4.4s %s\n",
370 $self->_pretty_print_type($attrs->{$attr}->{type}),
371 $attrs->{$attr}->{rw} eq "false" ? "[ro]" : "",$attrs->{$attr}->{desc});
372 } else {
373 $line .= sprintf(" $c_a%-31.31s$c_r %-13.13s %-4.4s %s\n",$attr,
374 $self->_pretty_print_type($attrs->{$attr}->{type}),
375 $attrs->{$attr}->{rw} eq "false" ? "[ro]" : "",$attrs->{$attr}->{desc});
376 }
377 push @lines,$line;
378 }
379 }
380 if (@lines) {
381 $p .= "Attributes:\n";
382 $p .= join "",@lines;
383 $p .= "\n";
384 }
385 }
386 my $ops = $info->{op};
387 if ($do_show_ops) {
388 my @lines = ();
389 for my $op (keys %$ops) {
390 my $line = "";
391 if ($self->_pass_filter($op,$filters)) {
392 my $overloaded = ref($ops->{$op}) eq "ARRAY" ? $ops->{$op} : [ $ops->{$op} ];
393 for my $m_info (@$overloaded) {
394 my $sig = $self->_signature_to_print($op,$m_info);
395 if (length($sig) > $op_len) {
396 $line .= sprintf(" %s\n",$sig);
397 $line .= sprintf(" %-50.50s %s\n","",$m_info->{desc}) if $m_info->{desc};
398 } else {
399 $line .= sprintf(" %-${op_len}.${op_len}s %s\n",$sig,$m_info->{desc});
400 }
401 }
402 push @lines,$line;
403 }
404 }
405 if (@lines) {
406 $p .= "Operations:\n";
407 $p .= join "",@lines;
408 $p .= "\n";
409 }
410 }
411 $self->print_paged($p);
412 #print Dumper($info);
413 }
414 }
415  
416 sub _pass_filter {
417 my $self = shift;
418 my $check = shift;
419 my $regexps = shift;
420 return 1 unless @$regexps;
421 for my $regexp (@$regexps) {
422 return 1 if $check =~ $regexp;
423 }
424 return 0;
425 }
426  
427 sub _show_what_from_mbean {
428 my $self = shift;
429 my ($info,@args) = @_;
430 my ($opts,@filter) = $self->extract_command_options(["attributes|a!","operations|ops|o!"],@args);
431 my $no_restrict = !defined($opts->{attributes}) && !defined($opts->{operations});
432 my $show_attrs = $info->{attr} && keys %{$info->{attr}} && ($opts->{attributes} || $no_restrict);
433 my $show_ops = $info->{op} && keys %{$info->{op}} && ($opts->{operations} || $no_restrict);
434 my @filter_regexp = map {
435 s/\*/.*/g;
436 s/\?/./g;
437 my $f = '^' . $_ . '$';
438 qr/$f/i
439 } @filter;
440 return ($show_attrs,$show_ops,\@filter_regexp);
441 }
442  
443 sub _line_aligned {
444 my $self = shift;
445 my $max_lengths = shift;
446 my $lengths = shift;
447 my $parts = shift;
448 my $opts = shift;
449
450 my $term_width = $self->context->term_width;
451 my $overflow = $opts->{overflow_col} || 0;
452 my $wrap_last = $opts->{wrap};
453 my $ret = "";
454 for my $i (0 .. $overflow) {
455 if ($lengths->[$i] > $max_lengths->[$i]) {
456
457 # Do overflow
458 }
459 }
460
461 }
462  
463 sub _signature_to_print {
464 my $self = shift;
465 my $op = shift;
466 my $info = shift;
467 my ($c_o,$c_r) = $self->color("operation_name","reset");
468 # print Dumper($info);
469 my $ret = $self->_pretty_print_type($info->{ret}) . " ";
470 $ret .= $c_o . $op . $c_r;
471 $ret .= "(";
472 my $args = $info->{args};
473 my @arg_cl = ();
474 for my $a (@$args) {
475 if (ref($a) eq "HASH") {
476 push @arg_cl,$self->_pretty_print_type($a->{type})
477 } else {
478 push @arg_cl,$self->_pretty_print_type($a);
479 }
480 }
481 $ret .= join ",",@arg_cl;
482 $ret .= ")";
483 return $ret;
484 }
485  
486 sub _pretty_print_type {
487 my $self = shift;
488 my $type = shift;
489 my $suffix = "";
490 my $type_p;
491 if ($type eq "[J") {
492 return "long[]";
493 } elsif ($type =~ /^\[L(.*);/) {
494 $type_p = $1;
495 $suffix = "[]";
496 } else {
497 $type_p = $type;
498 }
499 $type_p =~ s/^.*\.([^\.]+)$/$1/;
500 return $type_p . $suffix;
501 }
502  
503 sub show_mbeans {
504 my $self = shift;
505 my $opts = shift;
506 my $infos = shift;
507 my $mbean_filter;
508 my $l = "";
509 for my $m_info (sort { $a->{string} cmp $b->{string} } values %$infos) {
510 my ($c_d,$c_s,$c_r) = $self->color("domain_name","stat_val","reset");
511 $l .= $c_d . $m_info->{domain} . $c_r . ":";
512 $l .= $self->_color_props($m_info) . "\n";
513 }
514 $self->print_paged($l);
515 }
516  
517  
518 sub show_domain {
519 my $self = shift;
520 my $opts = shift;
521 my $infos = shift;
522  
523 my $mbean_filter = shift;
524 $mbean_filter = $self->convert_wildcard_pattern_to_regexp($mbean_filter) if $mbean_filter;
525 my $text = "";
526 for my $domain (keys %$infos) {
527 my ($c_d,$c_reset) = $self->color("domain_name","reset");
528 $text .= $c_d . "$domain:" . $c_reset . "\n";
529 for my $m_info (sort { $a->{string} cmp $b->{string} } @{$infos->{$domain}}) {
530 next if ($mbean_filter && $m_info->{string} !~ $mbean_filter);
531 $text .= " ".$self->_color_props($m_info)."\n";
532 $text .= $self->_list_details(" ",$m_info) if $opts->{l};
533 }
534 $text .= "\n";
535 }
536 $self->print_paged($text);
537 }
538  
539 sub _list_details {
540 my $self = shift;
541 my $indent = shift;
542 my $m_info = shift;
543 my ($c_s,$c_r) = $self->color("stat_val","reset");
544  
545 my $line = "";
546 if ($m_info->{info}->{desc}) {
547 $line .= $m_info->{info}->{desc};
548 }
549 my $nr_attr = scalar(keys %{$m_info->{info}->{attr}});
550 my $nr_op = scalar(keys %{$m_info->{info}->{op}});
551 my $nr_notif = scalar(keys %{$m_info->{info}->{notif}});
552 if ($nr_attr || $nr_op || $nr_notif) {
553 my @f;
554 push @f,"Attributes: " . $c_s . $nr_attr . $c_r if $nr_attr;
555 push @f,"Operations: " . $c_s . $nr_op . $c_r if $nr_op;
556 push @f,"Notifications: " . $c_s . $nr_notif . $c_r if $nr_notif;
557 $line .= $indent . join(", ",@f) . "\n";
558 }
559 return $line;
560 }
561  
562 sub _color_props {
563 my $self = shift;
564 my $info = shift;
565 my ($c_k,$c_v,$c_r) = $self->color("property_key","property_value","reset");
566 #return Dumper($info);
567 return join ",",map { $c_k . $_ . $c_r . "=" . $c_v . $info->{props}->{$_} . $c_r } sort keys %{$info->{props}};
568 }
569  
570 sub _filter {
571 my $self = shift;
572 my $map = shift;
573 my @filters = @_;
574 my @keys = keys %{$map};
575  
576 if (@filters) {
577 my %filtered;
578 for my $f (@filters) {
579 my $regexp = $self->convert_wildcard_pattern_to_regexp($f);
580 for my $d (@keys) {
581 $filtered{$d} = $map->{$d} if $d =~ $regexp;
582 }
583 }
584 return \%filtered;
585 } else {
586 return $map;
587 }
588 }
589  
590 =back
591  
592 =cut
593  
594 sub _cd_absolute {
595 my $self = shift;
596 my $domain = shift;
597 my $props;
598  
599 if ($domain) {
600 $domain =~ s/:+$//;
601 ($domain,$props) = split(/:/,$domain,2) if $domain =~ /:/;
602 }
603 die "No domain $domain\n" unless $self->_check_domain($domain);
604 $self->_check_mbean($domain,$props) if $props;
605 $self->reset_stack;
606 $self->_cd_domain($domain);
607 $self->_cd_mbean($domain,$props) if $props;
608 }
609  
610 sub _check_mbean {
611 my $self = shift;
612 my $domain = shift;
613 my $props = shift;
614 $self->_get_mbean($domain,$props) || die "No MBean $domain:$props\n";
615 }
616  
617 sub _cd_domain {
618 my $self = shift;
619 my $domain = shift;
620 die "No domain $domain\n" unless $self->_check_domain($domain);
621 my $prop_cmds = $self->property_commands($domain);
622 &{$self->push_on_stack($domain,$prop_cmds,":")};
623 }
624  
625 sub _cd_mbean {
626 my $self = shift;
627 my $domain = shift;
628 my $mbean = shift;
629
630 my $mbean_props = $self->_check_mbean($domain,$mbean);
631 my $mbean_cmds = $self->mbean_commands($mbean_props);
632 &{$self->push_on_stack($mbean_props->{prompt},$mbean_cmds)};
633 }
634  
635 sub _check_domain {
636 my $self = shift;
637 my $domain = shift;
638 my $context = $self->context;
639 return exists($context->mbeans_by_domain->{$domain});
640  
641 }
642  
643 sub _get_mbean {
644 my $self = shift;
645 my $domain = shift;
646 my $props = shift;
647 my $context = $self->context;
648 if ($props =~ /\*/) {
649 my $mbeans = $context->search_mbeans($domain . ":" . $props);
650 # TODO: If more than one, present a menu to select from. Now simply die
651 return undef unless @{$mbeans};
652 if (scalar(@$mbeans) > 1) {
653 my $toomany = "";
654 for my $m (@$mbeans) {
655 my ($s,$r) = $self->color("mbean_name","reset");
656 $toomany .=" >>> " . $s . $m->{full} . $r . "\n";
657 }
658 die "More than one MBean found:\n" . $toomany;
659 }
660 return $mbeans->[0];
661 }
662 return $context->mbeans_by_name->{$domain . ":" . $props};
663 }
664  
665 # Handle navigational commands
666 sub _handle_navigation {
667 my $self = shift;
668 my $input = shift;
669 if ($input eq "..") {
670 $self->pop_off_stack;
671 return 1;
672 } elsif ($input eq "/" || !$input) {
673 $self->reset_stack;
674 return 1;
675 } else {
676 return 0;
677 }
678 }
679  
680 sub _filter_domains {
681  
682 };
683  
684  
685 =head1 LICENSE
686  
687 This file is part of osgish.
688  
689 Osgish is free software: you can redistribute it and/or modify
690 it under the terms of the GNU General Public License as published by
691 the Free Software Foundation, either version 2 of the License, or
692 (at your option) any later version.
693  
694 osgish is distributed in the hope that it will be useful,
695 but WITHOUT ANY WARRANTY; without even the implied warranty of
696 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
697 GNU General Public License for more details.
698  
699 You should have received a copy of the GNU General Public License
700 along with osgish. If not, see <http://www.gnu.org/licenses/>.
701  
702 A commercial license is available as well. Please contact roland@cpan.org for
703 further details.
704  
705 =head1 PROFESSIONAL SERVICES
706  
707 Just in case you need professional support for this module (or JMX or OSGi in
708 general), you might want to have a look at www.consol.com Contact
709 roland.huss@consol.de for further information (or use the contact form at
710 http://www.consol.com/contact/)
711  
712 =head1 AUTHOR
713  
714 roland@cpan.org
715  
716 =cut
717  
718  
719  
720 1;
721