3 ##########################################
9 ## Which is part of the ADSL-optimizer.
21 ## Jesper Dangaard Brouer <hawk@diku.dk>, d.15/4-2004
24 ## 2004-04-15: Initial version.
25 ## 2005-04-18: Remove some warnings.
27 ##########################################
33 #our $tc_command="/sbin/tc";
35 my @input_htb = (<<"END_OF_HERE_HTB" =~ m/^\s*(.+)/gm);
36 class tbf 4220:1 parent 4220:
37 class htb 1:1 root rate 400Kbit ceil 400Kbit burst 2111b cburst 2111b
38 Sent 12369084336 bytes 80967118 pkts (dropped 0, overlimits 0)
40 lended: 23353805 borrowed: 0 giants: 0
41 tokens: 30210 ctokens: 30210
43 class htb 1:10 parent 1:1 prio 0 rate 80Kbit ceil 320Kbit burst 1701b cburst 2008b
44 Sent 80640087 bytes 247988 pkts (dropped 0, overlimits 0)
46 lended: 230876 borrowed: 17112 giants: 0
47 tokens: 127200 ctokens: 37940
49 class htb 1:20 parent 1:1 leaf 4220: prio 1 rate 100Kbit ceil 200Kbit burst 1727b cburst 1855b
50 Sent 2495181573 bytes 44034303 pkts (dropped 5837, overlimits 0)
51 lended: 43825585 borrowed: 208718 giants: 0
52 tokens: 103424 ctokens: 55808
54 class htb 1:30 parent 1:1 leaf 4230: prio 3 rate 80Kbit ceil 400Kbit burst 1701b cburst 2111b
55 Sent 2060213567 bytes 5465574 pkts (dropped 121, overlimits 0)
57 lended: 4556992 borrowed: 908582 giants: 0
58 tokens: -25364 ctokens: 32897
60 class htb 1:50 parent 1:1 leaf 4250: prio 5 rate 40Kbit ceil 120Kbit burst 1650b cburst 1752b
61 Sent 6071486687 bytes 24448436 pkts (dropped 8086739, overlimits 0)
62 rate 15801bps 85pps backlog 126p
63 lended: 8324530 borrowed: 16123780 giants: 0
64 tokens: -202717 ctokens: -172499
66 class htb 1:666 parent 1:1 leaf 666: prio 7 rate 4Kbit ceil 40Kbit burst 1604b cburst 1650b
67 Sent 2148626078 bytes 6771069 pkts (dropped 2078536, overlimits 0)
68 rate 5221bps 17pps backlog 125p
69 lended: 675330 borrowed: 6095613 giants: 0
70 tokens: -1149121 ctokens: -293386
75 my @input_hfsc = (<<"END_OF_HERE_HFSC
" =~ m/^\s*(.+)/gm);
77 Sent 0 bytes 0 pkts (dropped 0, overlimits 0)
80 class hfsc 1:1 parent 1: ls m1 0bps d 0us m2 250Kbit ul m1 0bps d 0us m2 250Kbit
81 Sent 0 bytes 0 pkts (dropped 0, overlimits 0)
82 period 6 work 131770097 bytes level 1
84 class hfsc 1:10 parent 1:1 rt m1 250Kbit d 30.0ms m2 50Kbit ls m1 250Kbit d 50.0ms m2 50Kbit
85 Sent 1300885 bytes 7052 pkts (dropped 0, overlimits 0)
86 period 6502 work 1300885 bytes rtwork 1245495 bytes level 0
88 class hfsc 1:20 parent 1: rt m1 0bps d 64.0ms m2 75Kbit ls m1 0bps d 0us m2 250Kbit
89 Sent 19144279 bytes 325503 pkts (dropped 46, overlimits 0)
91 period 20242 work 19143778 bytes level 0
93 class hfsc 1:30 parent 1:1 leaf 4230: ls m1 0bps d 150.0ms m2 50Kbit
94 Sent 45139930 bytes 74200 pkts (dropped 1664, overlimits 0)
96 period 140 work 44885232 bytes level 0
98 class hfsc 1:50 parent 1:1 leaf 4250: ls m1 0bps d 235.7ms m2 72Kbit
99 Sent 73910198 bytes 301294 pkts (dropped 104807, overlimits 0)
101 period 115 work 64625490 bytes level 0
103 class hfsc 1:666 parent 1:1 leaf 666: ls m1 0bps d 1.0s m2 2Kbit
104 Sent 2217104 bytes 17018 pkts (dropped 74526, overlimits 0)
106 period 1 work 1814712 bytes level 0
111 my $device = "$_[0]";
114 my $timestamp = time;
115 my @tc_output = `$tc_command -statistics class show dev $device`;
116 # my @tc_output = @input_hfsc;
117 # my @tc_output = @input_htb;
119 if ( $result != 0 ) {
120 print "Error executing
$tc_command\n";
124 $classes_data{last_update}{$device} = $timestamp;
125 $classes_info{last_update}{$device} = $timestamp;
127 #for my $line (@tc_output) {
128 for my $i (0 .. $#tc_output) {
130 my $line=$tc_output[$i];
133 if ( $line =~ m/class htb (\d+):(\d+)( root| parent )?(\d+:\d+)?( leaf )?(\d+)?:?( prio )?(\d+)? rate (.*) ceil (.*) burst (.*) cburst (.*)/ ) {
137 my $class = "${major
}-${minor
}";
138 #my $hash = "${class}_
${device
}";
147 #print "class: $class\n"."parent
: $parent\n"."leaf
: $leaf\n"."prio
: $prio\n";
148 #print "rate
: $rate\n"."ceil
: $ceil\n"."burst
: $burst\n"."cburst
: $cburst\n";
150 my ($bytes, $pkts, $dropped, $overlimits);
151 if ($tc_output[$i + 1] =~ m/Sent (\d+) bytes (\d+) pkt \(dropped (\d+), overlimits (\d+) requeues (\d+)\)/ ) {
157 #print "bytes
: $bytes\n"."pkts
: $pkts\n";
158 #print "dropped
: $dropped\n"."overlimits
: $overlimits\n"."requeues
: $requeues\n";
160 print "$timestamp: ERROR
(+1) - Unable to parse
(class ${class}_
$device): ";
161 print "\"$tc_output[$i + 1]\"\n";
167 # Sometimes the "rate
" line is not shown (when a rate cannot be calculated)
168 # And sometimes only "backlog
"...
169 # Use $next_index to specify the next line to parse
173 if ($tc_output[$i + 2] =~ m/((rate (\d+\w+) )|backlog )(\d+)?(pps )?(backlog )?(\d+)?p?/ ) {
175 #print "backlog
: $backlog\n";
178 # print "$timestamp: WARNING
\"rate
\" line missing
";
179 # print " very inactive
class ${class}_
$device).\n";
183 my ($lended, $borrowed, $giants);
184 if ($tc_output[$i + $next_index] =~ m/lended: (\d+) borrowed: (\d+) giants: (\d+)/ ) {
188 #print "lended
: $lended\n"."borrowed
: $borrowed\n"."giants
: $giants\n";
190 print "$timestamp: ERROR
(+$next_index) - Unable to parse
(class ${class}_
$device): ";
191 print "\"$tc_output[$i + $next_index]\"\n";
196 # Update the hash tables
197 my $hash="${class}_
$device";
199 # Tests if previous data have been updated to file
200 if ( (exists $classes_data{$hash}{last_update}) &&
201 (exists $classes_data{$hash}{file_update})) {
202 if ( $classes_data{$hash}{last_update} >
203 $classes_data{$hash}{file_update} ){
204 print "Warning
: old data from
$hash has
not been updated to file
!\n";
208 # Update the statistics data
209 # (need a function call for error checking)
210 $classes_data{$hash}{last_update} = $timestamp;
211 update_counter( $hash, $timestamp, "bytes
" , $bytes);
212 #(yes I know its bad/redundant, but it makes in easier elsewhere)
218 if ( $line =~ m/class hfsc (\d+):(\d+)( root| parent )?(\d+:\d?)?( leaf )?(\d+)?:?( rt m1 (\d+\w+?) d (\d+.?\d?\w+) m2 (\d+\w+?))?( ls m1 (\d+\w+?) d (\d+.?\d?\w+) m2 (\d+\w+?))?( ul m1 (\d+\w+?) d (\d+.?\d?\w+) m2 (\d+\w+?))? / ){
223 my $class = "${major
}-${minor
}";
224 #my $hash = "${class}_
${device
}";
228 my $realtime_m1; if (defined $8 && $8 ne '0bps') {$realtime_m1 = $8;}
229 my $realtime_d; if (defined $9 && $9 ne '0us' ) {$realtime_d = $9;}
230 my $realtime_m2 = $10;
232 my $linkshare_m1; if (defined $12 && $12 ne '0bps') { $linkshare_m1 = $12;}
233 my $linkshare_d ; if (defined $13 && $13 ne '0us' ) { $linkshare_d = $13;}
234 my $linkshare_m2 = $14;
236 my $upperlimit_m1; if (defined $16 && $16 ne '0bps') { $upperlimit_m1 = $16;}
237 my $upperlimit_d ; if (defined $17 && $17 ne '0us' ) { $upperlimit_d = $17;}
238 my $upperlimit_m2 = $18;
240 #print "\nType
: $type\n";
242 if ($tc_output[$i + 1] =~ m/Sent (\d+) bytes (\d+) pkts \(dropped (\d+), overlimits (\d+)\)/ ) {
244 #print "bytes
: $bytes\n"."pkts
: $pkts\n";
246 print "$timestamp: ERROR
(+1) - Unable to parse
(class ${class}_
$device): ";
247 print "\"$tc_output[$i + 1]\"\n";
252 # Sometimes the "backlog
" line is not shown (when there is no backlog...)
253 # Use $next_index to specify the next line to parse
257 if ($tc_output[$i + 2] =~ m/backlog (\d+)?p?/ ) {
259 #print "backlog
: $backlog\n";
264 my ($period, $work, $rtwork, $level);
265 if ($tc_output[$i + $next_index] =~ m/period (\d+) (work (\d+) bytes )?(rtwork (\d+) bytes )?level (\d+)/ ) {
271 print "$timestamp: ERROR
(+$next_index) - Unable to parse
(class ${class}_
$device): ";
272 print "\"$tc_output[$i + $next_index]\"\n";
278 # Update the hash tables
279 my $hash="${class}_
$device";
281 # Tests if previous data have been updated to file
282 if ( (exists $classes_data{$hash}{last_update}) &&
283 (exists $classes_data{$hash}{file_update})) {
284 if ( $classes_data{$hash}{last_update} >
285 $classes_data{$hash}{file_update} ){
286 print "Warning
: old data from
$hash has
not been updated to file
!\n";
290 # HFSC - Update the statistics data
291 # (need a function call for error checking)
292 $classes_data{$hash}{last_update} = $timestamp;
293 update_counter( $hash, $timestamp, "bytes
" , $bytes);
299 if ( $line =~ m/class XXX/ ) {
300 print "Matching
class XXX
\n";
307 # The main purpose of this function is to detect counter resets
308 # and avoid parsing them on to RRDtool which interprets them
309 # as counter overflows, thus updating with a very large number.
310 sub update_counter ($$$$) {
311 my $class_hash = "$_[0]";
312 my $timestamp = "$_[1]";
313 my $data_key = "$_[2]";
315 if ( defined $_[3]) {
316 $new_value = "$_[3]";
319 my $max_allowed_wrap_increase = 100000000;
321 if (exists $classes_data{$class_hash}{$data_key}) {
322 $old_value = $classes_data{$class_hash}{$data_key};
323 #print "old_value
: $old_value\n";
326 # # If the new and old value is not defined, nothing is done
327 # if ((not defined $new_value) && (not defined $old_value)) {
331 # Argh... the tc program outputs in unsigned long long (64 bit).
332 # but perls integers should be 32 bit, but some how perl
333 # manages to store numbers larger than 32 bit numbers.
334 my $MAX_VALUE=0xFFFFFFFF;
336 if ((defined $new_value) && (defined $old_value)) {
337 my $delta = $new_value - $old_value;
339 # Counter wrap around...
340 my $real_delta = $delta + $MAX_VALUE + 1;
341 if ($real_delta < 0) {
342 print "($class_hash:$data_key): Perl
-Magic using numbers bigger than
32bit
";
343 print "new
:$new_value - old
:$old_value = delta
:$delta, real_delta
:$real_delta.\n";
345 print time . " ($class_hash:$data_key) Info
: Counter wrap around
(real delta
:$real_delta)\n";
346 if ( ($real_delta > $max_allowed_wrap_increase) ||
348 # Properly a counter reset and not a wrap around
349 # A counter reset normally a result of a reload of the classes
350 $classes_data{$class_hash}{$data_key} = undef;
351 $classes_info{$class_hash}{counter_reset} = $timestamp;
352 $classes_info{$class_hash}{last_update} = $timestamp;
353 print time . "Warning
: Real_delta too big
, assuming Counter
reset";
354 print "($class_hash:$data_key)\n";
355 return "Counter
reset";
360 $classes_data{$class_hash}{$data_key} = $new_value;
364 sub update_info ($$$$) {
365 my $class_hash = "$_[0]";
366 my $timestamp = "$_[1]";
367 my $info_key = "$_[2]";
369 if ( defined $_[3]) {
370 $new_value = "$_[3]";
373 if (exists $classes_info{$class_hash}{$info_key}) {
374 $old_value = $classes_info{$class_hash}{$info_key};
375 #print "old_value
: $old_value\n";
378 # If the new and old value is not defined, nothing is done
379 if ((not defined $new_value) && (not defined $old_value)) {
383 # An update is needed
384 # - if the old_value is not defined and new_value is defined
385 # - if the new_value is not defined and old_value is defined
386 # - if the old_value differs from the new,
388 if ( ((not defined $old_value) and (defined $new_value)) ||
389 ((not defined $new_value) and (defined $old_value)) ||
390 ("$old_value" ne "$new_value")) {
392 # Special case: If the "type
" changes the hash should be cleared
393 if ( "$info_key" eq "type
") {
394 #print "Type has changed clearing hash
\n";
395 for my $key ( keys %{ $classes_info{$class_hash} } ) {
396 delete( $classes_info{$class_hash}{$key});
397 print " Deleting key
: $key from
: $class_hash \n";
401 if (defined $new_value) {
402 $classes_info{$class_hash}{$info_key} = $new_value;
404 #print "New value
undef -> Deleting key
: $info_key from
: $class_hash\n";
405 delete($classes_info{$class_hash}{$info_key});
408 # Mark the class for an info-file update
409 $classes_info{$class_hash}{last_update} = $timestamp;
411 # Update list/array of "changed
" keys
412 push @{ $classes_info{$class_hash}{changed} }, $info_key;
415 #print "Update
class:$class_hash $info_key=";
416 #if (defined $new_value) {print "$new_value"};
426 #print Dumper(%classes_data);
427 #print Dumper(%classes_info);