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