File Coverage

blib/lib/Siebel/Srvrmgr/ListParser/Output/Tabular/ListSessions.pm
Criterion Covered Total %
statement 70 83 84.3
branch 16 22 72.7
condition 4 6 66.6
subroutine 12 14 85.7
pod 5 5 100.0
total 107 130 82.3


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::ListParser::Output::Tabular::ListSessions;
2              
3 3     3   8570 use Moose;
  3         210332  
  3         22  
4 3     3   14886 use namespace::autoclean;
  3         1085  
  3         18  
5 3     3   186 use Carp qw(cluck);
  3         3  
  3         153  
6 3     3   11 use Siebel::Srvrmgr::Regexes qw(SIEBEL_SERVER);
  3         5  
  3         1909  
7              
8             =pod
9              
10             =head1 NAME
11              
12             Siebel::Srvrmgr::ListParser::Output::Tabular::ListSessions - subclass to parse list tasks command
13              
14             =cut
15              
16             extends 'Siebel::Srvrmgr::ListParser::Output::Tabular';
17              
18             =pod
19              
20             =head1 SYNOPSIS
21              
22             See L<Siebel::Srvrmgr::ListParser::Output::Tabular> for examples.
23              
24             =head1 DESCRIPTION
25              
26             This subclass of L<Siebel::Srvrmgr::ListParser::Output::Tabular> parses the output of the command C<list sessions>.
27              
28             It is expected that the C<srvrmgr> program has a proper configuration for the C<list sessions> command. The default configuration
29             can be seen below:
30              
31             srvrmgr:> configure list sessions
32             SV_NAME (31): Server name
33             CC_ALIAS (31): Component alias
34             CG_ALIAS (31): Component group alias
35             TK_TASKID (11): Internal task id
36             TK_PID (11): Task process id
37             TK_DISP_RUNSTATE (61): Task run once
38             TK_IDLE_STATE (31): Task idle or not
39             TK_PING_TIME (13): Last ping time for task
40             TK_HUNG_STATE (31): Task hung state
41             DB_SESSION_ID (76): Database session Id
42             OM_LOGIN (76): Object Manager Login
43             OM_BUSSVC (76): OM - Business Service
44             OM_VIEW (76): OM - View State
45             OM_APPLET (76): OM - Applet
46             OM_BUSCOMP (76): OM - Business Component
47              
48             Be sure to include this configuration when generating output, specially because the columns name width.
49              
50             The order of the fields is important too: everytime those fields are parsed, if they do not follow the order above an exception
51             will be raised.
52              
53             An instance of this class will also know how many sessions it has, independently of the sessions state. Since it cannot know which
54             string represents a session state, be sure to use the correct C<list sessions> variation to get the sessions that you're interested in.
55              
56             =cut
57              
58             has 'alias_sessions' => (
59             isa => 'HashRef',
60             is => 'ro',
61             reader => 'get_alias_sessions',
62             writer => '_set_alias_sessions'
63             );
64              
65             =pod
66              
67             =head1 METHODS
68              
69             Some methods from the parent classes are overrided.
70              
71             =head2 get_servers
72              
73             Returns a list of the Siebel Server names from the parsed output.
74              
75             =cut
76              
77             sub get_servers {
78              
79 2     2 1 13 my $self = shift;
80              
81 2         3 return keys( %{ $self->get_data_parsed() } );
  2         75  
82              
83             }
84              
85             =pod
86              
87             =head2 count_server_sessions
88              
89             Returns an integer representing the number of sessions recovered from the parsed output.
90              
91             Expects a string as parameter being the Siebel Server name, so the number of sessions are those related to the
92             server passed as argument.
93              
94             Beware that by number of sessions it means all sessions are the output, independent of the status of the session.
95              
96             =cut
97              
98             sub count_server_sessions {
99              
100 4     4 1 4378 my $self = shift;
101 4         5 my $server = shift;
102              
103 4         16 my $server_ref = $self->_assert_ses_server($server);
104              
105 2         3 return scalar( @{$server_ref} );
  2         8  
106              
107             }
108              
109             sub _assert_ses_server {
110              
111 6     6   6 my $self = shift;
112 6         5 my $server = shift;
113              
114 6 100 66     69 confess 'Siebel Server name parameter is required and must be valid'
115             unless ( ( defined($server) ) and ( $server =~ SIEBEL_SERVER ) );
116              
117 4         156 my $data_ref = $self->get_data_parsed();
118              
119 4 50       10 cluck "servername '$server' is not available in the output parsed"
120             unless ( exists( $data_ref->{$server} ) );
121              
122 4         6 return $data_ref->{$server};
123              
124             }
125              
126             =pod
127              
128             =head2 count_sv_alias_sessions
129              
130             Returns an integer representing the number of sessions retrieved from a C<list sessions> command output for a given
131             component alias in a server.
132              
133             Expects as parameters, in this order:
134              
135             =over
136              
137             =item
138              
139             servername
140              
141             =item
142              
143             component alias
144              
145             =back
146              
147             Beware that by number of sessions it means all sessions are the output, independent of the status of the session.
148              
149             =cut
150              
151             sub count_sv_alias_sessions {
152              
153 2     2 1 12 my $self = shift;
154 2         2 my $server = shift;
155 2         4 my $alias = shift;
156              
157 2         4 my $server_ref = $self->_assert_ses_server($server);
158              
159 2         73 my $alias_ref = $self->get_alias_sessions();
160              
161 2 50       8 cluck "$alias is not a valid component alias for server $server"
162             unless ( exists( $alias_ref->{$server}->{$alias} ) );
163              
164 2         10 return $alias_ref->{$server}->{$alias};
165              
166             }
167              
168             =pod
169              
170             =head2 count_alias_sessions
171              
172             Returns an integer representing the number of sessions retrieved from a C<list sessions> command output for a given
173             component alias. If multiple Siebel servers are available in the output, that will be the sum of all of them.
174              
175             Expects a component alias as parameter.
176              
177             Beware that by number of sessions it means all sessions in the output, independent of the status of the session.
178              
179             =cut
180              
181             sub count_alias_sessions {
182              
183 4     4 1 55 my $self = shift;
184 4         4 my $alias = shift;
185              
186 4 100 66     30 confess 'component alias is required and must be valid'
187             unless ( ( defined($alias) ) and ( $alias ne '' ) );
188              
189 2         86 my $aliases_ref = $self->get_alias_sessions;
190              
191 2         4 my $counter = 0;
192              
193 2         3 foreach my $server_name ( keys( %{$aliases_ref} ) ) {
  2         8  
194              
195 2 50       8 $counter += $aliases_ref->{$server_name}->{$alias}
196             if ( exists( $aliases_ref->{$server_name}->{$alias} ) );
197              
198             }
199              
200 2         9 return $counter;
201              
202             }
203              
204             =pod
205              
206             =head2 get_sessions
207              
208             Returns an iterator to iterate over the list of sessions of a Siebel Server given as argument.
209              
210             At each invocation of the iterator, a hash reference is returned or C<undef> in the case that there are no more sessions.
211              
212             The hash reference will have keys corresponding to the defined columns of the C<list sessions> command and the respective values:
213              
214             =over
215              
216             =item *
217              
218             comp_alias
219              
220             =item *
221              
222             comp_group_alias
223              
224             =item *
225              
226             task_id
227              
228             =item *
229              
230             task_pid
231              
232             =item *
233              
234             task_state
235              
236             =item *
237              
238             task_idle_state
239              
240             =item *
241              
242             task_ping_time
243              
244             =item *
245              
246             task_hung_state
247              
248             =item *
249              
250             db_session_id
251              
252             =item *
253              
254             om_login
255              
256             =item *
257              
258             om_service
259              
260             =item *
261              
262             om_view
263              
264             =item *
265              
266             om_applet
267              
268             =item *
269              
270             om_buscomp
271              
272             =back
273              
274             =cut
275              
276             sub get_sessions {
277              
278 0     0 1 0 my $self = shift;
279 0         0 my $server = shift;
280 0         0 my $counter = 0;
281              
282 0         0 my $server_ref = $self->_assert_ses_server($server);
283              
284 0         0 my $total = scalar( @{$server_ref} ) - 1;
  0         0  
285              
286             return sub {
287              
288 0 0   0   0 if ( $counter <= $total ) {
289              
290 0         0 my $fields_ref = $server_ref->[$counter];
291              
292 0         0 $counter++;
293              
294             return {
295 0         0 comp_alias => $fields_ref->[0],
296             comp_group_alias => $fields_ref->[1],
297             task_id => $fields_ref->[2],
298             task_pid => $fields_ref->[3],
299             task_state => $fields_ref->[4],
300             task_idle_state => $fields_ref->[5],
301             task_ping_time => $fields_ref->[6],
302             task_hung_state => $fields_ref->[7],
303             db_session_id => $fields_ref->[8],
304             om_login => $fields_ref->[9],
305             om_service => $fields_ref->[10],
306             om_view => $fields_ref->[11],
307             om_applet => $fields_ref->[12],
308             om_buscomp => $fields_ref->[13]
309             };
310              
311             }
312             else {
313              
314 0         0 return undef;
315              
316             }
317              
318             }
319 0         0 }
320              
321             sub _add_alias_ses {
322              
323 772     772   515 my $self = shift;
324 772         472 my $server_name = shift;
325 772         490 my $alias = shift;
326              
327 772         25797 my $aliases_ref = $self->get_alias_sessions;
328              
329 772 100       863 if ( defined($aliases_ref) ) {
330              
331 764 100       1190 if ( exists( $aliases_ref->{$server_name} ) ) {
332              
333 109 100       146 if ( exists( $aliases_ref->{$server_name}->{$alias} ) ) {
334              
335 90         154 $aliases_ref->{$server_name}->{$alias}++;
336              
337             }
338             else {
339              
340 19         39 $aliases_ref->{$server_name}->{$alias} = 1;
341              
342             }
343              
344             }
345              
346             }
347             else {
348              
349 8         27 $aliases_ref->{$server_name}->{$alias} = 1;
350 8         282 $self->_set_alias_sessions($aliases_ref);
351              
352             }
353              
354             }
355              
356             sub _consume_data {
357              
358 772     772   610 my $self = shift;
359 772         486 my $fields_ref = shift;
360 772         415 my $parsed_ref = shift;
361              
362 772         484 my $list_len = scalar( @{$fields_ref} );
  772         535  
363              
364             # to avoid repeating the servername in the hash reference
365 772         467 my $server_name = shift( @{$fields_ref} );
  772         774  
366              
367 772 100       1089 $parsed_ref->{$server_name} = []
368             unless ( exists( $parsed_ref->{$server_name} ) );
369              
370 772         563 my %alias_sessions;
371              
372 772 50       412 if ( @{$fields_ref} ) {
  772         850  
373              
374 772         427 push( @{ $parsed_ref->{$server_name} }, $fields_ref );
  772         896  
375 772         974 $self->_add_alias_ses($server_name, $fields_ref->[0]);
376              
377 772         2206 return 1;
378              
379             }
380             else {
381              
382 0         0 return 0;
383              
384             }
385              
386             }
387              
388             sub _build_expected {
389              
390 8     8   14 my $self = shift;
391              
392 8         290 $self->_set_expected_fields(
393             [
394             'SV_NAME', 'CC_ALIAS',
395             'CG_ALIAS', 'TK_TASKID',
396             'TK_PID', 'TK_DISP_RUNSTATE',
397             'TK_IDLE_STATE', 'TK_PING_TIME',
398             'TK_HUNG_STATE', 'DB_SESSION_ID',
399             'OM_LOGIN', 'OM_BUSSVC',
400             'OM_VIEW', 'OM_APPLET',
401             'OM_BUSCOMP'
402             ]
403             );
404              
405             }
406              
407             =pod
408              
409             =head1 CAVEATS
410              
411             Depending on the servers configurations, how output is being read, you might get truncated data from some fields if the
412             fixed width output type is being used.
413              
414             =head1 SEE ALSO
415              
416             =over
417              
418             =item *
419              
420             L<Siebel::Srvrmgr::ListParser::Output::Tabular>
421              
422             =item *
423              
424             L<Moose>
425              
426             =back
427              
428             =head1 AUTHOR
429              
430             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
431              
432             =head1 COPYRIGHT AND LICENSE
433              
434             This software is copyright (c) 2013 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>.
435              
436             This file is part of Siebel Monitoring Tools.
437              
438             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
439             it under the terms of the GNU General Public License as published by
440             the Free Software Foundation, either version 3 of the License, or
441             (at your option) any later version.
442              
443             Siebel Monitoring Tools is distributed in the hope that it will be useful,
444             but WITHOUT ANY WARRANTY; without even the implied warranty of
445             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
446             GNU General Public License for more details.
447              
448             You should have received a copy of the GNU General Public License
449             along with Siebel Monitoring Tools. If not, see L<http://www.gnu.org/licenses/>.
450              
451             =cut
452              
453             __PACKAGE__->meta->make_immutable;