| 1 | # | ||||||||||||||||||||
| 2 | # Daemontools.pm - Perl interface for the functionalities of Daemontools | ||||||||||||||||||||
| 3 | # Author: Bruno Negrao G. Zica - bnegrao@engepel.com.br | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Proc::Daemontools; | ||||||||||||||||||||
| 7 | 1 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | $VERSION = "1.06"; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | ######################################################### | ||||||||||||||||||||
| 16 | # ENVIRONMENTAL CONFIGURATION VARIABLES (default values) | ||||||||||||||||||||
| 17 | ######################################################### | ||||||||||||||||||||
| 18 | # | ||||||||||||||||||||
| 19 | # These values are the default values for these variables. | ||||||||||||||||||||
| 20 | # All of them can be overwritten by arguments passed to the new() method. | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | # Path to directory containing the daemontools executables | ||||||||||||||||||||
| 23 | # (supervise, svc, svok, svstat, etc): | ||||||||||||||||||||
| 24 | 1 | my $DAEMONTOOLS_DIR = "/usr/local/bin"; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | # Path to directory monitored by supervise | ||||||||||||||||||||
| 27 | 1 | my $SERVICE_DIR = "/service"; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # The default daemon | ||||||||||||||||||||
| 30 | 1 | my $DAEMON = undef; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # Required executables - without them we can´t work | ||||||||||||||||||||
| 33 | 1 | my @REQ_EXECS = qw ( svc svok svstat ); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 36 | # the code bellow add support for object cloning. | ||||||||||||||||||||
| 37 | # See Perl Cookbook, 13.6. Cloning Objects | ||||||||||||||||||||
| 38 | 0 | my $proto = shift; | |||||||||||||||||||
| 39 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 40 | 0 | ▶ | my $parent = ref($proto) && $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto && $proto
MC/DC0%0/2
ref $proto && $proto
ref $proto
$proto
Truth table0%0/3
A && B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 41 | 0 | my %options = @_; | |||||||||||||||||||
| 42 | 0 | my $self = { }; | |||||||||||||||||||
| 43 | 0 | bless($self, $class); | |||||||||||||||||||
| 44 | # Set the atribute values: | ||||||||||||||||||||
| 45 | 0 | ▶ | if ($parent) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | # Inherit these values from the father | ||||||||||||||||||||
| 47 | 0 | $self->{"DAEMONTOOLS_DIR"} = $parent->{"DAEMONTOOLS_DIR"}; | |||||||||||||||||||
| 48 | 0 | $self->{"SERVICE_DIR"} = $parent->{"SERVICE_DIR"}; | |||||||||||||||||||
| 49 | 0 | $self->{"DAEMON"} = $parent->{"DAEMON"}; | |||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||
| 51 | # Set the default values | ||||||||||||||||||||
| 52 | 0 | $self->{"DAEMONTOOLS_DIR"} = $DAEMONTOOLS_DIR; | |||||||||||||||||||
| 53 | 0 | $self->{"SERVICE_DIR"} = $SERVICE_DIR; | |||||||||||||||||||
| 54 | 0 | $self->{"DAEMON"} = $DAEMON; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | # now, override these values with those received by | ||||||||||||||||||||
| 57 | # the user: (ignoring misspelled options passed, if any) | ||||||||||||||||||||
| 58 | 0 | my $key; | |||||||||||||||||||
| 59 | 0 | foreach $key (keys %options) { | |||||||||||||||||||
| 60 | 0 | ▶ | if ( exists $self->{$key} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | # the regex bellow remove any trailing / from the end of | ||||||||||||||||||||
| 62 | # the directory name | ||||||||||||||||||||
| 63 | 0 | $options{$key} =~ s|/$||; | |||||||||||||||||||
| 64 | 0 | $self->{$key} = $options{$key}; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | # the function bellow tests if every system tool needed can be found | ||||||||||||||||||||
| 68 | 0 | $self->_checkEnv(); # this function will croak if something went wrong | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # declare the complete path to the required executables | ||||||||||||||||||||
| 71 | 0 | my $dir = $self->{"DAEMONTOOLS_DIR"}; | |||||||||||||||||||
| 72 | 0 | foreach (@REQ_EXECS) { | |||||||||||||||||||
| 73 | 0 | $self->{$_} = "$dir/$_"; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 0 | return $self; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # _checkEnv() Checks for the presence of the required executables | ||||||||||||||||||||
| 79 | 0 | ▶ | sub _checkEnv { # returns: void | ||||||||||||||||||
|
Subroutine
_checkEnv: not called CC=9 SCAR=45.0
| |||||||||||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||||||||||
| 81 | 0 | my $dir = $self->{"DAEMONTOOLS_DIR"}; | |||||||||||||||||||
| 82 | 0 | my @execs = @REQ_EXECS; | |||||||||||||||||||
| 83 | 0 | foreach (@execs) { | |||||||||||||||||||
| 84 | 0 | ▶ | unless ( -e "$dir/$_" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 0 | croak "ERROR: the file $_ cannot be found on directory $dir. " . | |||||||||||||||||||
| 86 | "Verify if you set the DAEMONTOOLS_DIR parameter correctly"; | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | 0 | ▶ | unless ( -x "$dir/$_" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | croak "ERROR: the file $_ is not executable by this user."; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | my $dir2 = $self->{"SERVICE_DIR"}; | |||||||||||||||||||
| 94 | 0 | ▶ | unless ( -e $dir2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | croak "ERROR: the directory $dir2 doesn´t exist. ". | |||||||||||||||||||
| 96 | "Verify if you set the SERVICE_DIR parameter correctly."; | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 0 | ▶ | unless ( -x $dir2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | croak "ERROR: the directory $dir2 is not executable by this user."; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | ▶ | if ( $self->{"DAEMON"} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | my $daemon = "$dir2/" . $self->{"DAEMON"}; | |||||||||||||||||||
| 104 | 0 | ▶ | unless ( -e $daemon ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | croak "ERROR: can´t find the daemon $daemon. Verify if you set ". | |||||||||||||||||||
| 106 | "the DAEMON parameter correctly."; | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 0 | ▶ | unless (-x $daemon ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | croak "ERROR: the directory $daemon is not executable by this user."; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # Acessor method daemon(): to get/set the DAEMON atribute | ||||||||||||||||||||
| 115 | 0 | ▶ | sub daemon() { # returns: string | ||||||||||||||||||
|
Subroutine
daemon: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||
| 117 | 0 | ▶ | if (@_) { $self->{"DAEMON"} = shift; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | return $self->{"DAEMON"}; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 0 | ▶ | sub up () { # returns: boolean | ||||||||||||||||||
|
Subroutine
up: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||||||||||
| 123 | 0 | return $self->doSvc(@_); #returns the boolean returned by doSvc() | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | ▶ | sub down () { # returns: boolean | ||||||||||||||||||
|
Subroutine
down: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 127 | 0 | my $self = shift; | |||||||||||||||||||
| 128 | 0 | return $self->doSvc(@_); #returns the boolean returned by doSvc() | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # doSvc() uses the svc executable. It only returns true when the svc command really | ||||||||||||||||||||
| 132 | # performed what it was suposed to do. | ||||||||||||||||||||
| 133 | # This method cannot be run directly, instead it have to be called from methods with | ||||||||||||||||||||
| 134 | # special names, as up() and down() | ||||||||||||||||||||
| 135 | # Ex: | ||||||||||||||||||||
| 136 | # To start qmail-send(within the up() method): $self->doSvc("qmail-send"); | ||||||||||||||||||||
| 137 | # To start the default daemon(within the up() method): $self->doSvc(); | ||||||||||||||||||||
| 138 | # To stop qmail-smtpd(within the down() method): $self->doSvc("qmail-smtpd"); | ||||||||||||||||||||
| 139 | 0 | ▶ | sub doSvc() { # returns: boolean | ||||||||||||||||||
|
Subroutine
doSvc: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||
| 141 | 0 | ▶ | my $daemon = $_[0] ? $_[0] : $self->daemon(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 0 | (caller(1))[3] =~ /::(\w+)$/; | |||||||||||||||||||
| 143 | 0 | my $caller = $1; # this variable will define what option svc will receive | |||||||||||||||||||
| 144 | 0 | my $daemon_dir = $self->{"SERVICE_DIR"} . "/" . $daemon; | |||||||||||||||||||
| 145 | 0 | ▶ | croak "ERROR: directory $daemon_dir doesn´t exist or is not " . | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | "accessible. " unless ( -x $daemon_dir ); | ||||||||||||||||||||
| 147 | 0 | my $svc = $self->{"svc"}; | |||||||||||||||||||
| 148 | 0 | my $svok = $self->{"svok"}; | |||||||||||||||||||
| 149 | 0 | my $opt; # option to svc (-u, -d, -t, etc) | |||||||||||||||||||
| 150 | # defining $opt | ||||||||||||||||||||
| 151 | 0 | ▶ | if ( $caller =~ /up/ ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 152 | 0 | $opt = "-u"; | |||||||||||||||||||
| 153 | } elsif ( $caller =~ /down/ ) { | ||||||||||||||||||||
| 154 | 0 | $opt = "-d"; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | # svok returns 0 for success and non-zero for failure. Also, it only | ||||||||||||||||||||
| 157 | # issues an output when there was an error | ||||||||||||||||||||
| 158 | 0 | ▶ | open (FH, "$svok $daemon_dir 2>&1|") or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | croak "ERROR: cannot run svok: $!"; | ||||||||||||||||||||
| 160 | 0 | my $output = <FH>; | |||||||||||||||||||
| 161 | 0 | close FH; | |||||||||||||||||||
| 162 | 0 | my $ERROR; | |||||||||||||||||||
| 163 | 0 | ▶ | if ($? != 0) { # there was an error | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 164 | 0 | $ERROR = "ERROR: svok said that supervise is not running successfully on " . | |||||||||||||||||||
| 165 | " directory $daemon_dir."; | ||||||||||||||||||||
| 166 | 0 | ▶ | if ($output) { $ERROR .= " svok said: "; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 167 | 0 | croak $ERROR; | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | # svc always return 0 even if it encountered an error. Also, it only outputs | ||||||||||||||||||||
| 170 | # anything when something bad occured, hence we need to check its stderr: | ||||||||||||||||||||
| 171 | 0 | $output = `$svc $opt $daemon_dir 2>&1`; | |||||||||||||||||||
| 172 | 0 | ▶ | croak "ERROR: $svc failed. It said: $output" if $output; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | # Now we gonna check if doSvc() actually did what it tried to do. | ||||||||||||||||||||
| 175 | 0 | ▶ | $self->_isDaemon($daemon, $caller) ? | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | return 1 : | ||||||||||||||||||||
| 177 | return 0; | ||||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | # _isDaemon() - An interface to svstat | ||||||||||||||||||||
| 181 | # Synopsis: $self->("daemon" [,state]); | ||||||||||||||||||||
| 182 | # Tells if the daemon is in a determined state. States are: up, down. | ||||||||||||||||||||
| 183 | # If it doesn´t receive an state string, it will return a string containing the output | ||||||||||||||||||||
| 184 | # of svstat about the required daemon. | ||||||||||||||||||||
| 185 | # To know if qmail-send is stopped: | ||||||||||||||||||||
| 186 | # my $boolean = $self->_isDaemon("qmail-send", "down"); | ||||||||||||||||||||
| 187 | # To know if qmail-smtpd is started: | ||||||||||||||||||||
| 188 | # my $boolean = $self->_isDaemon("qmail-smtpd", "up"); | ||||||||||||||||||||
| 189 | 0 | ▶ | sub _isDaemon (@) { # returns: boolean or string. | ||||||||||||||||||
|
Subroutine
_isDaemon: not called CC=7 SCAR=40.3
| |||||||||||||||||||||
| 190 | 0 | my $self = shift; | |||||||||||||||||||
| 191 | 0 | my $daemon = shift; | |||||||||||||||||||
| 192 | 0 | ▶ | my $state = @_ ? shift : "Print-Output"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 0 | my $daemon_dir = $self->{"SERVICE_DIR"} . "/" . $daemon; | |||||||||||||||||||
| 194 | 0 | ▶ | croak "ERROR: directory $daemon_dir doesn´t exist or is not " . | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | "accessible. " unless ( -x $daemon_dir ); | ||||||||||||||||||||
| 196 | 0 | my $svstat = $self->{"svstat"}; # the svstat executable | |||||||||||||||||||
| 197 | 0 | my $boolean = undef; # what will be returned by this method | |||||||||||||||||||
| 198 | # svstat always return 0 and always outputs to stdout even when there´s an error | ||||||||||||||||||||
| 199 | 0 | ▶ | open (FH, "$svstat $daemon_dir|" ) or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 200 | croak "ERROR: can´t execute the command \'$svstat $daemon_dir\': $!"; | ||||||||||||||||||||
| 201 | 0 | my $output = <FH>; | |||||||||||||||||||
| 202 | 0 | close FH; | |||||||||||||||||||
| 203 | 0 | ▶ | if ($state =~ /Print-Output/ ) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 204 | 0 | $boolean = $output; # output of svstat will be printed | |||||||||||||||||||
| 205 | } elsif ($state =~ /up/) { | ||||||||||||||||||||
| 206 | 0 | $boolean = $output =~ /^$daemon_dir: up/; | |||||||||||||||||||
| 207 | } elsif ($state =~ /down/) { | ||||||||||||||||||||
| 208 | 0 | $boolean = $output =~ /^$daemon_dir: down/; | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | 0 | return $boolean; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | # Ex: if ( $self->is_up("qmail-smtpd") ) { print "SMTPD IS UP";} | ||||||||||||||||||||
| 214 | # or: if ( $self->is_up() ) { print "$self->daemon() IS UP";} | ||||||||||||||||||||
| 215 | 0 | ▶ | sub is_up () { # returns: boolean | ||||||||||||||||||
|
Subroutine
is_up: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 216 | 0 | my $self = shift; | |||||||||||||||||||
| 217 | 0 | ▶ | my $daemon = $_[0] ? $_[0] : $self->daemon(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 218 | 0 | return $self->_isDaemon($daemon, "up"); | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 0 | ▶ | sub status () { # returns: string | ||||||||||||||||||
|
Subroutine
status: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 222 | 0 | my $self = shift; | |||||||||||||||||||
| 223 | 0 | ▶ | my $daemon = $_[0] ? $_[0] : $self->daemon(); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 224 | 0 | return $self->_isDaemon($daemon); | |||||||||||||||||||
| 225 | } | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | 1 | 1; | |||||||||||||||||||
| 228 | __END__ | ||||||||||||||||||||