]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/services.cgi
freeradius: Update to version 3.0.18
[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'} => '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 my @pak = `find /opt/pakfire/db/installed/meta-* 2>/dev/null | cut -d"-" -f2`;
165 foreach (@pak){
166 chomp($_);
167
168 # Check which of the paks are services
169 my @svc = `find /etc/init.d/$_ 2>/dev/null | cut -d"/" -f4`;
170 foreach (@svc){
171 # blacklist some packages
172 #
173 # alsa has trouble with the volume saving and was not really stopped
174 # mdadm should not stopped with webif because this could crash the system
175 #
176 chomp($_);
177 if ( $_ eq 'squid' ) {
178 next;
179 }
180 if ( ($_ ne "alsa") && ($_ ne "mdadm") ) {
181 $lines++;
182 if ($lines % 2){
183 print "<tr>";
184 $col="bgcolor='$color{'color22'}'";
185 }else{
186 print "<tr>";
187 $col="bgcolor='$color{'color20'}'";
188 }
189 print "<td align='left' $col width='31%'>$_</td> ";
190 my $status = isautorun($_,$col);
191 print "$status ";
192 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>";
193 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> ";
194 my $status = &isrunningaddon($_,$col);
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 $col = $_[1];
222 my $status = "<td align='center' $col></td>";
223 my $init = `find /etc/rc.d/rc3.d/S??${cmd} 2>/dev/null`;
224 chomp ($init);
225 if ($init ne ''){
226 $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>";
227 }
228 $init = `find /etc/rc.d/rc3.d/off/S??${cmd} 2>/dev/null`;
229 chomp ($init);
230 if ($init ne ''){
231 $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>";
232 }
233
234 return $status;
235 }
236
237 sub isrunning{
238 my $cmd = $_[0];
239 my $col = $_[1];
240 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
241 my $pid = '';
242 my $testcmd = '';
243 my $exename;
244 my $memory;
245
246 $cmd =~ /(^[a-z]+)/;
247
248 # Check if the exename needs to be overwritten.
249 # This happens if the expected process name string
250 # differs from the real one. This may happened if
251 # a service uses multiple processes or threads.
252 if (exists($overwrite_exename_hash{$1})) {
253 # Grab the string which will be reported by
254 # the process from the corresponding hash.
255 $exename = $overwrite_exename_hash{$1};
256 } else {
257 # Directly expect the launched command as
258 # process name.
259 $exename = $1;
260 }
261
262 if (open(FILE, "/var/run/${cmd}.pid")){
263 $pid = <FILE>; chomp $pid;
264 close FILE;
265 if (open(FILE, "/proc/${pid}/status")){
266 while (<FILE>){
267 if (/^Name:\W+(.*)/) {
268 $testcmd = $1;
269 }
270 }
271 close FILE;
272 }
273 if (open(FILE, "/proc/${pid}/status")) {
274 while (<FILE>) {
275 my ($key, $val) = split(":", $_, 2);
276 if ($key eq 'VmRSS') {
277 $memory = $val;
278 last;
279 }
280 }
281 close(FILE);
282 }
283 if ($testcmd =~ /$exename/){
284 $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>";
285 }
286 }
287 return $status;
288 }
289
290 sub isrunningaddon{
291 my $cmd = $_[0];
292 my $col = $_[1];
293 my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
294 my $pid = '';
295 my $testcmd = '';
296 my $exename;
297 my @memory;
298
299 my $testcmd = `/usr/local/bin/addonctrl $_ status 2>/dev/null`;
300
301 if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){
302 $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
303 $testcmd =~ s/.* //gi;
304 $testcmd =~ s/[a-z_]//gi;
305 $testcmd =~ s/\[[0-1]\;[0-9]+//gi;
306 $testcmd =~ s/[\(\)\.]//gi;
307 $testcmd =~ s/ //gi;
308 $testcmd =~ s/\e//gi;
309
310 my @pid = split(/\s/,$testcmd);
311 $status .="<td align='center' $col>$pid[0]</td>";
312
313 my $memory = 0;
314
315 foreach (@pid){
316 chomp($_);
317 if (open(FILE, "/proc/$_/statm")){
318 my $temp = <FILE>;
319 @memory = split(/ /,$temp);
320 }
321 $memory+=$memory[0];
322 }
323 $status .="<td align='center' $col>$memory KB</td>";
324 }else{
325 $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>";
326 }
327 return $status;
328 }