| 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 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 3 | ▶ | use Xymon::Plugin::Server; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | =head1 SUBROUTINES/METHODS | ||||||||||
| 31 | |||||||||||
| 32 | =head2 new | ||||||||||
| 33 | |||||||||||
| 34 | Just a constructor. | ||||||||||
| 35 | (Takes bbhostgrep options optionally) | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | 0 | 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 | 0 | ▶ | sub _select_test { | ||||||||
|
Subroutine
_select_test: not called CC=1 SCAR=6.9
| |||||||||||
| 51 | 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; | |||||||||
| 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 | 0 | ▶ | sub grep { | ||||||||
|
Subroutine
grep: not called CC=4 SCAR=29.0
Pod
documented
| |||||||||||
| 80 | 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); | |||||||||
| 89 | |||||||||||
| 90 | 0 | my @result; | |||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | open(my $fh, "-|", @cmdline) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | or croak "cannot execute $grep: $!"; | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | ▶ | if ($test =~ /\*/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 2 | 1; | |||||||||