File Coverage

blib/lib/Siebel/Srvrmgr/Daemon/Action/CheckComps.pm
Criterion Covered Total %
statement 20 20 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 28 29 96.5


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::Daemon::Action::CheckComps;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::Daemon::Action::CheckComps - subclass of Siebel::Srvrmgr::Daemon::Action to verify components status
8              
9             =head1 SYNOPSIS
10              
11             use Siebel::Srvrmgr::Daemon::Action::CheckComps;
12              
13             my $return_data = Siebel::Srvrmgr::Daemon::ActionStash->instance();
14              
15             my $comps = [ {name => 'SynchMgr', ok_status => 'Running'}, { name => 'WfProcMgr', ok_status => 'Running'} ];
16              
17             my $action = Siebel::Srvrmgr::Daemon::Action::CheckComps->new(
18             {
19             parser => Siebel::Srvrmgr::ListParser->new(),
20             params => [ $server1, $server2 ]
21             }
22             );
23              
24             $action->do();
25              
26             # do something with $return_data
27              
28             =cut
29              
30 4     4   25722 use Moose;
  4         6  
  4         28  
31 4     4   20680 use namespace::autoclean;
  4         8  
  4         30  
32 4     4   277 use Moose::Util qw(does_role);
  4         6  
  4         27  
33 4     4   1986 use Siebel::Srvrmgr::Daemon::ActionStash;
  4         9  
  4         136  
34 4     4   26 use Siebel::Srvrmgr;
  4         6  
  4         2313  
35              
36             extends 'Siebel::Srvrmgr::Daemon::Action';
37              
38             =pod
39              
40             =head1 DESCRIPTION
41              
42             This subclass of L<Siebel::Srvrmgr::Daemon::Action> will try to find a L<Siebel::Srvrmgr::ListParser::Output::Tabular::ListComp> object in the given array reference
43             given as parameter to the C<do> method and compares the status of the components with the array reference given as parameter.
44              
45             The C<do> method of C<Siebel::Srvrmgr::Daemon::Action::CheckComps> uses L<Siebel::Srvrmgr::Daemon::ActionStash> to enable the program that created the object
46             instance to be able to fetch the information returned.
47              
48             This module was created to work close with Nagios concepts, especially regarding threshold levels (see C<new> method for more details).
49              
50             =head1 METHODS
51              
52             =head2 new
53              
54             The new method returns a instance of L<Siebel::Srvrmgr::Daemon::Action::CheckComps>. The parameter expected are the same ones of any subclass of
55             L<Siebel::Srvrmgr::Daemon::Action>, but the C<params> attribute has a important difference: it expects an array reference with instances of classes
56             that have the role L<Siebel::Srvrmgr::Daemon::Action::CheckComps::Server>.
57              
58             The server instance itself will expect to have multiple instances of objects representing a Siebel Component and those instances must be of a class that has the
59             Moose role L<Siebel::Srvrmgr::Daemon::Action::CheckComps::Component> applied.
60              
61             See the C<examples> directory of this distribution, uh, examples of implementation.
62              
63             =head2 BUILD
64              
65             Validates if the params array reference have objects with the L<Siebel::Srvrmgr::Daemon::Action::Check::Server> role applied.
66              
67             =cut
68              
69             sub BUILD {
70              
71 25     25 1 40 my $self = shift;
72              
73 25         49 my $role = 'Siebel::Srvrmgr::Daemon::Action::Check::Server';
74              
75 25         32 foreach my $object ( @{ $self->get_params() } ) {
  25         972  
76              
77 25 50       121 confess "all params items must be classes with $role role applied"
78             unless ( does_role( $object, $role ) );
79              
80             }
81              
82             }
83              
84             override '_build_exp_output' => sub {
85              
86             return 'Siebel::Srvrmgr::ListParser::Output::Tabular::ListComp';
87              
88             };
89              
90             =head2 do_parsed
91              
92             Expects a array reference as the buffer output from C<srvrmgr> program as a parameter.
93              
94             This method will check the output from C<srvrmgr> program parsed by L<Siebel::Srvrmgr::ListParser::Output::Tabular::ListComp> object and compare each component recovered status
95             with the status defined in the array reference given to C<params> method during object creation.
96              
97             It will return 1 if this operation was executed successfuly and request a instance of L<Siebel::Srvrmgr::Daemon::ActionStash>, calling it's method C<instance> and then
98             C<set_stash> with a hash reference as it's content. Otherwise, the method will return 0 and no data will be set to the ActionStash object.
99              
100             The hash reference stored in the ActionStash object will have the following structure:
101              
102             $VAR1 = {
103             'foobar_server' => {
104             'CompAlias1' => 0,
105             'CompAlias2' => 1
106             },
107             'foobar2_server' => {
108             'CompAlias1' => 1,
109             'CompAlias2' => 1
110             }
111             };
112              
113             If the servername passed during the object creation (as C<params> attribute of C<new> method) cannot be found in the buffer parameter, the object will raise an
114             exception.
115              
116             Beware that this Action subclass can deal with multiple servers, as long as the buffer output is from a C<list comp>, list all server/components that
117             are part of the Siebel Enterprise.
118              
119             =cut
120              
121             override 'do_parsed' => sub {
122              
123             my $self = shift;
124             my $obj = shift;
125              
126             my $logger = Siebel::Srvrmgr->gimme_logger('Siebel::Srvrmgr::Daemon');
127             $logger->info('Starting run method');
128              
129             my $servers = $self->get_params(); # array reference
130             my %servers; # to locate the expected servers easier
131              
132             foreach my $server ( @{$servers} ) {
133              
134             $servers{ $server->get_name() } = $server;
135              
136             }
137              
138             my %checked_comps;
139              
140             if ( $obj->isa( $self->get_exp_output ) ) {
141              
142             my $out_servers_ref =
143             $obj->get_servers(); # servers retrieve from output of srvrmgr
144              
145             $logger->die( 'Could not fetch servers from the '
146             . $self->get_exp_output
147             . 'object returned by the parser' )
148             unless ( scalar( @{$out_servers_ref} ) > 0 );
149              
150             foreach my $out_name ( @{$out_servers_ref} ) {
151              
152             my $server = $obj->get_server($out_name);
153              
154             if (
155             $server->isa(
156             'Siebel::Srvrmgr::ListParser::Output::ListComp::Server')
157             )
158             {
159              
160             my $exp_name = $server->get_name(); # the expected server name
161              
162             if ( exists( $servers{$exp_name} ) ) {
163              
164             my $exp_srv =
165             $servers{$exp_name}; # the expected server reference
166              
167             foreach my $exp_comp ( @{ $exp_srv->get_components() } ) {
168              
169             my $comp = $server->get_comp( $exp_comp->get_alias() );
170              
171             if ( defined($comp) ) {
172              
173             my @valid_status =
174             split( /\|/, $exp_comp->get_OKStatus() );
175              
176             my $is_ok = 0;
177              
178             foreach my $valid_status (@valid_status) {
179              
180             if ( $valid_status eq
181             $comp->get_disp_run_state() )
182             {
183              
184             $is_ok = 1;
185             last;
186              
187             }
188              
189             }
190              
191             if ($is_ok) {
192              
193             $checked_comps{ $exp_srv->get_name() }
194             ->{ $exp_comp->get_alias() } = 1;
195              
196             }
197             else {
198              
199             $checked_comps{ $exp_srv->get_name() }
200             ->{ $exp_comp->get_alias() } = 0;
201              
202             $logger->warn( 'invalid status got for '
203             . $exp_comp->get_alias() . ' "'
204             . $comp->get_disp_run_state()
205             . '" instead of "'
206             . $exp_comp->get_OKStatus()
207             . '"' )
208             if ( $logger->is_warn() );
209              
210             }
211              
212             }
213             else {
214              
215             $logger->warn(
216             'Could not find any component with name [',
217             $exp_comp->get_alias() . ']' )
218             if ( $logger->is_warn() );
219              
220             }
221              
222             }
223              
224             } # end of foreach comp
225             else {
226              
227             $logger->logdie(
228             "Unexpected servername [$exp_name] retrieved from buffer.\n Expected servers names are "
229             . join( ', ',
230             map { '[' . $_->get_name() . ']' } @{$servers} )
231             );
232             }
233              
234             }
235             else {
236              
237             $logger->logdie("could not fetch $out_name data");
238              
239             }
240              
241             } # end of foreach server
242              
243             }
244             else {
245              
246             $logger->debug( 'object received ISA not' . $self->get_exp_output() )
247             if ( $logger->is_debug() );
248             return 0;
249              
250             }
251              
252             # found some servers
253             if ( keys(%checked_comps) ) {
254              
255             my $stash = Siebel::Srvrmgr::Daemon::ActionStash->instance();
256              
257             # :TODO :24/07/2013 12:32:51:: it should set the stash with more than just the ok/not ok status from the components
258             $stash->set_stash( [ \%checked_comps ] );
259              
260             return 1;
261              
262             }
263             else {
264              
265             return 0;
266              
267             }
268              
269             };
270              
271             =pod
272              
273             =head1 SEE ALSO
274              
275             =over
276              
277             =item *
278              
279             L<Siebel::Srvrmgr::ListParser::Output::Tabular::ListComp>
280              
281             =item *
282              
283             L<Siebel::Srvrmgr::ListParser::Output::ListComp::Server>
284              
285             =item *
286              
287             L<Siebel::Srvrmgr::Daemon::Action>
288              
289             =item *
290              
291             L<Siebel::Srvrmgr::Daemon::Action::Stash>
292              
293             =item *
294              
295             L<Nagios::Plugin>
296              
297             =back
298              
299             =head1 AUTHOR
300              
301             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
302              
303             =head1 COPYRIGHT AND LICENSE
304              
305             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
306              
307             This file is part of Siebel Monitoring Tools.
308              
309             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
310             it under the terms of the GNU General Public License as published by
311             the Free Software Foundation, either version 3 of the License, or
312             (at your option) any later version.
313              
314             Siebel Monitoring Tools is distributed in the hope that it will be useful,
315             but WITHOUT ANY WARRANTY; without even the implied warranty of
316             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
317             GNU General Public License for more details.
318              
319             You should have received a copy of the GNU General Public License
320             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
321              
322             =cut
323              
324             __PACKAGE__->meta->make_immutable;