source: MondoRescue/branches/stable/sar2rrd/sario2rrd.pm@ 1030

Last change on this file since 1030 was 1030, checked in by Bruno Cornec, 17 years ago

Creation of a pet project sar2rrd (put under subversion for ease of dev)
Not related to mondorescue

File size: 3.8 KB
Line 
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#
9package sario2rrd;
10use Exporter 'import';
11@EXPORT_OK = qw(create_attr create_gauge update_gauge from_epoch);
12
13use strict;
14use English; # for $UID
15use RRDs; # for data management and graphing
16use Data::Dumper; # for debug
17use 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#
23sub create_attr {
24
25my ($t,$attr,$m,$shot,$debug) = @_;
26
27#
28# Initialisation
29#
30if (not (defined $attr->{$m}{mintmst})) {
31 $attr->{$m}{mintmst} = 99999999999999;
32}
33if (not (defined $attr->{$m}{maxtmst})) {
34 $attr->{$m}{maxtmst} = 0;
35}
36foreach 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#
52push @{$attr->{$m}{measures}},(sort keys %{$t->{$attr->{$m}{goodtmst}}});
53
54foreach 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
80print Dumper($attr) if ($debug > 1);
81};
82
83#
84# Create the GAUGE entries in the rrd base
85#
86sub create_gauge {
87
88my ($t,$attr,$step,$rrd,$uid,$gid,$m,$debug) = @_;
89
90my @rrdc = ();
91foreach my $k (@{$attr->{$m}{measures}}) {
92 my $step4 = $step*4;
93 push @rrdc,"DS:$k:GAUGE:$step4:U:U";
94}
95
96print "Creating rrd base $rrd for $m " if ($debug > 0);
97print "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");
99my @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");
100unlink $rrd;
101RRDs::create ("$rrd","-b $attr->{$m}{mintmst}","-s $step",@rrdc,@rrdc2);
102my $rrderror = RRDs::error;
103 die "Problem while updating rrd: $rrderror\n" if ($rrderror);
104printf "(%d fields)\n",$attr->{$m}{nbfields} if ($debug > 0);
105printf "[ ",$attr->{$m}{nbfields} if ($debug > 0);
106foreach my $f (@{$attr->{$m}{measures}}) {
107 print "$f " if ($debug > 0);
108}
109printf " ]\n",$attr->{$m}{nbfields} if ($debug > 0);
110chown $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#
116sub update_gauge {
117
118my ($t,$attr,$rrd,$m,$debug) = @_;
119
120print "Updating rrd base for $m\n" if ($debug > 0);
121foreach 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#
145sub from_epoch {
146
147my ($t) = @_;
148
149return UnixDate(ParseDate("epoch $t"),"%Y-%m-%d %H:%M:%S");
150};
151
1521;
Note: See TracBrowser for help on using the repository browser.