From d300601538e15e5e69f2c798deec7bc7d525a1d7 Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Tue, 25 Jul 2017 12:25:01 +0200 Subject: [PATCH] redirect all the output into an array to support parallelizing in 'du.pm' --- lib/du.pm | 67 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/du.pm b/lib/du.pm index e3e7481..37bacfb 100644 --- a/lib/du.pm +++ b/lib/du.pm @@ -1,7 +1,7 @@ # # Monitorix - A lightweight system monitoring tool. # -# Copyright (C) 2005-2016 by Jordi Sanfeliu +# Copyright (C) 2005-2017 by Jordi Sanfeliu # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -178,6 +178,7 @@ sub du_update { sub du_cgi { my ($package, $config, $cgi) = @_; + my @output; my $du = $config->{du}; my @rigid = split(',', ($du->{rigid} || "")); @@ -236,21 +237,21 @@ sub du_cgi { # if(lc($config->{iface_mode}) eq "text") { if($title) { - main::graph_header($title, 2); - print(" \n"); - print(" \n"); + push(@output, main::graph_header($title, 2)); + push(@output, " \n"); + push(@output, " \n"); } my (undef, undef, undef, $data) = RRDs::fetch("$rrd", "--start=-$tf->{nwhen}$tf->{twhen}", "AVERAGE", "-r $tf->{res}"); $err = RRDs::error; - print("ERROR: while fetching $rrd: $err\n") if $err; + push(@output, "ERROR: while fetching $rrd: $err\n") if $err; my $line1; my $line2; my $line3; - print("
\n");
-		print("    ");
+		push(@output, "    
\n");
+		push(@output, "    ");
 		for($n = 0; $n < scalar(my @dl = split(',', $du->{list})); $n++) {
 			$line1 = "";
 			foreach my $i (split(',', $du->{desc}->{$n})) {
@@ -263,12 +264,12 @@ sub du_cgi {
 			}
 			if($line1) {
 				my $i = length($line1);
-				printf(sprintf("%${i}s", sprintf("%s", trim($dl[$n]))));
+				push(@output, sprintf(sprintf("%${i}s", sprintf("%s", trim($dl[$n])))));
 			}
 		}
-		print("\n");
-		print("Time$line2\n");
-		print("----$line3 \n");
+		push(@output, "\n");
+		push(@output, "Time$line2\n");
+		push(@output, "----$line3 \n");
 		my $line;
 		my @row;
 		my $time;
@@ -279,7 +280,7 @@ sub du_cgi {
 		for($n = 0, $time = $tf->{tb}; $n < ($tf->{tb} * $tf->{ts}); $n++) {
 			$line = @$data[$n];
 			$time = $time - (1 / $tf->{ts});
-			printf(" %2d$tf->{tc} ", $time);
+			push(@output, sprintf(" %2d$tf->{tc} ", $time));
 			for($n2 = 0; $n2 < scalar(my @dl = split(',', $du->{list})); $n2++) {
 				$n3 = 0;
 				foreach my $i (split(',', $du->{desc}->{$n2})) {
@@ -287,19 +288,19 @@ sub du_cgi {
 					$to = $from + 1;
 					my ($j) = @$line[$from..$to];
 					@row = ($j);
-					printf("%17d KB ", @row);
+					push(@output, sprintf("%17d KB ", @row));
 				}
 			}
-			print("\n");
+			push(@output, "\n");
 		}
-		print("    
\n"); + push(@output, "
\n"); if($title) { - print(" \n"); - print(" \n"); - main::graph_footer(); + push(@output, " \n"); + push(@output, " \n"); + push(@output, main::graph_footer()); } - print("
\n"); - return; + push(@output, "
\n"); + return @output; } @@ -330,14 +331,14 @@ sub du_cgi { while($n < scalar(my @dl = split(',', $du->{list}))) { if($title) { if($n == 0) { - main::graph_header($title, $du->{graphs_per_row}); + push(@output, main::graph_header($title, $du->{graphs_per_row})); } - print(" \n"); + push(@output, " \n"); } for($n2 = 0; $n2 < $du->{graphs_per_row}; $n2++) { last unless $n < scalar(my @dl = split(',', $du->{list})); if($title) { - print(" \n"); + push(@output, " \n"); } undef(@tmp); undef(@tmpz); @@ -397,7 +398,7 @@ sub du_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$n]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$n]: $err\n") if $err; if(lc($config->{enable_zoom}) eq "y") { ($width, $height) = split('x', $config->{graph_size}->{zoom}); $picz = $rrd{$version}->("$IMG_DIR" . "$IMGz[$n]", @@ -434,12 +435,12 @@ sub du_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$n]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$n]: $err\n") if $err; } if($title || ($silent =~ /imagetag/ && $graph =~ /du$n/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$n] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$n] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -448,26 +449,26 @@ sub du_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$n] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$n] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } if($title) { - print(" \n"); + push(@output, " \n"); } $n++; } if($title) { - print(" \n"); + push(@output, " \n"); } } if($title) { - main::graph_footer(); + push(@output, main::graph_footer()); } - print("
\n"); - return; + push(@output, "
\n"); + return @output; } 1;