]>
Commit | Line | Data |
---|---|---|
ac1cfefa | 1 | #!/usr/bin/perl |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
31c46c07 | 5 | # Copyright (C) 2005-2021 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 | |
22 | use strict; | |
23 | ||
2032b6f9 | 24 | # enable only the following on debugging purpose |
2d281532 CS |
25 | #use warnings; |
26 | #use CGI::Carp 'fatalsToBrowser'; | |
2032b6f9 | 27 | |
f2fdd0c1 | 28 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
29 | require "${General::swroot}/lang.pl"; |
30 | require "${General::swroot}/header.pl"; | |
773362c5 | 31 | require "${General::swroot}/graphs.pl"; |
5cc18f9c | 32 | require "/opt/pakfire/lib/functions.pl"; |
ac1cfefa | 33 | |
2032b6f9 CS |
34 | my %color = (); |
35 | my %mainsettings = (); | |
4e481c3a | 36 | my %netsettings=(); |
2032b6f9 | 37 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); |
8186b372 | 38 | &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color); |
2032b6f9 | 39 | &General::readhash("${General::swroot}/ethernet/settings", \%netsettings); |
ac1cfefa | 40 | |
4e481c3a CS |
41 | #workaround to suppress a warning when a variable is used only once |
42 | my @dummy = ( ${Header::colourred} ); | |
43 | undef (@dummy); | |
44 | ||
773362c5 | 45 | |
ac1cfefa | 46 | my %cgiparams=(); |
2032b6f9 CS |
47 | # Maps a nice printable name to the changing part of the pid file, which |
48 | # is also the name of the program | |
4e481c3a | 49 | my %servicenames =( |
2032b6f9 CS |
50 | $Lang::tr{'dhcp server'} => 'dhcpd', |
51 | $Lang::tr{'web server'} => 'httpd', | |
52 | $Lang::tr{'cron server'} => 'fcron', | |
9625be6f | 53 | $Lang::tr{'dns proxy server'} => 'unbound', |
2032b6f9 CS |
54 | $Lang::tr{'logging server'} => 'syslogd', |
55 | $Lang::tr{'kernel logging server'} => 'klogd', | |
56 | $Lang::tr{'ntp server'} => 'ntpd', | |
57 | $Lang::tr{'secure shell server'} => 'sshd', | |
70e8a248 | 58 | $Lang::tr{'vpn'} => 'charon', |
2032b6f9 | 59 | $Lang::tr{'web proxy'} => 'squid', |
0d8cc90f | 60 | $Lang::tr{'intrusion detection system'} => 'suricata', |
2032b6f9 CS |
61 | 'OpenVPN' => 'openvpn' |
62 | ); | |
63 | ||
533f1e11 JPT |
64 | my %link =( |
65 | $Lang::tr{'dhcp server'} => "<a href=\'dhcp.cgi\'>$Lang::tr{'dhcp server'}</a>", | |
66 | $Lang::tr{'web server'} => $Lang::tr{'web server'}, | |
67 | $Lang::tr{'cron server'} => $Lang::tr{'cron server'}, | |
8aab72f0 | 68 | $Lang::tr{'dns proxy server'} => "<a href=\'dns.cgi\'>$Lang::tr{'dns proxy server'}</a>", |
533f1e11 JPT |
69 | $Lang::tr{'logging server'} => $Lang::tr{'logging server'}, |
70 | $Lang::tr{'kernel logging server'} => $Lang::tr{'kernel logging server'}, | |
71 | $Lang::tr{'ntp server'} => "<a href=\'time.cgi\'>$Lang::tr{'ntp server'}</a>", | |
72 | $Lang::tr{'secure shell server'} => "<a href=\'remote.cgi\'>$Lang::tr{'secure shell server'}</a>", | |
73 | $Lang::tr{'vpn'} => "<a href=\'vpnmain.cgi\'>$Lang::tr{'vpn'}</a>", | |
74 | $Lang::tr{'web proxy'} => "<a href=\'proxy.cgi\'>$Lang::tr{'web proxy'}</a>", | |
75 | 'OpenVPN' => "<a href=\'ovpnmain.cgi\'>OpenVPN</a>", | |
0d8cc90f | 76 | "$Lang::tr{'intrusion detection system'}" => "<a href=\'ids.cgi\'>$Lang::tr{'intrusion detection system'}</a>", |
533f1e11 JPT |
77 | ); |
78 | ||
0d8cc90f SS |
79 | # Hash to overwrite the process name of a process if it differs fromt the launch command. |
80 | my %overwrite_exename_hash = ( | |
81 | "suricata" => "Suricata-Main" | |
82 | ); | |
4e481c3a | 83 | |
0d8cc90f | 84 | my $lines=0; # Used to count the outputlines to make different bgcolor |
ac1cfefa | 85 | |
4e481c3a CS |
86 | my @querry = split(/\?/,$ENV{'QUERY_STRING'}); |
87 | $querry[0] = '' unless defined $querry[0]; | |
88 | $querry[1] = 'hour' unless defined $querry[1]; | |
89 | ||
90 | if ( $querry[0] =~ "processescpu"){ | |
91 | print "Content-type: image/png\n\n"; | |
92 | binmode(STDOUT); | |
93 | &Graphs::updateprocessescpugraph($querry[1]); | |
94 | }elsif ( $querry[0] =~ "processesmemory"){ | |
95 | print "Content-type: image/png\n\n"; | |
96 | binmode(STDOUT); | |
97 | &Graphs::updateprocessesmemorygraph($querry[1]); | |
98 | }else{ | |
99 | &Header::showhttpheaders(); | |
100 | &Header::openpage($Lang::tr{'status information'}, 1, ''); | |
101 | &Header::openbigbox('100%', 'left'); | |
102 | ||
103 | &Header::openbox('100%', 'left', $Lang::tr{'services'}); | |
104 | print <<END | |
2032b6f9 | 105 | <div align='center'> |
91a81810 | 106 | <table width='80%' cellspacing='1' class='tbl'> |
a9d40c03 | 107 | <tr> |
91a81810 AM |
108 | <th align='left'><b>$Lang::tr{'services'}</b></th> |
109 | <th align='center' ><b>$Lang::tr{'status'}</b></th> | |
110 | <th align='center'><b>PID</b></th> | |
111 | <th align='center'><b>$Lang::tr{'memory'}</b></th> | |
112 | </tr> | |
2032b6f9 CS |
113 | END |
114 | ; | |
4e481c3a | 115 | my $key = ''; |
91a81810 | 116 | my $col=""; |
4e481c3a CS |
117 | foreach $key (sort keys %servicenames){ |
118 | $lines++; | |
119 | if ($lines % 2){ | |
91a81810 AM |
120 | $col="bgcolor='$color{'color22'}'"; |
121 | print "<tr><td align='left' $col>"; | |
62a13aec | 122 | print $link{$key}; |
91a81810 | 123 | print "</td>"; |
4e481c3a | 124 | }else{ |
91a81810 AM |
125 | $col="bgcolor='$color{'color20'}'"; |
126 | print "<tr><td align='left' $col>"; | |
62a13aec | 127 | print $link{$key}; |
91a81810 | 128 | print "</td>"; |
4e481c3a | 129 | } |
ac1cfefa | 130 | |
4e481c3a | 131 | my $shortname = $servicenames{$key}; |
91a81810 | 132 | my $status = &isrunning($shortname,$col); |
ac1cfefa | 133 | |
4e481c3a CS |
134 | print "$status\n"; |
135 | print "</tr>\n"; | |
136 | } | |
ac1cfefa | 137 | |
4e481c3a CS |
138 | print "</table></div>\n"; |
139 | &Header::closebox(); | |
ac1cfefa | 140 | |
4e481c3a CS |
141 | &Header::openbox('100%', 'left', "Addon - $Lang::tr{services}"); |
142 | my $paramstr=$ENV{QUERY_STRING}; | |
143 | my @param=split(/!/, $paramstr); | |
144 | if ($param[1] ne ''){ | |
150fadab | 145 | &General::system("/usr/local/bin/addonctrl", "$param[0]", "$param[1]"); |
4e481c3a | 146 | } |
ac1cfefa | 147 | |
4e481c3a | 148 | print <<END |
2032b6f9 | 149 | <div align='center'> |
91a81810 | 150 | <table width='80%' cellspacing='1' class='tbl'> |
a9d40c03 | 151 | <tr> |
91a81810 AM |
152 | <th align='center'><b>Addon</b></th> |
153 | <th align='center'><b>Boot</b></th> | |
154 | <th align='center' colspan=2><b>$Lang::tr{'action'}</b></th> | |
155 | <th align='center'><b>$Lang::tr{'status'}</b></th> | |
156 | <th align='center'><b>PID</b></th> | |
157 | <th align='center'><b>$Lang::tr{'memory'}</b></th> | |
2032b6f9 | 158 | </tr> |
ac1cfefa MT |
159 | END |
160 | ; | |
ac1cfefa | 161 | |
4e481c3a CS |
162 | my $lines=0; # Used to count the outputlines to make different bgcolor |
163 | ||
5cc18f9c RR |
164 | my @paks; |
165 | my @addon_services; | |
166 | ||
167 | # Generate list of installed addon pak services | |
168 | my %paklist = &Pakfire::dblist("installed"); | |
169 | ||
170 | foreach my $pak (keys %paklist) { | |
171 | my %metadata = &Pakfire::getmetadata($pak, "installed"); | |
172 | ||
173 | if ("$metadata{'Services'}") { | |
174 | foreach my $service (split(/ /, "$metadata{'Services'}")) { | |
175 | push(@addon_services, $service); | |
4e481c3a | 176 | } |
ac1cfefa | 177 | } |
ac1cfefa | 178 | } |
ac1cfefa | 179 | |
5cc18f9c RR |
180 | foreach (@addon_services) { |
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 | ||
4e481c3a CS |
202 | print "</table></div>\n"; |
203 | &Header::closebox(); | |
ac1cfefa | 204 | |
4e481c3a CS |
205 | &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'graph'}"); |
206 | &Graphs::makegraphbox("services.cgi","processescpu","day"); | |
207 | &Header::closebox(); | |
773362c5 | 208 | |
4e481c3a CS |
209 | &Header::openbox('100%', 'center', "$Lang::tr{'processes'} $Lang::tr{'memory'} $Lang::tr{'graph'}"); |
210 | &Graphs::makegraphbox("services.cgi","processesmemory","day"); | |
211 | &Header::closebox(); | |
212 | ||
213 | &Header::closebigbox(); | |
214 | &Header::closepage(); | |
773362c5 | 215 | } |
ac1cfefa | 216 | |
f6340997 SS |
217 | sub isautorun (@) { |
218 | my ($cmd, $col) = @_; | |
219 | ||
220 | # Init directory. | |
221 | my $initdir = "/etc/rc.d/rc3.d/"; | |
222 | ||
91a81810 | 223 | my $status = "<td align='center' $col></td>"; |
f6340997 SS |
224 | |
225 | # Check if autorun for the given cmd is enabled. | |
226 | if ( &find_init("$cmd", "$initdir") ) { | |
227 | # Adjust status. | |
df261521 | 228 | $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>"; |
f6340997 SS |
229 | } else { |
230 | # Adjust status. | |
df261521 | 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>"; |
ac1cfefa | 232 | } |
773362c5 | 233 | |
f6340997 | 234 | # Return the status. |
4e481c3a | 235 | return $status; |
2032b6f9 CS |
236 | } |
237 | ||
f6340997 SS |
238 | sub find_init (@) { |
239 | my ($cmd, $dir) = @_; | |
240 | ||
241 | # Open given init directory. | |
242 | opendir (INITDIR, "$dir") || die "Cannot opendir $dir: $!"; | |
243 | ||
244 | # Read-in init files from directory. | |
245 | my @inits = readdir(INITDIR); | |
246 | ||
247 | # Close directory handle. | |
248 | closedir(INITDIR); | |
249 | ||
250 | # Loop through the directory. | |
251 | foreach my $init (@inits) { | |
252 | # Check if the current processed file belongs to the given command. | |
253 | if ($init =~ /S\d+\d+$cmd\z/) { | |
254 | # Found, return "1" - True. | |
255 | return "1"; | |
256 | } | |
257 | } | |
258 | ||
259 | # Nothing found, return nothing. | |
260 | return; | |
261 | } | |
262 | ||
263 | sub isrunning (@) { | |
264 | my ($cmd, $col) = @_; | |
91a81810 | 265 | my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>"; |
2032b6f9 CS |
266 | my $pid = ''; |
267 | my $testcmd = ''; | |
268 | my $exename; | |
24c82ed2 | 269 | my $memory; |
2032b6f9 CS |
270 | |
271 | $cmd =~ /(^[a-z]+)/; | |
0d8cc90f SS |
272 | |
273 | # Check if the exename needs to be overwritten. | |
274 | # This happens if the expected process name string | |
275 | # differs from the real one. This may happened if | |
276 | # a service uses multiple processes or threads. | |
277 | if (exists($overwrite_exename_hash{$1})) { | |
278 | # Grab the string which will be reported by | |
279 | # the process from the corresponding hash. | |
280 | $exename = $overwrite_exename_hash{$1}; | |
281 | } else { | |
282 | # Directly expect the launched command as | |
283 | # process name. | |
284 | $exename = $1; | |
285 | } | |
2032b6f9 CS |
286 | |
287 | if (open(FILE, "/var/run/${cmd}.pid")){ | |
288 | $pid = <FILE>; chomp $pid; | |
289 | close FILE; | |
290 | if (open(FILE, "/proc/${pid}/status")){ | |
291 | while (<FILE>){ | |
4e481c3a CS |
292 | if (/^Name:\W+(.*)/) { |
293 | $testcmd = $1; | |
294 | } | |
ac1cfefa | 295 | } |
2032b6f9 | 296 | close FILE; |
ac1cfefa | 297 | } |
24c82ed2 MT |
298 | if (open(FILE, "/proc/${pid}/status")) { |
299 | while (<FILE>) { | |
300 | my ($key, $val) = split(":", $_, 2); | |
301 | if ($key eq 'VmRSS') { | |
302 | $memory = $val; | |
303 | last; | |
304 | } | |
305 | } | |
306 | close(FILE); | |
ac1cfefa | 307 | } |
4e481c3a | 308 | if ($testcmd =~ /$exename/){ |
91a81810 | 309 | $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>"; |
2032b6f9 | 310 | } |
4e481c3a CS |
311 | } |
312 | return $status; | |
ac1cfefa MT |
313 | } |
314 | ||
f6340997 SS |
315 | sub isrunningaddon (@) { |
316 | my ($cmd, $col) = @_; | |
317 | ||
91a81810 | 318 | my $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>"; |
2032b6f9 CS |
319 | my $pid = ''; |
320 | my $testcmd = ''; | |
321 | my $exename; | |
322 | my @memory; | |
773362c5 | 323 | |
f6340997 | 324 | my @testcmd = &General::system_output("/usr/local/bin/addonctrl", "$cmd", "status"); |
31c46c07 | 325 | my $testcmd = @testcmd[0]; |
ac1cfefa | 326 | |
2032b6f9 | 327 | if ( $testcmd =~ /is\ running/ && $testcmd !~ /is\ not\ running/){ |
4e481c3a | 328 | $status = "<td align='center' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>"; |
7fb1b9f6 | 329 | $testcmd =~ s/.* //gi; |
4e481c3a CS |
330 | $testcmd =~ s/[a-z_]//gi; |
331 | $testcmd =~ s/\[[0-1]\;[0-9]+//gi; | |
332 | $testcmd =~ s/[\(\)\.]//gi; | |
333 | $testcmd =~ s/ //gi; | |
334 | $testcmd =~ s/\e//gi; | |
2032b6f9 | 335 | |
4e481c3a | 336 | my @pid = split(/\s/,$testcmd); |
91a81810 | 337 | $status .="<td align='center' $col>$pid[0]</td>"; |
773362c5 | 338 | |
4e481c3a | 339 | my $memory = 0; |
773362c5 | 340 | |
4e481c3a CS |
341 | foreach (@pid){ |
342 | chomp($_); | |
343 | if (open(FILE, "/proc/$_/statm")){ | |
2032b6f9 | 344 | my $temp = <FILE>; |
4e481c3a CS |
345 | @memory = split(/ /,$temp); |
346 | } | |
347 | $memory+=$memory[0]; | |
ac1cfefa | 348 | } |
91a81810 | 349 | $status .="<td align='center' $col>$memory KB</td>"; |
4e481c3a | 350 | }else{ |
91a81810 | 351 | $status = "<td align='center' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan='2' $col></td>"; |
ac1cfefa | 352 | } |
4e481c3a | 353 | return $status; |
ac1cfefa | 354 | } |