File Coverage

blib/lib/Xymon/Plugin/Server/Hosts.pm
Criterion Covered Total %
statement 9 41 21.9
branch 0 4 0.0
condition n/a
subroutine 3 6 50.0
pod 2 2 100.0
total 14 53 26.4


line stmt bran cond sub pod time code
1             #
2             # bbhostgrep wrapper
3             #
4              
5             package Xymon::Plugin::Server::Hosts;
6              
7             =head1 NAME
8              
9             Xymon::Plugin::Server::Hosts - Xymon bbhostgrep wrapper
10              
11             =head1 SYNOPSIS
12              
13             use Xymon::Plugin::Server::Hosts;
14              
15             my $hosts = Xymon::Plugin::Server::Hosts->new();
16             my @result = $hosts->grep('bbd');
17              
18             for my $entry (@result) {
19             my ($ip, $host, $test) = @$entry;
20             print "ip = $ip, host = $host, test = $test\n";
21             }
22              
23             =cut
24              
25 2     2   9 use strict;
  2         2  
  2         53  
26 2     2   7 use Carp;
  2         2  
  2         156  
27              
28 2     2   510 use Xymon::Plugin::Server;
  2         2  
  2         813  
29              
30             =head1 SUBROUTINES/METHODS
31              
32             =head2 new
33              
34             Just a constructor.
35             (Takes bbhostgrep options optionally)
36              
37             =cut
38              
39             sub new {
40 0     0 1   my $class = shift;
41 0           my @params = @_;
42              
43 0           my $self = {
44             _params => \@params
45             };
46              
47 0           bless $self, $class;
48             }
49              
50             sub _select_test {
51 0     0     my $self = shift;
52 0           my $test = shift;
53 0           my $s = shift;
54              
55 0           my @tests = split(/\s+/, $s);
56              
57 0           my $re = $test;
58 0           $re =~ s/\*/.*/g;
59 0           return grep { /^$re/ } @tests;
  0            
60             }
61              
62             =head2 grep(TEST)
63              
64             Execute bbhostgrep and return its result.
65              
66             Return value is an array like:
67              
68             (entry1, entry2, ...)
69              
70             Each entiries are arrayref like:
71              
72             [ IP-address, hostname, tests ]
73              
74             If TEST does not contain wildcard character ('*'), tests is a scalar value.
75             Otherwise it is ARRAYREF contains test names which matchies TEST.
76              
77             =cut
78              
79             sub grep {
80 0     0 1   my $self = shift;
81 0           my $test = shift;
82              
83 0           my $xyhome = Xymon::Plugin::Server->home;
84              
85 0           my $grep = "$xyhome/bin/bbhostgrep";
86              
87 0           my @cmdline = ($grep);
88 0           push(@cmdline, @{$self->{_params}}, $test);
  0            
89              
90 0           my @result;
91              
92 0 0         open(my $fh, "-|", @cmdline)
93             or croak "cannot execute $grep: $!";
94              
95 0           while (<$fh>) {
96 0           chomp;
97 0           my ($iphost, $tests) = split(/\s*#\s*/);
98 0           my ($ip, $host) = split(/\s+/, $iphost);
99              
100 0           $tests =~ s/\s*$//;
101 0           $ip =~ s/^\s//;
102              
103 0           my @filtered = $self->_select_test($test, $tests);
104 0 0         if ($test =~ /\*/) {
105 0           push(@result, [$ip, $host, \@filtered]);
106             }
107             else {
108 0           push(@result, [$ip, $host, @filtered]);
109             }
110             }
111              
112 0           return @result;
113             }
114              
115             1;