Imported Upstream version 1.12

This commit is contained in:
Mario Fetka
2017-10-31 14:38:28 +01:00
commit ae1fc8494f
157 changed files with 35016 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
# ================================================
# Base definitions:
# Check for relative memory checks
<Check base_memory_relative>
Use = base_relative_threshold($0,$1)
Use = base_relative_label
Label = (base) $BASE
Unit = B
</Check>
<Check base_relative_threshold>
Critical = ${0:90}
Warning = ${1:80}
</Check>
<Check base_relative_label>
Label = (grandpa) %.2r% used (%.2v %u / %.2b %w)
</Check>

50
it/check_jmx4perl/base.pl Normal file
View File

@@ -0,0 +1,50 @@
# Base functions for various check_jmx4perl checks
use strict;
use FindBin;
use JMX::Jmx4Perl::Alias;
use JMX::Jmx4Perl::Request;
use JMX::Jmx4Perl::Response;
sub exec_check_perl4jmx {
my @args;
for (@_) {
push @args,split;
}
my ($url,$user,$password,$product,$target,$target_user,$target_password) =
@ENV{"JMX4PERL_GATEWAY","JMX4PERL_USER",
"JMX4PERL_PASSWORD","JMX4PERL_PRODUCT","JMX4PERL_TARGET_URL","JMX4PERL_TARGET_USER","JMX4PERL_TARGET_PASSWORD"};
push @args,("--user",$user,"--password",$password) if $user;
push @args,("--product",$product) if $product;
push @args,("--url",$url);
push @args,("--target",$target) if $target;
push @args,("--target-user",$target_user,"--target-password",$target_password) if $target_user;
#push @args,"--legacy-escape";
#push @args,("--verbose");
my $cmd = "perl $FindBin::Bin/../../scripts/check_jmx4perl "
.join(" ",map { '"' . $_ . '"' } @args);
#print $cmd,"\n";
open (F,"$cmd 2>&1 |")
|| die "Cannot open check_jmx4perl: $!";
my $content = join "",<F>;
close F;
if ($? == -1) {
die "check_jmx4perl: failed to execute: $!\n";
}
elsif ($? & 127) {
die "check_jmx4perl child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
return ($? >> 8,$content);
}
sub reset_history {
my $jmx = shift;
my ($mbean,$operation) = $jmx->resolve_alias(JMX4PERL_HISTORY_RESET);
my $req = new JMX::Jmx4Perl::Request(EXEC,$mbean,$operation,{target => undef});
my $resp = $jmx->request($req);
}
1;

View File

@@ -0,0 +1,195 @@
# Include base configuration
include base.cfg
# ==================================================================
# Various parameterized checks
<Check outer_arg>
Use = memory_heap
Critical = 90
Label = $0 $BASE (Warning: %.2y, Critical: %.2z)
</Check>
# ==================================================================
# Predefined Checks
# Heap Memory
<Check memory_heap>
Use = base_memory_relative
Value = java.lang:type=Memory/HeapMemoryUsage/used
Base = java.lang:type=Memory/HeapMemoryUsage/max
Name = Heap Memory ${0:default_name}
Label = Heap-Memory: $BASE
</Check>
<Check memory_heap2>
Use = base_memory_relative
MBean = java.lang:type=Memory
Attribute = HeapMemoryUsage
Path = used
BaseMBean = java.lang:type=Memory
BaseAttribute = HeapMemoryUsage
BasePath = max
Name = Heap Memory ${0:default_name}
Label = Heap-Memory: $BASE
</Check>
<Check memory_heap_with_label>
Value = java.lang:type=Memory/HeapMemoryUsage/used
Name = $1
Label = $0
Critical = 1:
</Check>
# Perm Gen Memory (used for class definitions)
<Check memory_non_heap>
Use = base_memory_relative($0,$1)
Value = java.lang:type=Memory/NonHeapMemoryUsage/used
Base = java.lang:type=Memory/HeapMemoryUsage/max
Label = NonHeap Memory: $BASE
</Check>
# ===============================================
# Thread count
<Check thread_count>
Value = java.lang:type=Threading/ThreadCount
Name = ${0} $1 $2
Label = "thread_count: $0 $1 $2 : Value %f in range"
Critical = ${0}
Warning = $1
Method = POST
</Check>
<Check invalid_method>
Value = java.lang:type=Threading/ThreadCount
Name = $0 $1 $2
Critical = $0
Warning = $1
Method = Bla
</Check>
# Child
<Check def_placeholder_1>
Use thread_count(,2)
</Check>
<Check def_placeholder_2>
Use thread_count(${0},2)
</Check>
<Check def_placeholder_3>
Use thread_count
</Check>
# =========================================================
# Operation checks
<Check overloaded_operation>
MBean = jolokia.it:type=operation
Operation = overloadedMethod(java.lang.String)
Argument = ${0}
Critical = 5
Warning = :1
</Check>
# =========================================================
# Bug specific checks
# MBean with '#'
<Check hash_check>
MBean = jolokia/it:pid=[ServiceRegistryProvider\#(null)],type=ParticipantMonitor,id=*
Attribute = Ok
String = 1
Label = ServiceRegistryProvider is running
Name = Running
Critical = !OK
</Check>
# Scripting check
<Check script_check>
Script <<EOT
my $pools = $j4p->search("java.lang:type=MemoryPool,*");
my @matched_pools;
my $pattern = "${0}";
for my $pool (@$pools) {
push @matched_pools,$pool if $pool =~ /$pattern/;
}
return $j4p->get_attribute($matched_pools[0],"Usage","used");
EOT
Name script_check $0
Critical ${1:10}
Unit B
</Check>
<MultiCheck script_multi_check>
Check memory_heap(90,80)
Check script_check('Eden|Java',1000000000)
Check memory_non_heap(90,80)
Check script_check($0,1000000000)
Check thread_count(1000,2000,3000)
</MultiCheck>
# Double values below a threshold
<Check double_min>
Name = double_min
MBean = jolokia.it:type=attribute
Attribute = DoubleValueMin
Critical = 1
Warning = 2
</Check>
<Check double_max>
Name = double_max
MBean = jolokia.it:type=attribute
Attribute = DoubleValueMax
Critical = 1
Warning = 2
</Check>
<Check with_name>
Use = thread_count
Critical = $1
Name = $0
</Check>
<Check without_threshold>
Use = thread_count
</Check>
# =================================
# #81699
# Find deadlocked Threads
<Check thread_deadlock>
MBean = java.lang:type=Threading
Operation = findDeadlockedThreads
Null = no deadlock
Name = Thread-Deadlock
String = 1
Critical = !no deadlock
</Check>
<Check counter_operation>
MBean jolokia.it:type=operation
Operation fetchNumber
Argument ${0:inc}
Critical 3
Warning 2
</Check>
# 75062
<Check memory_without_perfdata>
Use = base_memory_relative($0,$1)
Value = java.lang:type=Memory/HeapMemoryUsage/used
Base = java.lang:type=Memory/HeapMemoryUsage/max
Name = Heap Memory ${0:default_name}
Label = Heap-Memory: $BASE
PerfData = ${2:No}
</Check>
<Check memory_with_perfdata>
Use = memory_without_perfdata(90,80,'yes')
</Check>

View File

@@ -0,0 +1,64 @@
include checks.cfg
# =======================================================================
# Multi checks to check
<MultiCheck memory>
Check memory_non_heap
Check memory_heap
</MultiCheck>
<MultiCheck nested>
Check thread_count(400,,"'Thread-Count'")
# Multi-Check referenced via 'Check'
Check memory
</MultiCheck>
<MultiCheck with_inner_args>
Check thread_count(400)
Check memory_heap_with_label("HelloLabel","WithInnerArgs")
</MultiCheck>
<MultiCheck with_outer_args>
Check thread_count(400)
Check memory_heap_with_label("HelloLabel",$0)
</MultiCheck>
<MultiCheck failing_multi_check>
HtmlOutput
Check memory_non_heap(1,2)
Check memory_non_heap(30,20)
Check memory_heap(1,2)
</MultiCheck>
<MultiCheck error_multi_check>
Check memory_heap
Check kaputt
Check memory_heap(1,2)
</MultiCheck>
<Check kaputt>
MBean bla:type=blub
Attribute foobar
</Check>
<MultiCheck nested_with_args>
MultiCheck with_outer_args("NestedWithArgs")
</MultiCheck>
<MultiCheck nested_with_outer_args>
# MulitCheck referenced via Check
Check with_outer_args($0)
</MultiCheck>
<MultiCheck overloaded_multi_check>
Check overloaded_operation("blub")
</MultiCheck>
# Multicheck where the checks have different arguments
# but are otherwise the same checks.
<MultiCheck label_test>
Check with_name("bla",1)
Check with_name("blub",2)
</MultiCheck>

62
it/it.pl Executable file
View File

@@ -0,0 +1,62 @@
#!/usr/bin/perl
use FindBin;
use lib "$FindBin::Bin/../lib";
use Getopt::Long;
use strict;
use TAP::Harness;
use Data::Dumper;
my $dir = $FindBin::Bin . "/t";
my ($gateway_url,$user,$password,$product,$target_url,$target_user,$target_password);
GetOptions("dir=s" => \$dir,
"url=s" => \$gateway_url,
"target=s" => \$target_url,
"target-user=s" => \$target_user,
"target-password=s" => \$target_password,
"user=s" => \$user,
"password=s" => \$password,
"product=s" => \$product);
die "No gateway url given. Please use option '--url' for pointing to the server with the agent installed\n" unless $gateway_url;
my @testfiles;
if (@ARGV) {
@testfiles = prepare_filenames(@ARGV);
} else {
opendir(D,$dir) || die "Cannot open test dir $dir : $!";
@testfiles = prepare_filenames(grep { /\.t$/ } map { $dir . "/" . $_ } readdir(D));
closedir D;
}
my $harness = new TAP::Harness
({
verbosity => 1,
timer => 1,
show_count => 0,
color => 1,
merge => 1,
jobs => 1,
lib => [ "$FindBin::Bin/../lib", "$FindBin::Bin/../t/lib", "$FindBin::Bin" ]
});
$ENV{JMX4PERL_GATEWAY} = $gateway_url;
$ENV{JMX4PERL_TARGET_URL} = $target_url;
$ENV{JMX4PERL_TARGET_USER} = $target_user;
$ENV{JMX4PERL_TARGET_PASSWORD} = $target_password;
$ENV{JMX4PERL_USER} = $user;
$ENV{JMX4PERL_PASSWORD} = $password;
$ENV{JMX4PERL_PRODUCT} = $product;
$harness->runtests(@testfiles);
sub prepare_filenames {
my @files = @_;
my @ret = ();
for (@files) {
my $name = $_;
$name =~ s|.*/([^/]+)$|$1|;
push @ret,[ $_, $name ];
}
return @ret;
}

29
it/t/01_version.t Normal file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/perl
use It;
use Test::More tests => 1;
use strict;
use JMX::Jmx4Perl::Request;
use JMX::Jmx4Perl;
use Data::Dumper;
my $jmx = new It(verbose => 0)->jmx4perl;
my $resp = $jmx->request(new JMX::Jmx4Perl::Request(AGENT_VERSION));
my $value = $resp->{value};
my $version_exp = $JMX::Jmx4Perl::VERSION;
my ($base,$ext) = ($1,$3) if $version_exp =~ /^([\d.]+)(_(\d+))?$/;
$base = $base . ".0" unless $base =~ /^\d+\.\d+\.\d+$/;
$version_exp = $base . ($ext ? ".M" . $ext : "");
my $agent_version = $value->{agent};
if ($agent_version =~ /(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?/) {
$agent_version = "$1.$2$3";
}
#ok($agent_version >= $version_exp,"Jolokia-version " . $value->{agent} . " >= Jmx4Perl Version " . $version_exp);
print "Agent-Version:\n";
print Dumper($value);
ok($value->{protocol} > 0,"Protocol version " . $value->{protocol});
#print Dumper(\@resps);
my $resp = $jmx->request(new JMX::Jmx4Perl::Request(READ,"java.lang:type=Runtime","SystemProperties"));
$value = $resp->{value};
print "Java: ",$value->{'java.version'}," (",$value->{'java.vendor'},")\n";

14
it/t/02_http_header.t Normal file
View File

@@ -0,0 +1,14 @@
use It;
use Data::Dumper;
use Test::More tests => 2;
my $it = new It(verbose => 0);
my $agent = $it->userAgent;
my $j4p = $it->jmx4perl;
my $resp = $agent->get($j4p->url() . "/version");
my $date = $resp->date;
my $expire = $resp->expires;
#print Dumper($resp);
#print "Date: $date\nExpires: $expire\n";
ok($expire <= $date,"expires must be less or equal date");
ok($resp->header('Expires') =~ /\w{3}, \d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT/,"RFC-1123 Format matched");

25
it/t/10_base.t Normal file
View File

@@ -0,0 +1,25 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
BEGIN { use_ok("JMX::Jmx4Perl"); }
my $jmx = new It()->jmx4perl;
my $product = $ENV{JMX4PERL_PRODUCT};
# Test autodetection
if ($product) {
my $jmx_auto = new JMX::Jmx4Perl(map { $_ => $jmx->cfg($_) } qw(url user password));
$jmx_auto->info;
is($jmx_auto->product->id,$product,"Autodetected proper server " . $product);
}
# Test info and detected handler
my $info = $jmx->info();
my $info_product = $1 if $info =~ /^Name:\s+(.*)/m;
my $info_version = $1 if $info =~ /^Version:\s+(.*)/m;
is($jmx->product->name,$info_product || "unknown","Product name match");
is($jmx->product->version,$info_version,"Product version match") if $info_version;

59
it/t/30_naming.t Normal file
View File

@@ -0,0 +1,59 @@
# -*- mode: cperl -*-
use It;
use strict;
use warnings;
use Test::More qw(no_plan);
use File::Temp qw/tmpnam/;
use Data::Dumper;
BEGIN { use_ok("JMX::Jmx4Perl"); }
my $jmx = It->new(verbose => 0)->jmx4perl;
my $name_p = "jolokia.it:type=naming/,name=%s";
my @names =
(
"/slash-simple/",
"simple",
"/--/",
"with%3acolon",
"//server/client",
"service%3ajmx%3armi%3a///jndi/rmi%3a//bhut%3a9999/jmxrmi",
"name with space",
"n!a!m!e with !/!"
# "äöüßÄÖÜ"
);
my @searches =
(
[ "*:name=//server/client,*", qr#(jmx4perl|jolokia)\.it(\.hidden)?:.*name=//server/client# ]
);
# Basic check:
for my $name (@names) {
my $mbean = search($jmx,sprintf($name_p,$name));
my $scalar = $jmx->get_attribute($mbean,"Ok");
is($scalar,"OK",$name);
}
for my $s (@searches) {
my $r = $jmx->search($s->[0]);
#print Dumper($r);
ok($r->[0] =~ $s->[1],"Search " . $s->[0]);
}
sub search {
my $jmx = shift;
my $prefix = shift;
my $ret = $jmx->search($prefix . ",*");
#print Dumper($ret);
if (!defined($ret)) {
fail("Search " . $prefix . ",* gives no result");
exit;
}
is(scalar(@$ret),1,"One MBean found");
return $ret->[0];
}

20
it/t/40_alias.t Normal file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/perl
use It;
use Test::More tests => 2;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
BEGIN { use_ok("JMX::Jmx4Perl::Alias"); }
my $jmx = new It()->jmx4perl;
my @aliases = JMX::Jmx4Perl::Alias->all;
eval {
for my $alias (@aliases) {
if ($jmx->supports_alias($alias) && $alias->type eq "attribute") {
#print $alias->alias,": ",$jmx->get_attribute($alias),"\n";
$jmx->get_attribute($alias);
}
}
};
ok(!$@,"Aliased called: $@");

40
it/t/50_check_base.t Normal file
View File

@@ -0,0 +1,40 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>1)->jmx4perl;
my ($ret,$content);
# ====================================================
# Basic checks
my %s = (
":10000000000" => [ 0, "OK" ],
"0.2:" => [ 0, "OK" ],
":0.2" => [ 2, "CRITICAL" ],
"5:6" => [ 2, "CRITICAL" ]
);
for my $k (keys %s) {
($ret,$content) = exec_check_perl4jmx("--mbean java.lang:type=Memory --attribute HeapMemoryUsage",
"--path used -c $k");
#print Dumper($ret,$content);
is($ret,$s{$k}->[0],"Memory -c $k : $ret");
ok($content =~ /^$s{$k}->[1]/m,"Memory -c $k : " . $s{$k}->[1]);
}
# ====================================================
# Alias attribute checks
for my $k (keys %s) {
($ret,$content) = exec_check_perl4jmx("--alias MEMORY_HEAP_USED -c $k --method post");
#print Dumper($ret,$content);
is($ret,$s{$k}->[0],"MEMORY_HEAP_USED -c $k : $ret");
ok($content =~ /^$s{$k}->[1]/m,"MEMORY_HEAP_USED $k : " . $s{$k}->[1]);
}
($ret,$content) = exec_check_perl4jmx("--mbean java.lang:type=Memory --attribute HeapMemoryUsage --path used");
is($ret,0,"No warning and no critical is always success");
ok($content =~ /in range/,"Data has been povided");

37
it/t/51_check_relative.t Normal file
View File

@@ -0,0 +1,37 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>0)->jmx4perl;
my ($ret,$content);
# ====================================================
# Relative value checks
my %s = (
":90" => [ 0, "OK" ],
"0.2:" => [ 0, "OK" ],
":0.2" => [ 1, "WARNING" ],
"81:82" => [ 1, "WARNING" ]
);
my @args = ();
for my $base (qw(MEMORY_HEAP_MAX java.lang:type=Memory/HeapMemoryUsage/max 1000000000)) {
push @args,"--alias MEMORY_HEAP_USED --base $base"
}
push @args,"--alias MEMORY_HEAP_USED --base-mbean java.lang:type=Memory --base-attribute=HeapMemoryUsage --base-path=max";
for my $arg (@args) {
for my $k (keys %s) {
($ret,$content) = exec_check_perl4jmx("$arg -w $k");
#print Dumper($ret,$content);
is($ret,$s{$k}->[0],"$arg -w $k : $ret");
ok($content =~ /^$s{$k}->[1]/,"$arg -w $k : " . $s{$k}->[1]);
}
}

45
it/t/52_check_operation.t Normal file
View File

@@ -0,0 +1,45 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use It;
use FindBin;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>0)->jmx4perl;
my ($ret,$content);
# ====================================================
# Operation return value check
# A single slash argument
$jmx->execute("jolokia.it:type=operation","reset");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=operation --operation fetchNumber",
"-c 1 --name counter inc");
is($ret,0,"Initial operation");
ok($content =~ /counter=(\d+)/ && $1 eq "0","Initial operation returns 0");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=operation --operation fetchNumber",
"-c 1 --name counter inc");
is($ret,0,"Second operation");
ok($content =~ /counter=(\d+)/ && $1 eq "1","Second operation returns 1");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=operation --operation fetchNumber",
"-c 1 --name counter inc");
is($ret,2,"Third operation");
ok($content =~ /counter=(\d+)/ && $1 eq "2","Third operation returns 2");
my $config_file = $FindBin::Bin . "/../check_jmx4perl/checks.cfg";
($ret,$content) = exec_check_perl4jmx("--config $config_file --check counter_operation");
ok($content =~ /value (\d+)/ && $1 eq "3","Fourth operation return 3");
is($ret,1,"Fourth operation");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=operation --operation emptyStringArgumentCheck",
"-c 1 /");
#print Dumper($ret,$content);
is($ret,0,"Single slash argument (return code)");
ok($content =~ /false/,"Single slash argument (return message)");
$jmx->execute("jolokia.it:type=operation","reset");

View File

@@ -0,0 +1,59 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>0)->jmx4perl;
my ($ret,$content);
# ====================================================
# Non-numerice Attributes return value check
# Boolean values
$jmx->execute("jolokia.it:type=attribute","reset");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute State --critical false");
#print ($ret,$content);
is($ret,0,"Boolean: OK");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute State --critical false");
is($ret,2,"Boolean: CRITICAL");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute State --critical false --warning true");
is($ret,1,"Boolean: WARNING");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute State --critical false --warning true");
is($ret,2,"Boolean (as String): CRITICAL");
# String values
$jmx->execute("jolokia.it:type=attribute","reset");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute String --critical Started");
is($ret,2,"String: CRITICAL");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute String --critical Started");
is($ret,0,"String: OK");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute String --critical !Started");
is($ret,0,"String: OK");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute String --critical !Started");
is($ret,2,"String: CRITICAL");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute String --critical Stopped --warning qr/art/");
is($ret,1,"String: WARNING");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute String --critical qr/^St..p\\wd\$/ --warning qr/art/");
is($ret,2,"String: CRITICAL");
# Check for a null value
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute Null --critical null");
is($ret,2,"null: CRITICAL");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute Null --critical null --null bla");
is($ret,0,"null: OK");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute Null --critical bla --null bla");
is($ret,2,"null: CRITICAL");
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute Null --critical !null --string");
is($ret,0,"null: OK");
# Check for a string array value
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=attribute --attribute StringArray --string --critical qr/Stopped/");
is($ret,2,"String Array: CRITICAL");
ok($content =~ /Stopped/,"Matches Threshhold");

50
it/t/54_check_unit.t Normal file
View File

@@ -0,0 +1,50 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>0)->jmx4perl;
my ($ret,$content);
# ================================================================================
# Unit conversion checking
($ret,$content) = exec_check_perl4jmx
("--mbean jolokia.it:type=attribute --attribute Bytes --critical 10000:");
is($ret,0,"Bytes: OK");
ok($content =~ /3670016/,"Bytes: Perfdata");
ok($content !~ /3\.50 MB/,"Bytes: Output");
($ret,$content) = exec_check_perl4jmx
("--mbean jolokia.it:type=attribute --attribute Bytes --critical 10000: --unit B");
is($ret,0,"Bytes: OK");
ok($content =~ /3670016B/,"Bytes Unit: Perfdata");
ok($content =~ /3\.50 MB/,"Bytes Unit: Output");
($ret,$content) = exec_check_perl4jmx
("--mbean jolokia.it:type=attribute --attribute LongSeconds --critical :10000 ");
is($ret,2,"SecondsLong: CRITICAL");
ok($content =~ /172800/,"SecondsLong: Perfdata");
ok($content !~ /2 d/,"SecondsLong: Output");
($ret,$content) = exec_check_perl4jmx
("--mbean jolokia.it:type=attribute --attribute LongSeconds --critical :10000 --unit s");
is($ret,2,"SecondsLong: CRITICAL");
ok($content =~ /172800/,"SecondsLong: Perfdata");
ok($content =~ /2 d/,"SecondsLong: Output");
($ret,$content) = exec_check_perl4jmx
("--mbean jolokia.it:type=attribute --attribute SmallMinutes --critical :10000 --unit m");
#print Dumper($ret,$content);
is($ret,0,"SmallMinutes: OK");
ok($content =~ /10.00 ms/,"SmallMinutes: Output");
($ret,$content) = exec_check_perl4jmx
("--value jolokia.it:type=attribute/MemoryUsed --base jolokia.it:type=attribute/MemoryMax --critical 80 --unit B");
#print Dumper($ret,$content);
is($ret,0,"Relative Memory: OK");
ok($content =~ /1\.99 GB/,"Relative Memory: Output");

View File

@@ -0,0 +1,47 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use JMX::Jmx4Perl::Alias;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose => 1)->jmx4perl;
my ($ret,$content);
# ====================================================
# Incremental value checks
reset_history($jmx);
my $membean = "--mbean java.lang:type=Memory --attribute HeapMemoryUsage";
my $cparams = $membean . " --path used --unit B --delta --name mem";
($ret,$content) = exec_check_perl4jmx($cparams);
is($ret,0,"Initial history fetch returns OK");
#print $content;
ok($content =~ /mem=(\d+)/ && $1 eq "0","Initial history fetch returns 0 mem delta");
my $max_mem = $jmx->get_attribute("java.lang:type=Memory", "HeapMemoryUsage","max");
my $c = abs(0.50 * $max_mem);
#print "Mem Max: $mem\n";
my $mem = $jmx->get_attribute("java.lang:type=Memory", "HeapMemoryUsage","used");
#print "Used Memory: $mem\n";
# Trigger Garbage collection
$jmx->execute("java.lang:type=Memory","gc");
for my $i (0 .. 2) {
$jmx->execute("java.lang:type=Memory","gc");
($ret,$content) = exec_check_perl4jmx($cparams . " -c -$c:$c");
is($ret,0,($i+1) . ". history fetch returns OK for -c $c");
ok($content =~ /mem=([\-\d]+)/ && $1 ne "0",($i+1) . ". history fetch return non null Mem-Delta ($1)");
#print Dumper($ret,$content);
print "Heap: ",$jmx->get_attribute("java.lang:type=Memory","HeapMemoryUsage","used"),"\n";
}
#print "$c: $content\n";
reset_history($jmx);

32
it/t/56_check_value.t Normal file
View File

@@ -0,0 +1,32 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use JMX::Jmx4Perl::Alias;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>0)->jmx4perl;
my ($ret,$content);
# ====================================================
# Check for --value
($ret,$content) = exec_check_perl4jmx("--value java.lang:type=Memory/HeapMemoryUsage/used " .
"--base java.lang:type=Memory/HeapMemoryUsage/max " .
"--critical 90 ");
is($ret,0,"Memory with value OK");
ok($content =~ /^OK/,"Content contains OK");
# TODO: Check escaping
($ret,$content) = exec_check_perl4jmx("--value jolokia.it:name=\\/\\/server\\/client,type=naming\\//Ok " .
"--critical OK");
#print Dumper($ret,$content);
is($ret,2,"CRITICAL expected");
ok($content =~ m|jolokia.it:name=\\/\\/server\\/client,type=naming\\//Ok|,"Content contains MBean name");
($ret,$content) = exec_check_perl4jmx("--value jolokia.it:type=naming\\/,name=\\\"jdbc/testDB\\\"/Ok " .
"--critical OK");
is($ret,2,"CRITICAL expected");
ok($content =~ m|jolokia.it:type=naming\\/,name="jdbc/testDB"/Ok|,"Content contains weired MBean name");

113
it/t/57_check_config.t Normal file
View File

@@ -0,0 +1,113 @@
use FindBin;
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use JMX::Jmx4Perl::Alias;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>1)->jmx4perl;
my ($ret,$content);
# ====================================================
# Configuration check
my $config_file = $FindBin::Bin . "/../check_jmx4perl/checks.cfg";
for my $check (qw(memory_heap memory_heap2)) {
($ret,$content) = exec_check_perl4jmx("--config $config_file --check $check");
is($ret,0,"$check: Memory with value OK");
ok($content =~ /\(base\)/,"$check: First level inheritance");
ok($content =~ /\(grandpa\)/,"$check: Second level inheritance");
ok($content !~ /\$\{1:default_name\}/,"$check: Default replacement");
ok($content =~ /default_name/,"$check: Default replacement");
}
($ret,$content) = exec_check_perl4jmx("--config $config_file --check blubber");
is($ret,3,"Unknown check");
ok($content =~ /blubber/,"Unknown check name contained");
# ========================================================================
# With arguments
($ret,$content) = exec_check_perl4jmx("--config $config_file --check outer_arg OuterArg");
#print Dumper($ret,$content);
is($ret,0,"OuterArg OK");
ok($content =~ /OuterArg/,"OuterArg replaced");
ok($content =~ /Warning: 80/,"Warning included in label");
ok($content =~ /Critical: 90/,"Critical included in label");
# No replacement
($ret,$content) = exec_check_perl4jmx("--config $config_file --check outer_arg");
is($ret,0,"OuterArg OK");
ok($content =~ /default_name/,"OuterArg not-replaced");
# ===========================================================================
# No default value
($ret,$content) = exec_check_perl4jmx("--config $config_file --check def_placeholder_1");
is($ret,1,"WARNING");
ok($content =~ /warning/i,"Warning expected");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check def_placeholder_1 1");
is($ret,1,"WARNING");
ok($content =~ /warning/i,"Warning expected");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check def_placeholder_2");
is($ret,1,"WARNING");
ok($content =~ /warning/i,"Warning expected");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check def_placeholder_2 1");
is($ret,2,"CRITICAL");
ok($content =~ /critical/i,"Critical expected");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check def_placeholder_2 1 2 Blubber");
is($ret,2,"CRITICAL");
ok($content =~ /critical/i,"Critical expected");
ok($content =~ /Blubber/,"Name replacement from command line");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check invalid_method 10 20");
is($ret,3,"UNKNOWN");
ok($content =~ /Unknown.*method/,"Unknown request method");
($ret,$content) = exec_check_perl4jmx("--config $config_file --method invalid --check thread_count 10 20");
is($ret,3,"UNKNOWN");
ok($content =~ /Unknown.*method/,"Unknown request method");
($ret,$content) = exec_check_perl4jmx("--config $config_file --method get --check thread_count 300 400");
#print Dumper($ret,$content);
is($ret,0,"OK");
ok($content =~ /in range/,"In range");
# =============================================================================
# With scripting
($ret,$content) = exec_check_perl4jmx("--config $config_file --check script_check Eden|Java");
#print Dumper($ret,$content);
is($ret,2);
ok($content =~ /threshold/i,"Script-Check: Threshold contained");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check script_multi_check Perm|non-heap");
ok($ret != 3);
#print Dumper($ret,$content);
ok($content =~ /Perm/,"Multi-Script-Check: Perm contained");
ok($content =~ /Eden/,"Multi-Script-Check: Eden contained");
ok($content =~ /thread_count/,"Multi-Script-Check: Thread_count contained");
# ===========================================================================
# Double values
($ret,$content) = exec_check_perl4jmx("--config $config_file --check double_min");
$content =~ /double_min=(.*?);/;
my $min = $1;
#print Dumper($min,$ret ,$content,$1);
is($min,"0.000000","Small double numbers are converted to floats");
# ===========================================================================
# Without Thresholds
($ret,$content) = exec_check_perl4jmx("--config $config_file --check without_threshold");
#print Dumper($content);

View File

@@ -0,0 +1,104 @@
use FindBin;
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use JMX::Jmx4Perl::Alias;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>1)->jmx4perl;
my ($ret,$content);
# ====================================================
# Configuration check
my $config_file = $FindBin::Bin . "/../check_jmx4perl/multi_check.cfg";
# Simple multicheck
($ret,$content) = exec_check_perl4jmx("--config $config_file --check memory");
#print ($ret,$content);
is($ret,0,"Memory with value OK");
ok($content =~ /\(base\)/,"First level inheritance");
ok($content =~ /\(grandpa\)/,"Second level inheritance");
ok($content =~ /Heap Memory/,"Heap Memory Included");
ok($content =~ /NonHeap Memory/,"NonHeap Memory included");
#print Dumper($ret,$content);
# Nested multichecks
($ret,$content) = exec_check_perl4jmx("--config $config_file --check nested");
#print Dumper($ret,$content);
is($ret,0,"Multicheck with value OK");
ok($content =~ /\(base\)/,"First level inheritance");
ok($content =~ /\(grandpa\)/,"Second level inheritance");
ok($content =~ /Thread-Count/,"Threads");
ok($content =~ /'Thread-Count'/,"Threads");
ok($content =~ /Heap Memory/,"Heap Memory Included");
ok($content =~ /NonHeap Memory/,"Non Heap Memory included");
# Multicheck with reference to checks with parameters
($ret,$content) = exec_check_perl4jmx("--config $config_file --check with_inner_args");
is($ret,0,"Multicheck with value OK");
ok($content =~ /HelloLabel/,"First param");
ok($content =~ /WithInnerArgs/,"WithInnerArgs");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check with_outer_args WithOuterArgs");
is($ret,0,"Multicheck with value OK");
ok($content =~ /HelloLabel/,"First param");
ok($content =~ /WithOuterArgs/,"WithOuterArgs");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check nested_with_args");
is($ret,0,"Multicheck with value OK");
ok($content =~ /HelloLabel/,"First param");
ok($content =~ /NestedWithArgs/,"NestedWithArgs");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check nested_with_outer_args NestedWithOuterArgs");
is($ret,0,"Multicheck with value OK");
ok($content =~ /HelloLabel/,"First param");
ok($content =~ /NestedWithOuterArgs/,"NestedWithOuterArgs");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check overloaded_multi_check");
#print Dumper($ret,$content);
is($ret,0,"Multicheck with argument for operation");
ok($content =~ /Value 1 in range/,"OperationWithArgument");
($ret,$content) = exec_check_perl4jmx("--config $config_file --check failing_multi_check");
#print Dumper($ret,$content);
is($ret,2,"Failing memory multicheck is CRITICAL");
ok($content =~ /memory_non_heap/,"Failed check name is contained in summary");
# Check labeling of failed tests
($ret,$content) = exec_check_perl4jmx("--config $config_file --check label_test");
#print "==========================================\n";
#print Dumper($ret,$content);
is($ret,2,"Should fail as critical");
my @lines = split /\n/,$content;
is($#lines,2,"3 lines has been returned");
ok($lines[0] =~ /bla/ && $lines[0] =~ /blub/,"Name of checks should be returned as critical values");
#print Dumper($ret,$content);
($ret,$content) = exec_check_perl4jmx("--config $config_file --check error_multi_check");
is($ret,3,"Should fail as UNKNOWN");
@lines = split /\n/,$content;
is($#lines,3,"4 lines has been returned");
ok($lines[1] =~ /kaputt/ && $lines[1] =~ /UNKNOWN/,"First line is UNKNOWN Check");
#print Dumper($ret,$content);
($ret,$content) = exec_check_perl4jmx("--unknown-is-critical --config $config_file --check error_multi_check");
is($ret,2,"Should fail as CRITICAL");
@lines = split /\n/,$content;
is($#lines,3,"4 lines has been returned");
ok($lines[0] =~ /kaputt/ && $lines[0] =~ /CRITICAL/,"First line is UNKNOWN Check");
#print Dumper($ret,$content);
# TODO:
# Unknown multicheck name
# Unknown nested multicheck name
# Unknown check name within a multi check
# No multicheck name

22
it/t/59_check_timeout.t Normal file
View File

@@ -0,0 +1,22 @@
use FindBin;
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use JMX::Jmx4Perl::Alias;
use It;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose=>1)->jmx4perl;
my ($ret,$content);
my $time = time;
($ret,$content) = exec_check_perl4jmx("--mbean jolokia.it:type=operation --operation sleep --timeout 1 -c 1 2");
# print Dumper($ret,$content);
# print "Time ",time - $time,"\n";
ok($content =~ /timeout/i,"Timeout reached");
is($ret,3,"UNKNOWN status for timeouts");

22
it/t/60_bulk_request.t Normal file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
use Data::Dumper;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
my $jmx = new It(verbose => 0)->jmx4perl;
my @reqs = ( new JMX::Jmx4Perl::Request(READ,"java.lang:type=Memory", "HeapMemoryUsage", "used"),
new JMX::Jmx4Perl::Request(READ,"java.lang:type=Memory", "HeapMemoryUsage", "max"),
new JMX::Jmx4Perl::Request(READ,"java.lang:type=ClassLoading", "LoadedClassCount"),
new JMX::Jmx4Perl::Request(SEARCH,"*:type=Memory,*"));
my @resps = $jmx->request(@reqs);
is(scalar(@resps),4,"4 Responses");
for (my $i = 0 .. 3) {
is($resps[$i]->{request},$reqs[$i],"Request " . ($i+1));
}
#print Dumper(\@resps);

59
it/t/64_check_perfdata.t Normal file
View File

@@ -0,0 +1,59 @@
use strict;
use warnings;
use Test::More qw(no_plan);
use Data::Dumper;
use JMX::Jmx4Perl::Alias;
use It;
use FindBin;
require "check_jmx4perl/base.pl";
my $jmx = It->new(verbose =>0)->jmx4perl;
my ($ret,$content);
# ====================================================
# Given as command line
($ret,$content) = exec_check_perl4jmx("--value java.lang:type=Memory/HeapMemoryUsage/used " .
"--base java.lang:type=Memory/HeapMemoryUsage/max " .
"--critical 90 " .
"--perfdata no");
ok($content !~ /\s*\|\s*/,"1: Content contains no perfdata");
($ret,$content) = exec_check_perl4jmx("--value java.lang:type=Memory/HeapMemoryUsage/used " .
"--base java.lang:type=Memory/HeapMemoryUsage/max " .
"--warn 80 " .
"--critical 90 " .
"--perfdata %");
ok($content =~ /\s*\|\s*/,"2: Content contains perfdata");
ok($content =~ /80;90/,"2a: Perfdata is relative");
print Dumper($ret,$content);
($ret,$content) = exec_check_perl4jmx("--mbean java.lang:type=Threading " .
"--operation findDeadlockedThreads " .
"--null 'nodeadlock' " .
"--string " .
"--critical '!nodeadlock'");
ok($content !~ /\s*\|\s*/,"3: Content contains no perfdata");
# ====================================================
# Given in config
my $config_file = $FindBin::Bin . "/../check_jmx4perl/checks.cfg";
($ret,$content) = exec_check_perl4jmx("--config $config_file " .
"--check thread_deadlock");
ok($content !~ /\s*\|\s*/,"4: Content contains no perfdata");
($ret,$content) = exec_check_perl4jmx("--config $config_file " .
"--check memory_without_perfdata");
#print Dumper($ret,$content);
ok($content !~ /\s*\|\s*/,"5: Content contains no perfdata");
($ret,$content) = exec_check_perl4jmx("--config $config_file " .
"--check memory_with_perfdata");
#print Dumper($ret,$content);
ok($content =~ /\s*\|\s*/,"6: Content contains perfdata");

View File

@@ -0,0 +1,33 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
use Data::Dumper;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
my $jmx = new It(verbose => 0)->jmx4perl;
my $req = new JMX::Jmx4Perl::Request(EXEC,"jolokia.it:type=operation", "overloadedMethod","bla");
my $resp = $jmx->request($req);
ok($resp->{error},"Error must be set");
$req = new JMX::Jmx4Perl::Request(EXEC,"jolokia.it:type=operation", "overloadedMethod()");
$resp = $jmx->request($req);
is($resp->{value},0,"No-Arg operation called");
$req = new JMX::Jmx4Perl::Request(EXEC,"jolokia.it:type=operation", "overloadedMethod(java.lang.String)","bla");
$resp = $jmx->request($req);
is($resp->{value},1,"First operation called");
$req = new JMX::Jmx4Perl::Request(EXEC,"jolokia.it:type=operation", "overloadedMethod(java.lang.String,int)","bla",1);
$resp = $jmx->request($req);
#print Dumper($resp);
is($resp->{value},2,"Second operation called");
$req = new JMX::Jmx4Perl::Request(EXEC,"jolokia.it:type=operation", "overloadedMethod([Ljava.lang.String;)","bla,blub");
$resp = $jmx->request($req);
#print Dumper($resp);
is($resp->{value},3,"Third operation called");
$req = new JMX::Jmx4Perl::Request(EXEC,"jolokia.it:type=operation", "overloadedMethod(java.lang.String,int,long)","bla",3,3);
$resp = $jmx->request($req);
ok($resp->{error},"No such method");
#print Dumper($resp);
#print Dumper(\@resps);

95
it/t/80_read.t Normal file
View File

@@ -0,0 +1,95 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
use Data::Dumper;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
# Fetch all attributes
my $jmx = new It(verbose => 0)->jmx4perl;
my $req = new JMX::Jmx4Perl::Request(READ,"jolokia.it:type=attribute");
my $resp = $jmx->request($req);
my $value = $resp->{value};
#print Dumper($resp);
ok($value->{LongSeconds} == 60*60*24*2,"LongSeconds");
ok($value->{Bytes} == 3 * 1024 * 1024 + 1024 * 512,"Bytes");
ok(exists($value->{Null}) && !$value->{Null},"Null");
# Fetch an array ref of attributes
$jmx->execute("jolokia.it:type=attribute","reset");
my $req = new JMX::Jmx4Perl::Request(READ,"jolokia.it:type=attribute",["LongSeconds","State"],{method => "post"});
my $resp = $jmx->request($req);
my $value = $resp->{value};
#print Dumper($resp);
is(scalar(keys(%$value)),2,"2 Return values");
ok($value->{LongSeconds} == 60*60*24*2,"LongSeconds");
ok($value->{State},"State");
$jmx->execute("jolokia.it:type=attribute","reset");
my $value = $jmx->get_attribute("jolokia.it:type=attribute",["LongSeconds","State"]);
ok($value->{LongSeconds} == 60*60*24*2,"LongSeconds");
ok($value->{State},"State");
$jmx->execute("jolokia.it:type=attribute","reset");
# Fetch a pattern with a single attribute
my $value = $jmx->get_attribute("jolokia.it:*","LongSeconds");
ok($value->{"jolokia.it:type=attribute"}->{LongSeconds} == 60*60*24*2,"LongSeconds");
$jmx->execute("jolokia.it:type=attribute","reset");
# Fetch a pattern with all attributes
my $value = $jmx->get_attribute("jolokia.it:*",undef);
ok($value->{"jolokia.it:type=attribute"}->{LongSeconds} == 60*60*24*2,"LongSeconds");
$jmx->execute("jolokia.it:type=attribute","reset");
is($value->{"jolokia.it:type=operation"},undef,"Operation missing");
is($value->{"jolokia.it:type=attribute"}->{Bytes},3670016,"Bytes with pattern");
# Fetch a pattern with multiple attributes
my $value = $jmx->get_attribute("jolokia.it:*",["LongSeconds","State"]);
ok($value->{"jolokia.it:type=attribute"}->{LongSeconds} == 60*60*24*2,"LongSeconds");
ok($value->{"jolokia.it:type=attribute"}->{State},"State");
$jmx->execute("jolokia.it:type=attribute","reset");
my $value = $jmx->get_attribute("jolokia.it:type=attribute","ObjectName");
ok($value->{objectName} eq "bla:type=blub","object name simplified");
ok(!defined($value->{canonicalName}),"no superfluos parameters");
my $value = $jmx->get_attribute("jolokia.it:type=attribute","Set");
is(ref($value),"ARRAY","Set as array returned");
ok(scalar(grep("jolokia",@$value)),"contains 'jolokia'");
ok(scalar(grep("habanero",@$value)),"contains 'habanero'");
my $value = $jmx->get_attribute("jolokia.it:type=attribute","Utf8Content");
is($value,"☯","UTF-8 ☯ check passed");
my $value = $jmx->get_attribute("jolokia.it:type=attribute","Chili");
is($value,"AJI","Enum serialization passed");
# Fetch all attributes
$req = new JMX::Jmx4Perl::Request(READ,"jolokia.it.jsonmbean:type=plain");
$resp = $jmx->request($req);
$value = $resp->{value};
#print Dumper($resp);
is($resp->status,200);
# Check Tabular data
$value = $jmx->get_attribute("jolokia.it:type=tabularData","Table2","Value0.0/Value0.1");
is($value->{Column1},"Value0.0","First column");
is($value->{Column2},"Value0.1","Second column");
$req = new JMX::Jmx4Perl::Request(READ,"jolokia.it:type=tabularData","Table2","Value0.1/Value0.0");
$resp = $jmx->request($req);
#print Dumper($resp);
$value = $resp->{value};
is($value,undef,"Path with no value");
$value = $jmx->get_attribute("jolokia.it:type=mxbean","MapWithComplexKey");
is(scalar(keys %$value),2,"2 elements");
ok($value->{indexNames}->[0],"key");
is(@{$value->{values}},2,"2 values");
ok($value->{values}->[0]->{key}->{number} =~ /^(1|2)$/,"key match");
#print Dumper($value);

26
it/t/83_write.t Normal file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
use Data::Dumper;
use strict;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
# Write the object name ad re-read
my $jmx = new It(verbose => 0)->jmx4perl;
my $req = new JMX::Jmx4Perl::Request(WRITE,"jolokia.it:type=attribute","ObjectName","java.lang:type=Memory");
my $resp = $jmx->request($req);
#print Dumper(\$resp);
my $value = $resp->{value};
is($value->{objectName},"bla:type=blub","Set ObjectName: Old Name returned");
$value = $jmx->get_attribute("jolokia.it:type=attribute","ObjectName");
is($value->{objectName},"java.lang:type=Memory","Set ObjectName: New Name set");
$jmx->execute("jolokia.it:type=attribute","reset");

24
it/t/84_exec.t Normal file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
use Data::Dumper;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
# Fetch all attributes
my $jmx = new It(verbose => 0)->jmx4perl;
my $req = new JMX::Jmx4Perl::Request(EXEC,{ mbean => "jolokia.it:type=operation", operation => "mapArgument",arguments => [{ name => "Kyotake"}],method => "POST"} );
my $resp = $jmx->request($req);
my $value = $resp->{value};
is(ref($resp->{value}),"HASH","Response type");
is($resp->{value}->{name},"Kyotake","Response value");
$value = $jmx->execute("jolokia.it:type=operation","findTimeUnit","MINUTES");
is($value,"MINUTES","Enum serialization up and done");
$value = $jmx->execute("jolokia.it:type=operation","addBigDecimal",1,"1e3");
is($value,1001,"Adding big decimal");
#print Dumper($resp);

38
it/t/85_path_escaping.t Normal file
View File

@@ -0,0 +1,38 @@
# -*- mode: cperl -*-
use It;
use strict;
use warnings;
use Test::More tests => 16;
use File::Temp qw/tmpnam/;
use Data::Dumper;
use JMX::Jmx4Perl::Request;
my $jmx = It->new(verbose => 0)->jmx4perl;
my ($req,$resp,$list);
for my $method ("post","get") {
$req = new JMX::Jmx4Perl::Request(READ,"jolokia.it:type=attribute","ComplexNestedValue","Blub/1/numbers/1",{method => $method});
$resp = $jmx->request($req);
is($resp->{value},23);
for my $path ("",undef,"/") {
$req = new JMX::Jmx4Perl::Request(READ,"jolokia.it:type=attribute","Map",$path,{method => $method});
$resp = $jmx->request($req);
is($resp->{value}->{fcn},"meister");
$req = new JMX::Jmx4Perl::Request(LIST,$path,{method => $method});
$resp = $jmx->request($req);
ok($resp->{value}->{'jolokia.it'});
}
$req = new JMX::Jmx4Perl::Request(LIST,"/java.lang/",{method => $method});
$resp = $jmx->request($req);
#print Dumper($resp);
}
$list = $jmx->list("jolokia.it/name=!/!/server!/client,type=naming!//attr");
is($list->{Ok}->{type},"java.lang.String");
#my $list = $jmx->list("jolokia.it");
$req = new JMX::Jmx4Perl::Request(LIST,"jolokia.it/name=!/!/server!/client,type=naming!//attr",{method => "POST"});
$resp = $jmx->request($req);
#print Dumper($resp);
is($resp->{value}->{Ok}->{type},"java.lang.String");

18
it/t/90_search.t Normal file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Request;
use Data::Dumper;
#use Test::More tests => $ENV{JMX4PERL_PRODUCT} ? 2 : 1;
# Check for escaped pattern:
my $jmx = It->new(verbose => 0)->jmx4perl;
my $mbeans = $jmx->search("jolokia.it:type=escape,*");
for my $m (@$mbeans) {
my $value = $jmx->get_attribute($m,"Ok");
is($value,"OK",$m);
}

90
it/t/95_cors.t Normal file
View File

@@ -0,0 +1,90 @@
#!/usr/bin/perl
use It;
use Test::More (tests => 14);
use LWP::UserAgent;
use Data::Dumper;
use strict;
my $url = $ENV{JMX4PERL_GATEWAY} || $ARGV[0];
$url .= "/" unless $url =~ /\/$/;
my $origin = "http://localhost:8080";
my $ua = new LWP::UserAgent();
if ($ENV{JMX4PERL_USER}) {
my $netloc = $url;
$netloc =~ s|^.*/([^:]+:\d+).*$|$1|;
$ua->credentials($netloc,"jolokia",$ENV{JMX4PERL_USER},$ENV{JMX4PERL_PASSWORD});
}
$ua->default_headers()->header("Origin" => $origin);
# Test for CORS functionality. This is done without Jmx4Perl client library but
# with direct requests
# 1) Preflight Checks
my $req = new HTTP::Request("OPTIONS",$url);
my $resp = $ua->request($req);
#print Dumper($resp);
is($resp->header('Access-Control-Allow-Origin'),$origin,"Access-Control-Allow Origin properly set");
ok($resp->header('Access-Control-Allow-Max-Age') > 0,"Max Age set");
ok(!$resp->header('Access-Control-Allow-Request-Header'),"No Request headers set");
$req->header("Access-Control-Request-Headers","X-Extra, X-Extra2");
$req->header('X-Extra',"bla");
$resp = $ua->request($req);
is($resp->header('Access-Control-Allow-Headers'),'X-Extra, X-Extra2',"Allowed headers");
# 2) GET Requests with "Origin:"
$req = new HTTP::Request("GET",$url . "/read/java.lang:type=Memory/HeapMemoryUsage");
$resp = $ua->request($req);
verify_resp("GET",$resp);
# 3) POST Requests with "Origin:"
$req = new HTTP::Request("POST",$url);
$req->content(<<EOT);
{
"type" : "read",
"mbean" : "java.lang:type=Memory",
"attribute" : "HeapMemoryUsage",
"path" : "used"
}
EOT
$resp = $ua->request($req);
verify_resp("POST",$resp);
# 4) POST Request with "Origin:" and error
$req = new HTTP::Request("POST",$url);
$req->content(<<EOT);
{
"type" : "bla"
}
EOT
$resp = $ua->request($req);
verify_resp("POST-Error",$resp);
# 5) Try request splitting attack
my $ua2 = new LWP::UserAgent();
$req = new HTTP::Request("GET",$url . "/read/java.lang:type=Memory/HeapMemoryUsage");
$req->header("Origin","http://bla.com\r\n\r\nInjected content");
$resp = $ua2->request($req);
ok($resp->header('Access-Control-Allow-Origin') !~ /[\r\n]/,"No new lines included");
#print Dumper($resp);
# ---------------------------------------------
sub verify_resp {
my $pref = shift;
my $resp = shift;
is($resp->header('Access-Control-Allow-Origin'),$origin,"$pref: Access-Control-Allow Origin properly set");
ok(!$resp->header('Access-Control-Allow-Max-Age'),"$pref: No Max Age set");
ok(!$resp->header('Access-Control-Allow-Request-Header'),"$pref: No Request headers set");
}

35
it/t/99_discovery.t Normal file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/perl
use It;
use Test::More qw(no_plan);
use JMX::Jmx4Perl;
use Data::Dumper;
use strict;
my $jmx = new It(verbose => 0)->jmx4perl;
# Might find nothing, dependening on where it is run.
my $disc_class = urls(JMX::Jmx4Perl->discover_agents());
ok(defined($disc_class));
my $disc_obj = urls($jmx->discover_agents());
ok(defined($disc_obj));
my $agents_found = $jmx->execute("jolokia:type=Discovery","lookupAgents");
print Dumper($agents_found);
print Dumper($disc_class);
my $agent_urls = urls($agents_found);
for my $disc_p ($disc_class,$disc_obj) {
for my $k (keys %$disc_p) {
ok(defined($agent_urls->{$k}),"Agent URL " . $k . " detected");
}
}
sub urls {
my $agents = shift;
my $ret = {};
for my $agent (@$agents) {
$ret->{$agent->{url}}++;
}
return $ret;
}