X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/upsgraph/blobdiff_plain/b57167a10dcdf5c36f21f058e2aecf277aff54bc..3d7df03fdea9e5c8d72b08bf6df57203be5885e5:/upsgraph.pl?ds=inline
diff --git a/upsgraph.pl b/upsgraph.pl
index 10f622b..1e335ac 100755
--- a/upsgraph.pl
+++ b/upsgraph.pl
@@ -11,20 +11,26 @@ if ((@ARGV != 1) && (@ARGV != 2)) {
use Net::SNMP;
use IO::Socket::INET;
+use IO::Select;
use RRDs;
use File::Copy;
use Data::Dumper;
$UPSGRAPH::outdir = "";
+$UPSGRAPH::daysCovered = 370;
$UPSGRAPH::step = 60;
-$UPSGRAPH::keep = (370*24*60*60)/$UPSGRAPH::step;
+$UPSGRAPH::stepsPerHour = (60 * 60) / $UPSGRAPH::step;
+$UPSGRAPH::keep = ($UPSGRAPH::daysCovered*24*60*60)/$UPSGRAPH::step;
+$UPSGRAPH::keepHours = ($UPSGRAPH::daysCovered*24*60*60)/$UPSGRAPH::stepsPerHour/$UPSGRAPH::step;
$UPSGRAPH::hosts = ();
do $ARGV[0] or die "can't read config: $!";
my $outdir = $UPSGRAPH::outdir;
my $step = $UPSGRAPH::step;
+my $stepsPerHour = $UPSGRAPH::stepsPerHour;
my $keep = $UPSGRAPH::keep;
+my $keepHours = $UPSGRAPH::keepHours;
my $hosts = $UPSGRAPH::hosts;
sub rrd_update(@) {
@@ -78,9 +84,11 @@ sub rrdcreate(@) {
push @cmd, "DS:${field}:GAUGE:600:" .
$vars->{$field}->{'min'} . ":" .
- $vars->{$field}->{'max'} . " ";
+ $vars->{$field}->{'max'};
push @cmd, "RRA:AVERAGE:0.5:1:${keep}";
+ push @cmd, "RRA:MIN:0.4:${stepsPerHour}:${keepHours}";
+ push @cmd, "RRA:MAX:0.4:${stepsPerHour}:${keepHours}";
RRDs::create(@cmd);
if (RRDs::error) {
@@ -124,7 +132,13 @@ sub fetch_tcp(@) {
return undef if (!$sock);
- chomp(my $value = <$sock>);
+ my $select = IO::Select->new($sock);
+
+ my $value = undef;
+
+ if ($select->can_read(1)) {
+ chomp($value) if (sysread($sock, $value, 4096) > 0);
+ }
close($sock);
@@ -150,11 +164,19 @@ sub fetch_tcp_multi(@) {
return undef if (!$sock);
- while(<$sock>) {
- chomp;
- (my $key, my $value) = split(/${delimiter}/, $_);
- $value=~ s/\s//g;
- $values{$key} = $value;
+ my $select = IO::Select->new($sock);
+
+ while($select->can_read(1)) {
+ if (sysread($sock, my $buf, 16384) > 0) {
+ $buf=~s/\r//g;
+ foreach my $line (split(/\n/, $buf)) {
+ (my $key, my $value) = split(/${delimiter}/, $line);
+ $value=~ s/\s//g;
+ $values{$key} = $value;
+ }
+ } else {
+ last;
+ }
}
close($sock);
@@ -276,7 +298,9 @@ foreach my $host (@$hosts) {
exit 1;
}
- if ($rrdinfo->{'rra[0].rows'} != $keep) {
+ if ($rrdinfo->{'rra[0].rows'} != $keep ||
+ !defined($rrdinfo->{'rra[1].rows'}) || $rrdinfo->{'rra[1].rows'} != $keepHours) {
+
print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
" to ${keep} samples.\n";
@@ -295,7 +319,7 @@ foreach my $host (@$hosts) {
$host->{'vars'},
(${start}-${ostep}));
- print "Preserving data since " . localtime($start) . "\n";
+ print "Preserving data in file ${rrdfile}.${field} since " . localtime($start) . "\n";
my $pos = $start;
foreach my $line (@$data) {
@@ -401,7 +425,9 @@ while(1) {
foreach my $var (@{$host->{'fields'}}) {
my @graphdef = ('-P', "--lazy", "-t", $hostname." - ".$vars->{$var}->{'name'}, "DEF:${var}=${rrdfile}.${var}:${var}:AVERAGE", "LINE1:${var}#FF0000");
-
+
+ push @graphdef, "DEF:${var}-min=${rrdfile}.${var}:${var}:MIN", "LINE1:${var}-min#0000FF";
+ push @graphdef, "DEF:${var}-max=${rrdfile}.${var}:${var}:MAX", "LINE1:${var}-max#00FF00";
push @graphdef, "VDEF:cur=${var},LAST";
push @graphdef, 'GPRINT:cur:Current\\: %.2lf\\r';
@@ -428,16 +454,16 @@ while(1) {
print HTML "
\n";
open (HTML2, ">${outdir}/${hostname}.${var}.html.new");
- print HTML2 '