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