]>
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 | ||
a7fb5630 MT |
96 | # Find the class_device (keys) in %classes_data |
97 | for my $class_device ( keys %classes_data ) { | |
98 | ||
99 | if ("last_update" eq "$class_device") {next} | |
100 | ||
101 | # Verify file exist (else create it) | |
102 | my $filename = get_filename_rrd($class_device); | |
103 | if ( ! -f $filename ) { | |
104 | print "Creating RRDfile: $filename\n"; | |
105 | create_rrdfile($class_device); | |
106 | } | |
107 | #print "$class_device\n"; | |
108 | ||
109 | # Make a RRD suitable input format | |
110 | my ($rrd_template, $rrd_data) = format_class_data($classes_data{$class_device}); | |
111 | #print "rrd_template: $rrd_template\n"; | |
112 | #print "rrd_data: $rrd_data\n"; | |
113 | ||
114 | ||
115 | # WHAT ABOUT: | |
116 | # $classes_data{$device}{last_update} ???? | |
117 | my ($tmp, $device) = split /_/, $class_device; | |
118 | #print "device: $device $classes_data{last_update}{$device} \n"; | |
119 | if ( (exists $classes_data{last_update}{$device}) ) { | |
120 | if ((($classes_data{$class_device}{last_update} + $heartbeat) < | |
121 | $classes_data{last_update}{$device})) { | |
122 | print "WARNING: the class $class_device was"; | |
123 | print "not updated in lastrun + heartbeat...\n"; | |
124 | print "Assuming $class_device is removed,"; | |
125 | print " thus deleteing from hash table."; | |
126 | # # ??? MAYBE DELETE THE OLD HASH ??? | |
127 | $res="Deleting class $class_device"; | |
128 | for my $key ( keys %{ $classes_data{$class_device} } ) { | |
129 | delete( $classes_data{$class_device}{$key}); | |
130 | print " Deleting key: $key from: $class_device \n"; | |
131 | } | |
132 | delete $classes_data{$class_device}; | |
133 | next; | |
134 | } | |
135 | } | |
136 | ||
137 | # Verifies that it is new data, | |
138 | # and not old data which already have been updated | |
139 | # FIXME | |
140 | # print "$0 FIXME update_rrds \n"; | |
141 | if ( exists $classes_data{$class_device}{file_update} ) { | |
142 | if (($classes_data{$class_device}{file_update} >= | |
143 | $classes_data{$class_device}{last_update})) { | |
144 | print "Warning ($class_device):"; | |
145 | print " data already updated... old data or deleted class?\n"; | |
146 | $res="Old data or deleted class"; | |
147 | # ??? MAYBE DELETE THE OLD HASH ??? | |
148 | next; | |
149 | } | |
150 | } | |
151 | ||
152 | ||
153 | # Update the RRD file | |
154 | my $update_time = $classes_data{$class_device}{last_update}; | |
155 | # print "Updates: $filename time:$update_time\n"; | |
156 | # print " --template=$rrd_template\n"; | |
157 | # print " $update_time:$rrd_data\n"; | |
158 | ||
159 | # `rrdtool update $filename --template=$rrd_template $update_time:$rrd_data`; | |
160 | RRDs::update ($filename, "--template=$rrd_template", | |
161 | "N:$rrd_data"); | |
162 | ||
163 | my $ERROR = RRDs::error; | |
164 | if ($ERROR) { | |
165 | my $timestamp = time; | |
166 | print "$timestamp: WARNING - "; | |
167 | print "Unable to update RRDfile \"$filename\": $ERROR\n"; | |
168 | $res="Unable to update RRDfile \"$filename\""; | |
169 | } else { | |
170 | $classes_data{$class_device}{file_update} = time; | |
171 | } | |
172 | } | |
173 | return $res; | |
174 | } | |
175 | ||
176 | ||
177 | return 1; | |
178 |