From 1b76900b15d4605136be7891a3b88075e9127a7e Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Wed, 2 Aug 2017 19:25:36 +0200 Subject: [PATCH] redirect all the output into an array to support parallelizing in 'mysql.pm' --- lib/mysql.pm | 141 ++++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 70 deletions(-) diff --git a/lib/mysql.pm b/lib/mysql.pm index 83f4ca0..43c0cf3 100644 --- a/lib/mysql.pm +++ b/lib/mysql.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 @@ -485,6 +485,7 @@ sub mysql_update { sub mysql_cgi { my ($package, $config, $cgi) = @_; + my @output; my $mysql = $config->{mysql}; my @rigid = split(',', ($mysql->{rigid} || "")); @@ -540,21 +541,21 @@ sub mysql_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 @ml = split(',', $mysql->{list})); $n++) {
 			$line1 = "                                                                                                                                                                                                                          ";
 			$line2 .= "   Select  Commit  Delete  Insert  Insert_S  Update  Replace  Replace_S  Rollback  TCacheHit  QCache_U  Conns_U  KeyBuf_U  InnoDB_U  OpenedTbl  TLocks_W  Queries  SlowQrs  Conns  AbrtCli  AbrtConn  BytesRecv  BytesSent QCacheHitR StmtExec TmpTbToDsk";
@@ -562,16 +563,16 @@ sub mysql_cgi {
 			if($line1) {
 				my $i = length($line1);
 				if(lc($mysql->{conn_type}) eq "host") {
-					printf(sprintf("%${i}s", sprintf("%s:%s", $ml[$n], trim((split(',', $mysql->{desc}->{$ml[$n]}))[0]))));
+					push(@output, sprintf(sprintf("%${i}s", sprintf("%s:%s", $ml[$n], trim((split(',', $mysql->{desc}->{$ml[$n]}))[0])))));
 				}
 				if(lc($mysql->{conn_type}) eq "socket") {
-					printf(sprintf("%${i}s", sprintf("socket: %s", $ml[$n])));
+					push(@output, sprintf(sprintf("%${i}s", sprintf("socket: %s", $ml[$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;
@@ -581,24 +582,24 @@ sub mysql_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 @ml = split(',', $mysql->{list})); $n2++) {
 				undef(@row);
 				$from = $n2 * 38;
 				$to = $from + 38;
 				push(@row, @$line[$from..$to]);
-				printf("   %6d  %6d  %6d  %6d  %8d  %6d  %7d   %8d  %8d        %2d%%       %2d%%      %2d%%       %2d%%       %2d%%     %6d    %6d   %6d   %6d %6d   %6d    %6d  %9d  %9d        %2d%%   %6d        %2d%%", @row);
+				push(@output, sprintf("   %6d  %6d  %6d  %6d  %8d  %6d  %7d   %8d  %8d        %2d%%       %2d%%      %2d%%       %2d%%       %2d%%     %6d    %6d   %6d   %6d %6d   %6d    %6d  %9d  %9d        %2d%%   %6d        %2d%%", @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; } @@ -637,15 +638,15 @@ sub mysql_cgi { } 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])}; if($title) { - print(" \n"); - print(" \n"); + push(@output, " \n"); + push(@output, " \n"); } undef(@tmp); undef(@tmpz); @@ -748,7 +749,7 @@ sub mysql_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 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 * 6]", @@ -776,13 +777,13 @@ sub mysql_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6]: $err\n") if $err; } $e2 = $e + 1; if($title || ($silent =~ /imagetag/ && $graph =~ /mysql$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -791,10 +792,10 @@ sub mysql_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -879,7 +880,7 @@ sub mysql_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 1]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 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 * 6 + 1]", @@ -904,13 +905,13 @@ sub mysql_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 1]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 1]: $err\n") if $err; } $e2 = $e + 2; if($title || ($silent =~ /imagetag/ && $graph =~ /mysql$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 1] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 1] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -919,16 +920,16 @@ sub mysql_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 1] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 1] . "','','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[2], $limit[2])}; undef(@tmp); @@ -977,7 +978,7 @@ sub mysql_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 2]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 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 * 6 + 2]", @@ -998,13 +999,13 @@ sub mysql_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 2]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 2]: $err\n") if $err; } $e2 = $e + 3; if($title || ($silent =~ /imagetag/ && $graph =~ /mysql$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 2] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 2] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1013,10 +1014,10 @@ sub mysql_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 2] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 2] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -1067,7 +1068,7 @@ sub mysql_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 3]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 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 * 6 + 3]", @@ -1088,13 +1089,13 @@ sub mysql_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 3]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 3]: $err\n") if $err; } $e2 = $e + 4; if($title || ($silent =~ /imagetag/ && $graph =~ /mysql$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 3] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 3] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1103,10 +1104,10 @@ sub mysql_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 3] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 3] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -1163,7 +1164,7 @@ sub mysql_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 4]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 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 * 6 + 4]", @@ -1185,13 +1186,13 @@ sub mysql_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 4]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 4]: $err\n") if $err; } $e2 = $e + 5; if($title || ($silent =~ /imagetag/ && $graph =~ /mysql$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 4] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 4] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1200,10 +1201,10 @@ sub mysql_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 4] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 4] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); } } else { - print(" \n"); + push(@output, " \n"); } } @@ -1262,7 +1263,7 @@ sub mysql_cgi { @CDEF, @tmp); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 5]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 6 + 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 * 6 + 5]", @@ -1283,13 +1284,13 @@ sub mysql_cgi { @CDEF, @tmpz); $err = RRDs::error; - print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 5]: $err\n") if $err; + push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 6 + 5]: $err\n") if $err; } $e2 = $e + 6; if($title || ($silent =~ /imagetag/ && $graph =~ /mysql$e2/)) { if(lc($config->{enable_zoom}) eq "y") { if(lc($config->{disable_javascript_void}) eq "y") { - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 5] . "\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 5] . "\">\n"); } else { if($version eq "new") { $picz_width = $picz->{image_width} * $config->{global_zoom}; @@ -1298,31 +1299,31 @@ sub mysql_cgi { $picz_width = $width + 115; $picz_height = $height + 100; } - print(" {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 5] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\">\n"); + push(@output, " {url} . "/" . $config->{imgs_dir} . $IMGz[$e * 6 + 5] . "','','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"); - print(" \n"); - print " \n"; - print " \n"; - print " \n"; - print "   $uri\n"; - print " \n"; - print " \n"; - print(" \n"); - main::graph_footer(); + push(@output, " \n"); + push(@output, " \n"); + push(@output, " \n"); + push(@output, " \n"); + push(@output, "   $uri\n"); + push(@output, " \n"); + push(@output, " \n"); + push(@output, " \n"); + push(@output, main::graph_footer()); } $e++; } - print("
\n"); - return; + push(@output, "
\n"); + return @output; } 1;