Fix addon service pid/memory display if the addon name has numbers.
[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) 2005-2010 IPFire Team #
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 require "${General::swroot}/graphs.pl";
32
33 my %color = ();
34 my %mainsettings = ();
35 my %netsettings=();
36 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
37 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
38 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
39
40 #workaround to suppress a warning when a variable is used only once
41 my @dummy = ( ${Header::colourred} );
42 undef (@dummy);
43
44
45 my %cgiparams=();
46 # Maps a nice printable name to the changing part of the pid file, which
47 # is also the name of the program
48 my %servicenames =(
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 %link =(
63 $Lang::tr{'dhcp server'} => "<a href=\'dhcp.cgi\'>$Lang::tr{'dhcp server'}</a>",
64 $Lang::tr{'web server'} => $Lang::tr{'web server'},
65 $Lang::tr{'cron server'} => $Lang::tr{'cron server'},
66 $Lang::tr{'dns proxy server'} => $Lang::tr{'dns proxy server'},
67 $Lang::tr{'logging server'} => $Lang::tr{'logging server'},
68 $Lang::tr{'kernel logging server'} => $Lang::tr{'kernel logging server'},
69 $Lang::tr{'ntp server'} => "<a href=\'time.cgi\'>$Lang::tr{'ntp server'}</a>",
70 $Lang::tr{'secure shell server'} => "<a href=\'remote.cgi\'>$Lang::tr{'secure shell server'}</a>",
71 $Lang::tr{'vpn'} => "<a href=\'vpnmain.cgi\'>$Lang::tr{'vpn'}</a>",
72 $Lang::tr{'web proxy'} => "<a href=\'proxy.cgi\'>$Lang::tr{'web proxy'}</a>",
73 'OpenVPN' => "<a href=\'ovpnmain.cgi\'>OpenVPN</a>",
74 "$Lang::tr{'intrusion detection system'} (GREEN)" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'} (GREEN)</a>",
75 "$Lang::tr{'intrusion detection system'} (RED)" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'} (RED)</a>",
76 "$Lang::tr{'intrusion detection system'} (ORANGE)" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'} (ORANGE)</a>",
77 "$Lang::tr{'intrusion detection system'} (BLUE)" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'} (BLUE)</a>"
78 );
79
80 my $lines=0; # Used to count the outputlines to make different bgcolor
81
82 my $iface = '';
83 if (open(FILE, "${General::swroot}/red/iface")){
84 $iface = <FILE>;
85 close FILE;
86 chomp $iface;
87 }
88
89 $servicenames{"$Lang::tr{'intrusion detection system'} (RED)"} = "snort_${iface}";
90 $servicenames{"$Lang::tr{'intrusion detection system'} (GREEN)"} = "snort_$netsettings{'GREEN_DEV'}";
91
92 if ($netsettings{'ORANGE_DEV'} ne ''){
93 $servicenames{"$Lang::tr{'intrusion detection system'} (ORANGE)"} = "snort_$netsettings{'ORANGE_DEV'}";
94 }
95 if ($netsettings{'BLUE_DEV'} ne ''){
96 $servicenames{"$Lang::tr{'intrusion detection system'} (BLUE)"} = "snort_$netsettings{'BLUE_DEV'}";
97 }
98
99 my @querry = split(/\?/,$ENV{'QUERY_STRING'});
100 $querry[0] = '' unless defined $querry[0];
101 $querry[1] = 'hour' unless defined $querry[1];
102
103 if ( $querry[0] =~ "processescpu"){
104 print "Content-type: image/png\n\n";
105 binmode(STDOUT);
106 &Graphs::updateprocessescpugraph($querry[1]);
107 }elsif ( $querry[0] =~ "processesmemory"){
108 print "Content-type: image/png\n\n";
109 binmode(STDOUT);
110 &Graphs::updateprocessesmemorygraph($querry[1]);
111 }else{
112 &Header::showhttpheaders();
113 &Header::openpage($Lang::tr{'status information'}, 1, '');
114 &Header::openbigbox('100%', 'left');
115
116 &Header::openbox('100%', 'left', $Lang::tr{'services'});
117 print <<END
118 <div align='center'>
119 <table width='80%' cellspacing='1' border='0'>
120 <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>
121 END
122 ;
123 my $key = '';
124 foreach $key (sort keys %servicenames){
125 $lines++;
126 if ($lines % 2){
127 print "<tr bgcolor='$color{'color22'}'>\n<td align='left'>";
128 print %link->{$key};
129 print "</td>\n";
130 }else{
131 print "<tr bgcolor='$color{'color20'}'>\n<td align='left'>";
132 print %link->{$key};
133 print "</td>\n";
134 }
135
136 my $shortname = $servicenames{$key};
137 my $status = &isrunning($shortname);
138
139 print "$status\n";
140 print "</tr>\n";
141 }
142
143 print "</table></div>\n";
144 &Header::closebox();
145
146 &Header::openbox('100%', 'left', "Addon - $Lang::tr{services}");
147 my $paramstr=$ENV{QUERY_STRING};
148 my @param=split(/!/, $paramstr);
149 if ($param[1] ne ''){
150 system("/usr/local/bin/addonctrl @param[0] @param[1] > /dev/null 2>&1");
151 }
152
153 print <<END
154 <div align='center'>
155 <table width='80%' cellspacing='1' border='0'>
156 <tr bgcolor='$color{'color20'}'>
157 <td align='center'><b>Addon</b></td>
158 <td align='center'><b>Boot</b></td>
159 <td align='center' colspan=2><b>$Lang::tr{'action'}</b></td>
160 <td align='center'><b>$Lang::tr{'status'}</b></td>
161 <td align='center'><b>PID</b></td>
162 <td align='center'><b>$Lang::tr{'memory'}</b></td>
163 </tr>
164 END
165 ;
166
167 my $lines=0; # Used to count the outputlines to make different bgcolor
168
169 # Generate list of installed addon pak's
170 my @pak = `find /opt/pakfire/db/installed/meta-* 2>/dev/null | cut -d"-" -f2`;
171 foreach (@pak){
172 chomp($_);
173
174 # Check which of the paks are services
175 my @svc = `find /etc/init.d/$_ 2>/dev/null | cut -d"/" -f4`;
176 foreach (@svc){
177 # blacklist some packages
178 #
179 # alsa has trouble with the volume saving and was not really stopped
180 #
181 chomp($_);
182 if ($_ ne "alsa"){
183 $lines++;
184 if ($lines % 2){
185 print "<tr bgcolor='$color{'color22'}'>";
186 }else{
187 print "<tr bgcolor='$color{'color20'}'>";
188 }
189 print "<td align='left'>$_</td> ";
190 my $status = isautorun($_);
191 print "$status ";
192 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>";
193 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> ";
194 my $status = &isrunningaddon($_);
195 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
196
197 chomp($status);
198 print "$status";
199 print "</tr>";
200 }
201 }
202 }
203
204 print "</table></div>\n";
205 &Header::closebox();
206
207 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}");
208 &Graphs::makegraphbox("services.cgi","processescpu","day");
209 &Header::closebox();
210
211 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'memory'} $Lang::tr{'graph'}");
212 &Graphs::makegraphbox("services.cgi","processesmemory","day");
213 &Header::closebox();
214
215 &Header::closebigbox();
216 &Header::closepage();
217 }
218
219 sub isautorun{
220 my $cmd = $_[0];
221 my $status = "<td align='center'></td>";
222 my $init = `find /etc/rc.d/rc3.d/S??${cmd} 2>/dev/null`;
223 chomp ($init);
224 if ($init ne ''){
225 $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>";
226 }
227 $init = `find /etc/rc.d/rc3.d/off/S??${cmd} 2>/dev/null`;
228 chomp ($init);
229 if ($init ne ''){
230 $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>";
231 }
232
233 return $status;
234 }
235
236 sub isrunning{
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 $cmd =~ /(^[a-z]+)/;
245 $exename = $1;
246
247 if (open(FILE, "/var/run/${cmd}.pid")){
248 $pid = <FILE>; chomp $pid;
249 close FILE;
250 if (open(FILE, "/proc/${pid}/status")){
251 while (<FILE>){
252 if (/^Name:\W+(.*)/) {
253 $testcmd = $1;
254 }
255 }
256 close FILE;
257 }
258 if (open(FILE, "/proc/${pid}/statm")){
259 my $temp = <FILE>;
260 @memory = split(/ /,$temp);
261 }
262 close FILE;
263 if ($testcmd =~ /$exename/){
264 $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>";
265 }
266 }
267 return $status;
268 }
269
270 sub isrunningaddon{
271 my $cmd = $_[0];
272 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
273 my $pid = '';
274 my $testcmd = '';
275 my $exename;
276 my @memory;
277
278 my $testcmd = `/usr/local/bin/addonctrl $_ status 2>/dev/null`;
279
280 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
281 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
282 $testcmd =~ s/.* //gi;
283 $testcmd =~ s/[a-z_]//gi;
284 $testcmd =~ s/\[[0-1]\;[0-9]+//gi;
285 $testcmd =~ s/[\(\)\.]//gi;
286 $testcmd =~ s/ //gi;
287 $testcmd =~ s/\e//gi;
288
289 my @pid = split(/\s/,$testcmd);
290 $status .="<td align='center'>$pid[0]</td>";
291
292 my $memory = 0;
293
294 foreach (@pid){
295 chomp($_);
296 if (open(FILE, "/proc/$_/statm")){
297 my $temp = <FILE>;
298 @memory = split(/ /,$temp);
299 }
300 $memory+=$memory[0];
301 }
302 $status .="<td align='center'>$memory KB</td>";
303 }else{
304 $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
305 }
306 return $status;
307 }