calamaris: Make decompressing large logs more memory-friendly.
[ipfire-2.x.git] / html / cgi-bin / services.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
26906d98 5# Copyright (C) 2005-2010 IPFire Team #
70df8302
MT
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###############################################################################
ac1cfefa
MT
21
22use strict;
23
2032b6f9 24# enable only the following on debugging purpose
2d281532
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
2032b6f9 27
f2fdd0c1 28require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
773362c5 31require "${General::swroot}/graphs.pl";
ac1cfefa 32
2032b6f9
CS
33my %color = ();
34my %mainsettings = ();
4e481c3a 35my %netsettings=();
2032b6f9
CS
36&General::readhash("${General::swroot}/main/settings", \%mainsettings);
37&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
2032b6f9 38&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
ac1cfefa 39
4e481c3a
CS
40#workaround to suppress a warning when a variable is used only once
41my @dummy = ( ${Header::colourred} );
42undef (@dummy);
43
773362c5 44
ac1cfefa 45my %cgiparams=();
2032b6f9
CS
46# Maps a nice printable name to the changing part of the pid file, which
47# is also the name of the program
4e481c3a 48my %servicenames =(
2032b6f9
CS
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
533f1e11
JPT
62my %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
2032b6f9
CS
80my $lines=0; # Used to count the outputlines to make different bgcolor
81
82my $iface = '';
4e481c3a 83if (open(FILE, "${General::swroot}/red/iface")){
2032b6f9
CS
84 $iface = <FILE>;
85 close FILE;
86 chomp $iface;
ac1cfefa 87}
4e481c3a 88
2032b6f9
CS
89$servicenames{"$Lang::tr{'intrusion detection system'} (RED)"} = "snort_${iface}";
90$servicenames{"$Lang::tr{'intrusion detection system'} (GREEN)"} = "snort_$netsettings{'GREEN_DEV'}";
4e481c3a
CS
91
92if ($netsettings{'ORANGE_DEV'} ne ''){
2032b6f9 93 $servicenames{"$Lang::tr{'intrusion detection system'} (ORANGE)"} = "snort_$netsettings{'ORANGE_DEV'}";
ac1cfefa 94}
4e481c3a 95if ($netsettings{'BLUE_DEV'} ne ''){
2032b6f9 96 $servicenames{"$Lang::tr{'intrusion detection system'} (BLUE)"} = "snort_$netsettings{'BLUE_DEV'}";
ac1cfefa
MT
97}
98
4e481c3a
CS
99my @querry = split(/\?/,$ENV{'QUERY_STRING'});
100$querry[0] = '' unless defined $querry[0];
101$querry[1] = 'hour' unless defined $querry[1];
102
103if ( $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
2032b6f9
CS
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>
121END
122;
4e481c3a
CS
123 my $key = '';
124 foreach $key (sort keys %servicenames){
125 $lines++;
126 if ($lines % 2){
533f1e11
JPT
127 print "<tr bgcolor='$color{'color22'}'>\n<td align='left'>";
128 print %link->{$key};
129 print "</td>\n";
4e481c3a 130 }else{
533f1e11
JPT
131 print "<tr bgcolor='$color{'color20'}'>\n<td align='left'>";
132 print %link->{$key};
133 print "</td>\n";
4e481c3a 134 }
ac1cfefa 135
4e481c3a
CS
136 my $shortname = $servicenames{$key};
137 my $status = &isrunning($shortname);
ac1cfefa 138
4e481c3a
CS
139 print "$status\n";
140 print "</tr>\n";
141 }
ac1cfefa 142
4e481c3a
CS
143 print "</table></div>\n";
144 &Header::closebox();
ac1cfefa 145
4e481c3a
CS
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 }
ac1cfefa 152
4e481c3a 153 print <<END
2032b6f9
CS
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>
ac1cfefa
MT
164END
165;
ac1cfefa 166
4e481c3a
CS
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
bbd544ea 180 # mdadm should not stopped with webif because this could crash the system
4e481c3a
CS
181 #
182 chomp($_);
bbd544ea 183 if ( ($_ ne "alsa") && ($_ ne "mdadm") ) {
4e481c3a
CS
184 $lines++;
185 if ($lines % 2){
186 print "<tr bgcolor='$color{'color22'}'>";
187 }else{
188 print "<tr bgcolor='$color{'color20'}'>";
189 }
190 print "<td align='left'>$_</td> ";
191 my $status = isautorun($_);
192 print "$status ";
193 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>";
194 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> ";
195 my $status = &isrunningaddon($_);
196 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
197
198 chomp($status);
199 print "$status";
200 print "</tr>";
201 }
ac1cfefa 202 }
ac1cfefa 203 }
ac1cfefa 204
4e481c3a
CS
205 print "</table></div>\n";
206 &Header::closebox();
ac1cfefa 207
4e481c3a
CS
208 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}");
209 &Graphs::makegraphbox("services.cgi","processescpu","day");
210 &Header::closebox();
773362c5 211
4e481c3a
CS
212 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'memory'} $Lang::tr{'graph'}");
213 &Graphs::makegraphbox("services.cgi","processesmemory","day");
214 &Header::closebox();
215
216 &Header::closebigbox();
217 &Header::closepage();
773362c5 218}
ac1cfefa 219
4e481c3a 220sub isautorun{
2032b6f9
CS
221 my $cmd = $_[0];
222 my $status = "<td align='center'></td>";
bf660619 223 my $init = `find /etc/rc.d/rc3.d/S??${cmd} 2>/dev/null`;
2032b6f9 224 chomp ($init);
4e481c3a
CS
225 if ($init ne ''){
226 $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>";
ac1cfefa 227 }
bf660619 228 $init = `find /etc/rc.d/rc3.d/off/S??${cmd} 2>/dev/null`;
2032b6f9 229 chomp ($init);
4e481c3a
CS
230 if ($init ne ''){
231 $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>";
ac1cfefa 232 }
773362c5 233
4e481c3a 234 return $status;
2032b6f9
CS
235}
236
4e481c3a 237sub isrunning{
2032b6f9
CS
238 my $cmd = $_[0];
239 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
240 my $pid = '';
241 my $testcmd = '';
242 my $exename;
243 my @memory;
244
245 $cmd =~ /(^[a-z]+)/;
246 $exename = $1;
247
248 if (open(FILE, "/var/run/${cmd}.pid")){
249 $pid = <FILE>; chomp $pid;
250 close FILE;
251 if (open(FILE, "/proc/${pid}/status")){
252 while (<FILE>){
4e481c3a
CS
253 if (/^Name:\W+(.*)/) {
254 $testcmd = $1;
255 }
ac1cfefa 256 }
2032b6f9 257 close FILE;
ac1cfefa 258 }
2032b6f9
CS
259 if (open(FILE, "/proc/${pid}/statm")){
260 my $temp = <FILE>;
261 @memory = split(/ /,$temp);
ac1cfefa 262 }
2032b6f9 263 close FILE;
4e481c3a
CS
264 if ($testcmd =~ /$exename/){
265 $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>";
2032b6f9 266 }
4e481c3a
CS
267 }
268 return $status;
ac1cfefa
MT
269}
270
4e481c3a 271sub isrunningaddon{
2032b6f9
CS
272 my $cmd = $_[0];
273 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
274 my $pid = '';
275 my $testcmd = '';
276 my $exename;
277 my @memory;
773362c5 278
26906d98 279 my $testcmd = `/usr/local/bin/addonctrl $_ status 2>/dev/null`;
ac1cfefa 280
2032b6f9 281 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
4e481c3a 282 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
7fb1b9f6 283 $testcmd =~ s/.* //gi;
4e481c3a
CS
284 $testcmd =~ s/[a-z_]//gi;
285 $testcmd =~ s/\[[0-1]\;[0-9]+//gi;
286 $testcmd =~ s/[\(\)\.]//gi;
287 $testcmd =~ s/ //gi;
288 $testcmd =~ s/\e//gi;
2032b6f9 289
4e481c3a
CS
290 my @pid = split(/\s/,$testcmd);
291 $status .="<td align='center'>$pid[0]</td>";
773362c5 292
4e481c3a 293 my $memory = 0;
773362c5 294
4e481c3a
CS
295 foreach (@pid){
296 chomp($_);
297 if (open(FILE, "/proc/$_/statm")){
2032b6f9 298 my $temp = <FILE>;
4e481c3a
CS
299 @memory = split(/ /,$temp);
300 }
301 $memory+=$memory[0];
ac1cfefa 302 }
4e481c3a
CS
303 $status .="<td align='center'>$memory KB</td>";
304 }else{
305 $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2'></td>";
ac1cfefa 306 }
4e481c3a 307 return $status;
ac1cfefa 308}