redirect all the output into an array to support parallelizing in 'libvirt.pm'

This commit is contained in:
Jordi Sanfeliu 2017-07-27 12:19:32 +02:00
parent 66ed64abcd
commit 2447914d05
1 changed files with 54 additions and 53 deletions

View File

@ -1,7 +1,7 @@
#
# Monitorix - A lightweight system monitoring tool.
#
# Copyright (C) 2005-2016 by Jordi Sanfeliu <jordi@fibranet.cat>
# Copyright (C) 2005-2017 by Jordi Sanfeliu <jordi@fibranet.cat>
#
# 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
@ -292,6 +292,7 @@ sub libvirt_update {
sub libvirt_cgi {
my ($package, $config, $cgi) = @_;
my @output;
my $libvirt = $config->{libvirt};
my @rigid = split(',', ($libvirt->{rigid} || ""));
@ -359,20 +360,20 @@ sub libvirt_cgi {
#
if(lc($config->{iface_mode}) eq "text") {
if($title) {
main::graph_header($title, 2);
print(" <tr>\n");
print(" <td bgcolor='$colors->{title_bg_color}'>\n");
push(@output, main::graph_header($title, 2));
push(@output, " <tr>\n");
push(@output, " <td bgcolor='$colors->{title_bg_color}'>\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(" <pre style='font-size: 12px; color: $colors->{fg_color}';>\n");
push(@output, " <pre style='font-size: 12px; color: $colors->{fg_color}';>\n");
foreach my $vmg (sort keys %{$libvirt->{list}}) {
my @lvl = split(',', $libvirt->{list}->{$vmg});
for($n = 0; $n < scalar(@lvl); $n++) {
@ -392,9 +393,9 @@ sub libvirt_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;
@ -404,7 +405,7 @@ sub libvirt_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 $vmg (sort keys %{$libvirt->{list}}) {
my @lvl = split(',', $libvirt->{list}->{$vmg});
@ -416,20 +417,20 @@ sub libvirt_cgi {
$net *= 8;
}
@row = ($cpu || 0, ($mem || 0) / 1024 / 1024, ($dsk || 0) / 1024, ($net || 0) / 1024);
printf(" %4.1f%% %6dM %6.1fM %6.1fM ", @row);
push(@output, sprintf(" %4.1f%% %6dM %6.1fM %6.1fM ", @row));
}
$e++;
}
print("\n");
push(@output, "\n");
}
print(" </pre>\n");
push(@output, " </pre>\n");
if($title) {
print(" </td>\n");
print(" </tr>\n");
main::graph_footer();
push(@output, " </td>\n");
push(@output, " </tr>\n");
push(@output, main::graph_footer());
}
print(" <br>\n");
return;
push(@output, " <br>\n");
return @output;
}
@ -465,10 +466,10 @@ sub libvirt_cgi {
next if !scalar(@lvl);
if($e) {
print(" <br>\n");
push(@output, " <br>\n");
}
if($title) {
main::graph_header($title, 2);
push(@output, main::graph_header($title, 2));
}
@riglim = @{setup_riglim($rigid[0], $limit[0])};
@ -495,8 +496,8 @@ sub libvirt_cgi {
}
}
if($title) {
print(" <tr>\n");
print(" <td bgcolor='$colors->{title_bg_color}'>\n");
push(@output, " <tr>\n");
push(@output, " <td bgcolor='$colors->{title_bg_color}'>\n");
}
if(lc($config->{show_gaps}) eq "y") {
push(@tmp, "AREA:wrongdata#$colors->{gap}:");
@ -532,7 +533,7 @@ sub libvirt_cgi {
@CDEF,
@tmp);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 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 * 4]",
@ -558,13 +559,13 @@ sub libvirt_cgi {
@CDEF,
@tmpz);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4]: $err\n") if $err;
}
$e2 = $e . "1";
if($title || ($silent =~ /imagetag/ && $graph =~ /libvirt$e2/)) {
if(lc($config->{enable_zoom}) eq "y") {
if(lc($config->{disable_javascript_void}) eq "y") {
print(" <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4] . "' border='0'></a>\n");
push(@output, " <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4] . "' border='0'></a>\n");
} else {
if($version eq "new") {
$picz_width = $picz->{image_width} * $config->{global_zoom};
@ -573,16 +574,16 @@ sub libvirt_cgi {
$picz_width = $width + 115;
$picz_height = $height + 100;
}
print(" <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4] . "' border='0'></a>\n");
push(@output, " <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4] . "' border='0'></a>\n");
}
} else {
print(" <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4] . "'>\n");
push(@output, " <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4] . "'>\n");
}
}
if($title) {
print(" </td>\n");
print(" <td valign='top' bgcolor='" . $colors->{title_bg_color} . "'>\n");
push(@output, " </td>\n");
push(@output, " <td valign='top' bgcolor='" . $colors->{title_bg_color} . "'>\n");
}
@riglim = @{setup_riglim($rigid[1], $limit[1])};
undef(@tmp);
@ -651,7 +652,7 @@ sub libvirt_cgi {
@CDEF,
@tmp);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4 + 1]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4 + 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 * 4 + 1]",
@ -685,13 +686,13 @@ sub libvirt_cgi {
@CDEF,
@tmpz);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4 + 1]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4 + 1]: $err\n") if $err;
}
$e2 = $e . "2";
if($title || ($silent =~ /imagetag/ && $graph =~ /libvirt$e2/)) {
if(lc($config->{enable_zoom}) eq "y") {
if(lc($config->{disable_javascript_void}) eq "y") {
print(" <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 1] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 1] . "' border='0'></a>\n");
push(@output, " <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 1] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 1] . "' border='0'></a>\n");
} else {
if($version eq "new") {
$picz_width = $picz->{image_width} * $config->{global_zoom};
@ -700,10 +701,10 @@ sub libvirt_cgi {
$picz_width = $width + 115;
$picz_height = $height + 100;
}
print(" <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 1] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 1] . "' border='0'></a>\n");
push(@output, " <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 1] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 1] . "' border='0'></a>\n");
}
} else {
print(" <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 1] . "'>\n");
push(@output, " <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 1] . "'>\n");
}
}
@ -730,8 +731,8 @@ sub libvirt_cgi {
}
}
if($title) {
print(" <tr>\n");
print(" <td bgcolor='$colors->{title_bg_color}'>\n");
push(@output, " <tr>\n");
push(@output, " <td bgcolor='$colors->{title_bg_color}'>\n");
}
if(lc($config->{show_gaps}) eq "y") {
push(@tmp, "AREA:wrongdata#$colors->{gap}:");
@ -775,7 +776,7 @@ sub libvirt_cgi {
@CDEF,
@tmp);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4 + 2]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4 + 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 * 4 + 2]",
@ -809,13 +810,13 @@ sub libvirt_cgi {
"CDEF:m_dsk7=dsk7,1024,/,1024,/",
@tmpz);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4 + 2]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4 + 2]: $err\n") if $err;
}
$e2 = $e . "3";
if($title || ($silent =~ /imagetag/ && $graph =~ /libvirt$e2/)) {
if(lc($config->{enable_zoom}) eq "y") {
if(lc($config->{disable_javascript_void}) eq "y") {
print(" <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 2] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 2] . "' border='0'></a>\n");
push(@output, " <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 2] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 2] . "' border='0'></a>\n");
}
else { if($version eq "new") {
$picz_width = $picz->{image_width} * $config->{global_zoom};
@ -824,16 +825,16 @@ sub libvirt_cgi {
$picz_width = $width + 115;
$picz_height = $height + 100;
}
print(" <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 2] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 2] . "' border='0'></a>\n");
push(@output, " <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 2] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 2] . "' border='0'></a>\n");
}
} else {
print(" <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 2] . "'>\n");
push(@output, " <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 2] . "'>\n");
}
}
if($title) {
print(" </td>\n");
print(" <td valign='top' bgcolor='" . $colors->{title_bg_color} . "'>\n");
push(@output, " </td>\n");
push(@output, " <td valign='top' bgcolor='" . $colors->{title_bg_color} . "'>\n");
}
@riglim = @{setup_riglim($rigid[3], $limit[3])};
undef(@tmp);
@ -913,7 +914,7 @@ sub libvirt_cgi {
@CDEF,
@tmp);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4 + 3]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMG[$e * 4 + 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 * 4 + 3]",
@ -939,13 +940,13 @@ sub libvirt_cgi {
@CDEF,
@tmpz);
$err = RRDs::error;
print("ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4 + 3]: $err\n") if $err;
push(@output, "ERROR: while graphing $IMG_DIR" . "$IMGz[$e * 4 + 3]: $err\n") if $err;
}
$e2 = $e . "4";
if($title || ($silent =~ /imagetag/ && $graph =~ /libvirt$e2/)) {
if(lc($config->{enable_zoom}) eq "y") {
if(lc($config->{disable_javascript_void}) eq "y") {
print(" <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 3] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 3] . "' border='0'></a>\n");
push(@output, " <a href=\"" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 3] . "\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 3] . "' border='0'></a>\n");
} else {
if($version eq "new") {
$picz_width = $picz->{image_width} * $config->{global_zoom};
@ -954,22 +955,22 @@ sub libvirt_cgi {
$picz_width = $width + 115;
$picz_height = $height + 100;
}
print(" <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 3] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 3] . "' border='0'></a>\n");
push(@output, " <a href=\"javascript:void(window.open('" . $config->{url} . "/" . $config->{imgs_dir} . $IMGz[$e * 4 + 3] . "','','width=" . $picz_width . ",height=" . $picz_height . ",scrollbars=0,resizable=0'))\"><img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 3] . "' border='0'></a>\n");
}
} else {
print(" <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 3] . "'>\n");
push(@output, " <img src='" . $config->{url} . "/" . $config->{imgs_dir} . $IMG[$e * 4 + 3] . "'>\n");
}
}
if($title) {
print(" </td>\n");
print(" </tr>\n");
main::graph_footer();
push(@output, " </td>\n");
push(@output, " </tr>\n");
push(@output, main::graph_footer());
}
$e++;
}
print(" <br>\n");
return;
push(@output, " <br>\n");
return @output;
}
1;