-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrrd2csv_new.pl
executable file
·304 lines (262 loc) · 6.52 KB
/
rrd2csv_new.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/perl -w
#
# RRD to CSV export tool
#
# Based on rrd2csv.pl by wrhoop r3 Oct 29, 2010
# Last modified by Peter Mc Aulay 2013-02-04
#
#use strict;
use POSIX qw(strftime);
use Getopt::Long;
use RRDs;
use lib '/usr/opt/rrdtool/';
use Data::Dumper;
# Pre-declare some variables
my ($csvname, $point_in_time, $offset, $names, $data, $error);
my ($val, $units);
my @results;
my @fetch;
#
# Programme version
#
my $revision = "3.0 PMA";
sub version() {
print "rrd2csv $revision\n";
exit 0;
}
#
# Parse options
#
my ($debug, $scale, $conversion, $start, $end, $now, $step, $sep, $help, $version, @rrdfiles, @headers) = '';
GetOptions (
"debug" => \$debug,
"a|autoscale" => \$scale,
"c|conversion" => \$conversion,
"l|label=s" => \@headers,
"s|start=s" => \$start,
"e|end=s" => \$end,
"i|step" => \$step,
"n|now" => \$now,
"separator=s" => \$sep,
"h|help" => \$help,
"f|file=s" => \@rrdfiles,
"v|version" => \$version,
);
# Command line help
sub usage {
print <<EOF;
Usage: rrd2csv.pl [args] -f file.rrd [-f file2.rrd...]
Where args is one or more of:
-a autoscale DS values (also --autoscale)
-c num convert DS values by "num" (also --conversion)
-e end report ending at time "end" (also --end)
-l label label DS value column with "label" (also --label)
-n only report values around now (--now)
-s start report starting at time "start" (also --start)
-i step specify step interval in seconds (also --step)
--separator specify column separator (default ",")
-h print this screen (also --help)
-v print programme version (also --version)
The -s and -e options support the traditional "seconds since the Unix
epoch" and the AT-STYLE time specification (see man rrdfetch)
For multiple input files, the output will have multiple columns,
one per input DS. You can specify the column headers with "-l".
EOF
exit 1;
}
#
# Input validation
#
!($version) || version();
!($help) || usage();
(@rrdfiles) || usage();
foreach (@rrdfiles) {
if (! -f "$_" ) {
print "rrd2csv: can't find file: $_\n";
$error = 1;
}
if ($error) {
usage();
exit 1;
}
}
if ($conversion && $conversion !~ /^\d+$|^\d+\.\d+$|^\.\d+$/) {
print "Bad conversion factor \"$conversion\"\n";
usage();
exit 1;
}
# rrdtool understands human-readable times
if ($now) {
$start = $end = 'now';
}
# Make sure both start and end are initialised
if ($start && !$end) {
$end = 'now';
}
if ($end && !$start) {
if ($end =~ /^\d+$/) {
$start = $end - 1;
} else {
# Time specified as text
$start = "${end}-1sec";
}
$point_in_time = 1;
}
if (!$start && !$end) {
$start = $end = 'now';
$point_in_time = 1;
}
# Default step is 5 minutes
if (!$step) {
$step = 300;
}
# Default separator is a comma
if (!$sep) {
$sep = ',';
}
#
# Fetch data from files
#
foreach my $file (@rrdfiles) {
my $qstep = $step;
my ($qstart);
undef @fetch;
#
# Get data around a single point in time
#
if ($point_in_time) {
print "DEBUG: point-in-time fetch\n" if $debug;
push @fetch, $file, '-s', $start, '-e', $end, 'AVERAGE';
if (! $qstep) {
# Fetch data from RRD file to determine step
# Also rounds $start to nearest data point
print "DEBUG: rrdtool fetch ", join(' ', @fetch), " (figuring out step)\n" if $debug;
($start, $qstep, $names, $data) = RRDs::fetch @fetch;
if ($error = RRDs::error) {
print "rrd2csv: rrdtool fetch failed: \"$error\"\n";
exit 1;
}
}
# Fetch data from (point_in_time - step) to point_in_time
# Go back two steps so we report values on either side of the point in time
$offset = $qstep * 2;
if ($end =~ /^\d+$/) {
$start = $start - $offset;
} else {
$start = "${end}-${offset}sec";
}
# Start again now we know the step, with adjusted start and end times
undef @fetch;
push @fetch, $file, '-s', $start, '-e', $end, '-r', $qstep, 'AVERAGE';
# Fetch data from RRD file
print "DEBUG: rrdtool fetch ", join(' ', @fetch), " (getting data)\n" if $debug;
($start, $qstep, $names, $data) = RRDs::fetch @fetch;
if ($error = RRDs::error) {
print "rrd2csv: rrdtool fetch failed: \"$error\"\n";
exit 1;
}
#
# Otherwise, retrieve the requested range
#
} else {
push @fetch, $file;
push @fetch, '-s', $start if $start;
push @fetch, '-e', $end if $end;
push @fetch, '-r', $qstep if $qstep;
push @fetch, 'AVERAGE';
print "DEBUG: rrdtool fetch ", join(' ', @fetch), "\n" if $debug;
($qstart, $qstep, $names, $data) = RRDs::fetch @fetch;
if ($error = RRDs::error) {
print "rrd2csv: rrdtool fetch failed: \"$error\"\n";
exit 1;
}
# Incidentally converts "AT-style" timestamps to Unix epoch
$start = $qstart;
}
#print Dumper([$data]) if $debug;
# Create a new column
push(@results, $data);
}
#
# Print output
#
#print Dumper([\@results]) if $debug;
# Print DS names, or header labels in the order passed on the command line
$names = \@headers if @headers;
unshift @$names, "Timestamp";
print join($sep, @$names);
print "\n";
# Transpose the results matrix (swap columns and rows)
my @transposed = ();
for my $row (@results) {
for my $column (0 .. $#{$row}) {
push(@{$transposed[$column]}, $row->[$column]);
}
}
@results = @transposed;
# Print results
for my $row (@results) {
# Print timestamp
print strftime("%m/%d/%Y %H:%M:%S", localtime($start));
$start += $step;
# Print rows and columns of data
for my $col (@$row) {
if ($col) {
for my $val (@$col) {
if ($val) {
$val = $val * $conversion if $conversion;
$val = $val * autoscale($val) if $scale;
printf "$sep%4.2f", $val;
} else {
print $sep, "0.00";
}
}
} else {
print $sep, "0.00";
}
}
print "\n";
}
# Done!
exit 0;
#
# Subroutines
#
# Function to auto-scale units
sub autoscale {
my $value = @_;
my ($floor, $magnitude, $index, $symbol, $new_value);
# SI prefixes for the scaling factors (atto to exa)
my %scale_symbols = qw( -18 a -15 f -12 p -9 n -6 u -3 m 3 k 6 M 9 G 12 T 15 P 18 E );
# Strip null and invalid values from output
if ($value =~ /^\s*[0]+\s*$/ ||
$value =~ /^\s*[0]+.[0]+\s*$/ ||
$value =~ /^\s*NaN\s*$/) {
return $value, ' ';
}
# Round and scale where appropriate
$floor = floor($value);
$magnitude = int($floor/3);
$index = $magnitude * 3;
$symbol = $scale_symbols{$index};
$new_value = $value / (10 ** $index);
return $new_value, " $symbol";
}
# As the C floor(3) function
sub floor {
my $value = $_;
my $i = 0;
if ($value > 1.0) {
# scale downward...
while ($value > 10.0) {
$i++;
$value /= 10.0;
}
} else {
while ($value < 10.0) {
$i--;
$value *= 10.0;
}
}
return $i;
}