| 1 | # | ||||||||||
| 2 | # plugin dispatcher | ||||||||||
| 3 | # | ||||||||||
| 4 | package Xymon::Plugin::Server::Dispatch; | ||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Xymon::Plugin::Server::Dispatch - Xymon plugin dispatcher | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Xymon::Plugin::Server::Dispatch; | ||||||||||
| 16 | use YourMonitor; | ||||||||||
| 17 | |||||||||||
| 18 | # dispatch to class | ||||||||||
| 19 | my $dispatch1 = Xymon::Plugin::Server::Dispatch | ||||||||||
| 20 | ->new('test' => 'YourMonitor'); | ||||||||||
| 21 | $dispatch1->run; | ||||||||||
| 22 | |||||||||||
| 23 | # dispatch to method | ||||||||||
| 24 | my $dispatch2 = Xymon::Plugin::Server::Dispatch | ||||||||||
| 25 | ->new('test' => new YourMonitor()); | ||||||||||
| 26 | $dispatch2->run; | ||||||||||
| 27 | |||||||||||
| 28 | # dispatch to CODEREF | ||||||||||
| 29 | my $dispatch3 = Xymon::Plugin::Server::Dispatch | ||||||||||
| 30 | ->new('test' => sub { ... }); | ||||||||||
| 31 | $dispatch3->run; | ||||||||||
| 32 | |||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 3 | ▶ | use Xymon::Plugin::Server::Hosts; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | =head1 SUBROUTINES/METHODS | ||||||||||
| 40 | |||||||||||
| 41 | =head2 new(testName1 => ModuleName1, ...) | ||||||||||
| 42 | |||||||||||
| 43 | Create dispatch object for tests and modules. | ||||||||||
| 44 | |||||||||||
| 45 | If testName has wildcard character(like http:*), $test will be ARRAYREF | ||||||||||
| 46 | when run method is called. | ||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 51 | 1 | my $class = shift; | |||||||||
| 52 | 1 | my @keyvalue = @_; | |||||||||
| 53 | |||||||||||
| 54 | 1 | my @keys; | |||||||||
| 55 | my %dic; | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | while (@keyvalue > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 4 | my $key = shift @keyvalue; | |||||||||
| 59 | 4 | push(@keys, $key); | |||||||||
| 60 | 4 | $dic{$key} = shift @keyvalue; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 1 | my $self = { | |||||||||
| 64 | _keys => \@keys, | ||||||||||
| 65 | _dic => \%dic, | ||||||||||
| 66 | }; | ||||||||||
| 67 | |||||||||||
| 68 | 1 | bless $self, $class; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | =head1 SUBROUTINES/METHODS | ||||||||||
| 72 | |||||||||||
| 73 | =head2 run | ||||||||||
| 74 | |||||||||||
| 75 | For every host listed in bb-hosts(Xymon 4.2) or hosts.cfg (Xymon 4.3), | ||||||||||
| 76 | following operation is executed. | ||||||||||
| 77 | |||||||||||
| 78 | # if class name is given | ||||||||||
| 79 | my $module = YourMonitor->new($host, $test); | ||||||||||
| 80 | $module->run; | ||||||||||
| 81 | |||||||||||
| 82 | # if object is given | ||||||||||
| 83 | $module->run($host, $test); | ||||||||||
| 84 | |||||||||||
| 85 | # if CODEREF is given | ||||||||||
| 86 | &$code($host, $test); | ||||||||||
| 87 | |||||||||||
| 88 | =cut | ||||||||||
| 89 | |||||||||||
| 90 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 91 | 1 | my $self = shift; | |||||||||
| 92 | |||||||||||
| 93 | 1 | for my $key (@{$self->{_keys}}) { | |||||||||
| 94 | 4 | my $dest = $self->{_dic}->{$key}; | |||||||||
| 95 | 4 | my $code; | |||||||||
| 96 | 4 | ▶ | if (ref($dest)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 3 | ▶ | if (ref($dest) eq 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 2 | $code = $dest; | |||||||||
| 99 | } | ||||||||||
| 100 | else { | ||||||||||
| 101 | 1 | ▶ | $code = sub { $dest->run(@_); }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 102 | } | ||||||||||
| 103 | } | ||||||||||
| 104 | else { | ||||||||||
| 105 | 1 | ▶ | $code = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 106 | 1 | my $obj = $dest->new(@_); | |||||||||
| 107 | 1 | $obj->run; | |||||||||
| 108 | 1 | }; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 4 | for my $entry (Xymon::Plugin::Server::Hosts->new->grep($key)) { | |||||||||
| 112 | 4 | eval { | |||||||||
| 113 | 4 | my $host = $entry->[1]; | |||||||||
| 114 | 4 | my $test = $entry->[2]; | |||||||||
| 115 | 4 | my $ip = $entry->[0]; | |||||||||
| 116 | |||||||||||
| 117 | 4 | &$code($host, $test, $ip); | |||||||||
| 118 | }; | ||||||||||
| 119 | 4 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | print STDERR $@; | |||||||||
| 121 | } | ||||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 3 | 1; | |||||||||