From ee17ddbf2bafdfd4a905deb25c721135d0efee8a Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Mon, 28 Aug 2017 17:59:43 +0200 Subject: [PATCH] redirect all the output into an array to support parallelizing in 'process.pm' --- lib/process.pm | 163 +++++++++++++++++++++++++------------------------ 1 file changed, 82 insertions(+), 81 deletions(-) diff --git a/lib/process.pm b/lib/process.pm index 66e8acb..1503cb9 100644 --- a/lib/process.pm +++ b/lib/process.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 @@ -307,6 +307,7 @@ sub process_update { sub process_cgi { my ($package, $config, $cgi) = @_; + my @output; my $process = $config->{process}; my @rigid = split(',', ($process->{rigid} || "")); @@ -376,20 +377,20 @@ sub process_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");
+		push(@output, "    
\n");
 		foreach my $pg (sort keys %{$process->{list}}) {
 			my @lp = split(',', $process->{list}->{$pg});
 			for($n = 0; $n < scalar(@lp); $n++) {
@@ -401,9 +402,9 @@ sub process_cgi {
 				$line3 .=      "---------------------------------------------------------------";
 			}
 		}
-		print("     $line1\n");
-		print("Time $line2\n");
-		print("-----$line3 \n");
+		push(@output, "     $line1\n");
+		push(@output, "Time $line2\n");
+		push(@output, "-----$line3 \n");
 		my $line;
 		my @row;
 		my $time;
@@ -413,7 +414,7 @@ sub process_cgi {
 			$line = @$data[$n];
 			$time = $time - (1 / $tf->{ts});
 			my ($root, $swap) = @$line;
-			printf(" %2d$tf->{tc} ", $time);
+			push(@output, sprintf(" %2d$tf->{tc} ", $time));
 			$e = 0;
 			foreach my $pg (sort keys %{$process->{list}}) {
 				my @lp = split(',', $process->{list}->{$pg});
@@ -432,20 +433,20 @@ sub process_cgi {
 					$pro ||= 0;
 					$nth ||= 0;
 					my $cs = ($vcs || 0) + ($ics || 0) ;
-					printf("  %4.1f%% %6dM %6dM %6dM %7d %7d %7d %7d", $cpu, $mem, $dsk, $net, $nof, $pro, $nth, $cs);
+					push(@output, sprintf("  %4.1f%% %6dM %6dM %6dM %7d %7d %7d %7d", $cpu, $mem, $dsk, $net, $nof, $pro, $nth, $cs));
 				}
 				$e++;
 			}
-			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; } @@ -478,10 +479,10 @@ sub process_cgi { my @lp = split(',', $process->{list}->{$pg}); if($e) { - print("
\n"); + push(@output, "
\n"); } if($title) { - main::graph_header($title, 2); + push(@output, main::graph_header($title, 2)); } @riglim = @{setup_riglim($rigid[0], $limit[0])}; @@ -503,8 +504,8 @@ sub process_cgi { } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } if(lc($config->{show_gaps}) eq "y") { push(@tmp, "AREA:wrongdata#$colors->{gap}:"); @@ -542,7 +543,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10]: $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[$e * 10]", @@ -570,13 +571,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10]: $err\n") if $err; } $e2 = $e . "1"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -585,16 +586,16 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } @riglim = @{setup_riglim($rigid[1], $limit[1])}; undef(@tmp); @@ -663,7 +664,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 1]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 1]: $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[$e * 10 + 1]", @@ -701,13 +702,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 1]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 1]: $err\n") if $err; } $e2 = $e . "2"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 1] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 1] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -716,10 +717,10 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 1] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 1] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -742,8 +743,8 @@ sub process_cgi { } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } if(lc($config->{show_gaps}) eq "y") { push(@tmp, "AREA:wrongdata#$colors->{gap}:"); @@ -791,7 +792,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 2]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 2]: $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[$e * 10 + 2]", @@ -829,13 +830,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 2]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 2]: $err\n") if $err; } $e2 = $e . "3"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 2] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 2] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -844,16 +845,16 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 2] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 2] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } @riglim = @{setup_riglim($rigid[3], $limit[3])}; undef(@tmp); @@ -935,7 +936,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 3]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 3]: $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[$e * 10 + 3]", @@ -963,13 +964,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 3]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 3]: $err\n") if $err; } $e2 = $e . "4"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 3] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 3] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -978,10 +979,10 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 3] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 3] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -1004,8 +1005,8 @@ sub process_cgi { } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } if(lc($config->{show_gaps}) eq "y") { push(@tmp, "AREA:wrongdata#$colors->{gap}:"); @@ -1043,7 +1044,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 4]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 4]: $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[$e * 10 + 4]", @@ -1071,13 +1072,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 4]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 4]: $err\n") if $err; } $e2 = $e . "5"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 4] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 4] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1086,16 +1087,16 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 4] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 4] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } @riglim = @{setup_riglim($rigid[5], $limit[5])}; undef(@tmp); @@ -1154,7 +1155,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 5]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 5]: $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[$e * 10 + 5]", @@ -1182,13 +1183,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 5]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 5]: $err\n") if $err; } $e2 = $e . "6"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 5] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 5] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1197,10 +1198,10 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 5] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 5] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -1225,8 +1226,8 @@ sub process_cgi { } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } if(lc($config->{show_gaps}) eq "y") { push(@tmp, "AREA:wrongdata_p#$colors->{gap}:"); @@ -1298,7 +1299,7 @@ sub process_cgi { "CDEF:tcs9=vcs9,ics9,+", @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 6]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 6]: $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[$e * 10 + 6]", @@ -1357,13 +1358,13 @@ sub process_cgi { "CDEF:tcs9=vcs9,ics9,+", @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 6]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 6]: $err\n") if $err; } $e2 = $e . "7"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 6] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 6] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1372,16 +1373,16 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 6] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 6] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } @riglim = @{setup_riglim($rigid[7], $limit[7])}; undef(@tmp); @@ -1440,7 +1441,7 @@ sub process_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 7]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 10 + 7]: $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[$e * 10 + 7]", @@ -1468,13 +1469,13 @@ sub process_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 7]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 10 + 7]: $err\n") if $err; } $e2 = $e . "8"; if($title || ($silent =~ /imagetag/ && $graph =~ /process$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 7] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 7] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1483,22 +1484,22 @@ sub process_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 7] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 10 + 7] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - 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()); } $e++; } - print("
\n"); - return; + push(@output, "
\n"); + return @output; } 1;