/
Nginx.pm
128 lines (98 loc) · 3.61 KB
/
Nginx.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package WRE::Nginx;
#-------------------------------------------------------------------
# WRE is Copyright 2005-2011 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
use base 'WRE::Service';
use Carp qw(croak);
use Class::InsideOut qw(new);
use HTTP::Request;
use HTTP::Headers;
use LWP::UserAgent;
use WRE::Host;
=head1 ISA
WRE::Service
=cut
#-------------------------------------------------------------------
=head getName ()
Returns human readable name.
=cut
sub getName {
return "Nginx";
}
#-------------------------------------------------------------------
=head2 ping ( )
Returns a 1 if Nginx is running, or a 0 if it is not.
=cut
sub ping {
my $self = shift;
my $wreConfig = $self->wreConfig;
my $nginx = $wreConfig->get("nginx");
my $userAgent = new LWP::UserAgent;
$userAgent->agent("wre/1.0");
$userAgent->timeout($nginx->{connectionTimeout});
my $header = new HTTP::Headers;
my $url = "http://".$nginx->{defaultHostname}.":".$nginx->{port}."/";
my $request = new HTTP::Request( GET => $url, $header);
my $response = $userAgent->request($request);
if ($response->is_success || $response->code eq "401") {
return 1;
}
croak "Nginx received error code ".$response->code." with message ".$response->error_as_HTML;
return 0;
}
#-------------------------------------------------------------------
=head2 start ( )
Returns a 1 if the start was successful, or a 0 if it was not.
Note: The process that runs this command must be either root or the user specified in the WRE config file.
=cut
sub start {
my $self = shift;
my $wreConfig = $self->wreConfig;
$wreConfig->set("wreMonitor/nginxAdministrativelyDown", 0);
my $host = WRE::Host->new(wreConfig=>$wreConfig);
unless ($wreConfig->get("nginx/port") > 1024 || $host->isPrivilegedUser) {
croak "You are not an administrator on this machine so you cannot start services with ports 1-1024.";
}
my $cmd = "";
$cmd = $wreConfig->getRoot("/prereqs/bin/nginx")." -c ".$wreConfig->getRoot("/etc/nginx.conf");
my $count = 0;
my $success = 0;
`$cmd`; # catch command line output
while ($count < 10 && !$success) {
sleep(1);
eval {$success = $self->ping};
$count++;
}
return $success;
}
#-------------------------------------------------------------------
=head2 stop ( )
Returns a 1 if the stop was successful, or a 0 if it was not.
Note: The process that runs this command must be either root or the user specified in the WRE config file.
=cut
sub stop {
my $self = shift;
my $wreConfig = $self->wreConfig;
$wreConfig->set("wreMonitor/modproxyAdministrativelyDown", 1);
my $host = WRE::Host->new(wreConfig=>$wreConfig);
unless ($wreConfig->get("nginx/port") > 1024 || $host->isPrivilegedUser) {
croak "You are not an administrator on this machine so you cannot stop services with ports 1-1024.";
}
my $cmd = $wreConfig->getRoot("/prereqs/bin/nginx")." -c ".$wreConfig->getRoot("/etc/modproxy.conf")." stop";
`$cmd`; # catch command line output
my $count = 0;
my $success = 0;
while ($count < 10 && !$success) {
$success = !(eval {$self->ping});
$count++;
}
return $success;
}
1;