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