]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/services.cgi
fix service.cgi hangs if the initscript of an addon dosn't close output
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / services.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 #workaround to suppress a warning when a variable is used only once
33 my @dummy = ( ${Header::colourred} );
34 undef (@dummy);
35
36 my %color = ();
37 my %mainsettings = ();
38 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
39 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
40
41 my %netsettings=();
42 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
43
44 my %cgiparams=();
45 # Maps a nice printable name to the changing part of the pid file, which
46 # is also the name of the program
47 my %servicenames =
48 (
49 $Lang::tr{'dhcp server'} => 'dhcpd',
50 $Lang::tr{'web server'} => 'httpd',
51 $Lang::tr{'cron server'} => 'fcron',
52 $Lang::tr{'dns proxy server'} => 'dnsmasq',
53 $Lang::tr{'logging server'} => 'syslogd',
54 $Lang::tr{'kernel logging server'} => 'klogd',
55 $Lang::tr{'ntp server'} => 'ntpd',
56 $Lang::tr{'secure shell server'} => 'sshd',
57 $Lang::tr{'vpn'} => 'pluto',
58 $Lang::tr{'web proxy'} => 'squid',
59 'OpenVPN' => 'openvpn'
60 );
61
62 my $lines=0; # Used to count the outputlines to make different bgcolor
63
64 my $iface = '';
65 if (open(FILE, "${General::swroot}/red/iface"))
66 {
67 $iface = <FILE>;
68 close FILE;
69 chomp $iface;
70 }
71 $servicenames{"$Lang::tr{'intrusion detection system'} (RED)"} = "snort_${iface}";
72 $servicenames{"$Lang::tr{'intrusion detection system'} (GREEN)"} = "snort_$netsettings{'GREEN_DEV'}";
73 if ($netsettings{'ORANGE_DEV'} ne '') {
74 $servicenames{"$Lang::tr{'intrusion detection system'} (ORANGE)"} = "snort_$netsettings{'ORANGE_DEV'}";
75 }
76 if ($netsettings{'BLUE_DEV'} ne '') {
77 $servicenames{"$Lang::tr{'intrusion detection system'} (BLUE)"} = "snort_$netsettings{'BLUE_DEV'}";
78 }
79
80 &Header::showhttpheaders();
81 &Header::getcgihash(\%cgiparams);
82 &Header::openpage($Lang::tr{'status information'}, 1, '');
83 &Header::openbigbox('100%', 'left');
84
85 &Header::openbox('100%', 'left', $Lang::tr{'services'});
86
87 print <<END
88 <div align='center'>
89 <table width='80%' cellspacing='1' border='0'>
90 <tr bgcolor='$color{'color20'}'><td align='left'><b>$Lang::tr{'services'}</b></td><td align='center' ><b>$Lang::tr{'status'}</b></td><td align='center'><b>PID</b></td><td align='center'><b>$Lang::tr{'memory'}</b></td></tr>
91 END
92 ;
93
94 my $key = '';
95 foreach $key (sort keys %servicenames)
96 {
97 $lines++;
98 if ($lines % 2)
99 { print "<tr bgcolor='$color{'color22'}'>\n<td align='left'>$key</td>\n";}
100 else
101 { print "<tr bgcolor='$color{'color20'}'>\n<td align='left'>$key</td>\n";}
102
103 my $shortname = $servicenames{$key};
104 my $status = &isrunning($shortname);
105
106 print "$status\n";
107 print "</tr>\n";
108 }
109
110
111 print "</table></div>\n";
112
113 &Header::closebox();
114
115 &Header::openbox('100%', 'left', "Addon - $Lang::tr{services}");
116
117 my $paramstr=$ENV{QUERY_STRING};
118 my @param=split(/!/, $paramstr);
119 if ($param[1] ne '') {
120 system("/usr/local/bin/addonctrl @param[0] @param[1] > /dev/null 2>&1");
121 }
122
123 print <<END
124 <div align='center'>
125 <table width='80%' cellspacing='1' border='0'>
126 <tr bgcolor='$color{'color20'}'>
127 <td align='center'><b>Addon</b></td>
128 <td align='center'><b>Boot</b></td>
129 <td align='center' colspan=2><b>$Lang::tr{'action'}</b></td>
130 <td align='center'><b>$Lang::tr{'status'}</b></td>
131 <td align='center'><b>PID</b></td>
132 <td align='center'><b>$Lang::tr{'memory'}</b></td>
133 </tr>
134 END
135 ;
136
137 my $lines=0; # Used to count the outputlines to make different bgcolor
138
139 # Generate list of installed addon pak's
140 my @pak = `find /opt/pakfire/db/installed/meta-* | cut -d"-" -f2`;
141 foreach (@pak)
142 {
143 chomp($_);
144
145 # Check which of the paks are services
146 my @svc = `find /etc/init.d/$_ | cut -d"/" -f4`;
147 foreach (@svc)
148 {
149 # blacklist some packages
150 #
151 # alsa has trouble with the volume saving and was not really stopped
152 #
153 chomp($_);
154 if ($_ ne "alsa")
155 {
156 $lines++;
157 if ($lines % 2)
158 {
159 print "<tr bgcolor='$color{'color22'}'>";
160 }
161 else
162 {
163 print "<tr bgcolor='$color{'color20'}'>";
164 }
165 print "<td align='left'>$_</td> ";
166 my $status = isautorun($_);
167 print "$status ";
168 print "<td align='center'><A HREF=services.cgi?$_!start><img alt='$Lang::tr{'start'}' title='$Lang::tr{'start'}' src='/images/go-up.png' border='0' /></A></td>";
169 print "<td align='center'><A HREF=services.cgi?$_!stop><img alt='$Lang::tr{'stop'}' title='$Lang::tr{'stop'}' src='/images/go-down.png' border='0' /></A></td> ";
170 my $status = &isrunningaddon($_);
171 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
172
173 chomp($status);
174 print "$status";
175 print "</tr>";
176 }
177 }
178 }
179
180 print "</table></div>\n";
181
182 &Header::closebox();
183 &Header::closebigbox();
184 &Header::closepage();
185
186 sub isautorun
187 {
188 my $cmd = $_[0];
189 my $status = "<td align='center'></td>";
190 my $init = `find /etc/rc.d/rc3.d/S??${cmd}`;
191 chomp ($init);
192 if ($init ne '') {
193 $status = "<td align='center'><A HREF=services.cgi?$_!disable><img alt='$Lang::tr{'deactivate'}' title='$Lang::tr{'deactivate'}' src='/images/on.gif' border='0' width='16' height='16' /></A></td>";
194 }
195 $init = `find /etc/rc.d/rc3.d/off/S??${cmd}`;
196 chomp ($init);
197 if ($init ne '') {
198 $status = "<td align='center'><A HREF=services.cgi?$_!enable><img alt='$Lang::tr{'activate'}' title='$Lang::tr{'activate'}' src='/images/off.gif' border='0' width='16' height='16' /></A></td>";
199 }
200
201 return $status;
202 }
203
204 sub isrunning
205 {
206 my $cmd = $_[0];
207 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
208 my $pid = '';
209 my $testcmd = '';
210 my $exename;
211 my @memory;
212
213 $cmd =~ /(^[a-z]+)/;
214 $exename = $1;
215
216 if (open(FILE, "/var/run/${cmd}.pid")){
217 $pid = <FILE>; chomp $pid;
218 close FILE;
219 if (open(FILE, "/proc/${pid}/status")){
220 while (<FILE>){
221 if (/^Name:\W+(.*)/) {$testcmd = $1; }
222 }
223 close FILE;
224 }
225 if (open(FILE, "/proc/${pid}/statm")){
226 my $temp = <FILE>;
227 @memory = split(/ /,$temp);
228 }
229 close FILE;
230 if ($testcmd =~ /$exename/){$status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td><td align='center'>$pid</td><td align='center'>$memory[0] KB</td>";}
231 }
232 return $status;
233 }
234
235 sub isrunningaddon
236 {
237 my $cmd = $_[0];
238 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
239 my $pid = '';
240 my $testcmd = '';
241 my $exename;
242 my @memory;
243
244 my $testcmd = `/usr/local/bin/addonctrl $_ status`;
245
246 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
247 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
248 $testcmd =~ s/[a-z_]//gi; $testcmd =~ s/\[[0-1]\;[0-9]+//gi; $testcmd =~ s/[\(\)\.]//gi; $testcmd =~ s/ //gi; $testcmd =~ s/\e//gi;
249
250 my @pid = split(/\s/,$testcmd);
251 $status .="<td align='center'>$pid[0]</td>";
252
253 my $memory = 0;
254
255 foreach (@pid){
256 chomp($_);
257 if (open(FILE, "/proc/$_/statm")){
258 my $temp = <FILE>;
259 @memory = split(/ /,$temp);
260 }
261 $memory+=$memory[0];
262 }
263 $status .="<td align='center'>$memory KB</td>";
264 }
265 else {$status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";}
266 return $status;
267 }