]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/services.cgi
Hardcode theme to ipfire
[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/ipfire/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'} => 'unbound',
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'} => 'charon',
58 $Lang::tr{'web proxy'} => 'squid',
59 $Lang::tr{'intrusion detection system'} => 'suricata',
60 'OpenVPN' => 'openvpn'
61 );
62
63 my %link =(
64 $Lang::tr{'dhcp server'} => "<a href=\'dhcp.cgi\'>$Lang::tr{'dhcp server'}</a>",
65 $Lang::tr{'web server'} => $Lang::tr{'web server'},
66 $Lang::tr{'cron server'} => $Lang::tr{'cron server'},
67 $Lang::tr{'dns proxy server'} => $Lang::tr{'dns proxy server'},
68 $Lang::tr{'logging server'} => $Lang::tr{'logging server'},
69 $Lang::tr{'kernel logging server'} => $Lang::tr{'kernel logging server'},
70 $Lang::tr{'ntp server'} => "<a href=\'time.cgi\'>$Lang::tr{'ntp server'}</a>",
71 $Lang::tr{'secure shell server'} => "<a href=\'remote.cgi\'>$Lang::tr{'secure shell server'}</a>",
72 $Lang::tr{'vpn'} => "<a href=\'vpnmain.cgi\'>$Lang::tr{'vpn'}</a>",
73 $Lang::tr{'web proxy'} => "<a href=\'proxy.cgi\'>$Lang::tr{'web proxy'}</a>",
74 'OpenVPN' => "<a href=\'ovpnmain.cgi\'>OpenVPN</a>",
75 "$Lang::tr{'intrusion detection system'}" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'}</a>",
76 );
77
78 # Hash to overwrite the process name of a process if it differs fromt the launch command.
79 my %overwrite_exename_hash = (
80 "suricata" => "Suricata-Main"
81 );
82
83 my $lines=0; # Used to count the outputlines to make different bgcolor
84
85 my @querry = split(/\?/,$ENV{'QUERY_STRING'});
86 $querry[0] = '' unless defined $querry[0];
87 $querry[1] = 'hour' unless defined $querry[1];
88
89 if ( $querry[0] =~ "processescpu"){
90 print "Content-type: image/png\n\n";
91 binmode(STDOUT);
92 &Graphs::updateprocessescpugraph($querry[1]);
93 }elsif ( $querry[0] =~ "processesmemory"){
94 print "Content-type: image/png\n\n";
95 binmode(STDOUT);
96 &Graphs::updateprocessesmemorygraph($querry[1]);
97 }else{
98 &Header::showhttpheaders();
99 &Header::openpage($Lang::tr{'status information'}, 1, '');
100 &Header::openbigbox('100%', 'left');
101
102 &Header::openbox('100%', 'left', $Lang::tr{'services'});
103 print <<END
104 <div align='center'>
105 <table width='80%' cellspacing='1' class='tbl'>
106 <tr>
107 <th align='left'><b>$Lang::tr{'services'}</b></th>
108 <th align='center' ><b>$Lang::tr{'status'}</b></th>
109 <th align='center'><b>PID</b></th>
110 <th align='center'><b>$Lang::tr{'memory'}</b></th>
111 </tr>
112 END
113 ;
114 my $key = '';
115 my $col="";
116 foreach $key (sort keys %servicenames){
117 $lines++;
118 if ($lines % 2){
119 $col="bgcolor='$color{'color22'}'";
120 print "<tr><td align='left' $col>";
121 print $link{$key};
122 print "</td>";
123 }else{
124 $col="bgcolor='$color{'color20'}'";
125 print "<tr><td align='left' $col>";
126 print $link{$key};
127 print "</td>";
128 }
129
130 my $shortname = $servicenames{$key};
131 my $status = &isrunning($shortname,$col);
132
133 print "$status\n";
134 print "</tr>\n";
135 }
136
137 print "</table></div>\n";
138 &Header::closebox();
139
140 &Header::openbox('100%', 'left', "Addon - $Lang::tr{services}");
141 my $paramstr=$ENV{QUERY_STRING};
142 my @param=split(/!/, $paramstr);
143 if ($param[1] ne ''){
144 system("/usr/local/bin/addonctrl @param[0] @param[1] > /dev/null 2>&1");
145 }
146
147 print <<END
148 <div align='center'>
149 <table width='80%' cellspacing='1' class='tbl'>
150 <tr>
151 <th align='center'><b>Addon</b></th>
152 <th align='center'><b>Boot</b></th>
153 <th align='center' colspan=2><b>$Lang::tr{'action'}</b></th>
154 <th align='center'><b>$Lang::tr{'status'}</b></th>
155 <th align='center'><b>PID</b></th>
156 <th align='center'><b>$Lang::tr{'memory'}</b></th>
157 </tr>
158 END
159 ;
160
161 my $lines=0; # Used to count the outputlines to make different bgcolor
162
163 # Generate list of installed addon pak's
164 opendir (DIR, "/opt/pakfire/db/installed") || die "Cannot opendir /opt/pakfire/db/installed/: $!";
165 my @pak = sort readdir DIR;
166 foreach (@pak){
167 chomp($_);
168 next unless (m/^meta-/);
169 s/^meta-//;
170
171 # Check which of the paks are services
172 if (-e "/etc/init.d/$_") {
173 # blacklist some packages
174 #
175 # alsa has trouble with the volume saving and was not really stopped
176 # mdadm should not stopped with webif because this could crash the system
177 #
178 if ( $_ eq 'squid' ) {
179 next;
180 }
181 if ( ($_ ne "alsa") && ($_ ne "mdadm") ) {
182 $lines++;
183 if ($lines % 2){
184 print "<tr>";
185 $col="bgcolor='$color{'color22'}'";
186 }else{
187 print "<tr>";
188 $col="bgcolor='$color{'color20'}'";
189 }
190 print "<td align='left' $col width='31%'>$_</td> ";
191 my $status = isautorun($_,$col);
192 print "$status ";
193 print "<td align='center' $col width='8%'><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' $col width='8%'><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($_,$col);
196 $status =~ s/\\e\[[0-1]\;[0-9]+m//g;
197
198 chomp($status);
199 print "$status";
200 print "</tr>";
201 }
202 }
203 }
204
205 print "</table></div>\n";
206 &Header::closebox();
207
208 &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}");
209 &Graphs::makegraphbox("services.cgi","processescpu","day");
210 &Header::closebox();
211
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();
218 }
219
220 sub isautorun{
221 my $cmd = $_[0];
222 my $col = $_[1];
223 my $status = "<td align='center' $col></td>";
224 my $init = `find /etc/rc.d/rc3.d/S??${cmd} 2>/dev/null`;
225 chomp ($init);
226 if ($init ne ''){
227 $status = "<td align='center' $col><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>";
228 }
229 $init = `find /etc/rc.d/rc3.d/off/S??${cmd} 2>/dev/null`;
230 chomp ($init);
231 if ($init ne ''){
232 $status = "<td align='center' $col><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>";
233 }
234
235 return $status;
236 }
237
238 sub isrunning{
239 my $cmd = $_[0];
240 my $col = $_[1];
241 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
242 my $pid = '';
243 my $testcmd = '';
244 my $exename;
245 my $memory;
246
247 $cmd =~ /(^[a-z]+)/;
248
249 # Check if the exename needs to be overwritten.
250 # This happens if the expected process name string
251 # differs from the real one. This may happened if
252 # a service uses multiple processes or threads.
253 if (exists($overwrite_exename_hash{$1})) {
254 # Grab the string which will be reported by
255 # the process from the corresponding hash.
256 $exename = $overwrite_exename_hash{$1};
257 } else {
258 # Directly expect the launched command as
259 # process name.
260 $exename = $1;
261 }
262
263 if (open(FILE, "/var/run/${cmd}.pid")){
264 $pid = <FILE>; chomp $pid;
265 close FILE;
266 if (open(FILE, "/proc/${pid}/status")){
267 while (<FILE>){
268 if (/^Name:\W+(.*)/) {
269 $testcmd = $1;
270 }
271 }
272 close FILE;
273 }
274 if (open(FILE, "/proc/${pid}/status")) {
275 while (<FILE>) {
276 my ($key, $val) = split(":", $_, 2);
277 if ($key eq 'VmRSS') {
278 $memory = $val;
279 last;
280 }
281 }
282 close(FILE);
283 }
284 if ($testcmd =~ /$exename/){
285 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td><td align='center' $col>$pid</td><td align='center' $col>$memory</td>";
286 }
287 }
288 return $status;
289 }
290
291 sub isrunningaddon{
292 my $cmd = $_[0];
293 my $col = $_[1];
294 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
295 my $pid = '';
296 my $testcmd = '';
297 my $exename;
298 my @memory;
299
300 my $testcmd = `/usr/local/bin/addonctrl $_ status 2>/dev/null`;
301
302 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
303 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
304 $testcmd =~ s/.* //gi;
305 $testcmd =~ s/[a-z_]//gi;
306 $testcmd =~ s/\[[0-1]\;[0-9]+//gi;
307 $testcmd =~ s/[\(\)\.]//gi;
308 $testcmd =~ s/ //gi;
309 $testcmd =~ s/\e//gi;
310
311 my @pid = split(/\s/,$testcmd);
312 $status .="<td align='center' $col>$pid[0]</td>";
313
314 my $memory = 0;
315
316 foreach (@pid){
317 chomp($_);
318 if (open(FILE, "/proc/$_/statm")){
319 my $temp = <FILE>;
320 @memory = split(/ /,$temp);
321 }
322 $memory+=$memory[0];
323 }
324 $status .="<td align='center' $col>$memory KB</td>";
325 }else{
326 $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
327 }
328 return $status;
329 }