From 5828a81086b8b3266b8247d65d45eafe1a264db9 Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Wed, 19 Jul 2017 16:26:10 +0200 Subject: [PATCH] introduced the option 'enable_parallelizing' in order to speed up the graph generation in multi-core systems --- Changes | 2 ++ monitorix.cgi | 69 ++++++++++++++++++++++++++++++++++++++++---------- monitorix.conf | 1 + 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/Changes b/Changes index 508da95..0671106 100644 --- a/Changes +++ b/Changes @@ -13,6 +13,8 @@ in bytes, so the y-axis will scale accordingly. - Changed to be more thickness the lines of Greylisting graph in 'mail.pm'. - Changed the colors of the main graph in 'ftp.pm'. +- Introduced the option 'enable_parallelizing' in order to speed up the graph + generation in multi-core systems. - Added a new option in 'port.rrd' to enable/disable background red color for each port monitored. [#182] - Added to be able to change the real names in the Voltage graph. [#183] diff --git a/monitorix.cgi b/monitorix.cgi index d021f1c..7d0246d 100755 --- a/monitorix.cgi +++ b/monitorix.cgi @@ -169,21 +169,27 @@ sub multihost { sub graph_header { my ($title, $colspan) = @_; - print("\n"); - print("\n"); - print(" \n"); - print(" \n"); - print(" \n"); - print(" \n"); + my @output; + + push(@output, "\n"); + push(@output, "\n"); + push(@output, "
\n"); - print(" \n"); - print("   $title\n"); - print(" \n"); - print("
\n"); + push(@output, " \n"); + push(@output, " \n"); + push(@output, " \n"); + return @output; } sub graph_footer { - print("
\n"); + push(@output, " \n"); + push(@output, "   $title\n"); + push(@output, " \n"); + push(@output, "
\n"); - print("\n"); + my @output; + + push(@output, " \n"); + push(@output, "\n"); + return @output; } @@ -506,6 +512,9 @@ $cgi{val} = $val; $cgi{silent} = $silent; if($mode eq "localhost") { + my $children = 0; + my %outputs; # a hash of arrays + foreach (split(',', $config{graph_name})) { my $gn = trim($_); my $g = ""; @@ -524,12 +533,46 @@ if($mode eq "localhost") { if($graph eq "all" || $gn eq $g) { no strict "refs"; - &$cgi($gn, \%config, \%cgi); + + if(lc($config{enable_parallelizing} || "") eq "y") { + pipe(CHILD_RDR, PARENT_WTR); + PARENT_WTR->autoflush(1); + $children++; + + if(!fork()) { # child + my @output; + close(CHILD_RDR); + @output = &$cgi($gn, \%config, \%cgi); + print(PARENT_WTR @output); + close(PARENT_WTR); + exit(0); + } else { # parent + my @output; + close(PARENT_WTR); + @output = ; + $outputs{$gn} = \@output; + close(CHILD_RDR); + } + } else { + my @output = &$cgi($gn, \%config, \%cgi); + print @output; + } } } } + if(lc($config{enable_parallelizing} || "") eq "y") { + while($children--) { + waitpid(-1, 0); # wait for all children + } + foreach (split(',', $config{graph_name})) { + my $gn = trim($_); + print @{$outputs{$gn}} if $outputs{$gn}; + } + } + } elsif($mode eq "multihost") { multihost(\%config, \%colors, \%cgi); + } elsif($mode eq "traffacct") { eval "use $mode qw(traffacct_cgi)"; if($@) { diff --git a/monitorix.conf b/monitorix.conf index 6746aef..0af613c 100644 --- a/monitorix.conf +++ b/monitorix.conf @@ -17,6 +17,7 @@ global_zoom = 1 max_historic_years = 1 accept_selfsigned_certs = y image_format = PNG +enable_parallelizing = y include_dir = /etc/monitorix/conf.d base_dir = /var/lib/monitorix/www/