File Coverage

blib/lib/Siebel/Srvrmgr/OS/Unix.pm
Criterion Covered Total %
statement 33 43 76.7
branch 1 10 10.0
condition 0 6 0.0
subroutine 8 8 100.0
pod 1 2 50.0
total 43 69 62.3


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