]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/qos/RRD-func.pl
Hinzugefuegt:
[people/pmueller/ipfire-2.x.git] / config / qos / RRD-func.pl
1
2 ##########################################
3 ##
4 ## DESCRIPTION
5 ##
6 ## RRD function for tc-graph.
7 ## Which is part of the ADSL-optimizer.
8 ##
9 ## REQUIRES
10 ##
11 ##
12 ## AUTHOR
13 ## Jesper Dangaard Brouer <hawk@diku.dk>, d.15/4-2004
14 ##
15 ## CHANGELOG
16 ## 2004-04-15: Initial version.
17 ##
18 ## $Id: RRD-func.pl,v 1.10 2004/05/27 17:02:12 hawk Exp $
19 ##########################################
20
21 use RRDs;
22
23 if (not defined $STEP) {
24 my $STEP=10;
25 }
26
27 my $heartbeat=$STEP*2;
28
29 # Update script samples every 10 seconds.
30 # 24*60*60 = 86400 seconds (== one day)
31 # 8640 *10 = 86400 seconds (== one day)
32 # 8640 * 5days = 43200 seconds with 10 sec samples
33 #
34 my @rrd_data_sources =
35 ("-s", $STEP,
36 "DS:bytes:COUNTER:$heartbeat:0:U",
37 "DS:bits:COUNTER:$heartbeat:0:U",
38 "DS:pkts:COUNTER:$heartbeat:0:U",
39 "DS:dropped:COUNTER:$heartbeat:0:U",
40 "DS:overlimits:COUNTER:$heartbeat:0:U",
41 "DS:lended:COUNTER:$heartbeat:0:U",
42 "DS:borrowed:COUNTER:$heartbeat:0:U",
43 "DS:giants:COUNTER:$heartbeat:0:U",
44 "DS:backlog:GAUGE:$heartbeat:0:U",
45 "RRA:AVERAGE:0.5:1:43200",
46 "RRA:AVERAGE:0.5:7:8640",
47 "RRA:AVERAGE:0.5:31:8640",
48 "RRA:AVERAGE:0.5:372:8640",
49 "RRA:MAX:0.5:7:8640",
50 "RRA:MAX:0.5:31:8640",
51 "RRA:MAX:0.5:372:8640"
52 );
53
54
55 sub get_filename_rrd($) {
56 my $class_device = "$_[0]";
57 my $filename = "${rrd_datadir}class_${class_device}.rrd";
58 return $filename;
59 }
60
61 sub create_rrdfile($) {
62 my $class_device = "$_[0]";
63 my $filename = get_filename_rrd($class_device);
64 RRDs::create $filename, @rrd_data_sources;
65 my $ERROR = RRDs::error;
66 if ($ERROR) {
67 my $timestamp = time;
68 die "$timestamp: ERROR - Unable to create RRDfile \"$filename\": $ERROR\n";
69 }
70 }
71
72 sub format_class_data($) {
73 my $class = $_[0];
74 my ($rrd_template, $rrd_data);
75 my (@array_template, @array_data);
76 #print "Ref:". ref($class) ."\n";
77
78 # Select and correct undef values and key
79 while ( (my $key, my $value) = each %{$class}) {
80 # Skip timestamps
81 if ( ($key eq "last_update") ||
82 ($key eq "file_update") ||
83 ($key =~ /hfsc_/ )) {next}
84
85 push @array_template, $key;
86
87 if ( (not defined $value) ||
88 ("$value" eq "") ) {
89 $value = "U";
90 }
91 push @array_data, $value;
92 }
93
94 # Makes a RRD suitable input format
95 $rrd_template = join(":",@array_template);
96 $rrd_data = join(":",@array_data);
97
98 return ($rrd_template, $rrd_data);
99 }
100
101 sub update_rrds {
102
103 my $res=0;
104
105 my @test = keys %classes_data;
106 if ( $#test <= 0) {
107 print time, " [update_rrds] WARNING: classes_data empty!\n";
108 return "classes_data empty";
109 }
110
111 # Find the class_device (keys) in %classes_data
112 for my $class_device ( keys %classes_data ) {
113
114 if ("last_update" eq "$class_device") {next}
115
116 # Verify file exist (else create it)
117 my $filename = get_filename_rrd($class_device);
118 if ( ! -f $filename ) {
119 print "Creating RRDfile: $filename\n";
120 create_rrdfile($class_device);
121 }
122 #print "$class_device\n";
123
124 # Make a RRD suitable input format
125 my ($rrd_template, $rrd_data) = format_class_data($classes_data{$class_device});
126 #print "rrd_template: $rrd_template\n";
127 #print "rrd_data: $rrd_data\n";
128
129
130 # WHAT ABOUT:
131 # $classes_data{$device}{last_update} ????
132 my ($tmp, $device) = split /_/, $class_device;
133 #print "device: $device $classes_data{last_update}{$device} \n";
134 if ( (exists $classes_data{last_update}{$device}) ) {
135 if ((($classes_data{$class_device}{last_update} + $heartbeat) <
136 $classes_data{last_update}{$device})) {
137 print "WARNING: the class $class_device was";
138 print "not updated in lastrun + heartbeat...\n";
139 print "Assuming $class_device is removed,";
140 print " thus deleteing from hash table.";
141 # # ??? MAYBE DELETE THE OLD HASH ???
142 $res="Deleting class $class_device";
143 for my $key ( keys %{ $classes_data{$class_device} } ) {
144 delete( $classes_data{$class_device}{$key});
145 print " Deleting key: $key from: $class_device \n";
146 }
147 delete $classes_data{$class_device};
148 next;
149 }
150 }
151
152 # Verifies that it is new data,
153 # and not old data which already have been updated
154 # FIXME
155 # print "$0 FIXME update_rrds \n";
156 if ( exists $classes_data{$class_device}{file_update} ) {
157 if (($classes_data{$class_device}{file_update} >=
158 $classes_data{$class_device}{last_update})) {
159 print "Warning ($class_device):";
160 print " data already updated... old data or deleted class?\n";
161 $res="Old data or deleted class";
162 # ??? MAYBE DELETE THE OLD HASH ???
163 next;
164 }
165 }
166
167
168 # Update the RRD file
169 my $update_time = $classes_data{$class_device}{last_update};
170 # print "Updates: $filename time:$update_time\n";
171 # print " --template=$rrd_template\n";
172 # print " $update_time:$rrd_data\n";
173
174 # `rrdtool update $filename --template=$rrd_template $update_time:$rrd_data`;
175 RRDs::update ($filename, "--template=$rrd_template",
176 "N:$rrd_data");
177
178 my $ERROR = RRDs::error;
179 if ($ERROR) {
180 my $timestamp = time;
181 print "$timestamp: WARNING - ";
182 print "Unable to update RRDfile \"$filename\": $ERROR\n";
183 $res="Unable to update RRDfile \"$filename\"";
184 } else {
185 $classes_data{$class_device}{file_update} = time;
186 }
187 }
188 return $res;
189 }
190
191
192 return 1;
193