Debian Codegraph @

monitorix.cgi

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/perl
2 #
3 # Monitorix - A lightweight system monitoring tool.
4 #
5 # Copyright (C) 2005-2022 by Jordi Sanfeliu <[email protected]>
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 2 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 along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #
21
22 use strict;
23 use warnings;
24 use File::Basename;
25 use FindBin qw($Bin);
26 use lib $Bin . "/lib", "/usr/lib/monitorix";
27
28 use Monitorix;
29 use CGI qw(:standard);
30 use Config::General;
31 use POSIX;
32 use RRDs;
33 use Encode;
34 use Fcntl qw(:flock);
35
36 my %config;
37 my %cgi;
38 my %colors;
39 my %tf;
40 my @version12;
41 my @version12_small;
42
43
44 sub multihost {
45 my ($config, $colors, $cgi) = @_;
46
47 my $n;
48 my $n2;
49 my @host;
50 my @url;
51 my @foot_url;
52 my $multihost = $config->{multihost};
53
54 if($cgi->{val} =~ m/group(\d*)/) {
55 my @remotegroup_desc;
56
57 # all groups
58 if($cgi->{val} eq "group") {
59 my @remotegroup_list = split(',', $multihost->{remotegroup_list});
60 for($n = 0; $n < scalar(@remotegroup_list); $n++) {
61 scalar(my @tmp = split(',', $multihost->{remotegroup_desc}->{$n}));
62 for($n2 = 0; $n2 < scalar(@tmp); $n2++) {
63 push(@remotegroup_desc, trim($tmp[$n2]));
64 }
65 }
66 }
67
68 # specific group
69 if($cgi->{val} =~ m/group(\d+)/) {
70 my $gnum = int($1);
71 @remotegroup_desc = split(',', $multihost->{remotegroup_desc}->{$gnum});
72 }
73
74 my @remotehost_list = split(',', $multihost->{remotehost_list});
75 for($n = 0; $n < scalar(@remotegroup_desc); $n++) {
76 my $h = trim($remotegroup_desc[$n]);
77 for($n2 = 0; $n2 < scalar(@remotehost_list); $n2++) {
78 my $h2 = trim($remotehost_list[$n2]);
79 if($h eq $h2) {
80 push(@host, $h);
81 push(@url, trim((split(',', $multihost->{remotehost_desc}->{$n2}))[0]) . trim((split(',', $multihost->{remotehost_desc}->{$n2}))[2]));
82 push(@foot_url, trim((split(',', $multihost->{remotehost_desc}->{$n2}))[0]) . trim((split(',', $multihost->{remotehost_desc}->{$n2}))[1]));
83 }
84 }
85 }
86 } else {
87 my @remotehost_list = split(',', $multihost->{remotehost_list});
88 for($n = 0; $n < scalar(@remotehost_list); $n++) {
89 push(@host, trim($remotehost_list[$n]));
90 push(@url, trim((split(',', $multihost->{remotehost_desc}->{$n}))[0]) . trim((split(',', $multihost->{remotehost_desc}->{$n}))[2]));
91 push(@foot_url, trim((split(',', $multihost->{remotehost_desc}->{$n}))[0]) . trim((split(',', $multihost->{remotehost_desc}->{$n}))[1]));
92 }
93 }
94
95 $multihost->{graphs_per_row} = 1 unless $multihost->{graphs_per_row} > 1;
96 my $graph = ($cgi->{graph} eq "all" || $cgi->{graph} =~ m/group\[0-9]*/) ? "_system1" : $cgi->{graph};
97
98 if($cgi->{val} eq "all" || $cgi->{val} =~ m/group[0-9]*/) {
99 if($cgi->{graph} eq "all") {
100 print "<table class='table-module' width='1' >\n";
101 my $g = 0;
102 foreach (split(',', $config{graph_name})) {
103 my $gn = trim($_);
104 if(lc($config{graph_enable}->{$gn}) eq "y") {
105 if(!$g) {
106 print " <tr>\n";
107 for($n = 0; $n < scalar(@host); $n++) {
108 print " <td class='text-title-multihost'>\n";
109 print " <b>&nbsp;&nbsp;" . $host[$n] . "</b>\n";
110 print " </td>\n";
111 }
112 print " </tr>\n";
113 }
114 for(my $sg = 1; $config{graphs}->{"_$gn$sg"}; $sg++) {
115 my $sgd = $sg;
116 if($gn eq "fs" || $gn eq "net") {
117 $sgd = sprintf("%02d", $sg);
118 }
119 print " <tr>\n";
120 for($n = 0; $n < scalar(@host); $n++) {
121 print " <td style='vertical-align: top; height: 10%; width: 10%;'>\n";
122 print " <iframe src='" . $url[$n] . "/monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=_${gn}$sgd&color=$cgi->{color}&silent=imagetag' height=201 width=397 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
123 print " </td>\n";
124 }
125 print " </tr>\n";
126 }
127 }
128 $g++;
129 }
130 print "</table>\n";
131 print "<br>\n";
132 } else {
133 for($n = 0; $n < scalar(@host); $n += $multihost->{graphs_per_row}) {
134 print "<table class='table-module' width='1' >\n";
135 print " <tr>\n";
136 for($n2 = 0; $n2 < $multihost->{graphs_per_row}; $n2++) {
137 if($n < scalar(@host)) {
138 print " <td class='text-title-multihost'>\n";
139 print " <b>&nbsp;&nbsp;" . $host[$n] . "</b>\n";
140 print " </td>\n";
141 }
142 $n++;
143 }
144 print " </tr>\n";
145 print " <tr>\n";
146 for($n2 = 0, $n = $n - $multihost->{graphs_per_row}; $n2 < $multihost->{graphs_per_row}; $n2++) {
147 if($n < scalar(@host)) {
148 print " <td style='vertical-align: top; height: 10%; width: 10%;'>\n";
149 print " <iframe src='" . $url[$n] . "/monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=$graph&color=$cgi->{color}&silent=imagetag' height=201 width=397 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
150 print " </td>\n";
151
152 }
153 $n++;
154 }
155 print " </tr>\n";
156 print " <tr>\n";
157 for($n2 = 0, $n = $n - $multihost->{graphs_per_row}; $n2 < $multihost->{graphs_per_row}; $n2++) {
158 if($n < scalar(@host)) {
159 if(lc($multihost->{footer_url}) eq "y") {
160 print " <td class='text-title'>\n";
161 print " <font size='-1'>\n";
162 print " <b>&nbsp;&nbsp;<a href='" . $foot_url[$n] . "' style='color: " . $colors->{title_fg_color} . ";'>$foot_url[$n]</a></b>\n";
163 print " </td>\n";
164 }
165 }
166 $n++;
167 }
168 $n = $n - $multihost->{graphs_per_row};
169 print " </tr>\n";
170 print "</table>\n";
171 print "<br>\n";
172 }
173 }
174 } else {
175 if($cgi->{graph} eq "all") {
176 print " <iframe src='" . trim((split(',', $multihost->{remotehost_desc}->{$cgi->{val}}))[0]) . trim((split(',', $multihost->{remotehost_desc}->{$cgi->{val}}))[2]) . "/monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=all&color=$cgi->{color}' height=100% width=100% frameborder=0 marginwidth=0 marginheight=0 scrolling=yes></iframe>\n";
177 } else {
178 print " <table class='table-module' width='1' >\n";
179 print " <tr>\n";
180 print " <td class='text-title-multihost-one'>\n";
181 print " <b>&nbsp;&nbsp;" . $host[$cgi->{val}] . "</b>\n";
182 print " </td>\n";
183 print " </tr>\n";
184 print " <tr>\n";
185 print " <td class='text-title-multihost-one td-valign-top' height: 10%; width: 10%;'>\n";
186 print " <iframe src='" . trim((split(',', $multihost->{remotehost_desc}->{$cgi->{val}}))[0]) . trim((split(',', $multihost->{remotehost_desc}->{$cgi->{val}}))[2]) . "/monitorix.cgi?mode=localhost&when=$cgi->{when}&graph=$graph&color=$cgi->{color}&silent=imagetagbig' height=249 width=545 frameborder=0 marginwidth=0 marginheight=0 scrolling=no></iframe>\n";
187 print " </td>\n";
188 print " </tr>\n";
189 print " <tr>\n";
190 if(lc($multihost->{footer_url}) eq "y") {
191 print " <td class='text-title'>\n";
192 print " <font size='-1'>\n";
193 print " <b>&nbsp;&nbsp;<a href='" . $foot_url[$cgi->{val}] . "' style='color: " . $colors->{title_fg_color} . ";'>$foot_url[$cgi->{val}]</a></b>\n";
194 print " </td>\n";
195 }
196 print " </tr>\n";
197 print " </table>\n";
198 print " <br>\n";
199 }
200 }
201 }
202
203 sub graph_header {
204 my ($title, $colspan) = @_;
205 my @output;
206
207 push(@output, "\n");
208 push(@output, "<!-- graph table begins -->\n");
209 push(@output, " <table class='table-module' width='1' >\n");
210 push(@output, " <tr>\n");
211 push(@output, " <td class='td-title' colspan='$colspan'>\n");
212 push(@output, " <b>&nbsp;&nbsp;$title</b>\n");
213 push(@output, " </td>\n");
214 push(@output, " </tr>\n");
215 return @output;
216 }
217
218 sub graph_footer {
219 my @output;
220
221 push(@output, " </table>\n");
222 push(@output, "<!-- graph table ends -->\n");
223 return @output;
224 }
225
226
227 # MAIN
228 # ----------------------------------------------------------------------------
229 open(IN, dirname(__FILE__)."/monitorix.conf.path");
230 my $config_path = <IN>;
231 my $split_policy = <IN>;
232 chomp($config_path);
233 close(IN);
234
235 $split_policy = "guess" if !defined($split_policy);
236 chomp($split_policy);
237
238 if(! -f $config_path) {
239 print(<< "EOF");
240 Content-Type: text/plain
241 <pre>
242 FATAL: Monitorix is unable to continue!
243 =======================================
244
245 File 'monitorix.conf.path' was not found.
246
247 Please make sure that 'base_dir' option is correctly configured and this
248 CGI (monitorix.cgi) is located in the 'base_dir'/cgi/ directory.
249
250 And don't forget to restart Monitorix for the changes to take effect!
251 EOF
252 die "FATAL: File 'monitorix.conf.path' was not found!";
253 }
254
255 # load main configuration file
256 my $conf = new Config::General(
257 -ConfigFile => $config_path,
258 -SplitPolicy => $split_policy,
259 );
260 %config = $conf->getall;
261
262 # load additional configuration files
263 if($config{include_dir} && opendir(DIR, $config{include_dir})) {
264 my @files = grep { !/^[.]/ } readdir(DIR);
265 closedir(DIR);
266 foreach my $c (sort @files) {
267 next unless -f $config{include_dir} . "/$c";
268 next unless $c =~ m/\.conf$/;
269 my $conf_inc = new Config::General(
270 -ConfigFile => $config{include_dir} . "/$c",
271 -SplitPolicy => $split_policy,
272 );
273 my %config_inc = $conf_inc->getall;
274 while(my ($key, $val) = each(%config_inc)) {
275 if(ref($val) eq "HASH") {
276 # two level options (a subsection)
277 while(my ($key2, $val2) = each(%{$val})) {
278 # delete first this whole subsection
279 delete($config{$key}->{$key2});
280 if(ref($val2) eq "HASH") {
281 # three level options (a subsubsection)
282 while(my ($key3, $val3) = each(%{$val2})) {
283 $config{$key}->{$key2}->{$key3} = $val3;
284 delete $config_inc{$key}->{$key2}->{$key3};
285 }
286 next;
287 }
288 $config{$key}->{$key2} = $val2;
289 delete $config_inc{$key}->{$key2};
290 }
291 next;
292 }
293 # graph_name option is special
294 #if($key eq "graph_name") {
295 # $config{graph_name} .= ", $val";
296 # delete $config_inc{graph_name};
297 # next;
298 #}
299 if($key eq "additional_graph_name") {
300 $config{graph_name} .= ", $val";
301 delete $config_inc{$key};
302 next;
303 }
304 # one level options
305 $config{$key} = $val;
306 delete $config_inc{$key};
307 }
308 }
309 }
310
311 $config{url} = ($config{url_prefix_proxy} || "");
312 if(!$config{url}) {
313 $config{hostname} = $config{hostname} || $ENV{SERVER_NAME};
314 if(lc($config{enable_relative_urls} || "") ne "y") {
315 $config{url} = ($ENV{HTTPS} || ($config{httpd_builtin}->{https_url} || "n") eq "y") ? "https://" . $ENV{HTTP_HOST} : "http://" . $ENV{HTTP_HOST};
316 if(!($config{hostname})) { # called from the command line
317 $config{hostname} = "127.0.0.1";
318 $config{url} = "http://127.0.0.1";
319 }
320 }
321 }
322 $config{url} .= $config{base_url};
323
324 our $mode = defined(param('mode')) ? param('mode') : '';
325 our $graph = param('graph');
326 our $when = param('when');
327 our $color = param('color');
328 our $val = defined(param('val')) ? param('val') : '';
329 our $silent = defined(param('silent')) ? param('silent') : '';
330 if($mode ne "localhost") {
331 ($mode, $val) = split(/\./, $mode);
332 }
333
334 # this should disarm all XSS and Cookie Injection attempts
335 my $OK_CHARS='-a-zA-Z0-9_'; # a restrictive list of valid chars
336 $graph =~ s/[^$OK_CHARS]/_/go; # only $OK_CHARS are allowed
337 $mode =~ s/[^$OK_CHARS]/_/go; # only $OK_CHARS are allowed
338 $when =~ s/[^$OK_CHARS]/_/go; # only $OK_CHARS are allowed
339 $color =~ s/[^$OK_CHARS]/_/go; # only $OK_CHARS are allowed
340 $val =~ s/[^$OK_CHARS]/_/go; # only $OK_CHARS are allowed
341 $silent =~ s/[^$OK_CHARS]/_/go; # only $OK_CHARS are allowed
342
343 #$graph =~ s/\&/&amp;/g;
344 #$graph =~ s/\</&lt;/g;
345 #$graph =~ s/\>/&gt;/g;
346 #$graph =~ s/\"/&quot;/g;
347 #$graph =~ s/\'/&#x27;/g;
348 #$graph =~ s/\(/&#x28;/g;
349 #$graph =~ s/\)/&#x29;/g;
350 #$graph =~ s/\//&#x2F;/g;
351
352 if(lc($config{httpd_builtin}->{enabled}) ne "y") {
353 print("Content-Type: text/html\n");
354 print("\n");
355 }
356
357 # get the current OS and kernel version
358 my $release;
359 ($config{os}, undef, $release) = uname();
360 if(!($release =~ m/^(\d+)\.(\d+)/)) {
361 die "FATAL: unable to get the kernel version.";
362 }
363 $config{kernel} = "$1.$2";
364
365 $colors{graph_colors} = ();
366 $colors{warning_color} = "--color=CANVAS#880000";
367
368 # keep backwards compatibility for v3.2.1 and less
369 if(ref($config{theme}) ne "HASH") {
370 delete($config{theme});
371 }
372
373 if(!$config{theme}->{$color}) {
374 $color = "white";
375
376 $config{theme}->{$color}->{main_bg} = "FFFFFF";
377 $config{theme}->{$color}->{main_fg} = "000000";
378 $config{theme}->{$color}->{title_bg} = "777777";
379 $config{theme}->{$color}->{title_fg} = "CCCC00";
380 $config{theme}->{$color}->{graph_bg} = "CCCCCC";
381 $config{theme}->{$color}->{gap} = "000000";
382 }
383
384 push(@{$colors{graph_colors}}, "--color=CANVAS#" . $config{theme}->{$color}->{canvas}) if defined($config{theme}->{$color}->{canvas});
385 push(@{$colors{graph_colors}}, "--color=BACK#" . $config{theme}->{$color}->{back}) if defined($config{theme}->{$color}->{back});
386 push(@{$colors{graph_colors}}, "--color=FONT#" . $config{theme}->{$color}->{font}) if defined($config{theme}->{$color}->{font});
387 push(@{$colors{graph_colors}}, "--color=MGRID#" . $config{theme}->{$color}->{mgrid}) if defined($config{theme}->{$color}->{mgrid});
388 push(@{$colors{graph_colors}}, "--color=GRID#" . $config{theme}->{$color}->{grid}) if defined($config{theme}->{$color}->{grid});
389 push(@{$colors{graph_colors}}, "--color=FRAME#" . $config{theme}->{$color}->{frame}) if defined($config{theme}->{$color}->{frame});
390 push(@{$colors{graph_colors}}, "--color=ARROW#" . $config{theme}->{$color}->{arrow}) if defined($config{theme}->{$color}->{arrow});
391 push(@{$colors{graph_colors}}, "--color=SHADEA#" . $config{theme}->{$color}->{shadea}) if defined($config{theme}->{$color}->{shadea});
392 push(@{$colors{graph_colors}}, "--color=SHADEB#" . $config{theme}->{$color}->{shadeb}) if defined($config{theme}->{$color}->{shadeb});
393 push(@{$colors{graph_colors}}, "--color=AXIS#" . $config{theme}->{$color}->{axis}) if defined($config{theme}->{$color}->{axis});
394
395 $colors{bg_color} = $config{theme}->{$color}->{main_bg};
396 $colors{fg_color} = $config{theme}->{$color}->{main_fg};
397 $colors{title_bg_color} = $config{theme}->{$color}->{title_bg};
398 $colors{title_fg_color} = $config{theme}->{$color}->{title_fg};
399 $colors{graph_bg_color} = $config{theme}->{$color}->{graph_bg};
400 $colors{gap} = $config{theme}->{$color}->{gap};
401
402
403 ($tf{twhen}) = ($when =~ m/^\d+(hour|day|week|month|year)$/);
404 ($tf{nwhen} = $when) =~ s/$tf{twhen}// unless !$tf{twhen};
405 $tf{nwhen} = 1 unless $tf{nwhen};
406 $tf{twhen} = "day" unless $tf{twhen};
407 $tf{when} = $tf{nwhen} . $tf{twhen};
408
409 # toggle this to 1 if you want to maintain old (2.3-) Monitorix with Multihost
410 if($config{backwards_compat_old_multihost}) {
411 $tf{when} = $tf{twhen};
412 }
413
414 # make sure that some options are correctly defined
415 if(!$config{global_zoom}) {
416 $config{global_zoom} = 1;
417 }
418 if(!$config{image_format}) {
419 $config{image_format} = "PNG";
420 }
421
422 our ($res, $tc, $tb, $ts);
423 if($tf{twhen} eq "day") {
424 ($tf{res}, $tf{tc}, $tf{tb}, $tf{ts}) = (3600, 'h', 24, 1);
425 }
426 if($tf{twhen} eq "week") {
427 ($tf{res}, $tf{tc}, $tf{tb}, $tf{ts}) = (108000, 'd', 7, 1);
428 }
429 if($tf{twhen} eq "month") {
430 ($tf{res}, $tf{tc}, $tf{tb}, $tf{ts}) = (216000, 'd', 30, 1);
431 }
432 if($tf{twhen} eq "year") {
433 ($tf{res}, $tf{tc}, $tf{tb}, $tf{ts}) = (5184000, 'd', 365, 1);
434 }
435
436
437 if($RRDs::VERSION > 1.2) {
438 push(@version12, "--slope-mode");
439 push(@version12, "--font=LEGEND:7:");
440 push(@version12, "--font=TITLE:9:");
441 push(@version12, "--font=UNIT:8:");
442 if($RRDs::VERSION >= 1.3) {
443 push(@version12, "--font=DEFAULT:0:Mono");
444 }
445 if($tf{twhen} eq "day") {
446 push(@version12, "--x-grid=HOUR:1:HOUR:6:HOUR:6:0:%R");
447 }
448 push(@version12_small, "--font=TITLE:8:");
449 push(@version12_small, "--font=UNIT:7:");
450 if($RRDs::VERSION >= 1.3) {
451 push(@version12_small, "--font=DEFAULT:0:Mono");
452 }
453 }
454
455
456 if(!$silent) {
457 my $title;
458 my $str;
459 my @output;
460
461 my $piwik_code = "";
462 my ($piwik_url, $piwik_sid, $piwik_img);
463
464 # Piwik tracking code
465 if(lc($config{piwik_tracking}->{enabled}) eq "y") {
466 $piwik_url = $config{piwik_tracking}->{url} || "";
467 $piwik_sid = $config{piwik_tracking}->{sid} || "";
468 $piwik_img = $config{piwik_tracking}->{img} || "";
469 $piwik_code = <<"EOF";
470
471 <!-- Piwik -->
472 <script type="text/javascript">
473 var _paq = _paq || [];
474 _paq.push(['trackPageView']);
475 _paq.push(['enableLinkTracking']);
476 (function() {
477 var u=(("https:" == document.location.protocol) ? "https" : "http") + "$piwik_url";
478 _paq.push(['setTrackerUrl', u+'piwik.php']);
479 _paq.push(['setSiteId', $piwik_sid]);
480 var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
481 g.defer=true; g.async=true; g.src=u+'piwik.js';
482 s.parentNode.insertBefore(g,s);
483 })();
484 </script>
485 <noscript>
486 <p><img src="$piwik_img" style="border:0;" alt=""/></p>
487 </noscript>
488 <!-- End Piwik Code -->
489 EOF
490 }
491
492 print("<!DOCTYPE html '-//W3C//DTD HTML 4.01 Final//EN'>\n");
493 print("<html>\n");
494 print(" <head>\n");
495 print(" <title>$config{title}</title>\n");
496 print(" <link rel='shortcut icon' href='" . $config{url} . "/" . $config{favicon} . "'>\n");
497 print(" <link href='" . $config{url} . "/css/" . $color . ".css' rel='stylesheet'>\n");
498 if($config{refresh_rate}) {
499 print(" <meta http-equiv='Refresh' content='" . $config{refresh_rate} . "'>\n");
500 }
501 print(" <meta name='apple-mobile-web-app-capable' content='yes' />\n");
502 print(" <meta name='apple-mobile-web-app-status-bar-style' content='black' />\n");
503 print(" </head>\n");
504 print(" <body>\n");
505 print(" $piwik_code\n");
506 print(" <center>\n");
507 push(@output, " <table class='cgi-header-table' >\n");
508 push(@output, " <tr>\n");
509
510 if(lc($config{enable_mainmenu_button} || "") eq "y") {
511 push(@output, " <span style='color:#888888;position:fixed;left:3em;font-size:32px;letter-spacing:-1px;'><a href='" . $config{url} . "/index.html' style='text-decoration:none;'>&#8962;</a>\n");
512 }
513
514 if(lc($config{enable_back_button} || "") eq "y") {
515 push(@output, " <span style='color:#888888;position:fixed;left:1em;font-size:32px;letter-spacing:-1px;'><a href='javascript:history.back()' style='text-decoration:none;'>&#9664;</a>\n");
516 }
517
518 if(($val ne "all" || $val ne "group") && $mode ne "multihost") {
519 push(@output, " <th class='td-title'><b>&nbsp;&nbsp;Host:&nbsp;</b></th>\n");
520 }
521
522 if($val =~ m/group(\d+)/) {
523 my $gnum = $1;
524 my $gname = (split(',', $config{multihost}->{remotegroup_list}))[$gnum];
525 $gname = trim($gname);
526 push(@output, " <th class='td-title-host' ><b>&nbsp;&nbsp;$gname&nbsp;</b></td>\n");
527 }
528
529 push(@output, " <td class='td-title-host' >\n");
530 if($mode eq "localhost" || $mode eq "traffacct") {
531 $title = $config{hostname};
532 } elsif($mode eq "multihost") {
533 if($graph ne "all") {
534 my ($g1, $g2) = ($graph =~ /(_\D+).*?(\d)$/);
535 if($g1 eq "_port") {
536 $title = $config{graphs}->{$g1};
537 $g2 = trim((split(',', $config{port}->{list}))[$g2]);
538 $title .= " " . $g2;
539 $g2 = (split(',', $config{port}->{desc}->{$g2}))[0];
540 $title .= " (" . trim($g2) . ")";
541 } else {
542 $g2 = "" if $g1 eq "_proc"; # '_procn' must be converted to '_proc'
543 $title = $config{graphs}->{$g1 . $g2};
544 }
545 } else {
546 $title = $graph eq "all" ? "all graphs" : $graph;
547 }
548 }
549 $title =~ s/ /&nbsp;/g;
550 my $twhen = $tf{nwhen} > 1 ? "$tf{nwhen} $tf{twhen}" : $tf{twhen};
551 $twhen .= "s" if $tf{nwhen} > 1;
552
553 if($mode ne "multihost" || $graph ne "all" || $val eq "all") {
554 print @output;
555 print(" <b>&nbsp;&nbsp;$title&nbsp;&nbsp;</b>\n");
556 print(" </td>\n");
557 print(" <td class='td-title' ><b>&nbsp;&nbsp;last&nbsp;$twhen&nbsp;&nbsp;</b></td>\n");
558 print(" </tr>\n");
559 print(" </table>\n");
560 print encode('utf-8', " <h4 class='text-title-date'>" . strftime("%a %b %e %H:%M:%S %Z %Y", localtime) . "</h4>\n");
561 }
562 }
563
564
565 $cgi{colors} = \%colors;
566 $cgi{tf} = \%tf;
567 $cgi{version12} = \@version12;
568 $cgi{version12_small} = \@version12_small;
569 $cgi{graph} = $graph;
570 $cgi{when} = $when;
571 $cgi{color} = $color;
572 $cgi{val} = $val;
573 $cgi{silent} = $silent;
574
575 if($mode eq "localhost") {
576 my %outputs; # a hash of arrays
577 my @readers; # array of file descriptors
578 my @writers; # array of file descriptors
579 my $children = 0;
580
581 my $lockfile_handler = lockfile_handler(\%config) unless $ENV{INHIBIT_LOCKING};
582 global_flock($lockfile_handler, LOCK_SH);
583 foreach (split(',', $config{graph_name})) {
584 my $gn = trim($_);
585 my $g = "";
586 if($graph ne "all") {
587 ($g) = ($graph =~ m/^_*($gn)\d*$/);
588 next unless $g;
589 }
590 if(lc($config{graph_enable}->{$gn}) eq "y") {
591 my $cgi = $gn . "_cgi";
592
593 eval "use $gn qw(" . $cgi . ")";
594 if($@) {
595 print(STDERR "WARNING: unable to load module '$gn. $@'\n");
596 next;
597 }
598
599 if($graph eq "all" || $gn eq $g) {
600 no strict "refs";
601
602 if(lc($config{enable_parallelizing} || "") eq "y") {
603 pipe($readers[$children], $writers[$children]);
604 $writers[$children]->autoflush(1);
605
606 if(!fork()) {
607 my $child;
608
609 close($readers[$children]);
610
611 pipe(CHILD_RDR, PARENT_WTR);
612 PARENT_WTR->autoflush(1);
613
614 if(!($child = fork())) {
615 # child
616 my @output;
617 close(CHILD_RDR);
618 @output = &$cgi($gn, \%config, \%cgi);
619 print(PARENT_WTR @output);
620 close(PARENT_WTR);
621 exit(0);
622 }
623
624 # parent
625 my @output;
626 close(PARENT_WTR);
627 @output = <CHILD_RDR>;
628 close(CHILD_RDR);
629 waitpid($child, 0);
630 my $fd = $writers[$children];
631 print($fd @output);
632 close($writers[$children]);
633 exit(0);
634 }
635 close($writers[$children]);
636 $children++;
637
638 } else {
639 my @output = &$cgi($gn, \%config, \%cgi);
640 print @output;
641 }
642 }
643 }
644 }
645 if(lc($config{enable_parallelizing} || "") eq "y") {
646 my $n;
647 my @output;
648
649 for($n = 0; $n < $children; $n++) {
650 my $fd = $readers[$n];
651 @output = <$fd>;
652 close($readers[$n]);
653 @{$outputs{$n}} = @output;
654 waitpid(-1, 0); # wait for each child
655 print @{$outputs{$n}} if $outputs{$n};
656 }
657 }
658 global_flock($lockfile_handler, LOCK_UN);
659
660 } elsif($mode eq "multihost") {
661 multihost(\%config, \%colors, \%cgi);
662
663 } elsif($mode eq "traffacct") {
664 eval "use $mode qw(traffacct_cgi)";
665 if($@) {
666 print(STDERR "WARNING: unable to load module '$mode'. $@\n");
667 exit;
668 }
669 traffacct_cgi($mode, \%config, \%cgi);
670 }
671
672 if(!$silent) {
673 if($mode ne "multihost" || $graph ne "all" || $val eq "all") {
674 print("\n");
675 print(" </center>\n");
676 print("<!-- footer begins -->\n");
677 print(" <p class='text-copyright'>\n");
678 print(" <a href='https://www.monitorix.org'><img src='" . $config{url} . "/" . $config{logo_bottom} . "' border='0'></a>\n");
679 print(" <br>\n");
680 print("Copyright &copy; 2005-2024 Jordi Sanfeliu\n");
681 }
682 print(" </body>\n");
683 print("</html>\n");
684 print("<!-- footer ends -->\n");
685 }
686
687 0;