[1030] | 1 | #!/usr/bin/perl -w #-d
|
---|
| 2 | #
|
---|
| 3 | # sar/iostat graphing tool based on rrdtool
|
---|
| 4 | # (c) 2004 B. Cornec HP <bruno.cornec@hp.com>
|
---|
| 5 | # Licensed under the GPL or Artistic License
|
---|
| 6 | #
|
---|
| 7 | # Common functions
|
---|
| 8 | #
|
---|
| 9 | package sario2rrd;
|
---|
| 10 | use Exporter 'import';
|
---|
| 11 | @EXPORT_OK = qw(create_attr create_gauge update_gauge from_epoch);
|
---|
| 12 |
|
---|
| 13 | use strict;
|
---|
| 14 | use English; # for $UID
|
---|
| 15 | use RRDs; # for data management and graphing
|
---|
| 16 | use Data::Dumper; # for debug
|
---|
| 17 | use Date::Manip; # for timestamp
|
---|
| 18 |
|
---|
| 19 | #
|
---|
| 20 | # Compute the number of fields to create in rrdtool
|
---|
| 21 | # and the mini/maxi timestamp for the start/end (-s/-e option)
|
---|
| 22 | #
|
---|
| 23 | sub create_attr {
|
---|
| 24 |
|
---|
| 25 | my ($t,$attr,$m,$shot,$debug) = @_;
|
---|
| 26 |
|
---|
| 27 | #
|
---|
| 28 | # Initialisation
|
---|
| 29 | #
|
---|
| 30 | if (not (defined $attr->{$m}{mintmst})) {
|
---|
| 31 | $attr->{$m}{mintmst} = 99999999999999;
|
---|
| 32 | }
|
---|
| 33 | if (not (defined $attr->{$m}{maxtmst})) {
|
---|
| 34 | $attr->{$m}{maxtmst} = 0;
|
---|
| 35 | }
|
---|
| 36 | foreach my $ts (keys %{$t}) {
|
---|
| 37 | print "ts: $ts\n" if ($debug > 3);
|
---|
| 38 | #
|
---|
| 39 | # Compute the number of fields for the rrd base
|
---|
| 40 | # and a good timestamp corresponding to the presence
|
---|
| 41 | # of all the fields
|
---|
| 42 | #
|
---|
| 43 | my $nb = keys %{$t->{$ts}};
|
---|
| 44 | if ($nb > $attr->{$m}{nbfields}) {
|
---|
| 45 | $attr->{$m}{nbfields} = $nb;
|
---|
| 46 | $attr->{$m}{goodtmst} = $ts;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | #
|
---|
| 50 | # List of all tags in rrd base
|
---|
| 51 | #
|
---|
| 52 | push @{$attr->{$m}{measures}},(sort keys %{$t->{$attr->{$m}{goodtmst}}});
|
---|
| 53 |
|
---|
| 54 | foreach my $ts (keys %{$t}) {
|
---|
| 55 | #
|
---|
| 56 | # Compute the first/last timestamp
|
---|
| 57 | #
|
---|
| 58 | if ($shot == 1) {
|
---|
| 59 | #
|
---|
| 60 | # Fill empty parts of the hash in case of need for
|
---|
| 61 | # fixed dates in extraction
|
---|
| 62 | #
|
---|
| 63 | foreach my $f (@{$attr->{$m}{measures}}) {
|
---|
| 64 | $t->{$ts}{$f} = 0 if (not (defined $t->{$ts}{$f}));
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | my $nb = keys %{$t->{$ts}};
|
---|
| 68 | if ($nb == $attr->{$m}{nbfields}) {
|
---|
| 69 | $attr->{$m}{mintmst} = $ts if ($ts < $attr->{$m}{mintmst});
|
---|
| 70 | $attr->{$m}{maxtmst} = $ts if ($ts > $attr->{$m}{maxtmst});
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | #
|
---|
| 74 | # Avoids boundaries problems during update
|
---|
| 75 | #
|
---|
| 76 | $attr->{$m}{mintmst}--;
|
---|
| 77 | $attr->{$m}{maxtmst}++;
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | print Dumper($attr) if ($debug > 1);
|
---|
| 81 | };
|
---|
| 82 |
|
---|
| 83 | #
|
---|
| 84 | # Create the GAUGE entries in the rrd base
|
---|
| 85 | #
|
---|
| 86 | sub create_gauge {
|
---|
| 87 |
|
---|
| 88 | my ($t,$attr,$step,$rrd,$uid,$gid,$m,$debug) = @_;
|
---|
| 89 |
|
---|
| 90 | my @rrdc = ();
|
---|
| 91 | foreach my $k (@{$attr->{$m}{measures}}) {
|
---|
| 92 | my $step4 = $step*4;
|
---|
| 93 | push @rrdc,"DS:$k:GAUGE:$step4:U:U";
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | print "Creating rrd base $rrd for $m " if ($debug > 0);
|
---|
| 97 | print "rrdc:\n",Dumper(@rrdc) if ($debug > 2);
|
---|
| 98 | #my @rrdc2 = ("RRA:AVERAGE:0.5:1:4800","RRA:MAX:0.5:1:4800","RRA:MIN:0.5:1:4800");
|
---|
| 99 | my @rrdc2 = ("RRA:AVERAGE:0.5:1:4800","RRA:AVERAGE:0.5:6:4900","RRA:MAX:0.5:1:4800","RRA:MAX:0.5:6:4900","RRA:MIN:0.5:1:4800","RRA:MIN:0.5:6:700");
|
---|
| 100 | unlink $rrd;
|
---|
| 101 | RRDs::create ("$rrd","-b $attr->{$m}{mintmst}","-s $step",@rrdc,@rrdc2);
|
---|
| 102 | my $rrderror = RRDs::error;
|
---|
| 103 | die "Problem while updating rrd: $rrderror\n" if ($rrderror);
|
---|
| 104 | printf "(%d fields)\n",$attr->{$m}{nbfields} if ($debug > 0);
|
---|
| 105 | printf "[ ",$attr->{$m}{nbfields} if ($debug > 0);
|
---|
| 106 | foreach my $f (@{$attr->{$m}{measures}}) {
|
---|
| 107 | print "$f " if ($debug > 0);
|
---|
| 108 | }
|
---|
| 109 | printf " ]\n",$attr->{$m}{nbfields} if ($debug > 0);
|
---|
| 110 | chown $uid, $gid, "$rrd" || warn "Unable to chown $rrd to $uid" if ($UID == 0);
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | #
|
---|
| 114 | # Update the GAUGE entries in the rrd base
|
---|
| 115 | #
|
---|
| 116 | sub update_gauge {
|
---|
| 117 |
|
---|
| 118 | my ($t,$attr,$rrd,$m,$debug) = @_;
|
---|
| 119 |
|
---|
| 120 | print "Updating rrd base for $m\n" if ($debug > 0);
|
---|
| 121 | foreach my $ts (sort keys %{$t}) {
|
---|
| 122 | my $rrdu = "";
|
---|
| 123 | $rrdu .= "$ts";
|
---|
| 124 | foreach my $k (sort keys %{$t->{$ts}}) {
|
---|
| 125 | $rrdu .= ":$t->{$ts}{$k}";
|
---|
| 126 | }
|
---|
| 127 | # Depends on localisation
|
---|
| 128 | $rrdu =~ s/,/./g;
|
---|
| 129 | my $cnt = () = $rrdu =~ /:/g;
|
---|
| 130 | if ($cnt != $attr->{$m}{nbfields}) {
|
---|
| 131 | printf "Skiping update %s ... (%d fields)\n",substr($rrdu,0,15),$cnt if ($debug > 1);
|
---|
| 132 | } else {
|
---|
| 133 | printf "Updating with %s ... (%d fields)\n",substr($rrdu,0,15),$cnt if ($debug > 1);
|
---|
| 134 | printf "rrdu: $rrdu\n" if ($debug > 2);
|
---|
| 135 | RRDs::update("$rrd","$rrdu");
|
---|
| 136 | my $rrderror = RRDs::error;
|
---|
| 137 | die "Problem while updating rrd: $rrderror\n" if ($rrderror);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | #
|
---|
| 143 | # Print Date correctly from epoch sec
|
---|
| 144 | #
|
---|
| 145 | sub from_epoch {
|
---|
| 146 |
|
---|
| 147 | my ($t) = @_;
|
---|
| 148 |
|
---|
| 149 | return UnixDate(ParseDate("epoch $t"),"%Y-%m-%d %H:%M:%S");
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 | 1;
|
---|