I suggest for collectd2html.pl to write SVG-plots not in separate files
(as it does at present), but to write them into the XHTML-file.
Benefits:
1) saves the filesystem space;
2) saves in number request to the HTTP-server (all the data are
transferred in one request to the server);
3) it's easier to copy just one file.
In order to realize it, the command string formation was replaced by the
command list formation.
--- programmi/contrib_collectd2html.pl 2010-09-10 16:42:20.000000000 +0700
+++ programmi/files_am1.org/progaaa_dl9_diff 2010-09-11 01:11:44.000000000 +0700
@@ -37,7 +37,7 @@
use warnings;
use strict;
-use Fatal qw(open close);
+use Fatal qw(close);
use File::Basename;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);
@@ -45,12 +45,14 @@
my $HOST = undef;
my $IMG_FMT = "PNG";
my $RECURSIVE = 1;
+my $embed_p = "";
GetOptions (
"host=s" => \$HOST,
"data-dir=s" => \$DIR,
"image-format=s" => \$IMG_FMT,
- "recursive" => \$RECURSIVE
+ "recursive" => \$RECURSIVE,
+ "embed-img" => \$embed_p
);
if (($DIR !~ m/\/rrd\/?$/) && (-d "$DIR/rrd")) {
@@ -70,6 +72,12 @@
my $IMG_DIR = "${HOST}.dir";
my $HTML = "${HOST}.xhtml";
+if ($embed_p && ! $svg_p) {
+ $embed_p = "";
+ warn ("The --embed-img option is not compatible with formats",
+ " other than SVG");
+}
+
################################################################################
#
# fade_component
@@ -114,7 +122,8 @@
system("rm -fR $IMG_DIR");
system("mkdir -p $IMG_DIR");
local *OUT;
-open(OUT, ">$HTML");
+open(OUT, ">$HTML")
+ or die "Couldn't write ($HTML): $!";
my $title="Rrd plot for $HOST";
print OUT <<END;
@@ -182,11 +191,12 @@
# all DEF
my $j = 0;
- my $defs = "";
+ my @defs = ();
foreach my $ds (@dss){
- $defs .= " DEF:${ds}_avg=$rrd:$ds:AVERAGE"
- ." DEF:${ds}_max=$rrd:$ds:MAX ";
+ push (@defs,
+ "DEF:${ds}_avg=$rrd:$ds:AVERAGE",
+ "DEF:${ds}_max=$rrd:$ds:MAX");
}
# all AREA
@@ -195,7 +205,9 @@
foreach my $ds (@dss){
my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
my $faded_color = fade_color($color);
- $defs .= sprintf(" AREA:${ds}_max#%06x ", $faded_color);
+ push (@defs,
+ sprintf ("AREA:${ds}_max#%06x", $faded_color));
+
}
# all LINE
@@ -203,10 +215,10 @@
foreach my $ds (@dss){
my $color = $COLORS[$j % scalar(@COLORS)]; $j++;
- $defs .= sprintf(" LINE2:${ds}_avg#%06x:$ds"
- ." GPRINT:${ds}_avg:AVERAGE:%%5.1lf%%sAvg"
- ." GPRINT:${ds}_max:MAX:%%5.1lf%%sMax"
- , $color);
+ push (@defs,
+ sprintf("LINE2:${ds}_avg#%06x:$ds", $color),
+ "GPRINT:${ds}_avg:AVERAGE:%5.1lf%sAvg",
+ "GPRINT:${ds}_max:MAX:%5.1lf%sMax");
}
my $cleaned_bn = $bn;
@@ -218,19 +230,39 @@
# graph various ranges
foreach my $span qw(1hour 1day 1week 1month){
system("mkdir -p $IMG_DIR/" . dirname($bn));
- my $img = "$IMG_DIR/${bn}-$span$IMG_SFX";
-
- my $cmd = "rrdtool graph $img"
- ." -t \"$bn $span\" --imgformat $IMG_FMT --width 600 --height 100"
- ." --start now-$span --end now --interlaced"
- ." $defs >/dev/null 2>&1";
- system($cmd);
+ my $img = ($embed_p
+ ? "/dev/stdout"
+ : "$IMG_DIR/${bn}-$span$IMG_SFX");
+ my $title = "$bn $span";
+ my @rrdtool = ("rrdtool", "graph", $img,
+ "-t", $title,
+ "--imgformat", $IMG_FMT,
+ "--width", 600, "--height", 100,
+ "--start", "now-$span",
+ "--end", "now", "--interlaced",
+ @defs);
+ my $out = "";
+ open (my $in, '-|', @rrdtool)
+ or die "Couldn't read (@rrdtool): $!";
+ {
+ local $/ = "";
+ while (<$in>) {
+ # do something with $_
+ $out .= $_;
+ }
+ }
+ close $in;
+ if ($embed_p) {
+ $out =~ s/^<[?]xml[^>]*[?]>//;
+ }
my $cleaned_img = $img; $cleaned_img =~ s/%/%25/g;
if (! $svg_p) {
print OUT <<END;
<p class="graph"><img src="$cleaned_img" alt="${bn} $span" /></p>
END
+ } elsif ($embed_p) {
+ print OUT $out;
} else {
print OUT <<END;
<p class="graph"><object data="$cleaned_img" type="image/svg+xml">
_______________________________________________
collectd mailing list
[email protected]
http://mailman.verplant.org/listinfo/collectd