File Coverage

blib/lib/Siebel/Srvrmgr/OS/Unix.pm
Criterion Covered Total %
statement 39 43 90.7
branch 4 10 40.0
condition 0 6 0.0
subroutine 8 8 100.0
pod 1 2 50.0
total 52 69 75.3


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::OS::Unix;
2              
3 1     1   687 use Moose;
  1         2  
  1         11  
4 1     1   8230 use Proc::ProcessTable;
  1         6002  
  1         50  
5 1     1   8 use namespace::autoclean;
  1         2  
  1         12  
6 1     1   70 use Set::Tiny;
  1         3  
  1         40  
7 1     1   5 use Carp qw(cluck confess);
  1         2  
  1         52  
8 1     1   656 use Siebel::Srvrmgr::OS::Process;
  1         368  
  1         631  
9              
10             =pod
11              
12             =head1 NAME
13              
14             Siebel::Srvrmgr::OS::Unix - module to recover information from OS processes of Siebel components
15              
16             =head1 SYNOPSIS
17              
18             use Siebel::Srvrmgr::OS::Unix;
19             my $procs = Siebel::Srvrmgr::OS::Unix->new(
20             {
21             comps_source =>
22             Siebel::Srvrmgr::Log::Enterprise::Parser::Comp_alias->new(
23             {
24             process_regex => 'Created\s(multithreaded)?\sserver\sprocess',
25             log_path => $enterprise_log,
26             archive => Archive->new( { dbm_path => $MY_DBM } )
27             }
28             ),
29             cmd_regex => $siebel_path,
30             }
31             );
32              
33             # hash reference of objects Siebel::Srvrmgr::OS::Process
34             my $procs_ref = $procs->get_procs;
35             foreach my $comp_pid( keys( %{$procs_ref} ) ) {
36              
37             print 'Component ', $procs_ref->{$comp_pid}->{comp_alias}, ' is using ', $procs_ref->{$comp_pid}->{pctcpu}, "% of CPU now\n";
38              
39             }
40              
41             =head1 DESCRIPTION
42              
43             This module is a L<Moose> class.
44              
45             It is responsible to recover information from processes executing on a UNIX-like O.S. and merging that with information of Siebel components.
46              
47             Details on running processes are recovered from C</proc> directory meanwhile the details about the components are read from a class that implements the L<Siebel::Srvrmgr::Comps_source> role.
48              
49             =head1 ATTRIBUTES
50              
51             =head2 comps_source
52              
53             Required attribute.
54              
55             An instance of a class that implements the Moose Role L<Siebel::Srvrmgr::Comps_source>. Those classes are supposed to recover information about the current modules available in a Siebel Server.
56              
57             =cut
58              
59             has comps_source => (
60             is => 'ro',
61             does => 'Siebel::Srvrmgr::Comps_source',
62             required => 1,
63             reader => 'get_comps_source'
64             );
65              
66             =head2 cmd_regex
67              
68             Required attribute.
69              
70             A string of the regular expression to match the command executed by the Siebel user from the C<cmdline> file in C</proc>.
71             This usually is the path included in the binary when you check with C<ps -aux> command.
72              
73             This attribute is a string, not a compiled regular expression with C<qr>.
74              
75             The amount of processes returned will depend on the regular expression used: one can match anything execute by the Siebel
76             OS user or only the processes related to the Siebel components.
77              
78             =cut
79              
80             has cmd_regex => (
81             is => 'rw',
82             isa => 'Str',
83             reader => 'get_cmd',
84             writer => 'set_cmd',
85             required => 1
86             );
87              
88             =head2 mem_limit
89              
90             Optional attribute.
91              
92             A integer representing the maximum bytes of RSS a Siebel process might have.
93              
94             If set together with C<limits_callback>, this class can execute some action when this threshold is exceeded.
95              
96             =cut
97              
98             has mem_limit => (
99             is => 'rw',
100             isa => 'Int',
101             reader => 'get_mem_limit',
102             writer => 'set_mem_limit',
103             default => 0
104             );
105              
106             =head2 cpu_limit
107              
108             Optional attribute.
109              
110             A integer representing the maximum CPU percentage a Siebel process might have.
111              
112             If set together with C<limits_callback>, this class can execute some action when this threshold is exceeded.
113              
114             =cut
115              
116             has cpu_limit => (
117             is => 'rw',
118             isa => 'Int',
119             reader => 'get_cpu_limit',
120             writer => 'set_cpu_limit',
121             default => 0
122             );
123              
124             =head2 limits_callback
125              
126             Optional attribute.
127              
128             A code reference that will be executed when one of the attributes C<mem_limit> and C<cpu_limit> threshold is exceeded.
129              
130             This is useful, for example, with you want to set a alarm or something like that.
131              
132             The code reference will receive a hash reference as parameter which keys and values will depend on the type of limit triggered:
133              
134             =over
135              
136             =item *
137              
138             memory:
139              
140             type => 'memory'
141             rss => <processes RSS>
142             vsz => <process VSZ>
143             pid => <process id>
144             fname => <process fname>,
145             cmd => <process cmndline>
146              
147             =item *
148              
149             CPU:
150              
151             type => 'cpu'
152             cpu => <process % of cpu>
153             pid => <process id>
154             fname => <process fname>
155             cmd => <process cmndline>
156              
157             =back
158              
159             =cut
160              
161             has limits_callback => (
162             is => 'rw',
163             isa => 'CodeRef',
164             reader => 'get_callback',
165             writer => 'set_callback'
166             );
167              
168             =head1 METHODS
169              
170             =head2 new
171              
172             To create new instances of Siebel::Srvrmgr::OS::Unix.
173              
174             The constructor expects a hash reference with the attributes required plus those that are marked as optional.
175              
176             =head2 get_procs
177              
178             Searches through C</proc> and and returns an hash reference with the pids as keys and L<Siebel::Srvrmgr::OS::Process> instances as values.
179              
180             Those instances will be created by merging information from C</proc> and the C<comps_source> attribute instance.
181              
182             =cut
183              
184             sub get_procs {
185              
186 1     1 1 5581 my $self = shift;
187 1         3 my $cmd_regex;
188              
189             {
190              
191 1         3 my $regex = $self->get_cmd;
  1         131  
192 1         89 $cmd_regex = qr/$regex/;
193              
194             }
195              
196 1         44 my $t = Proc::ProcessTable->new( enable_ttys => 0 );
197              
198 1         1241 my %procs;
199              
200 1         4 for my $process ( @{ $t->table } ) {
  1         2534  
201              
202 11 100       271 next unless ( $process->cmndline =~ $cmd_regex );
203              
204             # :WORKAROUND:22-03-2015 20:54:51:: forcing conversion to number
205 1         28 my $pctcpu = $process->pctcpu;
206 1         14 $pctcpu =~ s/\s//;
207 1         13 $pctcpu += 0;
208              
209 1         11 $procs{ $process->pid } = Siebel::Srvrmgr::OS::Process->new(
210             {
211             pid => $process->pid,
212             fname => $process->fname,
213             pctcpu => $pctcpu,
214             pctmem => ( $process->pctmem + 0 ),
215             rss => ( $process->rss + 0 ),
216             vsz => ( $process->size + 0 )
217             }
218             );
219              
220 1 50       72 if ( $self->get_mem_limit > 0 ) {
221              
222 0 0 0     0 if ( ( $process->rss > $self->get_mem_limit )
223             and ( defined( $self->get_callback ) ) )
224             {
225              
226 0         0 $self->get_callback->(
227             {
228             type => 'memory',
229             rss => $process->rss,
230             vsz => $process->size,
231             pid => $process->pid,
232             fname => $process->fname,
233             cmd => $process->cmndline
234             }
235             );
236              
237             }
238              
239             }
240              
241 1 50       54 if ( $self->get_cpu_limit > 0 ) {
242              
243 0 0 0     0 if ( ( $process->pctcpu > $self->get_cpu_limit )
244             and ( defined( $self->get_callback ) ) )
245             {
246              
247 0         0 $self->get_callback->(
248             {
249             type => 'cpu',
250             cpu => $process->pctcpu,
251             pid => $process->pid,
252             fname => $process->fname,
253             cmd => $process->cmndline
254             }
255             );
256              
257             }
258              
259             }
260              
261             }
262              
263 1         10 $self->find_comps( \%procs );
264              
265 1         75 return \%procs;
266              
267             }
268              
269             sub find_comps {
270              
271 1     1 0 3 my $self = shift;
272 1         2 my $procs_ref = shift;
273 1         112 $self->get_comps_source()->find_comps($procs_ref);
274              
275             }
276              
277             =head1 SEE ALSO
278              
279             =over
280              
281             =item *
282              
283             L<Moose>
284              
285             =item *
286              
287             L<Proc::ProcessTable>
288              
289             =item *
290              
291             L<Siebel::Srvrmgr::Log::Enterprise::Parser::Comp_alias>
292              
293             =item *
294              
295             L<Siebel::Srvrmgr::Comps_source>
296              
297             =item *
298              
299             L<Siebel::Srvrmgr::OS::Process>
300              
301             =back
302              
303             =head1 AUTHOR
304              
305             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
306              
307             =head1 COPYRIGHT AND LICENSE
308              
309             This software is copyright (c) 2015 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
310              
311             This file is part of Siebel Monitoring Tools.
312              
313             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
314             it under the terms of the GNU General Public License as published by
315             the Free Software Foundation, either version 3 of the License, or
316             (at your option) any later version.
317              
318             Siebel Monitoring Tools is distributed in the hope that it will be useful,
319             but WITHOUT ANY WARRANTY; without even the implied warranty of
320             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
321             GNU General Public License for more details.
322              
323             You should have received a copy of the GNU General Public License
324             along with Siebel Monitoring Tools. If not, see <http://www.gnu.org/licenses/>.
325              
326             =cut
327              
328             __PACKAGE__->meta->make_immutable;