VBTK/Actions.pm

filter: Statementstmt 23.8% 27 / 113 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.8% 7 / 13 Podpod 16.6% 1 / 6 totaltotal 18.3% 35 / 191 CC 28 SCAR 40.4
CC
28
Cov
16%
trigger
132.0
new
56.0
add_message
42.0
CRAP 56.6
« VBTK/Objects/UpgradeRules.pm ↑ Summary VBTK/Common.pm »
1 #! /bin/perl
2 #############################################################################
3 #
4 # NOTE: This file under revision control using RCS
5 # Any changes made without RCS will be lost
6 #
7 # $Source: /usr/local/cvsroot/vbtk/VBTK/Actions.pm,v $
8 # $Revision: 1.7 $
9 # $Date: 2002/03/04 20:53:06 $
10 # $Author: bhenry $
11 # $Locker: $
12 # $State: Exp $
13 #
14 # Purpose: A common perl library used to define and trigger actions
15 # based on pmserver object statuses
16 #
17 # Description: This perl library is used by the pmserver process to
18 # define and trigger actions based on the status of objects
19 # defined on the pmserver process.
20 #
21 # Directions:
22 #
23 # Invoked by: pmserver
24 #
25 # Depends on: VBTK::Common.pm, Date::Manip
26 #
27 # Copyright (C) 1996 - 2002 Brent Henry
28 #
29 # This program is free software; you can redistribute it and/or
30 # modify it under the terms of version 2 of the GNU General Public
31 # License as published by the Free Software Foundation available at:
32 # http://www.gnu.org/copyleft/gpl.html
33 #
34 # This program is distributed in the hope that it will be useful,
35 # but WITHOUT ANY WARRANTY; without even the implied warranty of
36 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 # GNU General Public License for more details.
38 #
39 #############################################################################
40 #
41 #
42 # REVISION HISTORY:
43 #
44 # $Log: Actions.pm,v $
45 # Revision 1.7 2002/03/04 20:53:06 bhenry
46 # *** empty log message ***
47 #
48 # Revision 1.6 2002/03/04 16:49:08 bhenry
49 # Changed requirement back to perl 5.6.0
50 #
51 # Revision 1.5 2002/03/02 00:53:54 bhenry
52 # Documentation updates
53 #
54 # Revision 1.4 2002/02/20 19:25:18 bhenry
55 # *** empty log message ***
56 #
57 # Revision 1.3 2002/02/19 19:01:19 bhenry
58 # Rewrote Actions to make use of inheritance
59 #
60  
61 package VBTK::Actions;
62  
63 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 # I like using undef as a value so I'm turning off the uninitialized warnings
67 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 1 ▶use VBTK::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use POSIX qw(waitpid WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 our $VERSION = '0.01';
74  
75 1 our %PENDING_QUEUE;
76 1 our @LOG_ACTION_LIST;
77 1 our %ACTION_LIST;
78 1 our $VERBOSE=$ENV{VERBOSE};
79 1 our $LAST_HANDLER_PID;
80  
81 #-------------------------------------------------------------------------------
82 # Function: new
83 # Description: Object constructor. Allocates memory for all class members
84 # Input Parms: Configuration filename
85 # Output Parms: Pointer to class
86 #-------------------------------------------------------------------------------
87 sub new
88 0 ▶{
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
89 0 my ($type,$self);
90
91 # If we're passed a hash, then it's probably from an inheriting class
92 0 ▶ if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH')))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and UNIVERSAL::isa($_[0], 'HASH')) { } 0 0 0
Condition0%0/3
defined $_[0] and UNIVERSAL::isa($_[0], "HASH")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and UNIVERSAL::isa($_[0], "HASH")
defined $_[0] UNIVERSAL::isa($_[0], "HASH")
Truth table0%0/3
A and B
A= defined $_[0]B= UNIVERSAL::isa($_[0], "HASH")
ABresult
0X0
100
111
93 {
94 0 $self = shift;
95 }
96 # Otherwise, allocate a new hash, bless it and handle any passed parms
97 else
98 {
99 0 $type = shift;
100 0 $self = {};
101 0 bless $self, $type;
102  
103 # Set any passed parms
104 0 $self->set(@_);
105  
106 # Setup a hash of default parameters
107 0 my $defaultParms = {
108 Name => $::REQUIRED,
109 Execute => undef,
110 SendUrl => 1,
111 LimitToEvery => '2 min',
112 SubActionList => undef,
113 LogActionFlag => undef,
114 };
115  
116 # Validate the passed parms against the default parms.
117 0 ▶ $self->validateParms($defaultParms) || &fatal("Exiting");
Branch: 1 branch
BranchTrueFalseTotal
$self->validateParms($defaultParms) or &fatal("Exiting") 0 0 0
118 }
119  
120 # Convert the SubActionList to an array if it's not already one.
121 0 my $SubActionList = $self->{SubActionList};
122 0 ▶ $self->{SubActionList} = [ split(/,/,$SubActionList) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $SubActionList 0 0 0
123 unless (ref($SubActionList));
124  
125 # Create a per-action queue of pending messages
126 0 $self->{messages} = [];
127
128 # Calculate 'LimitToEvery' in seconds
129 0 ▶ $self->{limitToEverySec} = &deltaSec($self->{LimitToEvery}) ||
Condition0%0/3
&deltaSec($self->{'LimitToEvery'}) || &fatal("Invalid 'LimitToEvery' setting in action '$self->{'Name'}'")
l!l&&r!l&&!r
000
MC/DC0%0/2
&deltaSec($self->{'LimitToEvery'}) || &fatal("Invalid 'LimitToEvery' setting in action '$self->{'Name'}'")
&deltaSec($self->{'LimitToEvery'}) &fatal("Invalid 'LimitToEvery' setting in action '$self->{'Name'}'")
Truth table0%0/3
A || B
A= &deltaSec($self->{'LimitToEvery'})B= &fatal("Invalid 'LimitToEvery' setting in action '$self->{'Name'}'")
ABresult
1X1
011
000
130 &fatal("Invalid 'LimitToEvery' setting in action '$self->{Name}'");
131  
132 0 ▶ &log("Creating new action '$self->{Name}'") if ($VERBOSE);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE 0 0 0
133  
134 # Store myself in the global hash
135 0 $ACTION_LIST{$self->{Name}} = $self;
136  
137 0 return $self;
138 }
139  
140 #-------------------------------------------------------------------------------
141 # Function: run
142 # Description: Run the action. Override this class when subclassing to create
143 # a different action. Note that the triggerAll method has already
144 # forked a separate process at this point, so there's no need to
145 # worry about doing a fork here to avoid blocking the engine if the
146 # action takes a while to run. Also, this method must return a
147 # 1 or the action won't be cleared from the queue.
148 # Input Parms: None
149 # Output Parms: None
150 #-------------------------------------------------------------------------------
151 sub run
152 0 ▶{
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
153 0 my $self = shift;
154 0 my $message = shift;
155 0 my $Execute = $self->{Execute};
156
157 # If nothing was passed in for the execute, then just return.
158 0 ▶ return 1 unless ($Execute);
Branch: 1 branch
BranchTrueFalseTotal
unless $Execute 0 0 0
159
160 0 &log("Executing '$Execute'");
161 0 ▶ unless(open(EXEC, "| $Execute"))
Branch: 1 branch
BranchTrueFalseTotal
unless (open EXEC, "| $Execute") 0 0 0
162 {
163 0 &error("Cannot exec '$Execute'");
164 0 return 0;
165 };
166 0 print EXEC $message;
167 0 close(EXEC);
168  
169 # An error code can appear in either the lower or the higher byte of $?.
170 # By or-ing the lower byte with the upper byte, we get an accurate error code.
171 0 my $ret_code = ($? & 0xFF) | ($? >> 8);
172  
173 0 ▶ &log("Command exited with return code '$ret_code'.") if ($ret_code);
Branch: 1 branch
BranchTrueFalseTotal
if $ret_code 0 0 0
174  
175 0 (1);
176 }
177  
178 #-------------------------------------------------------------------------------
179 # Function: getAction
180 # Description: Retrieve the action object associated with the specified name
181 # Input Parms: Action name
182 # Output Parms: Object
183 #-------------------------------------------------------------------------------
184 sub getAction
185 0 ▶{
Subroutine getAction: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my $name = shift;
187  
188 0 $ACTION_LIST{$name};
189 }
190  
191 #-------------------------------------------------------------------------------
192 # Function: add_message
193 # Description: Add a message to an action to be executed at a later time
194 # Input Parms: Object name, status, object text
195 # Output Parms: None
196 #-------------------------------------------------------------------------------
197 sub add_message
198 0 ▶{
Subroutine add_message: not called CC=6 SCAR=37.4
Pod undocumented
199 0 my $self = shift;
200  
201 0 my ($fullName,$status,$url) = @_;
202 0 my $SendUrl = $self->{SendUrl};
203 0 my $Execute = $self->{Execute};
204 0 my $SubActionList = $self->{SubActionList};
205 0 my $Name = $self->{Name};
206 0 my $LogActionFlag = $self->{LogActionFlag};
207 0 my $messages = $self->{messages};
208 0 my ($message,$subActionName,$subActionObj);
209  
210 # Forming message to be sent
211 0 $message = "$fullName - $status";
212  
213 # Only include the URL in the message if the SendURL option was specified
214 0 ▶ $message .= ($SendUrl) ? "\n$url\n" : ", ";
Branch: 1 branch
BranchTrueFalseTotal
$SendUrl ? : 0 0 0
215 0 ▶ &log("Adding message '$message' to action '$Name'") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
216  
217 # Add the message to the queue of messages to be sent out the next time the
218 # action is triggered.
219 0 push(@{$messages},$message);
220  
221 # Step through each sub-action, passing along the parameters.
222 0 foreach $subActionName (@{$SubActionList})
223 {
224 # Log an error if the action name is invalid
225 0 ▶ unless($subActionObj = &getAction($subActionName))
Branch: 1 branch
BranchTrueFalseTotal
unless ($subActionObj = &getAction($subActionName)) 0 0 0
226 {
227 0 &error("Invalid action name '$subActionName' specified");
228 0 next;
229 }
230 0 $subActionObj->add_message($fullName,$status,$url);
231 }
232  
233 # If the LogAction parm was specified, then add it to the action log.
234 0 ▶ if ($LogActionFlag)
Branch: 1 branch
BranchTrueFalseTotal
if ($LogActionFlag) 0 0 0
235 {
236 0 VBTK::LogActionList::add($fullName,$status,$url,$self);
237 }
238  
239 # Mark this action as pending execution
240 0 $PENDING_QUEUE{$self} = $self;
241 0 (0);
242 }
243  
244 #-------------------------------------------------------------------------------
245 # Function: triggerAll
246 # Description: Attempt to trigger all pending actions.
247 # Input Parms: None
248 # Output Parms: None
249 #-------------------------------------------------------------------------------
250 sub triggerAll
251 0 ▶{
Subroutine triggerAll: not called CC=4 SCAR=30.0
Pod undocumented
252 0 my($actionObj,$actionKey,$pid);
253 0 my @pendingActions = keys %PENDING_QUEUE;
254
255 0 ▶ return unless(@pendingActions > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @pendingActions > 0 0 0 0
256  
257 # # Make sure the previous action handler isn't still running
258 # if(($LAST_HANDLER_PID) && (waitpid($LAST_HANDLER_PID,&WNOHANG) == 0))
259 # {
260 # &error("Previous action handler is still running. Will try again later");
261 # return 1;
262 # }
263 #
264 # # Parent process
265 # if($pid = fork)
266 # {
267 # &log("Forked off action handler - pid $pid");
268 # $LAST_HANDLER_PID = $pid;
269 # }
270 # # Child process
271 # elsif(defined $pid)
272 # {
273 0 ▶ &log("Checking for pending requests") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
274 0 foreach $actionKey (@pendingActions)
275 {
276 0 $actionObj = $PENDING_QUEUE{$actionKey};
277 0 $actionObj->trigger();
278 }
279 # exit(0);
280 # }
281 # else
282 # {
283 # &error("Can't fork action handler! Will try again later");
284 # }
285  
286 0 (0);
287 }
288  
289 #-------------------------------------------------------------------------------
290 # Function: trigger
291 # Description: Check the time window and frequency limits and if okay, fork off
292 # a process to execute the specified action.
293 # Input Parms: None
294 # Output Parms: None
295 #-------------------------------------------------------------------------------
296 sub trigger
297 0 ▶{
Subroutine trigger: not called CC=11 SCAR=48.8
Pod undocumented
298 0 my $self = shift;
299 0 my $Name = $self->{Name};
300  
301 0 ▶ &log("Checking action '$Name' for messages") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
302  
303 # Check for messages. If none, then just return
304 0 my $message = join('',@{$self->{messages}});
305 0 ▶ return 0 if ($message eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $message eq "" 0 0 0
306  
307 0 my $limitToEverySec = $self->{LimitToEverySec};
308 0 my $nextAllowedOccurrence = $self->{nextAllowedOccurrence};
309  
310 0 my ($pid,$ret_code,$now);
311  
312 0 ▶ &log("Checking occurence limit for action '$Name'") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
313 0 $now = time;
314  
315 0 ▶ if(($nextAllowedOccurrence)&&($now < $nextAllowedOccurrence))
Branch: 1 branch
BranchTrueFalseTotal
if ($nextAllowedOccurrence and $now < $nextAllowedOccurrence) 0 0 0
Condition0%0/3
$nextAllowedOccurrence and $now < $nextAllowedOccurrence
!ll&&!rl&&r
000
MC/DC0%0/2
$nextAllowedOccurrence and $now < $nextAllowedOccurrence
$nextAllowedOccurrence $now < $nextAllowedOccurrence
Truth table0%0/3
A and B
A= $nextAllowedOccurrenceB= $now < $nextAllowedOccurrence
ABresult
0X0
100
111
316 {
317 0 ▶ &log("Ignoring trigger of action '$Name'") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
318 0 ▶ &log("Next occurrence allowed at '$nextAllowedOccurrence'")
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
319 if ($VERBOSE > 1);
320 0 return 0;
321 }
322  
323 0 $self->{nextAllowedOccurrence} = $now + $limitToEverySec;
324  
325 0 ▶ &log("Triggering action '$Name'") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
326  
327 # Run the trigger action. The 'run' method must return a positive value
328 # or we won't clear out the messages queue.
329 0 ▶ $self->run($message) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->run($message) or return 0 0 0 0
330  
331 0 ▶ &log("Clearing all messages out of action '$Name'") if ($VERBOSE > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $VBTK::Actions::VERBOSE > 1 0 0 0
332 0 @{$self->{messages}} = ();
333  
334 # Remove object from the pending queue
335 0 delete $PENDING_QUEUE{$self};
336  
337 0 (0);
338 }
339  
340  
341  
342  
343 1 1;
344 __END__
« VBTK/Objects/UpgradeRules.pm ↑ Summary VBTK/Common.pm »