]>
Commit | Line | Data |
---|---|---|
a7fb5630 MT |
1 | #!/usr/bin/perl |
2 | ||
3 | ########################################## | |
4 | ## | |
5 | ## NAME | |
6 | ## | |
7 | ## DESCRIPTION | |
8 | ## | |
9 | ## Which is part of the ADSL-optimizer. | |
10 | ## | |
11 | ## USAGE / FUNCTIONS | |
12 | ## | |
13 | ## | |
14 | ## | |
15 | ## | |
16 | ## | |
17 | ## REQUIRES | |
18 | ## | |
19 | ## | |
20 | ## AUTHOR | |
21 | ## Jesper Dangaard Brouer <hawk@diku.dk>, d.21/4-2004 | |
22 | ## | |
23 | ## CHANGELOG | |
24 | ## 2004-04-21: Initial version. | |
25 | ## | |
a7fb5630 MT |
26 | ########################################## |
27 | ||
a7fb5630 MT |
28 | sub update_event_file($$$) { |
29 | my $filename = $_[0]; | |
30 | my $information = $_[1]; | |
31 | my $timestamp = $_[2]; | |
32 | ||
33 | if ("$information" ne "") { | |
34 | # Append to file | |
35 | open( OUTPUT, ">>$filename") | |
36 | or print "ERROR: Opening/updating event file $filename\n"; | |
37 | print OUTPUT "$timestamp $information\n"; | |
38 | close(OUTPUT); | |
39 | } | |
40 | } | |
41 | ||
42 | sub update_info_file($$$) { | |
43 | my $filename = $_[0]; | |
44 | my $information = $_[1]; | |
45 | my $timestamp = $_[2]; | |
46 | # Truncate file | |
47 | open( OUTPUT, ">$filename") | |
48 | or print "ERROR: Opening/updating info event file $filename\n"; | |
49 | print OUTPUT "$timestamp $information\n"; | |
50 | close(OUTPUT); | |
51 | ||
52 | } | |
53 | ||
54 | sub process_events { | |
55 | ||
56 | my @test = keys %classes_info; | |
57 | if ( $#test < 0) { | |
58 | print time, " [process_events] WARNING: classes_info empty!\n"; | |
59 | return "classes_info empty"; | |
60 | } | |
61 | ||
62 | my @bandwidth_items = ( "type", "prio", "rate", "ceil" ); | |
63 | ||
64 | my $event_reduced = ""; | |
65 | my $last_update; | |
66 | ||
67 | # Find the class_device (keys) in %classes_info | |
68 | for my $class_device ( sort keys %classes_info ) { | |
69 | ||
70 | if ("$class_device" eq "last_update") {next} | |
71 | ||
72 | my $event_class = ""; | |
73 | my $bandwidth_info = ""; | |
74 | ||
75 | # Tests if something has changed | |
76 | if ((not exists $classes_info{$class_device}{file_update}) || | |
77 | ($classes_info{$class_device}{last_update} > | |
78 | $classes_info{$class_device}{file_update})) { | |
79 | ||
80 | $last_update = $classes_info{$class_device}{last_update}; | |
81 | ||
82 | $event_class .= "($class_device)"; | |
83 | if ( "$event_reduced" eq "" ) {$event_reduced="Class changed:"} | |
84 | $event_reduced .= " ($class_device)"; | |
85 | # The list of changed keys | |
86 | while( $changed_key = | |
87 | shift @{ $classes_info{$class_device}{changed} }) | |
88 | { | |
89 | my $value = $classes_info{$class_device}{$changed_key}; | |
90 | $event_class .= " $changed_key=$value"; | |
91 | } | |
92 | ||
93 | # When something changed always update all the bandwidth info | |
94 | foreach my $item (@bandwidth_items) { | |
95 | if (exists $classes_info{$class_device}{$item}) { | |
96 | my $value = $classes_info{$class_device}{$item}; | |
97 | if (defined $value) { | |
98 | $bandwidth_info .= " $item:$value"; | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | print time . "($class_device) changes... ($last_update) \"$bandwidth_info\" \n"; | |
104 | ||
105 | $classes_info{$class_device}{file_update}=$last_update; | |
106 | ||
107 | my $event_file = get_filename_event($class_device); | |
108 | update_event_file($event_file , $event_class, $last_update); | |
109 | ||
110 | my $info_file = get_filename_bandwidth_info($class_device); | |
111 | update_info_file($info_file, $bandwidth_info, $last_update); | |
112 | } | |
113 | ||
114 | } | |
115 | # Only one line per process_events call | |
116 | # (notice $last_update is the latest timestamp assignment) | |
117 | if (defined $last_update) { | |
118 | update_event_file($event_file_all, $event_reduced, $last_update); | |
119 | } | |
120 | } | |
121 | ||
122 | ||
123 | 1; |