File Coverage

blib/lib/Xymon/Plugin/Server/Dispatch.pm
Criterion Covered Total %
statement 38 39 97.4
branch 5 6 83.3
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 52 54 96.3


line stmt bran cond sub pod time code
1             #
2             # plugin dispatcher
3             #
4             package Xymon::Plugin::Server::Dispatch;
5              
6 2     2   31174 use strict;
  2         5  
  2         81  
7 2     2   12 use warnings;
  2         5  
  2         96  
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 2     2   1501 use Xymon::Plugin::Server::Hosts;
  2         7  
  2         868  
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             sub new {
51 1     1 1 40 my $class = shift;
52 1         5 my @keyvalue = @_;
53              
54 1         2 my @keys;
55             my %dic;
56              
57 1         6 while (@keyvalue > 0) {
58 4         7 my $key = shift @keyvalue;
59 4         6 push(@keys, $key);
60 4         17 $dic{$key} = shift @keyvalue;
61             }
62              
63 1         6 my $self = {
64             _keys => \@keys,
65             _dic => \%dic,
66             };
67              
68 1         6 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             sub run {
91 1     1 1 7 my $self = shift;
92              
93 1         2 for my $key (@{$self->{_keys}}) {
  1         9  
94 4         9 my $dest = $self->{_dic}->{$key};
95 4         6 my $code;
96 4 100       12 if (ref($dest)) {
97 3 100       8 if (ref($dest) eq 'CODE') {
98 2         3 $code = $dest;
99             }
100             else {
101 1     1   5 $code = sub { $dest->run(@_); };
  1         23  
102             }
103             }
104             else {
105             $code = sub {
106 1     1   6 my $obj = $dest->new(@_);
107 1         13 $obj->run;
108 1         7 };
109             }
110              
111 4         17 for my $entry (Xymon::Plugin::Server::Hosts->new->grep($key)) {
112 4         45 eval {
113 4         6 my $host = $entry->[1];
114 4         6 my $test = $entry->[2];
115 4         5 my $ip = $entry->[0];
116              
117 4         10 &$code($host, $test, $ip);
118             };
119 4 50       68 if ($@) {
120 0           print STDERR $@;
121             }
122             }
123             }
124             }
125              
126             1;