blib/lib/RRD/Editor.pm

filter: Statementstmt 86.8% 1133 / 1304 Branchbran 70.3% 412 / 586 Conditioncond 45.6% 79 / 173 MC/DCmcdc 18.8% 20 / 106 Subroutinesub 96.6% 87 / 90 Podpod 100% 36 / 36 totaltotal 76.9% 1767 / 2295 CC 351 SCAR 38.8
CC
351
Cov
79%
update
187.2
_unpackd
45.6
_packd
34.6
CRAP 48.5
↑ Summary
1 ############################################################
2 #
3 # RRD::Editor - Portable, pure perl tool to create and edit RRD files.
4 #
5 ############################################################
6  
7 package RRD::Editor;
8  
9 7 ▶use 5.8.8; # nan doesn't seem to be supported properly by perl before this
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 require Exporter;
14 7 ▶use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Carp qw(croak carp cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use Getopt::Long qw(GetOptionsFromString :config pass_through);
17 7 ▶use Getopt::Long qw(:config pass_through);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 $VERSION = '0.21';
24  
25 7 @ISA = qw(Exporter);
26 7 @EXPORT = qw();
27 7 @EXPORT_OK = qw(
28 open close create update info dump fetch
29 last set_last lastupdate minstep
30 DS_names DS_heartbeat set_DS_heartbeat DS_min set_DS_min DS_max set_DS_max DS_type set_DS_type rename_DS add_DS delete_DS
31 num_RRAs RRA_numrows RRA_type RRA_step RRA_xff set_RRA_xff RRA_el set_RRA_el add_RRA delete_RRA resize_RRA
32 );
33 7 %EXPORT_TAGS = (all => \@EXPORT_OK);
34  
35  
36 # define sizes of long ints and floats for the various file encodings
37 7 ▶use constant NATIVE_LONG_EL_SIZE => $Config{longsize};
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use constant NATIVE_DOUBLE_EL_SIZE => $Config{doublesize};
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use constant PORTABLE_LONG_EL_SIZE => 4; # 32 bits
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use constant PORTABLE_SINGLE_EL_SIZE => 4; # IEEE 754 single is 32 bits
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use constant PORTABLE_DOUBLE_EL_SIZE => 8; # IEEE 754 double is 64 bits
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44 # try to figure out the endian-ness of this machine; assumes IEEE 754 doubles which should be ok on all modern machines
45 7 ▶sub _endian {
Subroutine _endian: called CC=4 SCAR=21.7
46 7 my $endiantest=unpack("h*", pack("d","1000.1234"));
47 7 ▶if ($endiantest eq "c92a329bcf04f804") {
Branch: 3 branches
BranchTrueFalseTotal
if ($endiantest eq 'c92a329bcf04f804') { } 7 0 7
elsif ($endiantest eq '04f804cf9b322ac9') { } 0 0 0
elsif ($endiantest eq 'cf04f804c92a329b') { } 0 0 0
48 7 return "little";
49 } elsif ($endiantest eq "04f804cf9b322ac9") {
50 0 return "big"; # big endian
51 } elsif ($endiantest eq "cf04f804c92a329b") {
52 0 return "mixed"; # mixed endian (used by some ARM processors)
53 } else {
54 0 return "unknown";
55 }
56 }
57 7 ▶use constant ENDIAN => _endian();
Subroutine BEGIN: called CC=1 SCAR=0.0
58
59 # sort out the float cookie used by RRD files
60 7 ▶sub _cookie {
Subroutine _cookie: called CC=1 SCAR=0.1
61 # Getting RRD file float cookie right is a little tricky because Perl rounds 8.642135E130 up to 8.6421500000000028e+130 on
62 # Intel 32 bit machines, and rounds to something else on 64 bit machines, and neither of these give the same bit sequence as
63 # C when Perl stores 8.642135E130. Sigh ...
64
65 # See if we can make a call to C to get the float cookie. Reliable, but needs Inline module to be available.
66 7 eval {
67 7 load Inline C => <<'END';
68 double _cookie_C() {
69 return 8.642135E130;
70 }
71 END
72 0 return pack("d",_cookie_C());
73 };
74 # Inline not available.
75 # Try approach that avoids need for Inline module. Ok so long as machine uses IEEE doubles (i.e. seems like all modern machines)
76 # and little-endian, big-endian and mixed-endian byte order (all modern machines that I know of, museum pieces excepted):
77 7 if (ENDIAN eq "little") {
78 7 return chr(47). chr(37). chr(192). chr(199). chr(67). chr(43). chr(31). chr(91); # little endian
79 } elsif (ENDIAN eq "big") {
80 return chr(91). chr(31). chr(43). chr(67). chr(199). chr(192). chr(37). chr(47); # big endian
81 } elsif (ENDIAN eq "mixed") {
82 return chr(67). chr(43). chr(31). chr(91). chr(47). chr(37). chr(192). chr(199); # mixed endian (used by some ARM processors)
83 } else {
84 warn("Warning: To work with native (non-portable) RRD files, you need to install the perl Inline C module (e.g. by typing 'cpan -i Inline')\n");
85 return chr(67). chr(43). chr(31). chr(91). chr(47). chr(37). chr(192). chr(199);
86 }
87 }
88 7 ▶use constant DOUBLE_FLOATCOOKIE => 8.642135E130;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 7 ▶use constant NATIVE_BINARY_FLOATCOOKIE => _cookie();
Subroutine BEGIN: called CC=1 SCAR=0.0
90 7 ▶use constant PORTABLE_BINARY_FLOATCOOKIE => chr(47). chr(37). chr(192). chr(199). chr(67). chr(43). chr(31). chr(91); # portable format is always little-endian
Subroutine BEGIN: called CC=1 SCAR=0.0
91 7 ▶use constant SINGLE_FLOATCOOKIE => 8.6421343830016e+13; # cookie to use when storing floats in single precision as +130 exponent on old cookie is too large
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 0 ▶sub _native_double {
Subroutine _native_double: not called CC=3 SCAR=24.8
94 # try to figure out the long/double alignment needed for the RRDTOOL file format
95 0 ▶ if ($Config{myarchname} =~ m/(sun|sparc|mips|irix|ppc|powerpc|arm)/i && NATIVE_LONG_EL_SIZE==4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RRD::Editor::Config{'myarchname'} =~ /(sun|sparc|mips|irix|ppc|powerpc|arm)/i and !1) { } 0 0 0
Condition0%0/2
$RRD::Editor::Config{"myarchname"} =~ /(sun|sparc|mips|irix|ppc|powerpc|arm)/i and
!ll
00
MC/DC0%0/1
$RRD::Editor::Config{"myarchname"} =~ /(sun|sparc|mips|irix|ppc|powerpc|arm)/i and
$RRD::Editor::Config{"myarchname"} =~ /(sun|sparc|mips|irix|ppc|powerpc|arm)/i
Truth table0%0/2
A
A= $RRD::Editor::Config{"myarchname"} =~ /(sun|sparc|mips|irix|ppc|powerpc|arm)/i
Aresult
00
11
96 # Only affects behaviour when writing new files from scratch, otherwise can figure out the right alignment to use when
97 # reading an existing file
98 # Align longs on 32 bit boundaries and doubles on 64 bit boundaries.
99 0 return "native-double-mixed";
100 } else {
101 # Intel/AMD processors, DEC alpha processors - should probably swap these checks around since most machines are double-mixed ?
102 # Otherwise, align longs/doubles on 32 bit machines on 32 bit boundaries, and 64 bit machines on 64 bit boundaries.
103 0 return "native-double-simple";
104 }
105 }
106  
107 # check whether perl pack function supports little-endian usage:
108 7 eval {
109 7 my $test=pack("d<",\(DOUBLE_FLOATCOOKIE));
110 };
111 7 ▶our $PACK_LITTLE_ENDIAN_SUPPORT = (length($@)>0 ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
length $@ > 0 ? : 0 7 7
112  
113 # Define NaN, Inf, -Inf. Not as easy as it sounds - usually "nan", "inf", "-inf" works, but not always e.g. on older versions of perl, on SH4 etc
114 2028 ▶sub _isNan {
Subroutine _isNan: called CC=2 SCAR=7.2
115 2028 ▶ return $_[0] eq "nan" || $_[0] != $_[0]; # NaN is the only quantity that does not equal itself
Condition66%2/3
$_[0] eq 'nan' || $_[0] != $_[0]
l!l&&r!l&&!r
05701458
MC/DC50%1/2
$_[0] eq 'nan' || $_[0] != $_[0]
$_[0] eq 'nan' $_[0] != $_[0]
Truth table66%2/3
A || B
A= $_[0] eq 'nan'B= $_[0] != $_[0]
ABresult
1X1
011
000
116 }
117 1138 ▶sub _isInf {
Subroutine _isInf: called CC=2 SCAR=6.9
118 1138 ▶ return $_[0] > 0 && ($_[0]*10 == $_[0]); # Inf remains equal to itself after arithmetic
Condition100%3/3
$_[0] > 0 && $_[0] * 10 == $_[0]
!ll&&!rl&&r
63046345
MC/DC100%2/2
$_[0] > 0 && $_[0] * 10 == $_[0]
$_[0] > 0 $_[0] * 10 == $_[0]
Truth table100%3/3
A && B
A= $_[0] > 0B= $_[0] * 10 == $_[0]
ABresult
0X0
100
111
119 }
120 7 ▶sub _NaN {
Subroutine _NaN: called CC=3 SCAR=15.4
121 # try using the IEEE 754 NaN bit pattern
122 7 my $nan;
123 7 if (ENDIAN eq "little") {
124 7 $nan= unpack("d", scalar reverse pack "H*", "7FF8000000000000");# little endian
125 7 ▶ if (_isNan($nan)) { return $nan;}
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($nan)) 7 0 7
126 } elsif (ENDIAN eq "big") {
127 $nan= unpack("d", scalar pack "H*", "7FF8000000000000"); # big endian
128 if (_isNan($nan)) { return $nan;}
129 } elsif (ENDIAN eq "mixed") {
130 $nan= unpack("d", scalar pack "H*", "000000007FF80000"); # mixed endian (used by some ARM processors)
131 if (_isNan($nan)) { return $nan;}
132 }
133 # last ditch attempt. try perl "nan" string. Doesn't work on all OS's since its relies on the interpretation made by a native C library call
134 0 $nan = 0+"nan";
135 0 ▶ if (_isNan($nan)) {return $nan;}
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($nan)) 0 0 0
136 0 warn("Warning: Looks like you have no NaN support. Might have problems reading rrd files.");
137 0 return 0;
138 }
139 7 ▶sub _Inf {
Subroutine _Inf: called CC=3 SCAR=15.4
140 # try using the IEEE 754 Inf bit pattern
141 7 my $inf;
142 7 if (ENDIAN eq "little") {
143 7 $inf= unpack("d", scalar reverse pack "H*", "7FF0000000000000");# little endian
144 7 ▶ if (_isInf($inf)) {return $inf;}
Branch: 1 branch
BranchTrueFalseTotal
if (_isInf($inf)) 7 0 7
145 } elsif (ENDIAN eq "big") {
146 $inf= unpack("d", scalar pack "H*", "7FF0000000000000"); # big endian
147 if (_isInf($inf)) {return $inf;}
148 } elsif (ENDIAN eq "mixed") {
149 $inf= unpack("d", scalar pack "H*", "000000007FF00000"); # mixed endian (used by some ARM processors)
150 if (_isInf($inf)) {return $inf;}
151 }
152 # didn't work.
153 0 $inf = 0+"inf";
154 0 ▶ if (_isInf($inf)) {return $inf;}
Branch: 1 branch
BranchTrueFalseTotal
if (_isInf($inf)) 0 0 0
155 0 warn("Warning: Looks like you have no Inf support. Might have problems reading rrd files.");
156 0 return 1;
157 }
158 805 ▶sub _strfloat {
Subroutine _strfloat: called CC=6 SCAR=18.1
159 # convert a float to a string in a standard way i.e. removing cross-platform variation in strings displayed for nan and inf
160 805 ▶ if (_isNan($_[0])) {
Branch: 3 branches
BranchTrueFalseTotal
if (_isNan($_[0])) { } 232 573 805
elsif (_isInf($_[0])) { } 19 554 573
elsif (_isInf(-$_[0])) { } 19 535 554
161 232 return "nan";
162 } elsif (_isInf($_[0])) {
163 19 return "inf" ;
164 } elsif (_isInf(-$_[0])) {
165 19 return "-inf" ;
166 } else {
167 535 my $digits=10;
168 535 ▶ if ($_[1]) {$digits=$_[1];}
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 535 0 535
169 535 my $str=sprintf "%0.".$digits."e",$_[0];
170 0 ▶ if ($str =~ m/^([+|-]?\d*[.]?\d*e[+|-]?)0(\d\d)$/) {$str=$1.$2; } # for windows - convert 3 digit exponent to 2 digits
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([+|-]?\d*[.]?\d*e[+|-]?)0(\d\d)$/) 0 535 535
171 535 return $str;
172 }
173 }
174 40 ▶sub _strint {
Subroutine _strint: called CC=4 SCAR=15.6
175 # convert an integer to a string in a standard way i.e. removing cross-platform variation in strings displayed for nan and inf
176 40 ▶ if (_isNan($_[0])) {
Branch: 3 branches
BranchTrueFalseTotal
if (_isNan($_[0])) { } 38 2 40
elsif (_isInf($_[0])) { } 0 2 2
elsif (_isInf(-$_[0])) { } 0 2 2
177 38 return "nan";
178 } elsif (_isInf($_[0])) {
179 0 return "inf" ;
180 } elsif (_isInf(-$_[0])) {
181 0 return "-inf" ;
182 } else {
183 2 return sprintf "%d",$_[0];
184 }
185 }
186  
187 7 ▶use constant NAN => _NaN();
Subroutine BEGIN: called CC=1 SCAR=0.0
188 7 ▶use constant INF => _Inf();
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 # define index into elements in CDP_PREP array
191 7 ▶use constant VAL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 7 ▶use constant UNKN_PDP_CNT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 7 ▶use constant HW_INTERCEPT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 7 ▶use constant HW_LAST_INTERCEPT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
195 7 ▶use constant HW_SLOPE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 7 ▶use constant HW_LAST_SLOPE => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 7 ▶use constant NULL_COUNT => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
198 7 ▶use constant LAST_NULL_COUNT=> 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 7 ▶use constant PRIMARY_VAL => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
200 7 ▶use constant SECONDARY_VAL => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
201  
202 ###### private functions
203  
204 # older versions of Getopt::Long (e.g. used on Mac OS X) don't have this function, so lets add it explicitly
205 17 ▶sub _GetOptionsFromString(@) {
Subroutine _GetOptionsFromString: called CC=1 SCAR=0.0
206 17 my ($string) = shift;
207 17 require Text::ParseWords;
208 17 my @temp=@ARGV;
209 17 @ARGV = Text::ParseWords::shellwords($string);
210 17 my $ret = GetOptions(@_);
211 17 my @args=@ARGV;
212 17 @ARGV=@temp;
213 17 return ( $ret, \@args );
214 }
215  
216 ### used to extract information from raw RRD file and build corresponding structured arrays
217 31 ▶sub _get_header_size {
Subroutine _get_header_size: called CC=1 SCAR=0.0
218 # size of file header, in bytes
219 31 my $self = $_[0]; my $rrd=$self->{rrd};
220
221 return $self->{DS_DEF_IDX} +
222 $self->{DS_EL_SIZE} * $rrd->{ds_cnt} +
223 $self->{RRA_DEF_EL_SIZE} * $rrd->{rra_cnt} +
224 $self->{LIVE_HEAD_SIZE} +
225 $self->{PDP_PREP_EL_SIZE} * $rrd->{ds_cnt} +
226 $self->{CDP_PREP_EL_SIZE} * $rrd->{ds_cnt} * $rrd->{rra_cnt} +
227 $self->{RRA_PTR_EL_SIZE} * $rrd->{rra_cnt}
228 31 +$self->{HEADER_PAD};
229 }
230  
231 ####
232 344 ▶sub _packd {
Subroutine _packd: called CC=33 SCAR=35.4
233 # pack an array of doubles into a binary string, format determined by $self->{encoding}
234 # - will do packing manually if necessary, to guarantee portability
235 #my ($self,$list_ptr,$encoding) = @_;
236 344 my $encoding=$_[0]->{encoding};
237 344 ▶ if (defined($_[2])) {$encoding=$_[2];}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 2 342 344
238  
239 344 ▶ if ($encoding eq "native-double-simple" || $encoding eq "native-double-mixed") {
Branch: 4 branches
BranchTrueFalseTotal
if ($encoding eq 'native-double-simple' or $encoding eq 'native-double-mixed') { } 0 344 344
elsif ($encoding eq 'native-single') { } 6 338 344
elsif ($RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq 'litteendian-single') { } 0 338 338
elsif ($RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq 'littleendian-double') { } 136 202 338
Condition33%1/3
$encoding eq "native-double-simple" or $encoding eq "native-double-mixed"
l!l&&r!l&&!r
00344
MC/DC0%0/2
$encoding eq "native-double-simple" or $encoding eq "native-double-mixed"
$encoding eq "native-double-simple" $encoding eq "native-double-mixed"
Truth table33%1/3
A or B
A= $encoding eq "native-double-simple"B= $encoding eq "native-double-mixed"
ABresult
1X1
011
000
240 # backwards-compatible (with RRDTOOL) RRD format
241 0 return pack("d*", @{$_[1]});
242 } elsif ($encoding eq "native-single") {
243 # save some work - we can pack a portable-single using native float
244 6 return pack("f*", @{$_[1]});
245 ▶ } elsif ($PACK_LITTLE_ENDIAN_SUPPORT && $encoding eq "litteendian-single") {
Condition33%1/3
$RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "litteendian-single"
!ll&&!rl&&r
03380
MC/DC0%0/2
$RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "litteendian-single"
$RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT $encoding eq "litteendian-single"
Truth table33%1/3
A and B
A= $RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORTB= $encoding eq "litteendian-single"
ABresult
0X0
100
111
246 # save some work - we can pack a portable-single using native float
247 0 return pack("f<*", @{$_[1]});
248 ▶ } elsif ($PACK_LITTLE_ENDIAN_SUPPORT && $encoding eq "littleendian-double") {
Condition66%2/3
$RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "littleendian-double"
!ll&&!rl&&r
0202136
MC/DC50%1/2
$RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "littleendian-double"
$RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORT $encoding eq "littleendian-double"
Truth table66%2/3
A and B
A= $RRD::Editor::PACK_LITTLE_ENDIAN_SUPPORTB= $encoding eq "littleendian-double"
ABresult
0X0
100
111
249 # shortcut - only difference from portable format is that native format is big-endian
250 136 return pack("d<*", @{$_[1]});
251 }
252 202 my $f; my $sign; my $shift; my $exp; my $mant; my $string=''; my $significand; my $significandlo; my $significandhi;
253 202 ▶ if ($encoding eq "portable-single" || $encoding eq "ieee-32") {
Branch: 2 branches
BranchTrueFalseTotal
if ($encoding eq 'portable-single' or $encoding eq 'ieee-32') { } 75 127 202
elsif ($encoding eq 'portable-double' or $encoding eq 'ieee-64') { } 127 0 127
Condition66%2/3
$encoding eq "portable-single" or $encoding eq "ieee-32"
l!l&&r!l&&!r
750127
MC/DC50%1/2
$encoding eq "portable-single" or $encoding eq "ieee-32"
$encoding eq "portable-single" $encoding eq "ieee-32"
Truth table66%2/3
A or B
A= $encoding eq "portable-single"B= $encoding eq "ieee-32"
ABresult
1X1
011
000
254 # manually pack an IEEE 754 32bit single precision number in little-endian order
255 75 ▶ for (my $i=0; $i<@{$_[1]}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[1];}) 159 75 234
256 159 $f=@{$_[1]}[$i];
257 159 ▶ if (_isNan($f)) {
Branch: 4 branches
BranchTrueFalseTotal
if (_isNan($f)) { } 16 143 159
elsif ($f == -9**9**9) { } 4 139 143
elsif ($f == 9**9**9) { } 4 135 139
elsif ($f == 0) { } 92 43 135
258 16 $sign=0; $exp=255; $significand=1;
259 } elsif ($f == -1 * INF) {
260 4 $sign=1; $exp=255; $significand=0;
261 } elsif ($f == INF) {
262 4 $sign=0; $exp=255; $significand=0;
263 } elsif ($f == 0) {
264 92 $sign=0; $exp=0; $significand=0;
265 } else {
266 43 ▶ $sign = ($f<0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 ? : 1 42 43
267 43 ▶ $f = ($f<0) ? -$f : $f;
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 ? : 1 42 43
268 # get the normalized form of f and track the exponent
269 43 $shift = 0;
270 43 ▶ while($f >= 2) { $f /= 2; $shift++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f >= 2) 284 43 327
271 43 ▶ while($f < 1 && $f>0) { $f *= 2; $shift--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f < 1 and $f > 0) 71 43 114
Condition66%2/3
$f < 1 and $f > 0
!ll&&!rl&&r
43071
MC/DC50%1/2
$f < 1 and $f > 0
$f < 1 $f > 0
Truth table66%2/3
A and B
A= $f < 1B= $f > 0
ABresult
0X0
100
111
272 43 $f -= 1;
273 # calculate the binary form (non-float) of the significand data
274 43 $significand = int($f*(2**23));
275 # get the biased exponent
276 43 $exp = int($shift + ((1<<7) - 1)); # shift + bias
277 }
278 159 $string.=pack("V",($sign<<31) | ($exp<<23) | $significand);
279 }
280 75 return $string;
281 ▶ } elsif ($encoding eq "portable-double" || $encoding eq "ieee-64") {
Condition33%1/3
$encoding eq "portable-double" or $encoding eq "ieee-64"
l!l&&r!l&&!r
12700
MC/DC0%0/2
$encoding eq "portable-double" or $encoding eq "ieee-64"
$encoding eq "portable-double" $encoding eq "ieee-64"
Truth table33%1/3
A or B
A= $encoding eq "portable-double"B= $encoding eq "ieee-64"
ABresult
1X1
011
000
282 # manuallly pack IEEE 754 64 bit double precision in little-endian order
283 127 ▶ for (my $i=0; $i<@{$_[1]}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[1];}) 373 127 500
284 373 $f=@{$_[1]}[$i];
285 373 ▶ if (_isNan($f)) {
Branch: 4 branches
BranchTrueFalseTotal
if (_isNan($f)) { } 136 237 373
elsif ($f == -9**9**9) { } 4 233 237
elsif ($f == 9**9**9) { } 4 229 233
elsif ($f == 0) { } 93 136 229
286 136 $sign=0; $exp=2047; $significandhi=1;$significandlo=1;
287 } elsif ($f == -1 * INF) {
288 4 $sign=1; $exp=2047; $significandhi=0;$significandlo=0;
289 } elsif ($f == INF) {
290 4 $sign=0; $exp=2047; $significandhi=0;$significandlo=0;
291 } elsif ($f ==0) {
292 93 $sign=0; $exp=0; $significandhi=0;$significandlo=0;
293 } else {
294 136 ▶ $sign = ($f<0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 ? : 11 125 136
295 136 ▶ $f = ($f<0) ? -$f : $f;
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 ? : 11 125 136
296 # get the normalized form of f and track the exponent
297 136 $shift = 0;
298 136 ▶ while($f >= 2) { $f /= 2; $shift++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f >= 2) 607 136 743
299 136 ▶ while($f < 1 && $f>0 ) { $f *= 2; $shift--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($f < 1 and $f > 0) 210 136 346
Condition66%2/3
$f < 1 and $f > 0
!ll&&!rl&&r
1360210
MC/DC50%1/2
$f < 1 and $f > 0
$f < 1 $f > 0
Truth table66%2/3
A and B
A= $f < 1B= $f > 0
ABresult
0X0
100
111
300 136 $f -= 1;
301 # calculate the binary form (non-float) of the significand data
302 136 $significandhi = int($f*(2**20));
303 136 $significandlo = int( ($f-$significandhi/(2**20))*(2**52));
304 # get the biased exponent
305 136 $exp = int($shift + ((1<<10) - 1)); # shift + bias
306 }
307 373 $string.=pack("V V",$significandlo, ($sign<<31) | ($exp<<20) | $significandhi);
308 }
309 127 return $string;
310 } else {
311 0 croak("packd:unknown encoding: ".$encoding."\n");
312 }
313 }
314  
315 #####
316  
317 587 ▶sub _unpackd {
Subroutine _unpackd: called CC=30 SCAR=38.2
318 # unpack binary string into array of doubles, format determined by $self->{encoding}
319 # - will do unpacking manually if necessary, to guarantee portability
320 #my ($self, $string, $encoding) = @_;
321 587 my $encoding=$_[0]->{encoding};
322 587 ▶ if (defined($_[2])) {$encoding=$_[2];}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 30 557 587
323
324 587 ▶ if ($encoding eq "native-double-simple" || $encoding eq "native-double-mixed") {
Branch: 4 branches
BranchTrueFalseTotal
if ($encoding eq 'native-double-simple' or $encoding eq 'native-double-mixed') { } 0 587 587
elsif ($encoding eq 'native-single') { } 19 568 587
elsif ($PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq 'littleendian-single') { } 6 562 568
elsif ($PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq 'littleendian-double') { } 525 37 562
Condition33%1/3
$encoding eq "native-double-simple" or $encoding eq "native-double-mixed"
l!l&&r!l&&!r
00587
MC/DC0%0/2
$encoding eq "native-double-simple" or $encoding eq "native-double-mixed"
$encoding eq "native-double-simple" $encoding eq "native-double-mixed"
Truth table33%1/3
A or B
A= $encoding eq "native-double-simple"B= $encoding eq "native-double-mixed"
ABresult
1X1
011
000
325 # backwards-compatible (with RRDTOOL) RRD format
326 0 return unpack("d*", $_[1]);
327 } elsif ($encoding eq "native-single" ) {
328 # save some work - we can unpack portable-single using native float
329 19 return unpack("f*", $_[1]);
330 ▶ } elsif ($PACK_LITTLE_ENDIAN_SUPPORT && $encoding eq "littleendian-single" ) {
Condition66%2/3
$PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "littleendian-single"
!ll&&!rl&&r
05626
MC/DC50%1/2
$PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "littleendian-single"
$PACK_LITTLE_ENDIAN_SUPPORT $encoding eq "littleendian-single"
Truth table66%2/3
A and B
A= $PACK_LITTLE_ENDIAN_SUPPORTB= $encoding eq "littleendian-single"
ABresult
0X0
100
111
331 # save some work - we can unpack portable-single using native float
332 6 return unpack("f<*", $_[1]);
333 ▶ } elsif ($PACK_LITTLE_ENDIAN_SUPPORT && $encoding eq "littleendian-double") {
Condition66%2/3
$PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "littleendian-double"
!ll&&!rl&&r
037525
MC/DC50%1/2
$PACK_LITTLE_ENDIAN_SUPPORT and $encoding eq "littleendian-double"
$PACK_LITTLE_ENDIAN_SUPPORT $encoding eq "littleendian-double"
Truth table66%2/3
A and B
A= $PACK_LITTLE_ENDIAN_SUPPORTB= $encoding eq "littleendian-double"
ABresult
0X0
100
111
334 # shortcut - only difference from portable format is that native format is big-endian
335 525 return unpack("d<*", $_[1]);
336 }
337 37 my $word; my $sign; my $expo; my $mant; my $manthi; my $mantlo; my @list; my $num; my $i;
338 37 ▶ if ($encoding eq "portable-single" || $encoding eq "ieee-32") {
Branch: 2 branches
BranchTrueFalseTotal
if ($encoding eq 'portable-single' or $encoding eq 'ieee-32') { } 6 31 37
elsif ($encoding eq 'portable-double' or $encoding eq 'ieee-64') { } 31 0 31
Condition66%2/3
$encoding eq "portable-single" or $encoding eq "ieee-32"
l!l&&r!l&&!r
6031
MC/DC50%1/2
$encoding eq "portable-single" or $encoding eq "ieee-32"
$encoding eq "portable-single" $encoding eq "ieee-32"
Truth table66%2/3
A or B
A= $encoding eq "portable-single"B= $encoding eq "ieee-32"
ABresult
1X1
011
000
339 # manually unpack a little-endian IEEE 754 32bit single-precision number
340 6 ▶ for ($i=0; $i<length($_[1]); $i=$i+4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $_[1]) 6 6 12
341 6 $word = (unpack("C",substr($_[1],$i+3,1)) << 24) + (unpack("C",substr($_[1],$i+2,1)) << 16) + (unpack("C",substr($_[1],$i+1,1)) << 8) + unpack("C",substr($_[1],$i,1));
342 6 $expo = (($word & 0x7F800000) >> 23) - 127;
343 6 $mant = (($word & 0x007FFFFF) | 0x00800000);
344 6 ▶ $sign = ($word & 0x80000000) ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$word & 2147483648 ? : 5 1 6
345 6 ▶ if ($expo == 128 && $mant == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($expo == 128 and $mant == 0) { } 0 6 6
elsif ($expo == 128) { } 0 6 6
elsif ($expo == -127 and $mant == 0) { } 0 6 6
Condition33%1/3
$expo == 128 and $mant == 0
!ll&&!rl&&r
600
MC/DC0%0/2
$expo == 128 and $mant == 0
$expo == 128 $mant == 0
Truth table33%1/3
A and B
A= $expo == 128B= $mant == 0
ABresult
0X0
100
111
346 0 ▶ $num=$sign>0 ? INF : -1 * INF;
Branch: 1 branch
BranchTrueFalseTotal
$sign > 0 ? : 0 0 0
347 } elsif ($expo == 128) {
348 0 $num=NAN;
349 ▶ } elsif ($expo == -127 && $mant ==0) {
Condition33%1/3
$expo == -127 and $mant == 0
!ll&&!rl&&r
600
MC/DC0%0/2
$expo == -127 and $mant == 0
$expo == -127 $mant == 0
Truth table33%1/3
A and B
A= $expo == -127B= $mant == 0
ABresult
0X0
100
111
350 0 $num=0;
351 } else {
352 6 $num = $sign * (2**($expo-23))*$mant;
353 }
354 6 push (@list, $num);
355 }
356 6 return @list;
357 ▶ } elsif ($encoding eq "portable-double" || $encoding eq "ieee-64") {
Condition33%1/3
$encoding eq "portable-double" or $encoding eq "ieee-64"
l!l&&r!l&&!r
3100
MC/DC0%0/2
$encoding eq "portable-double" or $encoding eq "ieee-64"
$encoding eq "portable-double" $encoding eq "ieee-64"
Truth table33%1/3
A or B
A= $encoding eq "portable-double"B= $encoding eq "ieee-64"
ABresult
1X1
011
000
358 # manually unpack IEEE 754 64 bit double-precision number.
359 31 ▶ for ($i=0; $i<length($_[1]); $i=$i+8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $_[1]) 106 31 137
360 106 $word = (unpack("C",substr($_[1],$i+7,1)) << 24) + (unpack("C",substr($_[1],$i+6,1)) << 16) + (unpack("C",substr($_[1],$i+5,1)) << 8) + unpack("C",substr($_[1],$i+4,1));
361 106 $mantlo = (unpack("C",substr($_[1],$i+3,1)) << 24) + (unpack("C",substr($_[1],$i+2,1)) << 16) + (unpack("C",substr($_[1],$i+1,1)) << 8) + unpack("C",substr($_[1],$i,1));
362 106 $expo = (($word & 0x7FF00000) >> 20) - 1023;
363 106 $manthi = ($word & 0x000FFFFF) ;
364 106 ▶ $sign = ($word & 0x80000000) ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$word & 2147483648 ? : 9 97 106
365 106 ▶ if ($expo == 1024 && $mantlo == 0 && $manthi==0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($expo == 1024 and $mantlo == 0 and $manthi == 0) { } 0 106 106
elsif ($expo == 1024) { } 16 90 106
elsif ($expo == -1023 and $manthi == 0 and $mantlo == 0) { } 1 89 90
Condition50%3/6
$expo == 1024 and $mantlo == 0 and $manthi == 0
!ll&&!rl&&r
10600
$expo == 1024 and $mantlo == 0
!ll&&!rl&&r
90160
MC/DC0%0/3
$expo == 1024 and $mantlo == 0 and $manthi == 0
$expo == 1024 $mantlo == 0 $manthi == 0
Truth table50%2/4
A and B and C
A= $expo == 1024B= $mantlo == 0C= $manthi == 0
ABCresult
0XX0
10X0
1100
1111
366 0 $num=$sign * INF;
367 } elsif ($expo == 1024) {
368 16 $num=NAN;
369 ▶ } elsif ($expo==-1023 && $manthi==0 && $mantlo==0) {
Condition83%5/6
$expo == -1023 and $manthi == 0 and $mantlo == 0
!ll&&!rl&&r
8901
$expo == -1023 and $manthi == 0
!ll&&!rl&&r
8811
MC/DC66%2/3
$expo == -1023 and $manthi == 0 and $mantlo == 0
$expo == -1023 $manthi == 0 $mantlo == 0
Truth table75%3/4
A and B and C
A= $expo == -1023B= $manthi == 0C= $mantlo == 0
ABCresult
0XX0
10X0
1100
1111
370 1 $num=0;
371 } else {
372 89 $num = $sign * ( (2**$expo) + (2**($expo-20))*$manthi + (2**($expo-52))*$mantlo );
373 }
374 106 push (@list, $num);
375 }
376 31 return @list;
377 } else {
378 0 croak("unpackd:unknown encoding: ".$encoding."\n");
379 }
380 }
381  
382 #####
383 39 ▶sub _packlongchar {
Subroutine _packlongchar: called CC=3 SCAR=13.3
384 # pack encoding specification for integers. no need for manual packing/unpacking of integers as agreed portable formats already available
385 39 my $self=$_[0];
386 39 ▶ if ($self->{encoding} eq "native-double-simple" || $self->{encoding} eq "native-double-mixed") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'encoding'} eq 'native-double-simple' or $self->{'encoding'} eq 'native-double-mixed') { } 0 39 39
Condition33%1/3
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
l!l&&r!l&&!r
0039
MC/DC0%0/2
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
$self->{"encoding"} eq "native-double-simple" $self->{"encoding"} eq "native-double-mixed"
Truth table33%1/3
A or B
A= $self->{"encoding"} eq "native-double-simple"B= $self->{"encoding"} eq "native-double-mixed"
ABresult
1X1
011
000
387 # backwards-compatible (with RRDTOOL) RRD format
388 0 return "L!"; # native long int
389 } else {
390 # portable format, little-endian 32bit long int
391 39 return "V";
392 }
393 }
394  
395 ####
396 10 ▶sub _sizes {
Subroutine _sizes: called CC=10 SCAR=24.9
397 # define the sizes of the various elements in RRD binary file
398 10 my ($self)=@_;
399
400 10 $self->{OFFSET} = 12; # byte position of start of float cookie.
401 10 $self->{RRA_DEL_PAD} = 0; # for byte alignment in RRA_DEF after char(20) string
402 10 $self->{STAT_PAD} = 0; # for byte alignment at end of static header.
403 10 $self->{RRA_PAD} = 0; # for byte alignment at end of RRAD_DEF float array
404 10 ▶ if ($self->{encoding} eq "native-double-simple" || $self->{encoding} eq "native-double-mixed") {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'encoding'} eq 'native-double-simple' or $self->{'encoding'} eq 'native-double-mixed') { } 0 10 10
elsif ($self->{'encoding'} eq 'littleendian-single' or $self->{'encoding'} eq 'native-single' or $self->{'encoding'} eq 'portable-single' or $self->{'encoding'} eq 'ieee-32') { } 2 8 10
elsif ($self->{'encoding'} eq 'littleendian-double' or $self->{'encoding'} eq 'portable-double' or $self->{'encoding'} eq 'ieee-64') { } 8 0 8
Condition33%1/3
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
l!l&&r!l&&!r
0010
MC/DC0%0/2
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
$self->{"encoding"} eq "native-double-simple" $self->{"encoding"} eq "native-double-mixed"
Truth table33%1/3
A or B
A= $self->{"encoding"} eq "native-double-simple"B= $self->{"encoding"} eq "native-double-mixed"
ABresult
1X1
011
000
405 0 $self->{LONG_EL_SIZE} = NATIVE_LONG_EL_SIZE;
406 0 $self->{FLOAT_EL_SIZE}= NATIVE_DOUBLE_EL_SIZE;
407 0 $self->{COOKIE} = NATIVE_BINARY_FLOATCOOKIE;
408 0 if ( NATIVE_LONG_EL_SIZE == 8) {
409 # long ints and doubles are both 64 bits, alignment is at 64 bit boundaries for both native-double-simple and native-double-mixed
410 0 $self->{OFFSET} = 16; # 64 bit alignment of the float cookie
411 0 $self->{RRA_DEL_PAD} = 4; # 64 bit alignment for first long in RRA_DEF after char(20) string
412 } elsif ( NATIVE_LONG_EL_SIZE == 4 && $self->{encoding} eq "native-double-mixed") {
413 # 32 bit native-double-mixed: align long ints at 32 bit boundaries and doubles at 64 bit boundaries.
414 $self->{OFFSET} = 16; # 64 bit alignment of the float cookie
415 $self->{RRA_DEL_PAD} = 0; # 32 bit alignment first long in RRA_DEF after char(20) string
416 $self->{STAT_PAD} = 4; # 64 bit alignment for start of DS defs
417 $self->{RRA_PAD} = 4; # 64 bit alignment for first double in RRA_DEF
418 } else {
419 # default is 32 bit native-double-simple: align both long ints and doubles at 32 bit boundaries.
420 }
421 ▶ } elsif ($self->{encoding} eq "littleendian-single" || $self->{encoding} eq "native-single" || $self->{encoding} eq "portable-single" || $self->{encoding} eq "ieee-32") {
Condition77%7/9
$self->{"encoding"} eq "littleendian-single" or $self->{"encoding"} eq "native-single" or $self->{"encoding"} eq "portable-single" or $self->{"encoding"} eq "ieee-32"
l!l&&r!l&&!r
208
$self->{"encoding"} eq "littleendian-single" or $self->{"encoding"} eq "native-single" or $self->{"encoding"} eq "portable-single"
l!l&&r!l&&!r
118
$self->{"encoding"} eq "littleendian-single" or $self->{"encoding"} eq "native-single"
l!l&&r!l&&!r
019
MC/DC50%2/4
$self->{"encoding"} eq "littleendian-single" or $self->{"encoding"} eq "native-single" or $self->{"encoding"} eq "portable-single" or $self->{"encoding"} eq "ieee-32"
$self->{"encoding"} eq "littleendian-single" $self->{"encoding"} eq "native-single" $self->{"encoding"} eq "portable-single" $self->{"encoding"} eq "ieee-32"
Truth table60%3/5
A or B or C or D
A= $self->{"encoding"} eq "littleendian-single"B= $self->{"encoding"} eq "native-single"C= $self->{"encoding"} eq "portable-single"D= $self->{"encoding"} eq "ieee-32"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
422 2 $self->{LONG_EL_SIZE} = PORTABLE_LONG_EL_SIZE;
423 2 $self->{FLOAT_EL_SIZE}= PORTABLE_SINGLE_EL_SIZE; # 32 bits
424 2 my @cookie=(SINGLE_FLOATCOOKIE);
425 2 $self->{COOKIE} = $self->_packd(\@cookie,"portable-single");
426 ▶ } elsif ($self->{encoding} eq "littleendian-double" || $self->{encoding} eq "portable-double" || $self->{encoding} eq "ieee-64") {
Condition50%3/6
$self->{"encoding"} eq "littleendian-double" or $self->{"encoding"} eq "portable-double" or $self->{"encoding"} eq "ieee-64"
l!l&&r!l&&!r
800
$self->{"encoding"} eq "littleendian-double" or $self->{"encoding"} eq "portable-double"
l!l&&r!l&&!r
620
MC/DC0%0/3
$self->{"encoding"} eq "littleendian-double" or $self->{"encoding"} eq "portable-double" or $self->{"encoding"} eq "ieee-64"
$self->{"encoding"} eq "littleendian-double" $self->{"encoding"} eq "portable-double" $self->{"encoding"} eq "ieee-64"
Truth table50%2/4
A or B or C
A= $self->{"encoding"} eq "littleendian-double"B= $self->{"encoding"} eq "portable-double"C= $self->{"encoding"} eq "ieee-64"
ABCresult
1XX1
01X1
0011
0000
427 8 $self->{LONG_EL_SIZE} = PORTABLE_LONG_EL_SIZE;
428 8 $self->{FLOAT_EL_SIZE}= PORTABLE_DOUBLE_EL_SIZE; # 64 bits
429 8 $self->{COOKIE} = PORTABLE_BINARY_FLOATCOOKIE;
430 }
431 10 $self->{DIFF_SIZE} = $self->{FLOAT_EL_SIZE} - $self->{LONG_EL_SIZE};
432 10 $self->{STAT_HEADER_SIZE} = $self->{OFFSET} + $self->{FLOAT_EL_SIZE} + 3 * $self->{LONG_EL_SIZE};
433 10 $self->{STAT_HEADER_SIZE0} = $self->{STAT_HEADER_SIZE} + 10 * $self->{FLOAT_EL_SIZE} + $self->{STAT_PAD};
434 10 $self->{RRA_PTR_EL_SIZE} = $self->{LONG_EL_SIZE};
435 10 $self->{CDP_PREP_EL_SIZE} = 10 * $self->{FLOAT_EL_SIZE};
436 10 $self->{PDP_PREP_PAD} = 2; # for byte alignment of char(30) string in PDP_PREP
437 10 $self->{PDP_PREP_EL_SIZE} = 30 + $self->{PDP_PREP_PAD} + 10 * $self->{FLOAT_EL_SIZE};
438 10 $self->{RRA_DEF_EL_SIZE} = 20 + $self->{RRA_DEL_PAD} + 2 * $self->{LONG_EL_SIZE} + 10 * $self->{FLOAT_EL_SIZE} +$self->{RRA_PAD};
439 10 $self->{DS_DEF_IDX} = $self->{STAT_HEADER_SIZE0};
440 10 $self->{DS_EL_SIZE} = 40 + 10 * $self->{FLOAT_EL_SIZE} ;
441 10 $self->{LIVE_HEAD_SIZE} = 2 * $self->{LONG_EL_SIZE};
442 10 $self->{HEADER_PAD} = 0; # accounting for pad bytes at end of header (e.g. 8 pad bytes are added on Linux/Intel 64 bit platforms)
443 }
444  
445 ####
446 6 ▶sub _extractDSdefs {
Subroutine _extractDSdefs: called CC=2 SCAR=6.9
447 # extract DS definitions from raw header (which must have been already read using rrd_open)
448 6 my ($self, $header, $idx) = @_; my $rrd=$self->{rrd};
449
450 6 my $i;
451 6 my $L=$self->_packlongchar();
452 6 @{$rrd->{ds}}=[];
453 6 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 24 6 30
454 24 my $ds={};
455 #($ds->{name}, $ds->{type}, $ds->{hb}, $ds->{min}, $ds->{max})= unpack("Z20 Z20 $L x".DIFF_SIZE." d d",substr(${$header},$idx,DS_EL_SIZE));
456 24 ($ds->{name}, $ds->{type}, $ds->{hb})= unpack("Z20 Z20 $L",substr(${$header},$idx,40+$self->{LONG_EL_SIZE}));
457 24 ($ds->{min}, $ds->{max})= $self->_unpackd(substr(${$header},$idx+40+$self->{FLOAT_EL_SIZE},2*$self->{FLOAT_EL_SIZE}));
458 24 $rrd->{ds}[$i] = $ds;
459 24 $idx+=$self->{DS_EL_SIZE};
460 #print $ds->{name}," ",$ds->{type}," ",$ds->{hb}," ",$ds->{min}," ",$ds->{max},"\n";
461 }
462 }
463  
464 ###
465 6 ▶sub _extractRRAdefs {
Subroutine _extractRRAdefs: called CC=2 SCAR=6.9
466 # extract RRA definitions from raw header (which must have been already read using rrd_open)
467 6 my ($self, $header, $idx) = @_; my $rrd=$self->{rrd};
468  
469 6 my $i;
470 6 my $L=$self->_packlongchar();
471 6 @{$rrd->{rra}}=[];
472 6 ▶ for ($i=0; $i<$rrd->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra_cnt"}) 30 6 36
473 30 my $rra={};
474 30 ($rra->{name}, $rra->{row_cnt}, $rra->{pdp_cnt})= unpack("Z".(20+$self->{RRA_DEL_PAD})." $L $L",substr(${$header},$idx,20+$self->{RRA_DEL_PAD}+2*$self->{LONG_EL_SIZE}));
475 30 ($rra->{xff})= $self->_unpackd(substr(${$header},$idx+20+$self->{RRA_DEL_PAD} + 2*$self->{LONG_EL_SIZE}+$self->{RRA_PAD}, $self->{FLOAT_EL_SIZE}));
476 30 $rrd->{rra}[$i] = $rra;
477 30 $idx+=$self->{RRA_DEF_EL_SIZE};
478 #print $rra->{name}," ",$rra->{row_cnt}," ",$rra->{pdp_cnt}," ",$rra->{xff},"\n";
479 }
480 }
481  
482 ####
483 6 ▶sub _extractPDPprep {
Subroutine _extractPDPprep: called CC=2 SCAR=6.9
484 # extract PDP prep from raw header (which must have been already read using rrd_open)
485 6 my ($self, $header, $idx) = @_; my $rrd=$self->{rrd};
486  
487 6 my $i;
488 6 my $L=$self->_packlongchar();
489 6 @{$rrd->{pdp_prep}}=[];
490 6 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 24 6 30
491 24 my $pdp={};
492 24 ($pdp->{last_ds}, $pdp->{unkn_sec_cnt})= unpack("Z".(30+$self->{PDP_PREP_PAD})." $L",substr(${$header},$idx,30+$self->{PDP_PREP_PAD}+$self->{LONG_EL_SIZE})); # NB Z32 instead of Z30 due to byte alignment
493 24 ($pdp->{val})= $self->_unpackd(substr(${$header},$idx+30+$self->{PDP_PREP_PAD}+$self->{FLOAT_EL_SIZE},$self->{FLOAT_EL_SIZE}));
494 24 $rrd->{ds}[$i]->{pdp_prep} = $pdp;
495 24 $idx+=$self->{PDP_PREP_EL_SIZE};
496 #print $pdp->{last_ds}," ",$pdp->{unkn_sec_cnt}," ",$pdp->{val},"\n";
497 }
498 }
499  
500 ###
501 6 ▶sub _extractCDPprep {
Subroutine _extractCDPprep: called CC=6 SCAR=18.1
502 # extract CDP prep from raw header (which must have been already read using rrd_open)
503 6 my ($self, $header, $idx) = @_; my $rrd=$self->{rrd};
504
505 6 my $i; my $ii;
506 6 my $L=$self->_packlongchar();
507 6 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 30 6 36
508 #@{$rrd->{cdp_prep}[$ii]}=[];
509 30 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 120 30 150
510 # do a bit of code optimisation to aggregate function calls and array allocation here, since run inside inner loop.
511 120 ▶ if ($self->{encoding} eq "native-double-simple" || $self->{encoding} eq "native-double-mixed") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'encoding'} eq 'native-double-simple' or $self->{'encoding'} eq 'native-double-mixed') { } 0 120 120
elsif ($self->{'encoding'} eq 'native-single') { } 20 100 120
Condition33%1/3
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
l!l&&r!l&&!r
00120
MC/DC0%0/2
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
$self->{"encoding"} eq "native-double-simple" $self->{"encoding"} eq "native-double-mixed"
Truth table33%1/3
A or B
A= $self->{"encoding"} eq "native-double-simple"B= $self->{"encoding"} eq "native-double-mixed"
ABresult
1X1
011
000
512 0 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]} = unpack("d $L x".$self->{DIFF_SIZE}." d d d d $L x".$self->{DIFF_SIZE}." $L x".$self->{DIFF_SIZE}." d d",substr(${$header},$idx,$self->{CDP_PREP_EL_SIZE}));
513 0 $idx+=$self->{CDP_PREP_EL_SIZE};
514 } elsif ($self->{encoding} eq "native-single") {
515 20 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]} = unpack("f $L x".$self->{DIFF_SIZE}." f f f f $L x".$self->{DIFF_SIZE}." $L x".$self->{DIFF_SIZE}." f f",substr(${$header},$idx,$self->{CDP_PREP_EL_SIZE}));
516 20 $idx+=$self->{CDP_PREP_EL_SIZE};
517 } else {
518 100 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}=(0,0,0,0,0,0,0,0,0,0); # pre-allocate array
519 100 (@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[0])=$self->_unpackd(substr(${$header},$idx,$self->{FLOAT_EL_SIZE})); $idx+=$self->{FLOAT_EL_SIZE};
520 100 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[1]=unpack("$L x".$self->{DIFF_SIZE},substr(${$header},$idx,$self->{FLOAT_EL_SIZE})); $idx+=$self->{FLOAT_EL_SIZE};
521 100 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[2..5]=$self->_unpackd(substr(${$header},$idx,4*$self->{FLOAT_EL_SIZE})); $idx+=4*$self->{FLOAT_EL_SIZE};
522 100 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[6..7]=unpack("$L x".$self->{DIFF_SIZE}." $L x".$self->{DIFF_SIZE},substr(${$header},$idx,2*$self->{FLOAT_EL_SIZE})); $idx+=2*$self->{FLOAT_EL_SIZE};
523 100 @{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[8..9]=$self->_unpackd(substr(${$header},$idx,2*$self->{FLOAT_EL_SIZE})); $idx+=2*$self->{FLOAT_EL_SIZE};
524 }
525 }
526 }
527 }
528  
529 ###
530 6 ▶sub _extractRRAptr {
Subroutine _extractRRAptr: called CC=2 SCAR=6.9
531 # array of { cur_row } pointers into current row in rra from raw header (which must have been already read using rrd_open)
532 6 my ($self, $header, $idx) = @_; my $rrd=$self->{rrd};
533  
534 6 my $L=$self->_packlongchar();
535 6 my @ptr=unpack("$L*",substr(${$header},$idx,$self->{RRA_PTR_EL_SIZE}*$rrd->{rra_cnt}));
536 6 my $i;
537 6 ▶ for ($i=0; $i<$rrd->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra_cnt"}) 30 6 36
538 30 $rrd->{rra}[$i]->{ptr}=$ptr[$i];
539 }
540 #print @ptr;
541 }
542  
543 ###
544 4 ▶sub _loadRRAdata {
Subroutine _loadRRAdata: called CC=4 SCAR=13.9
545 # read in and extract the RRA data. assumes rrd_open has been called to read in file header and
546 # populate the RRD data structure
547 4 my $self = $_[0]; my $rrd=$self->{rrd};
548 0 ▶ if (!defined($self->{fd})) {croak("loadRRDdata: must call open() first\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fd"}) 0 4 4
549  
550 4 my $data; my $ds_cnt=$self->{FLOAT_EL_SIZE} * $rrd->{ds_cnt};
551 4 seek $self->{fd},$self->_get_header_size,0; # move to start of RRA data within file
552 4 ▶ for (my $ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 20 4 24
553 20 my $idx=0;
554 20 read($self->{fd}, $data, $self->{FLOAT_EL_SIZE} * $rrd->{ds_cnt}* $rrd->{rra}[$ii]->{row_cnt} );
555 20 my $row_cnt=$rrd->{rra}[$ii]->{row_cnt};
556 20 ▶ for (my $i=0; $i<$row_cnt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $row_cnt) 100 20 120
557 # rather than unpack here, do "lazy" unpacks i.e. only when needed - much faster
558 #@{$rrd->{rra}[$ii]->{data}[$i]}=unpack("d*",substr($data,$idx,$ds_cnt}) );
559 100 $rrd->{rra}[$ii]->{data}[$i]=substr($data,$idx,$ds_cnt);
560 100 $idx+=$ds_cnt;
561 }
562 #print "rra $ii:", join(", ",@{$rrd->{rra_data}[$ii][$rrd->{rra_ptr}[$ii]+1]}),"\n";
563 }
564 4 $rrd->{dataloaded}=1; # record the fact that the data is now loaded in memory
565 }
566  
567 ####
568 23 ▶sub _findDSidx {
Subroutine _findDSidx: called CC=3 SCAR=11.2
569 # find the index of a DS given its name
570 23 my ($self, $name) = @_; my $rrd=$self->{rrd};
571 23 my $i;
572 23 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 39 0 39
573 39 ▶ if ($rrd->{ds}[$i]->{name} eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"ds"}[$i]{"name"} eq $name) 23 16 39
574 23 return $i;
575 }
576 }
577 0 return -1; # unknown source
578 }
579  
580 ################ public functions
581 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
582 # create new object
583 5 my $self;
584 5 $self->{file_name}=undef; # name of RRD file
585 5 $self->{fd}=undef; # file handle
586 5 $self->{encoding}=undef; # binary encoding within file.
587 5 $self->{rrd}->{version}=undef;
588 5 $self->{rrd}->{rra_cnt}= undef; # number of RRAs
589 5 $self->{rrd}->{ds_cnt}=undef; # number of DSs
590 5 $self->{rrd}->{pdp_step}=undef; # min time step size
591 5 $self->{rrd}->{last_up} = undef; # time when last updated
592 5 $self->{rrd}->{ds}=undef; # array of DS definitions
593 5 $self->{rrd}->{rra}=undef; # array of RRA info
594 5 $self->{rrd}->{dataloaded}=undef; # has body of RRD file been loaded into memory ?
595 5 bless $self;
596 5 return $self;
597 }
598  
599 2 ▶sub DS_names {
Subroutine DS_names: called CC=2 SCAR=6.9
Pod documented
600 # return a list containing the names of the DS's in the RRD database.
601 2 my $rrd=$_[0]->{rrd};
602 2 my @names=(); my $i;
603 2 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 8 2 10
604 8 push(@names, $rrd->{ds}[$i]->{name});
605 }
606 2 return @names;
607 }
608  
609 6 ▶sub num_RRAs {
Subroutine num_RRAs: called CC=1 SCAR=0.0
Pod documented
610 # returns the number of RRA's in the database. RRAs are indexed from 0 .. num_RRAs-1.
611 6 return $_[0]->{rrd}->{rra_cnt};
612 }
613  
614 8 ▶sub RRA_numrows {
Subroutine RRA_numrows: called CC=3 SCAR=12.7
Pod documented
615 # return number of rows in a RRA
616 8 my ($self, $rraidx) = @_; my $rrd=$self->{rrd};
617 0 ▶ if ($rraidx > $rrd->{rra_cnt} || $rraidx<0) {croak("RRA index out of range\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rraidx > $rrd->{"rra_cnt"} or $rraidx < 0) 0 8 8
Condition33%1/3
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
l!l&&r!l&&!r
008
MC/DC0%0/2
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
$rraidx > $rrd->{"rra_cnt"} $rraidx < 0
Truth table33%1/3
A or B
A= $rraidx > $rrd->{"rra_cnt"}B= $rraidx < 0
ABresult
1X1
011
000
618 8 return $rrd->{rra}[$rraidx]->{row_cnt};
619 }
620  
621 0 ▶sub RRA_type {
Subroutine RRA_type: not called CC=3 SCAR=24.8
Pod documented
622 # return the type of an RRA (AVERAGE, MAX etc)
623 0 my ($self, $rraidx) = @_; my $rrd=$self->{rrd};
624 0 ▶ if ($rraidx > $rrd->{rra_cnt} || $rraidx<0) {croak("RRA index out of range\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rraidx > $rrd->{"rra_cnt"} or $rraidx < 0) 0 0 0
Condition0%0/3
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
$rraidx > $rrd->{"rra_cnt"} $rraidx < 0
Truth table0%0/3
A or B
A= $rraidx > $rrd->{"rra_cnt"}B= $rraidx < 0
ABresult
1X1
011
000
625 0 return $rrd->{rra}[$rraidx]->{name};
626 }
627  
628 2 ▶sub RRA_step {
Subroutine RRA_step: called CC=3 SCAR=12.7
Pod documented
629 # return the step size (in seconds) used in an RRA
630 2 my ($self, $rraidx) = @_; my $rrd=$self->{rrd};
631 0 ▶ if ($rraidx > $rrd->{rra_cnt} || $rraidx<0) {croak("RRA index out of range\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rraidx > $rrd->{"rra_cnt"} or $rraidx < 0) 0 2 2
Condition33%1/3
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
l!l&&r!l&&!r
002
MC/DC0%0/2
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
$rraidx > $rrd->{"rra_cnt"} $rraidx < 0
Truth table33%1/3
A or B
A= $rraidx > $rrd->{"rra_cnt"}B= $rraidx < 0
ABresult
1X1
011
000
632 2 return $rrd->{rra}[$rraidx]->{pdp_cnt}*$rrd->{pdp_step};
633 }
634  
635 2 ▶sub RRA_xff {
Subroutine RRA_xff: called CC=3 SCAR=12.7
Pod documented
636 # return the xff value for an RRA
637 2 my ($self, $idx) = @_; my $rrd=$self->{rrd};
638 0 ▶ if ($idx > $rrd->{rra_cnt} || $idx<0) {croak("RRA index out of range\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > $rrd->{"rra_cnt"} or $idx < 0) 0 2 2
Condition33%1/3
$idx > $rrd->{"rra_cnt"} or $idx < 0
l!l&&r!l&&!r
002
MC/DC0%0/2
$idx > $rrd->{"rra_cnt"} or $idx < 0
$idx > $rrd->{"rra_cnt"} $idx < 0
Truth table33%1/3
A or B
A= $idx > $rrd->{"rra_cnt"}B= $idx < 0
ABresult
1X1
011
000
639 2 return $rrd->{rra}[$idx]->{xff};
640 }
641  
642 3 ▶sub RRA_el {
Subroutine RRA_el: called CC=6 SCAR=21.2
Pod documented
643 # fetch a specified element from a specified RRA.
644 # given the index number of the RRA, the index of the DS and the row within the RRA (oldest row is 0),
645 # returns a pair (t,d) where t is the unix timestamp of the data point and d is the data value
646 3 my ($self, $rraidx, $ds_name, $tidx) = @_; my $rrd=$self->{rrd};
647
648 0 ▶ if ($rraidx > $rrd->{rra_cnt} || $rraidx<0) {croak("RRA index out of range\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rraidx > $rrd->{"rra_cnt"} or $rraidx < 0) 0 3 3
Condition33%1/3
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
l!l&&r!l&&!r
003
MC/DC0%0/2
$rraidx > $rrd->{"rra_cnt"} or $rraidx < 0
$rraidx > $rrd->{"rra_cnt"} $rraidx < 0
Truth table33%1/3
A or B
A= $rraidx > $rrd->{"rra_cnt"}B= $rraidx < 0
ABresult
1X1
011
000
649 3 my $dsidx=$self->_findDSidx($ds_name);
650 0 ▶ if ($tidx >= $rrd->{rra}[$rraidx]->{row_cnt} || $tidx<0) {croak("Row index out of range\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($tidx >= $rrd->{"rra"}[$rraidx]{"row_cnt"} or $tidx < 0) 0 3 3
Condition33%1/3
$tidx >= $rrd->{"rra"}[$rraidx]{"row_cnt"} or $tidx < 0
l!l&&r!l&&!r
003
MC/DC0%0/2
$tidx >= $rrd->{"rra"}[$rraidx]{"row_cnt"} or $tidx < 0
$tidx >= $rrd->{"rra"}[$rraidx]{"row_cnt"} $tidx < 0
Truth table33%1/3
A or B
A= $tidx >= $rrd->{"rra"}[$rraidx]{"row_cnt"}B= $tidx < 0
ABresult
1X1
011
000
651  
652 # load RRA data, if not already loaded
653 0 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 0 3 3
654  
655 3 my $t = $rrd->{last_up} - $rrd->{last_up}%($rrd->{rra}[$rraidx]->{pdp_cnt}*$rrd->{pdp_step}) -($rrd->{rra}[$rraidx]->{row_cnt}-1-$tidx)*$rrd->{rra}[$rraidx]->{pdp_cnt}*$rrd->{pdp_step};
656 3 my $jj= ($rrd->{rra}[$rraidx]->{ptr}+1+ $tidx)%$rrd->{rra}[$rraidx]->{row_cnt};
657 3 my @line=$self->_unpackd($rrd->{rra}[$rraidx]->{data}[$jj]);
658 3 return ($t, $line[$dsidx]);
659 }
660  
661 1 ▶sub set_RRA_el {
Subroutine set_RRA_el: called CC=2 SCAR=6.9
Pod documented
662 # change value of a specified element from a specified RRA
663 # given the index number of the RRA, the index of the DS and the row within the RRA (oldest row is 0),
664 # updates the data value to be $val
665 1 my ($self, $rraidx, $ds_name, $tidx, $val) = @_; my $rrd=$self->{rrd};
666 1 my $dsidx=$self->_findDSidx($ds_name);
667
668 # load RRA data, if not already loaded
669 1 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 1 0 1
670  
671 1 my $jj= ($rrd->{rra}[$rraidx]->{ptr}+1 + $tidx)%$rrd->{rra}[$rraidx]->{row_cnt};
672 1 my @line=$self->_unpackd($rrd->{rra}[$rraidx]->{data}[$jj]);
673 1 $line[$dsidx] = $val;
674 1 $rrd->{rra}[$rraidx]->{data}[$jj]=$self->_packd(\@line);
675 }
676  
677 3 ▶sub last {
Subroutine last: called CC=1 SCAR=0.0
Pod documented
678 # return time of last update
679 3 return $_[0]->{rrd}->{last_up};
680 }
681  
682 0 ▶sub set_last {
Subroutine set_last: not called CC=1 SCAR=6.9
Pod documented
683 # change time of last update; use with caution !
684 0 $_[0]->{rrd}->{last_up} = $_[1];
685 0 return 1;
686 }
687  
688 3 ▶sub lastupdate {
Subroutine lastupdate: called CC=2 SCAR=6.9
Pod documented
689 # return the most recent update values
690 3 my $self=$_[0]; my $rrd=$self->{rrd};
691  
692 3 my @vals;
693 3 ▶ for (my $i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 12 3 15
694 12 push(@vals,$rrd->{ds}[$i]->{pdp_prep}->{last_ds});
695 }
696 3 return @vals;
697 }
698  
699 2 ▶sub minstep {
Subroutine minstep: called CC=1 SCAR=0.0
Pod documented
700 # return the min step size, in seconds
701 2 my $self=$_[0]; my $rrd=$self->{rrd};
702 2 return $rrd->{pdp_step};
703 }
704  
705 2 ▶sub DS_heartbeat {
Subroutine DS_heartbeat: called CC=2 SCAR=7.2
Pod documented
706 # return heartbeat for DS
707 2 my ($self, $name) = @_; my $rrd=$self->{rrd};
708
709 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 2 2
710 2 return $rrd->{ds}[$idx]->{hb};
711 }
712  
713 1 ▶sub set_DS_heartbeat {
Subroutine set_DS_heartbeat: called CC=3 SCAR=11.8
Pod documented
714 # change heartbeat for DS
715 1 my ($self, $name, $hb) = @_; my $rrd=$self->{rrd};
716
717 0 ▶ if ($hb < $rrd->{pdp_step}) {croak("Heartbeat value must be at least the minimum step size ".$rrd->{pdp_step}." secs\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($hb < $rrd->{"pdp_step"}) 0 1 1
718
719 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
720 # update to new value
721 1 $rrd->{ds}[$idx]->{hb}=$hb;
722 1 return 1;
723 }
724  
725 1 ▶sub DS_min {
Subroutine DS_min: called CC=2 SCAR=7.2
Pod documented
726 # return min value for DS
727 1 my ($self, $name) = @_; my $rrd=$self->{rrd};
728
729 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
730 1 return $rrd->{ds}[$idx]->{min};
731 }
732  
733 1 ▶sub set_DS_min {
Subroutine set_DS_min: called CC=2 SCAR=7.1
Pod documented
734 # change min value for DS
735 1 my ($self, $name, $min) = @_; my $rrd=$self->{rrd};
736
737 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
738 # update to new value
739 1 $rrd->{ds}[$idx]->{min}=$min;
740 1 return 1;
741 }
742  
743 1 ▶sub DS_max {
Subroutine DS_max: called CC=2 SCAR=7.2
Pod documented
744 # return max value for DS
745 1 my ($self, $name) = @_; my $rrd=$self->{rrd};
746
747 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
748 1 return $rrd->{ds}[$idx]->{max};
749 }
750  
751 1 ▶sub set_DS_max {
Subroutine set_DS_max: called CC=2 SCAR=7.1
Pod documented
752 # change max value for DS
753 1 my ($self, $name, $max) = @_; my $rrd=$self->{rrd};
754
755 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
756 # update to new value
757 1 $rrd->{ds}[$idx]->{max}=$max;
758 1 return 1;
759 }
760  
761 2 ▶sub DS_type {
Subroutine DS_type: called CC=2 SCAR=7.2
Pod documented
762 # return type of DS
763 2 my ($self, $name) = @_; my $rrd=$self->{rrd};
764
765 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 2 2
766 2 return $rrd->{ds}[$idx]->{type};
767 }
768  
769 1 ▶sub set_DS_type {
Subroutine set_DS_type: called CC=3 SCAR=11.8
Pod documented
770 # change type of DS
771 1 my ($self, $name, $type) = @_; my $rrd=$self->{rrd};
772
773 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
774 0 ▶ if ($type !~ m/(GAUGE|COUNTER|DERIVE|ABSOLUTE)/) { croak("Invalid DS type\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /(GAUGE|COUNTER|DERIVE|ABSOLUTE)/) 0 1 1
775 # update to new value
776 1 $rrd->{ds}[$idx]->{type}=$type;
777 1 return 1;
778 }
779  
780 1 ▶sub rename_DS {
Subroutine rename_DS: called CC=2 SCAR=7.1
Pod documented
781 1 my ($self, $old, $new) = @_; my $rrd=$self->{rrd};
782  
783 0 ▶ my $idx=$self->_findDSidx($old); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
784 1 $rrd->{ds}[$idx]->{name}=$new;
785 1 return 1;
786 }
787  
788 1 ▶sub add_DS {
Subroutine add_DS: called CC=8 SCAR=20.9
Pod documented
789 # add a new DS. argument is is same format as used by create
790 1 my ($self, $arg) = @_; my $rrd=$self->{rrd};
791
792 0 ▶ if ($arg !~ m/^DS:([a-zA-Z0-9_\-]+):(GAUGE|COUNTER|DERIVE|ABSOLUTE):([0-9]+):(U|[-\+]?[0-9\.]+):(U|[-\+]?[0-9\.]+)$/) { croak("Invalid DS spec\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg =~ /^DS:([a-zA-Z0-9_\-]+):(GAUGE|COUNTER|DERIVE|ABSOLUTE):([0-9]+):(U|[-\+]?[0-9\.]+):(U|[-\+]?[0-9\.]+)$/) 0 1 1
793  
794 # load RRA data, if not already loaded
795 1 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 1 0 1
796  
797 # update DS definitions
798 1 my $ds;
799 1 ▶ my $min=$4; if ($min eq "U") {$min=NAN;} # set to NaN
Branch: 1 branch
BranchTrueFalseTotal
if ($min eq "U") 1 0 1
800 1 ▶ my $max=$5; if ($max eq "U") {$max=NAN;} # set to NaN
Branch: 1 branch
BranchTrueFalseTotal
if ($max eq "U") 1 0 1
801 ($ds->{name}, $ds->{type}, $ds->{hb}, $ds->{min}, $ds->{max},
802 $ds->{pdp_prep}->{last_ds}, $ds->{pdp_prep}->{unkn_sec_cnt}, $ds->{pdp_prep}->{val},
803 1 )= ($1,$2,$3,$min,$max,"U", $rrd->{last_up}%$rrd->{pdp_step}, 0.0);
804 1 $rrd->{ds}[@{$rrd->{ds}}]=$ds;
805 1 $rrd->{ds_cnt}++;
806
807 # update RRAs
808 1 my $ii;
809 1 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 5 1 6
810 5 @{$rrd->{rra}[$ii]->{cdp_prep}[$rrd->{ds_cnt}-1]} = (NAN,(($rrd->{last_up}-$rrd->{last_up}%$rrd->{pdp_step})%($rrd->{pdp_step}*$rrd->{rra}[$ii]->{pdp_cnt}))/$rrd->{pdp_step},0,0,0,0,0,0,0,0);
811 }
812 # update data
813 1 my @line; my $i;
814 1 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 5 1 6
815 5 ▶ for ($i=0; $i<$rrd->{rra}[$ii]->{row_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra"}[$ii]{"row_cnt"}) 25 5 30
816 25 @line=$self->_unpackd($rrd->{rra}[$ii]->{data}[$i]);
817 25 $line[$rrd->{ds_cnt}-1]=NAN;
818 25 $rrd->{rra}[$ii]->{data}[$i]=$self->_packd(\@line);
819 }
820 }
821 1 return 1;
822 }
823  
824 1 ▶sub delete_DS {
Subroutine delete_DS: called CC=9 SCAR=22.1
Pod documented
825 # delete a DS
826 1 my ($self, $name) = @_; my $rrd=$self->{rrd};
827 0 ▶ my $idx=$self->_findDSidx($name); if ($idx<0) {croak("Unknown source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 1 1
828  
829 # load RRA data, if not already loaded
830 0 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 0 1 1
831  
832 # update DS definitions
833 1 my $i;
834 1 $rrd->{ds_cnt}--;
835 1 ▶ for ($i=$idx; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 2 1 3
836 2 $rrd->{ds}[$i]=$rrd->{ds}[$i+1];
837 }
838
839 # update RRAs
840 1 my $ii;
841 1 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 5 1 6
842 5 ▶ for ($i=$idx; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 10 5 15
843 10 $rrd->{rra}[$ii]->{cdp_prep}[$i]=$rrd->{rra}[$ii]->{cdp_prep}[$i+1];
844 }
845 }
846  
847 # update data
848 1 my $j; my @line;
849 1 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 5 1 6
850 5 ▶ for ($i=0; $i<$rrd->{rra}[$ii]->{row_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra"}[$ii]{"row_cnt"}) 25 5 30
851 25 @line=$self->_unpackd($rrd->{rra}[$ii]->{data}[$i]);
852 25 ▶ for ($j=$idx; $j<$rrd->{ds_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"ds_cnt"}) 50 25 75
853 50 $line[$j]=$line[$j+1];
854 }
855 25 $rrd->{rra}[$ii]->{data}[$i]=$self->_packd([@line[0..$rrd->{ds_cnt}-1]]);
856 }
857 }
858 1 return 1;
859 }
860  
861 1 ▶sub add_RRA {
Subroutine add_RRA: called CC=9 SCAR=23.8
Pod documented
862 # add a new RRA
863 1 my ($self, $args) = @_; my $rrd=$self->{rrd};
864 0 ▶ if ($args !~ m/^RRA:(AVERAGE|MAX|MIN|LAST):([0-9\.]+):([0-9]+):([0-9]+)$/) {croak("Invalid RRA spec\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($args =~ /^RRA:(AVERAGE|MAX|MIN|LAST):([0-9\.]+):([0-9]+):([0-9]+)$/) 0 1 1
865 # load RRA data, if not already loaded
866 0 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 0 1 1
867 # update RRA definitions
868 1 my $rra;
869 0 ▶ if ($4<1) { croak("Invalid row count $4\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($4 < 1) 0 1 1
870 0 ▶ if ($2<0.0 || $2>1.0) { croak("Invalid xff $2: must be between 0 and 1\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($2 < 0 or $2 > 1) 0 1 1
Condition33%1/3
$2 < 0 or $2 > 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$2 < 0 or $2 > 1
$2 < 0 $2 > 1
Truth table33%1/3
A or B
A= $2 < 0B= $2 > 1
ABresult
1X1
011
000
871 0 ▶ if ($3<1) { croak("Invalid step $3: must be >= 1\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($3 < 1) 0 1 1
872 1 ($rra->{name}, $rra->{xff}, $rra->{pdp_cnt}, $rra->{row_cnt}, $rra->{ptr}, $rra->{data})=($1,$2,$3,$4,int(rand($4)),undef);
873 1 my $idx=@{$rrd->{rra}};
874 1 $rrd->{rra}[$idx]=$rra;
875 1 $rrd->{rra_cnt}++;
876
877 1 my $i;
878 1 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 4 1 5
879 4 @{$rrd->{rra}[$idx]->{cdp_prep}[$i]} = (NAN,(($rrd->{last_up}-$rrd->{last_up}%$rrd->{pdp_step})%($rrd->{pdp_step}*$rrd->{rra}[$idx]->{pdp_cnt}))/$rrd->{pdp_step},0,0,0,0,0,0,0,0);
880 }
881 # update data
882 1 my @empty=((NAN)x$rrd->{ds_cnt});
883 1 ▶ for ($i=0; $i<$rrd->{rra}[$idx]->{row_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra"}[$idx]{"row_cnt"}) 10 1 11
884 10 $rrd->{rra}[$idx]->{data}[$i] = $self->_packd(\@empty);
885 }
886 1 return 1;
887 }
888  
889 9 ▶sub delete_RRA {
Subroutine delete_RRA: called CC=4 SCAR=13.9
Pod documented
890 # delete an RRA
891 9 my ($self, $idx) = @_; my $rrd=$self->{rrd};
892 9 ▶ if ($idx >= $rrd->{rra_cnt} || $idx < 0) { croak("RRA index out of range\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= $rrd->{"rra_cnt"} or $idx < 0) 3 6 9
Condition100%3/3
$idx >= $rrd->{"rra_cnt"} or $idx < 0
l!l&&r!l&&!r
216
MC/DC100%2/2
$idx >= $rrd->{"rra_cnt"} or $idx < 0
$idx >= $rrd->{"rra_cnt"} $idx < 0
Truth table100%3/3
A or B
A= $idx >= $rrd->{"rra_cnt"}B= $idx < 0
ABresult
1X1
011
000
893 # load RRA data, if not already loaded
894 0 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 0 6 6
895 # update RRA
896 6 $rrd->{rra_cnt}--;
897 6 splice(@{$rrd->{rra}}, $idx, 1);
898 6 return 1;
899 }
900  
901 9 ▶sub resize_RRA {
Subroutine resize_RRA: called CC=10 SCAR=23.2
Pod documented
902 9 my ($self, $idx, $size) = @_; my $rrd=$self->{rrd};
903  
904 9 ▶ if ($idx >= $rrd->{rra_cnt} || $idx < 0) { croak("RRA index out of range\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= $rrd->{"rra_cnt"} or $idx < 0) 2 7 9
Condition100%3/3
$idx >= $rrd->{"rra_cnt"} or $idx < 0
l!l&&r!l&&!r
117
MC/DC100%2/2
$idx >= $rrd->{"rra_cnt"} or $idx < 0
$idx >= $rrd->{"rra_cnt"} $idx < 0
Truth table100%3/3
A or B
A= $idx >= $rrd->{"rra_cnt"}B= $idx < 0
ABresult
1X1
011
000
905 0 ▶ if ($size < 0) {$size=0;}
Branch: 1 branch
BranchTrueFalseTotal
if ($size < 0) 0 7 7
906 # load RRA data, if not already loaded
907 0 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 0 7 7
908 # update data
909 7 my $row_cnt = $rrd->{rra}[$idx]->{row_cnt};
910 7 my $ptr = $rrd->{rra}[$idx]->{ptr};
911 7 my $empty = $self->_packd([(NAN)x$rrd->{ds_cnt}]);
912  
913 7 ▶ if ($size > $row_cnt) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size > $row_cnt) { } 2 5 7
elsif ($size < $row_cnt) { } 5 0 5
914 # Expand
915 2 splice( @{$rrd->{rra}[$idx]->{data}}, $ptr+1, 0, ( ($empty)x($size-$row_cnt) ) );
916 } elsif ($size < $row_cnt) {
917 # Shrink; removing tail
918 5 my $cnt_strip = $row_cnt-$size;
919 5 ▶ my $tail = ($ptr+1 + $cnt_strip > $row_cnt ? $row_cnt - $ptr-1 : $cnt_strip);
Branch: 1 branch
BranchTrueFalseTotal
$ptr + 1 + $cnt_strip > $row_cnt ? : 3 2 5
920 5 ▶ splice(@{$rrd->{rra}[$idx]->{data}}, $ptr+1, $tail) if $tail > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tail > 0 4 1 5
921  
922 # then removing head if remainder
923 5 ▶ splice(@{$rrd->{rra}[$idx]->{data}}, 0, $cnt_strip-$tail) if $tail < $cnt_strip;
Branch: 1 branch
BranchTrueFalseTotal
if $tail < $cnt_strip 3 2 5
924 }
925  
926 7 $rrd->{rra}[$idx]->{row_cnt} = $size;
927 7 return 1;
928 }
929  
930 1 ▶sub set_RRA_xff {
Subroutine set_RRA_xff: called CC=3 SCAR=12.3
Pod documented
931 # schange xff value for an RRA
932 1 my ($self, $idx, $xff) = @_; my $rrd=$self->{rrd};
933 0 ▶ if ($idx >= $rrd->{rra_cnt} || $idx < 0) { croak("RRA index out of range\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= $rrd->{"rra_cnt"} or $idx < 0) 0 1 1
Condition33%1/3
$idx >= $rrd->{"rra_cnt"} or $idx < 0
l!l&&r!l&&!r
001
MC/DC0%0/2
$idx >= $rrd->{"rra_cnt"} or $idx < 0
$idx >= $rrd->{"rra_cnt"} $idx < 0
Truth table33%1/3
A or B
A= $idx >= $rrd->{"rra_cnt"}B= $idx < 0
ABresult
1X1
011
000
934 1 $rrd->{rra}[$idx]->{xff}=$xff;
935 1 return 1;
936 }
937  
938 #sub set_RRA_step {
939 # TODO: change RRA step size - will require resampling
940 #}
941  
942 10 ▶sub update {
Subroutine update: called CC=83 SCAR=52.3
Pod documented
943 # a re-implementation of rrdupdate. updates file in place on disk, if possible - much faster.
944
945 10 my ($self, $args_str) = @_; my $rrd=$self->{rrd};
946
947 10 my $ret; my $args; my $template='';
948 10 ($ret, $args) = _GetOptionsFromString($args_str,
949 "template|t:s" => \$template,
950 );
951  
952 # update file in place ?
953 10 my $inplace; my $fd;
954 10 ▶ if (defined($rrd->{dataloaded})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rrd->{'dataloaded'}) { } 10 0 10
955 10 $inplace="memory"; # data is already loaded into memory so do update there. will need to subsequently call save() to write data to disk
956 } else {
957 0 ▶ if (defined($self->{fd})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'fd'}) { } 0 0 0
958 0 $inplace="file"; # data is not loaded yet, so carry out update in place in file. more efficient - no need to call save() to write data to disk.
959 0 ▶ open $self->{fd}, "+<", $self->{file_name} or croak "Couldn't open file ".$self->{file_name}.": $!\n"; # reopen file in update mode ($self->open() opens in read-only mode)
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $self->{"fd"}, "+<", $self->{"file_name"} or croak(q[Couldn't open file ] . $self->{"file_name"} . ": $!\n") 0 0 0
960 0 binmode($self->{fd});
961 0 $fd=$self->{fd};
962 } else {
963 0 croak("update: must call open() or create() first\n");
964 }
965 }
966  
967 # Parse template, if provided
968 10 my $i; my $j;
969 10 my @tmp=split(/:/,$template);
970 10 my @idx;
971 10 ▶ if (@tmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp == 0) { } 8 2 10
972 # no template, default to complete DS list
973 8 @idx=(0 .. $rrd->{ds_cnt}-1);
974 } else {
975 # read DS list from template
976 2 ▶ for ($i=0; $i<@tmp; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tmp) 7 2 9
977 0 ▶ $idx[$i]=$self->_findDSidx($tmp[$i]); if($idx[$i]<0) {croak("Unknown DS name ".$tmp[$i]."\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($idx[$i] < 0) 0 7 7
978 }
979 }
980 # Parse update strings - updates the primary data points (PDPs)
981 # and consolidated data points (CDPs), and writes changes to the RRAs.
982 10 my @updvals; my @bits; my $rate; my $current_time; my $interval;
983 10 ▶ for ($i=0; $i<@{$args}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$args;}) 10 10 20
984 #parse colon-separated DS string
985 0 ▶ if ($args->[$i] =~ m/(-t|--template)/) {next;} # ignore option here
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[$i] =~ /(-t|--template)/) 0 10 10
986 0 ▶ if ($args->[$i] =~ m/\@/) {croak("\@ time format not supported - use either N or a unix timestamp\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[$i] =~ /\@/) 0 10 10
987 10 @bits=split(/:/,$args->[$i]);
988 0 ▶ if (@bits-1 < @idx) {croak("expected ".@idx." data source readings (got ".(@bits-1).") from ".$args->[$i],"\n");}
Branch: 1 branch
BranchTrueFalseTotal
if (@bits - 1 < @idx) 0 10 10
989 #get_time_from_reading
990 10 ▶ if ($bits[0] eq "N") {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits[0] eq 'N') { } 0 10 10
991 0 $current_time=time();
992 #normalize_time
993 } else {
994 10 $current_time=$bits[0];
995 }
996 0 ▶ if ($current_time < $rrd->{last_up}) {croak("attempt to update using time $current_time when last update time is ". $rrd->{last_up}."\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($current_time < $rrd->{"last_up"}) 0 10 10
997 10 $interval=$current_time - $rrd->{last_up};
998 # initialise values to NaN
999 10 ▶ for ($j=0; $j<$rrd->{ds_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"ds_cnt"}) 40 10 50
1000 40 $updvals[$j]="U";
1001 }
1002 10 ▶ for ($j=0; $j<@idx; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @idx) 39 10 49
1003 39 $updvals[$idx[$j]] = $bits[$j+1];
1004 }
1005 # process the data sources and update the pdp_prep area accordingly
1006 10 my @pdp_new=();
1007 10 ▶ for ($j=0;$j<@updvals; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @updvals) 40 10 50
1008 40 ▶ if ($rrd->{ds}[$j]->{hb} < $interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"ds"}[$j]{"hb"} < $interval) 0 40 40
1009 # make sure we do not build diffs with old last_ds values
1010 0 $rrd->{ds}[$j]->{pdp_prep}->{last_ds}="U";
1011 }
1012 40 ▶ if ($updvals[$j] ne "U" && $rrd->{ds}[$j]->{hb} >= $interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($updvals[$j] ne 'U' and $rrd->{'ds'}[$j]{'hb'} >= $interval) { } 39 1 40
Condition66%2/3
$updvals[$j] ne "U" and $rrd->{"ds"}[$j]{"hb"} >= $interval
!ll&&!rl&&r
1039
MC/DC50%1/2
$updvals[$j] ne "U" and $rrd->{"ds"}[$j]{"hb"} >= $interval
$updvals[$j] ne "U" $rrd->{"ds"}[$j]{"hb"} >= $interval
Truth table66%2/3
A and B
A= $updvals[$j] ne "U"B= $rrd->{"ds"}[$j]{"hb"} >= $interval
ABresult
0X0
100
111
1013 39 $rate=NAN;
1014 39 ▶ if ( $rrd->{ds}[$j]->{type} eq "COUNTER" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rrd->{'ds'}[$j]{'type'} eq 'COUNTER') { } 10 29 39
elsif ($rrd->{'ds'}[$j]{'type'} eq 'DERIVE') { } 9 20 29
elsif ($rrd->{'ds'}[$j]{'type'} eq 'GAUGE') { } 10 10 20
1015 0 ▶ if ($updvals[$j] !~ m/^\d+$/) {croak("not a simple unsigned integer ".$updvals[$j]);}
Branch: 1 branch
BranchTrueFalseTotal
unless ($updvals[$j] =~ /^\d+$/) 0 10 10
1016 10 ▶ if ($rrd->{ds}[$j]->{pdp_prep}->{last_ds} ne "U") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{'ds'}[$j]{'pdp_prep'}{'last_ds'} ne 'U') { } 9 1 10
1017 #use bignum; # need this for next line as might be large integers
1018 9 $pdp_new[$j] = $updvals[$j] - $rrd->{ds}[$j]->{pdp_prep}->{last_ds};
1019 # simple overflow catcher
1020 9 ▶ if ($pdp_new[$j] < 0) {$pdp_new[$j]+=4294967296; } #2^32
Branch: 1 branch
BranchTrueFalseTotal
if ($pdp_new[$j] < 0) 3 6 9
1021 0 ▶ if ($pdp_new[$j] < 0) {$pdp_new[$j]+=18446744069414584320; } #2^64-2^32
Branch: 1 branch
BranchTrueFalseTotal
if ($pdp_new[$j] < 0) 0 9 9
1022 9 $rate=$pdp_new[$j]/$interval;
1023 } else {
1024 1 $pdp_new[$j]=NAN;
1025 }
1026 } elsif ( $rrd->{ds}[$j]->{type} eq "DERIVE" ) {
1027 0 ▶ if ($updvals[$j] !~ m/^[+|-]?\d+$/) {croak("not a simple signed integer ".$updvals[$j]);}
Branch: 1 branch
BranchTrueFalseTotal
unless ($updvals[$j] =~ /^[+|-]?\d+$/) 0 9 9
1028 9 ▶ if ($rrd->{ds}[$j]->{pdp_prep}->{last_ds} ne "U") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{'ds'}[$j]{'pdp_prep'}{'last_ds'} ne 'U') { } 8 1 9
1029 #use bignum; # need this for next line as might be large integers
1030 8 $pdp_new[$j] = $updvals[$j] - $rrd->{ds}[$j]->{pdp_prep}->{last_ds};
1031 8 $rate=$pdp_new[$j]/$interval;
1032 } else {
1033 1 $pdp_new[$j]=NAN;
1034 }
1035 } elsif ( $rrd->{ds}[$j]->{type} eq "GAUGE" ) {
1036 0 ▶ if ($updvals[$j] !~ m/^(-)?[\d]+(\.[\d]+)?$/) {croak("not a number ".$updvals[$j]);}
Branch: 1 branch
BranchTrueFalseTotal
unless ($updvals[$j] =~ /^(-)?[\d]+(\.[\d]+)?$/) 0 10 10
1037 10 $pdp_new[$j] = $updvals[$j]*$interval;
1038 10 $rate=$pdp_new[$j]/$interval;
1039 } else { # ABSOLUTE
1040 10 $pdp_new[$j] = $updvals[$j];
1041 10 $rate=$pdp_new[$j]/$interval;
1042 }
1043 39 ▶ if (!_isNan($rate)
Branch: 1 branch
BranchTrueFalseTotal
if (not _isNan($rate) and !_isNan($rrd->{"ds"}[$j]{"max"}) && $rate > $rrd->{"ds"}[$j]{"max"} || !_isNan($rrd->{"ds"}[$j]{"min"}) && $rate < $rrd->{"ds"}[$j]{"min"}) 0 39 39
Condition41%5/12
not _isNan($rate) and !_isNan($rrd->{"ds"}[$j]{"max"}) && $rate > $rrd->{"ds"}[$j]{"max"} || !_isNan($rrd->{"ds"}[$j]{"min"}) && $rate < $rrd->{"ds"}[$j]{"min"}
!ll&&!rl&&r
2370
not _isNan($rrd->{"ds"}[$j]{"max"}) and $rate > $rrd->{"ds"}[$j]{"max"} or not _isNan($rrd->{"ds"}[$j]{"min"}) and $rate < $rrd->{"ds"}[$j]{"min"}
l!l&&r!l&&!r
0037
not _isNan($rrd->{"ds"}[$j]{"max"}) and $rate > $rrd->{"ds"}[$j]{"max"}
!ll&&!rl&&r
3700
not _isNan($rrd->{"ds"}[$j]{"min"}) and $rate < $rrd->{"ds"}[$j]{"min"}
!ll&&!rl&&r
3700
MC/DC0%0/5
not _isNan($rate) and !_isNan($rrd->{"ds"}[$j]{"max"}) && $rate > $rrd->{"ds"}[$j]{"max"} || !_isNan($rrd->{"ds"}[$j]{"min"}) && $rate < $rrd->{"ds"}[$j]{"min"}
not _isNan($rate) not _isNan($rrd->{"ds"}[$j]{"max"}) $rate > $rrd->{"ds"}[$j]{"max"} not _isNan($rrd->{"ds"}[$j]{"min"}) $rate < $rrd->{"ds"}[$j]{"min"}
Truth table25%2/8
not(A) and not(B) and C or not(D) and E
A= not _isNan($rate)B= not _isNan($rrd->{"ds"}[$j]{"max"})C= $rate > $rrd->{"ds"}[$j]{"max"}D= not _isNan($rrd->{"ds"}[$j]{"min"})E= $rate < $rrd->{"ds"}[$j]{"min"}
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
1044 && (
1045 (!_isNan($rrd->{ds}[$j]->{max}) && $rate >$rrd->{ds}[$j]->{max})
1046 || (!_isNan($rrd->{ds}[$j]->{min}) && $rate <$rrd->{ds}[$j]->{min})
1047 )) {
1048 0 $pdp_new[$j]=NAN;
1049 }
1050 } else {
1051 1 $pdp_new[$j]=NAN;
1052 }
1053 40 $rrd->{ds}[$j]->{pdp_prep}->{last_ds} = $updvals[$j];
1054 }
1055 # how many PDP steps have elapsed since the last update?
1056 10 my $proc_pdp_st = $rrd->{last_up} - $rrd->{last_up} % $rrd->{pdp_step};
1057 10 my $occu_pdp_age = $current_time % $rrd->{pdp_step};
1058 10 my $occu_pdp_st = $current_time - $occu_pdp_age;
1059 10 my $pre_int; my $post_int;
1060 10 ▶ if ($occu_pdp_st > $proc_pdp_st) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occu_pdp_st > $proc_pdp_st) { } 10 0 10
1061 # OK we passed the pdp_st moment
1062 10 $pre_int = $occu_pdp_st - $rrd->{last_up};
1063 10 $post_int = $occu_pdp_age;
1064 } else {
1065 0 $pre_int = $interval;
1066 0 $post_int=0;
1067 }
1068 10 my $proc_pdp_cnt = int( $proc_pdp_st / $rrd->{pdp_step} );
1069 10 my $elapsed_pdp_st = int( ($occu_pdp_st - $proc_pdp_st)/$rrd->{pdp_step} );
1070 # have we moved past a pdp step size since last run ?
1071 10 ▶ if ($elapsed_pdp_st == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elapsed_pdp_st == 0) { } 0 10 10
1072 # nope, simple_update
1073 0 ▶ for ($j=0; $j<$rrd->{ds_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"ds_cnt"}) 0 0 0
1074 0 ▶ if (_isNan($pdp_new[$j])) {
Branch: 2 branches
BranchTrueFalseTotal
if (_isNan($pdp_new[$j])) { } 0 0 0
elsif (_isNan($rrd->{'ds'}[$j]{'pdp_prep'}{'val'})) { } 0 0 0
1075 0 $rrd->{ds}[$j]->{pdp_prep}->{unkn_sec_cnt} += int($interval);
1076 } elsif (_isNan($rrd->{ds}[$j]->{pdp_prep}->{val}) ) {
1077 0 $rrd->{ds}[$j]->{pdp_prep}->{val} = $pdp_new[$j];
1078 } else {
1079 0 $rrd->{ds}[$j]->{pdp_prep}->{val} += $pdp_new[$j];
1080 }
1081 }
1082 } else {
1083 # yep
1084 # process_all_pdp_st
1085 10 my $pre_unknown; my @pdp_temp; my $diff_pdp_st;
1086 10 ▶ for ($j=0; $j<$rrd->{ds_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"ds_cnt"}) 40 10 50
1087 # Process an update that occurs after one of the PDP moments.
1088 # Increments the PDP value, sets NAN if time greater than the heartbeats have elapsed
1089 40 $pre_unknown=0;
1090 40 ▶ if (_isNan($pdp_new[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_new[$j])) { } 3 37 40
1091 3 $pre_unknown=$pre_int;
1092 } else {
1093 #print $rrd->{ds}[$j]->{pdp_prep}->{val}," ";
1094 37 ▶ if (_isNan($rrd->{ds}[$j]->{pdp_prep}->{val})) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($rrd->{"ds"}[$j]{"pdp_prep"}{"val"})) 2 35 37
1095 2 $rrd->{ds}[$j]->{pdp_prep}->{val} = 0;
1096 }
1097 37 $rrd->{ds}[$j]->{pdp_prep}->{val} += $pdp_new[$j]/$interval * $pre_int;
1098 }
1099 #print $pdp_new[$j]," ",$interval," ",$pre_int," ",$rrd->{ds}[$j]->{pdp_prep}->{val},"\n";
1100 40 ▶ if ($interval > $rrd->{ds}[$j]->{hb} || $rrd->{pdp_step}/2.0 < $rrd->{ds}[$j]->{pdp_prep}->{unkn_sec_cnt}+$pre_unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interval > $rrd->{'ds'}[$j]{'hb'} or $rrd->{'pdp_step'} / 2.0 < $rrd->{'ds'}[$j]{'pdp_prep'}{'unkn_sec_cnt'} + $pre_unknown) { } 5 35 40
Condition66%2/3
$interval > $rrd->{"ds"}[$j]{"hb"} or $rrd->{"pdp_step"} / 2 < $rrd->{"ds"}[$j]{"pdp_prep"}{"unkn_sec_cnt"} + $pre_unknown
l!l&&r!l&&!r
0535
MC/DC50%1/2
$interval > $rrd->{"ds"}[$j]{"hb"} or $rrd->{"pdp_step"} / 2 < $rrd->{"ds"}[$j]{"pdp_prep"}{"unkn_sec_cnt"} + $pre_unknown
$interval > $rrd->{"ds"}[$j]{"hb"} $rrd->{"pdp_step"} / 2 < $rrd->{"ds"}[$j]{"pdp_prep"}{"unkn_sec_cnt"} + $pre_unknown
Truth table66%2/3
A or B
A= $interval > $rrd->{"ds"}[$j]{"hb"}B= $rrd->{"pdp_step"} / 2 < $rrd->{"ds"}[$j]{"pdp_prep"}{"unkn_sec_cnt"} + $pre_unknown
ABresult
1X1
011
000
1101 5 $pdp_temp[$j]=NAN;
1102 } else {
1103 35 $pdp_temp[$j]=$rrd->{ds}[$j]->{pdp_prep}->{val}/($elapsed_pdp_st*$rrd->{pdp_step}-$rrd->{ds}[$j]->{pdp_prep}->{unkn_sec_cnt}-$pre_unknown);
1104 }
1105 #print $pdp_new[$j]," ",$pdp_temp[$j]," ",$rrd->{ds}[$j]->{pdp_prep}->{val}," ", $elapsed_pdp_st-$rrd->{ds}[$j]->{pdp_prep}->{unkn_sec_cnt}-$pre_unknown,"\n";
1106 40 ▶ if (_isNan($pdp_new[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_new[$j])) { } 3 37 40
1107 3 $rrd->{ds}[$j]->{pdp_prep}->{unkn_sec_cnt} = int($post_int);
1108 3 $rrd->{ds}[$j]->{pdp_prep}->{val}=NAN;
1109 } else {
1110 37 $rrd->{ds}[$j]->{pdp_prep}->{unkn_sec_cnt} = 0;
1111 37 $rrd->{ds}[$j]->{pdp_prep}->{val}=$pdp_new[$j]/$interval*$post_int;
1112 #print $pdp_new[$j]," ", $interval, " ", $post_int, " ",$rrd->{ds}[$j]->{pdp_prep}->{val},"\n";
1113 }
1114 }
1115 # update_all_cdp_prep. Iterate over all the RRAs for a given DS and update the CDP
1116 10 my $current_cf; my $start_pdp_offset; my @rra_step_cnt;
1117 10 my $cum_val; my $cur_val; my $pdp_into_cdp_cnt; my $ii;
1118 10 my $idx=$self->_get_header_size; # file position (used by in place updates)
1119 10 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 50 10 60
1120 50 $start_pdp_offset = $rrd->{rra}[$ii]->{pdp_cnt} - $proc_pdp_cnt % $rrd->{rra}[$ii]->{pdp_cnt};
1121 50 ▶ if ($start_pdp_offset <= $elapsed_pdp_st) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_pdp_offset <= $elapsed_pdp_st) { } 30 20 50
1122 30 $rra_step_cnt[$ii] = int(($elapsed_pdp_st - $start_pdp_offset)/$rrd->{rra}[$ii]->{pdp_cnt}) + 1;
1123 } else {
1124 20 $rra_step_cnt[$ii] = 0;
1125 }
1126 # update_cdp_prep. update CDP_PREP areas, loop over data sources within each RRA
1127 50 ▶ for ($j=0; $j<$rrd->{ds_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"ds_cnt"}) 200 50 250
1128 200 ▶ if ($rrd->{rra}[$ii]->{pdp_cnt} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{'rra'}[$ii]{'pdp_cnt'} > 1) { } 160 40 200
1129 # update_cdp. Given the new reading (pdp_temp_val), update or initialize the CDP value, primary value, secondary value, and # of unknowns.
1130 160 ▶ if ($rra_step_cnt[$ii]>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rra_step_cnt[$ii] > 0) { } 80 80 160
1131 80 ▶ if (_isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_temp[$j])) { } 16 64 80
1132 16 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[UNKN_PDP_CNT] +=$start_pdp_offset;
1133 16 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[SECONDARY_VAL] = NAN;
1134 } else {
1135 64 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[SECONDARY_VAL] = $pdp_temp[$j];
1136 }
1137 80 ▶ if ($rrd->{rra}[$ii]->{cdp_prep}[$j]->[UNKN_PDP_CNT] > $rrd->{rra}[$ii]->{pdp_cnt}*$rrd->{rra}[$ii]->{xff}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{'rra'}[$ii]{'cdp_prep'}[$j][1] > $rrd->{'rra'}[$ii]{'pdp_cnt'} * $rrd->{'rra'}[$ii]{'xff'}) { } 16 64 80
1138 16 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = NAN;
1139 } else {
1140 #initialize_cdp_val
1141 64 ▶ if ($rrd->{rra}[$ii]->{name} eq "AVERAGE") {
Branch: 3 branches
BranchTrueFalseTotal
if ($rrd->{'rra'}[$ii]{'name'} eq 'AVERAGE') { } 16 48 64
elsif ($rrd->{'rra'}[$ii]{'name'} eq 'MAX') { } 16 32 48
elsif ($rrd->{'rra'}[$ii]{'name'} eq 'MIN') { } 16 16 32
1142 16 ▶ if (_isNan($rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($rrd->{'rra'}[$ii]{'cdp_prep'}[$j][0])) { } 0 16 16
1143 0 $cum_val=0.0;
1144 } else {
1145 16 $cum_val = $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL];
1146 }
1147 16 ▶ if (_isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_temp[$j])) { } 0 16 16
1148 0 $cur_val=0.0;
1149 } else {
1150 16 $cur_val = $pdp_temp[$j];
1151 }
1152 16 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = ($cum_val+$cur_val*$start_pdp_offset)/($rrd->{rra}[$ii]->{pdp_cnt}-$rrd->{rra}[$ii]->{cdp_prep}[$j]->[UNKN_PDP_CNT]);
1153 } elsif ($rrd->{rra}[$ii]->{name} eq "MAX") {
1154 16 ▶ if (_isNan($rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($rrd->{'rra'}[$ii]{'cdp_prep'}[$j][0])) { } 0 16 16
1155 0 $cum_val=-1 * INF;
1156 } else {
1157 16 $cum_val = $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL];
1158 }
1159 16 ▶ if (_isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_temp[$j])) { } 0 16 16
1160 0 $cur_val=-1 * INF;
1161 } else {
1162 16 $cur_val = $pdp_temp[$j];
1163 }
1164 16 ▶ if ($cur_val > $cum_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_val > $cum_val) { } 8 8 16
1165 8 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = $cur_val;
1166 } else {
1167 8 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = $cum_val;
1168 }
1169 } elsif ($rrd->{rra}[$ii]->{name} eq "MIN") {
1170 16 ▶ if (_isNan($rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($rrd->{'rra'}[$ii]{'cdp_prep'}[$j][0])) { } 0 16 16
1171 0 $cum_val=INF;
1172 } else {
1173 16 $cum_val = $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL];
1174 }
1175 16 ▶ if (_isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_temp[$j])) { } 0 16 16
1176 0 $cur_val=INF;
1177 } else {
1178 16 $cur_val = $pdp_temp[$j];
1179 }
1180 16 ▶ if ($cur_val < $cum_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_val < $cum_val) { } 8 8 16
1181 8 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = $cur_val;
1182 } else {
1183 8 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = $cum_val;
1184 }
1185 } else {
1186 16 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = $pdp_temp[$j];
1187 }
1188 }
1189 #*cdp_val = initialize_carry_over
1190 80 $pdp_into_cdp_cnt=($elapsed_pdp_st - $start_pdp_offset) % $rrd->{rra}[$ii]->{pdp_cnt};
1191 80 ▶ if ($pdp_into_cdp_cnt == 0 || _isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdp_into_cdp_cnt == 0 or _isNan($pdp_temp[$j])) { } 80 0 80
Condition33%1/3
$pdp_into_cdp_cnt == 0 or _isNan($pdp_temp[$j])
l!l&&r!l&&!r
8000
MC/DC0%0/2
$pdp_into_cdp_cnt == 0 or _isNan($pdp_temp[$j])
$pdp_into_cdp_cnt == 0 _isNan($pdp_temp[$j])
Truth table33%1/3
A or B
A= $pdp_into_cdp_cnt == 0B= _isNan($pdp_temp[$j])
ABresult
1X1
011
000
1192 80 ▶ if ($rrd->{rra}[$ii]->{name} eq "MAX") {
Branch: 3 branches
BranchTrueFalseTotal
if ($rrd->{'rra'}[$ii]{'name'} eq 'MAX') { } 20 60 80
elsif ($rrd->{'rra'}[$ii]{'name'} eq 'MIN') { } 20 40 60
elsif ($rrd->{'rra'}[$ii]{'name'} eq 'AVERAGE') { } 20 20 40
1193 20 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=-1 * INF;
1194 } elsif ($rrd->{rra}[$ii]->{name} eq "MIN") {
1195 20 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=INF;
1196 } elsif ($rrd->{rra}[$ii]->{name} eq "AVERAGE") {
1197 20 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=0;
1198 } else {
1199 20 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=NAN;
1200 }
1201 } else {
1202 0 ▶ if ($rrd->{rra}[$ii]->{name} eq "AVERAGE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{'rra'}[$ii]{'name'} eq 'AVERAGE') { } 0 0 0
1203 0 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=$pdp_temp[$j]*$pdp_into_cdp_cnt;
1204 } else {
1205 0 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=$pdp_temp[$j];
1206 }
1207 }
1208 80 ▶ if (_isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_temp[$j])) { } 16 64 80
1209 16 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[UNKN_PDP_CNT] = ($elapsed_pdp_st - $start_pdp_offset) % $rrd->{rra}[$ii]->{pdp_cnt};
1210 } else {
1211 64 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[UNKN_PDP_CNT] = 0;
1212 }
1213 } else {
1214 80 ▶ if (_isNan($pdp_temp[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($pdp_temp[$j])) { } 4 76 80
1215 4 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[UNKN_PDP_CNT] += $elapsed_pdp_st;
1216 } else {
1217 #*cdp_val =calculate_cdp_val
1218 76 ▶ if (_isNan($rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isNan($rrd->{'rra'}[$ii]{'cdp_prep'}[$j][0])) { } 19 57 76
1219 19 ▶ if ($rrd->{rra}[$ii]->{name} eq "AVERAGE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"rra"}[$ii]{"name"} eq "AVERAGE") 0 19 19
1220 0 $pdp_temp[$j] *= $elapsed_pdp_st;
1221 }
1222 19 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]=$pdp_temp[$j];
1223 } else {
1224 57 ▶ if ($rrd->{rra}[$ii]->{name} eq "AVERAGE") {
Branch: 3 branches
BranchTrueFalseTotal
if ($rrd->{'rra'}[$ii]{'name'} eq 'AVERAGE') { } 19 38 57
elsif ($rrd->{'rra'}[$ii]{'name'} eq 'MIN') { } 19 19 38
elsif ($rrd->{'rra'}[$ii]{'name'} eq 'MAX') { } 19 0 19
1225 19 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]+=$pdp_temp[$j]*$elapsed_pdp_st;
1226 } elsif ($rrd->{rra}[$ii]->{name} eq "MIN") {
1227 19 ▶ if ($pdp_temp[$j] < $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdp_temp[$j] < $rrd->{"rra"}[$ii]{"cdp_prep"}[$j][0]) 19 0 19
1228 19 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL] = $pdp_temp[$j];
1229 }
1230 } elsif ($rrd->{rra}[$ii]->{name} eq "MAX") {
1231 19 ▶ if ($pdp_temp[$j] > $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pdp_temp[$j] > $rrd->{"rra"}[$ii]{"cdp_prep"}[$j][0]) 19 0 19
1232 19 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL] = $pdp_temp[$j];
1233 }
1234 } else {
1235 0 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[VAL] = $pdp_temp[$j];
1236 }
1237 }
1238 }
1239 }
1240 } else {
1241 # Nothing to consolidate if there's one PDP per CDP
1242 40 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[PRIMARY_VAL] = $pdp_temp[$j];
1243 40 ▶ if ($elapsed_pdp_st > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elapsed_pdp_st > 1) 0 40 40
1244 0 $rrd->{rra}[$ii]->{cdp_prep}[$j]->[SECONDARY_VAL] = $pdp_temp[$j];
1245 }
1246 # consolidated with update_aberrant_cdps
1247 }
1248 } # $j ds_cnt
1249 # write to RRA
1250 50 ▶ for (my $scratch_idx=PRIMARY_VAL; $rra_step_cnt[$ii] >0; $rra_step_cnt[$ii]--, $scratch_idx=SECONDARY_VAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rra_step_cnt[$ii] > 0) 30 50 80
1251 30 $rrd->{rra}[$ii]->{ptr} = ($rrd->{rra}[$ii]->{ptr}+1) % $rrd->{rra}[$ii]->{row_cnt};
1252 #write_RRA_row
1253 30 my @line;
1254 30 ▶ for ($j=0; $j<$rrd->{ds_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"ds_cnt"}) 120 30 150
1255 120 push(@line, $rrd->{rra}[$ii]->{cdp_prep}[$j]->[$scratch_idx]);
1256 }
1257 30 ▶ if ($inplace eq "memory") {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace eq 'memory') { } 30 0 30
1258 30 $rrd->{rra}[$ii]->{data}[$rrd->{rra}[$ii]->{ptr}] = $self->_packd(\@line);
1259 } else {
1260 # update in place
1261 0 seek $fd,$idx+$rrd->{rra}[$ii]->{ptr}*$rrd->{ds_cnt}*$self->{FLOAT_EL_SIZE},0;
1262 0 print $fd $self->_packd(\@line);
1263 }
1264 # rrd_notify_row
1265 }
1266 50 $idx+=$rrd->{rra}[$ii]->{row_cnt}*$rrd->{ds_cnt}*$self->{FLOAT_EL_SIZE}; # step file pointer to start of next RRA
1267 } # $ii rra_cnt
1268 } # complex update
1269 10 $rrd->{last_up}=$current_time;
1270 } # args
1271 10 ▶ if ($inplace eq "file") {
Branch: 1 branch
BranchTrueFalseTotal
if ($inplace eq "file") 0 10 10
1272 # update header
1273 0 seek $fd,0,0;
1274 #print $fd $self->getheader();
1275 0 $self->_saveheader($fd);
1276 #close($fd);
1277 }
1278 10 return 1;
1279 }
1280  
1281 1 ▶sub fetch {
Subroutine fetch: called CC=23 SCAR=33.8
Pod documented
1282 # dump out measurement data
1283 1 my ($self, $args_str) = @_; my $rrd=$self->{rrd};
1284 1 my $out='';
1285
1286 1 my $step=$rrd->{pdp_step}; my $start=time()-24*60*60; my $end=time(); my $digits=10; # number of digits printed for floats
1287 1 my $ret; my $args;
1288 1 ($ret, $args) = _GetOptionsFromString($args_str,
1289 "resolution|r:i" => \$step,
1290 "start|s:i" => \$start,
1291 "end|e:i" => \$end,
1292 "digits|d:i" => \$digits
1293 );
1294 # at the moment, start/end times are unix timestamps.
1295 0 ▶ if ($start < 3600 * 24 * 365 * 10) {croak("the first entry to fetch should be after 1980");}
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 315360000) 0 1 1
1296 0 ▶ if ($end < $start) {croak("start ($start) should be less than end ($end)");}
Branch: 1 branch
BranchTrueFalseTotal
if ($end < $start) 0 1 1
1297 0 ▶ if ($step<1) {croak("step must be >= 1 second");}
Branch: 1 branch
BranchTrueFalseTotal
if ($step < 1) 0 1 1
1298 1 my $cf=uc($args->[0]); my $i; # so CF must be first word in argument line
1299 0 ▶ if ($cf !~ m/AVERAGE|MIN|MAX|LAST/) {croak("unknown CF\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($cf =~ /AVERAGE|MIN|MAX|LAST/) 0 1 1
1300
1301 # find the RRA which best matches the requirements
1302 1 my $cal_end; my $cal_start; my $step_diff; my $firstfull=1; my $firstpart=1;
1303 1 my $full_match=$end-$start;
1304 1 my $best_full_step_diff=0; my $best_full_rra; my $best_match=0;
1305 1 my $best_part_step_diff=0; my $best_part_rra;
1306 my $tmp_match;
1307 1 ▶ for ($i = 0; $i < $rrd->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra_cnt"}) 5 1 6
1308 5 ▶ if ($rrd->{rra}[$i]->{name} eq $cf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"rra"}[$i]{"name"} eq $cf) 2 3 5
1309 2 $cal_end=$rrd->{last_up} - $rrd->{last_up}%($rrd->{rra}[$i]->{pdp_cnt}*$rrd->{pdp_step});
1310 2 $cal_start=$cal_end - $rrd->{rra}[$i]->{pdp_cnt}*$rrd->{rra}[$i]->{row_cnt}*$rrd->{pdp_step};
1311 2 $step_diff = $step-$rrd->{pdp_step}*$rrd->{rra}[$i]->{pdp_cnt};
1312 2 ▶ if ($step_diff<0) {$step_diff=-$step_diff;} # take absolute value
Branch: 1 branch
BranchTrueFalseTotal
if ($step_diff < 0) 1 1 2
1313 2 ▶ if ($cal_start <= $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cal_start <= $start) { } 1 1 2
1314 1 ▶ if ($firstfull || $step_diff < $best_full_step_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstfull or $step_diff < $best_full_step_diff) 1 0 1
Condition33%1/3
$firstfull or $step_diff < $best_full_step_diff
l!l&&r!l&&!r
100
MC/DC0%0/2
$firstfull or $step_diff < $best_full_step_diff
$firstfull $step_diff < $best_full_step_diff
Truth table33%1/3
A or B
A= $firstfullB= $step_diff < $best_full_step_diff
ABresult
1X1
011
000
1315 1 $firstfull=0; $best_full_step_diff = $step_diff; $best_full_rra=$i;
1316 }
1317 } else {
1318 1 $tmp_match = $full_match;
1319 1 ▶ if ($cal_start>$start) {$tmp_match-=($cal_start-$start);}
Branch: 1 branch
BranchTrueFalseTotal
if ($cal_start > $start) 1 0 1
1320 1 ▶ if ($firstpart || ($best_match<$tmp_match && $step_diff < $best_part_step_diff)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstpart or $best_match < $tmp_match and $step_diff < $best_part_step_diff) 1 0 1
Condition16%1/6
$firstpart or $best_match < $tmp_match and $step_diff < $best_part_step_diff
l!l&&r!l&&!r
100
$best_match < $tmp_match and $step_diff < $best_part_step_diff
!ll&&!rl&&r
000
MC/DC0%0/3
$firstpart or $best_match < $tmp_match and $step_diff < $best_part_step_diff
$firstpart $best_match < $tmp_match $step_diff < $best_part_step_diff
Truth table25%1/4
A or B and C
A= $firstpartB= $best_match < $tmp_matchC= $step_diff < $best_part_step_diff
ABCresult
1XX1
0111
00X0
0100
1321 1 $firstpart=0; $best_match=$tmp_match; $best_part_step_diff=$step_diff; $best_part_rra=$i;
1322 }
1323 }
1324 }
1325 }
1326 1 my $chosen_rra; my @line;
1327 1 ▶ if ($firstfull == 0) {$chosen_rra=$best_full_rra;}
Branch: 2 branches
BranchTrueFalseTotal
if ($firstfull == 0) { } 1 0 1
elsif ($firstpart == 0) { } 0 0 0
1328 0 elsif ($firstpart==0) {$chosen_rra=$best_part_rra;}
1329 0 else {croak("the RRD does not contain an RRA matching the chosen CF");}
1330 1 $step = $rrd->{rra}[$chosen_rra]->{pdp_cnt}*$rrd->{pdp_step};
1331 1 $start -= $start % $step;
1332 1 $end += ($step - $end % $step);
1333  
1334 # load RRA data, if not already loaded
1335 0 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 0 1 1
1336  
1337 # output column headings
1338 1 $out.=sprintf "%12s"," ";
1339 1 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 4 1 5
1340 4 $out.=sprintf "%-17s", $rrd->{ds}[$i]->{name};
1341 }
1342 1 $out.=sprintf "%s", "\n";
1343 1 my $t = $rrd->{last_up} - $rrd->{last_up}%($rrd->{rra}[$chosen_rra]->{pdp_cnt}*$rrd->{pdp_step}) -($rrd->{rra}[$chosen_rra]->{row_cnt}-1)*$rrd->{rra}[$chosen_rra]->{pdp_cnt}*$rrd->{pdp_step};
1344 1 my $jj; my $j;
1345 1 ▶ for ($j=0; $j<$rrd->{rra}[$chosen_rra]->{row_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"rra"}[$chosen_rra]{"row_cnt"}) 5 1 6
1346 5 ▶ if ($t > $start && $t <= $end+$step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t > $start and $t <= $end + $step) 5 0 5
Condition33%1/3
$t > $start and $t <= $end + $step
!ll&&!rl&&r
005
MC/DC0%0/2
$t > $start and $t <= $end + $step
$t > $start $t <= $end + $step
Truth table33%1/3
A and B
A= $t > $startB= $t <= $end + $step
ABresult
0X0
100
111
1347 5 $out.=sprintf "%10u: ",$t;
1348 5 $jj= ($rrd->{rra}[$chosen_rra]->{ptr}+1 + $j)%$rrd->{rra}[$chosen_rra]->{row_cnt};
1349 5 @line=$self->_unpackd($rrd->{rra}[$chosen_rra]->{data}[$jj]);
1350 5 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 20 5 25
1351 20 $out.=sprintf "%-17s",_strfloat($line[$i],$digits);
1352 }
1353 5 $out.=sprintf "%s", "\n";
1354 }
1355 5 $t+=$step;
1356 }
1357 1 return $out;
1358 }
1359  
1360 1 ▶sub info {
Subroutine info: called CC=9 SCAR=23.4
Pod documented
1361 # dump out header info
1362 1 my $self=$_[0]; my $rrd = $self->{rrd};
1363 1 my $out='';
1364
1365 1 my $digits=10; my $noencoding=0;
1366 1 ▶ if (defined($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 1 0 1
1367 1 my $ret; my $args;
1368 1 ($ret, $args) = _GetOptionsFromString($_[1],
1369 "digits|d:i" => \$digits,
1370 "noformat|n" => \$noencoding
1371 );
1372 }
1373
1374 1 $out.=sprintf "%s", "rrd_version = ".$rrd->{version}."\n";
1375 1 ▶ if ($noencoding<0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noencoding < "0.5") 0 1 1
1376 0 $out.=sprintf "%s", "encoding = ";
1377 0 ▶ if ($self->{encoding} eq "native-double-simple" || $self->{encoding} eq "native-double-mixed") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'encoding'} eq 'native-double-simple' or $self->{'encoding'} eq 'native-double-mixed') { } 0 0 0
elsif ($self->{'encoding'} =~ /double/) { } 0 0 0
Condition0%0/3
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
$self->{"encoding"} eq "native-double-simple" $self->{"encoding"} eq "native-double-mixed"
Truth table0%0/3
A or B
A= $self->{"encoding"} eq "native-double-simple"B= $self->{"encoding"} eq "native-double-mixed"
ABresult
1X1
011
000
1378 0 $out.="native-double";
1379 } elsif ($self->{encoding} =~ /double/) {
1380 0 $out.="portable-double";
1381 } else {
1382 0 $out.="portable-single";
1383 }
1384 0 $out.=" (".$self->{encoding}.")\n";
1385 }
1386 1 $out.=sprintf "%s", "step = ".$rrd->{pdp_step}."\n";
1387 1 $out.=sprintf "%s", "last_update = ".int($rrd->{last_up})."\n";
1388 1 my $i; my $str; my $ii;
1389 1 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 4 1 5
1390 4 $str="ds[".$rrd->{ds}[$i]->{name}."]";
1391 4 $out.=sprintf "%s", "$str.index = ".$i."\n";
1392 4 $out.=sprintf "%s", "$str.type = \"".$rrd->{ds}[$i]->{type}."\"\n";
1393 4 $out.=sprintf "%s", "$str.minimal_heartbeat = ".$rrd->{ds}[$i]->{hb}."\n";
1394 4 $out.=sprintf "%s.min = %s\n",$str,_strint($rrd->{ds}[$i]->{min});
1395 4 $out.=sprintf "%s.max = %s\n",$str,_strint($rrd->{ds}[$i]->{max});
1396 4 $out.=sprintf "%s", "$str.last_ds = \"".$rrd->{ds}[$i]->{pdp_prep}->{last_ds}."\"\n";
1397 4 $out.=sprintf "%s.value = %s\n",$str,_strfloat($rrd->{ds}[$i]->{pdp_prep}->{val}, $digits);
1398 4 $out.=sprintf "%s", "$str.unknown_sec = ".$rrd->{ds}[$i]->{pdp_prep}->{unkn_sec_cnt}."\n";
1399 }
1400 1 ▶ for ($i=0; $i<$rrd->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra_cnt"}) 5 1 6
1401 5 $str="rra[$i]";
1402 5 $out.=sprintf "%s", "$str.cf = \"".$rrd->{rra}[$i]->{name}."\"\n";
1403 5 $out.=sprintf "%s", "$str.rows = ".$rrd->{rra}[$i]->{row_cnt}."\n";
1404 5 $out.=sprintf "%s", "$str.cur_row = ".$rrd->{rra}[$i]->{ptr}."\n";
1405 5 $out.=sprintf "%s", "$str.pdp_per_row = ".$rrd->{rra}[$i]->{pdp_cnt}."\n";
1406 5 $out.=sprintf "%s.xff = %s\n",$str,_strfloat($rrd->{rra}[$i]->{xff},$digits);
1407 5 ▶ for ($ii=0; $ii<$rrd->{ds_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"ds_cnt"}) 20 5 25
1408 20 $out.=sprintf "%s.cdp_prep[$ii].value = %s\n",$str,_strfloat($rrd->{rra}[$i]->{cdp_prep}[$ii]->[VAL],$digits);
1409 20 $out.=sprintf "%s", "$str.cdp_prep[$ii].unknown_datapoints = ".$rrd->{rra}[$i]->{cdp_prep}[$ii]->[UNKN_PDP_CNT]."\n";
1410 }
1411 }
1412 1 return $out;
1413 }
1414  
1415 #sub xport {
1416 # # TO DO, incl JSON format
1417 # my ($self, $args_str) = @_; my $rrd=$self->{rrd};
1418 #}
1419  
1420 4 ▶sub dump {
Subroutine dump: called CC=12 SCAR=24.9
Pod documented
1421 # XML dump of RRD file
1422 4 my ($self, $args_str) = @_; my $rrd=$self->{rrd};
1423  
1424 4 my $noheader=0; my $notimecomments=0; my $digits=10;
1425 4 ▶ if (defined($args_str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_str) 4 0 4
1426 4 my $ret; my $args;
1427 4 ($ret, $args) = _GetOptionsFromString($args_str,
1428 "no-header|n" => \$noheader,
1429 "notimecomments|t" => \$notimecomments,
1430 "digits|d:i" => \$digits
1431 );
1432 }
1433 4 ▶ my $timecomments = $notimecomments>0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$notimecomments > 0 ? : 4 0 4
1434
1435 # load RRA data, if not already loaded
1436 4 ▶ if (!defined($rrd->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rrd->{"dataloaded"}) 1 3 4
1437  
1438 4 my $out=''; my @line;
1439
1440 4 ▶ if ($noheader<1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($noheader < 1) 4 0 4
1441 4 $out.=sprintf "%s", '<?xml version="1.0" encoding="utf-8"?>'."\n";
1442 4 $out.=sprintf "%s", '<!DOCTYPE rrd SYSTEM "http://oss.oetiker.ch/rrdtool/rrdtool.dtd">'."\n";
1443 }
1444 4 $out.=sprintf "%s", "<!-- Round Robin Database Dump -->\n<rrd>\n\t<version>".$rrd->{version}."</version>\n";
1445 4 $out.=sprintf "%s", "\t<step>".$rrd->{pdp_step}."</step> <!-- Seconds -->\n\t<lastupdate>".$rrd->{last_up}."</lastupdate>";
1446 0 ▶ if ($timecomments) {$out.=" <!-- ".strftime("%Y-%m-%d %T %Z",localtime($rrd->{last_up})). "-->";}
Branch: 1 branch
BranchTrueFalseTotal
if ($timecomments) 0 4 4
1447 4 $out.="\n\t";
1448 4 my $i; my $ii; my $j; my $jj; my $t; my $val;
1449 4 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 16 4 20
1450 16 $out.=sprintf "%s", "\n\t<ds>\n\t\t<name>".$rrd->{ds}[$i]->{name}."</name>\n\t\t";
1451 16 $out.=sprintf "%s", "<type>".$rrd->{ds}[$i]->{type}."</type>\n\t\t";
1452 16 $out.=sprintf "%s", "<minimal_heartbeat>".$rrd->{ds}[$i]->{hb}."</minimal_heartbeat>\n\t\t";
1453 16 $out.=sprintf "<min>%s</min>\n\t\t<max>%s</max>\n\t\t",_strint($rrd->{ds}[$i]->{min}),_strint($rrd->{ds}[$i]->{max});
1454 16 $out.=sprintf "%s", "\n\t\t<!-- PDP Status -->\n\t\t<last_ds>".$rrd->{ds}[$i]->{pdp_prep}->{last_ds}."</last_ds>\n\t\t";
1455 16 $out.=sprintf "<value>%s</value>\n\t\t",_strfloat($rrd->{ds}[$i]->{pdp_prep}->{val},$digits);
1456 16 $out.=sprintf "%s", "<unknown_sec>".$rrd->{ds}[$i]->{pdp_prep}->{unkn_sec_cnt}."</unknown_sec>\n\t";
1457 16 $out.=sprintf "%s", "</ds>\n";
1458 }
1459 4 $out.=sprintf "%s", "\n\t<!-- Round Robin Archives -->\n";
1460 4 ▶ for ($i=0; $i<$rrd->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra_cnt"}) 20 4 24
1461 20 $out.=sprintf "%s", "\t<rra>\n\t\t";
1462 20 $out.=sprintf "%s", "<cf>".$rrd->{rra}[$i]->{name}."</cf>\n\t\t";
1463 20 $out.=sprintf "%s", "<pdp_per_row>".$rrd->{rra}[$i]->{pdp_cnt}."</pdp_per_row> <!-- ".$rrd->{rra}[$i]->{pdp_cnt}*$rrd->{pdp_step}." seconds -->\n\n\t\t";
1464 20 $out.=sprintf "<params>\n\t\t<xff>%s</xff>\n\t\t</params>\n\t\t",_strfloat($rrd->{rra}[$i]->{xff},$digits);
1465 20 $out.=sprintf "%s", "<cdp_prep>\n\t\t";
1466 20 ▶ for ($ii=0; $ii<$rrd->{ds_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"ds_cnt"}) 80 20 100
1467 80 $out.=sprintf "\t<ds>\n\t\t\t<primary_value>%s</primary_value>\n\t\t\t", _strfloat($rrd->{rra}[$i]->{cdp_prep}[$ii]->[PRIMARY_VAL],$digits);
1468 80 $out.=sprintf "<secondary_value>%s</secondary_value>\n\t\t\t", _strfloat($rrd->{rra}[$i]->{cdp_prep}[$ii]->[SECONDARY_VAL],$digits);
1469 80 $out.=sprintf "<value>%s</value>\n\t\t\t",_strfloat($rrd->{rra}[$i]->{cdp_prep}[$ii]->[VAL],$digits);
1470 80 $out.=sprintf "%s", "<unknown_datapoints>". $rrd->{rra}[$i]->{cdp_prep}[$ii]->[UNKN_PDP_CNT]."</unknown_datapoints>\n\t\t\t";
1471 80 $out.=sprintf "%s", "</ds>\n\t\t";
1472 }
1473 20 $out.=sprintf "%s", "</cdp_prep>\n\t\t";
1474 20 $out.=sprintf "%s", "<database>\n\t\t";
1475 20 $t = $rrd->{last_up} - $rrd->{last_up}%($rrd->{rra}[$i]->{pdp_cnt}*$rrd->{pdp_step}) -($rrd->{rra}[$i]->{row_cnt}-1)*$rrd->{rra}[$i]->{pdp_cnt}*$rrd->{pdp_step};
1476 20 ▶ for ($j=0; $j<$rrd->{rra}[$i]->{row_cnt}; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rrd->{"rra"}[$i]{"row_cnt"}) 120 20 140
1477 120 $jj= ($rrd->{rra}[$i]->{ptr}+1 + $j)%$rrd->{rra}[$i]->{row_cnt};
1478 0 ▶ if ($timecomments) {$out.=sprintf "\t%s", "<!-- ".strftime("%Y-%m-%d %T %Z",localtime($t)). " / $t --> ";}
Branch: 1 branch
BranchTrueFalseTotal
if ($timecomments) 0 120 120
1479 120 $out.="<row>";
1480 120 @line=$self->_unpackd($rrd->{rra}[$i]->{data}[$jj]);
1481 120 ▶ for ($ii=0; $ii<$rrd->{ds_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"ds_cnt"}) 480 120 600
1482 480 $out.=sprintf "<v>%s</v>",_strfloat($line[$ii],$digits);
1483 }
1484 120 $out.=sprintf "%s", "</row>\n\t\t";
1485 120 $t+=$rrd->{rra}[$i]->{pdp_cnt}*$rrd->{pdp_step};
1486 }
1487 20 $out.=sprintf "%s", "</database>\n\t";
1488 20 $out.=sprintf "%s", "</rra>\n";
1489 }
1490 4 $out.=sprintf "%s", "</rrd>\n";
1491 4 return $out;
1492 }
1493  
1494 ####
1495 3 ▶sub _saveheader {
Subroutine _saveheader: called CC=10 SCAR=23.3
1496 # construct binary header for RRD file
1497 3 my $self=$_[0];
1498 3 my $fd=$_[1];
1499  
1500 3 my $L=$self->_packlongchar();
1501 3 my $header="\0"x $self->_get_header_size; # preallocate header
1502 3 substr($header,0,9,"RRD\0".$self->{rrd}->{version});
1503 3 substr($header,$self->{OFFSET},$self->{FLOAT_EL_SIZE}+3*$self->{LONG_EL_SIZE}, $self->{COOKIE}.pack("$L $L $L",$self->{rrd}->{ds_cnt}, $self->{rrd}->{rra_cnt}, $self->{rrd}->{pdp_step}));
1504 # DS defs
1505 3 my $idx=$self->{STAT_HEADER_SIZE0};
1506 3 ▶ for (my $i=0; $i<$self->{rrd}->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"rrd"}{"ds_cnt"}) 12 3 15
1507 substr($header,$idx,40+$self->{FLOAT_EL_SIZE},pack("Z20 Z20 $L x".$self->{DIFF_SIZE},
1508 12 $self->{rrd}->{ds}[$i]->{name}, $self->{rrd}->{ds}[$i]->{type}, $self->{rrd}->{ds}[$i]->{hb}));
1509 12 $idx+=40+$self->{FLOAT_EL_SIZE};
1510 12 my @minmax=($self->{rrd}->{ds}[$i]->{min}, $self->{rrd}->{ds}[$i]->{max});
1511 12 substr($header,$idx,2*$self->{FLOAT_EL_SIZE},$self->_packd(\@minmax));
1512 12 $idx+=9*$self->{FLOAT_EL_SIZE};
1513 }
1514 # RRA defs
1515 3 my $i;
1516 3 ▶ for ($i=0; $i<$self->{rrd}->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"rrd"}{"rra_cnt"}) 15 3 18
1517 15 substr($header,$idx,20+$self->{RRA_DEL_PAD}+2*$self->{LONG_EL_SIZE},pack("Z".(20+$self->{RRA_DEL_PAD})." $L $L",$self->{rrd}->{rra}[$i]->{name}, $self->{rrd}->{rra}[$i]->{row_cnt}, $self->{rrd}->{rra}[$i]->{pdp_cnt}));
1518 15 $idx+=20+$self->{RRA_DEL_PAD}+2*$self->{LONG_EL_SIZE};
1519 15 my @xff=($self->{rrd}->{rra}[$i]->{xff});
1520 15 substr($header,$idx+$self->{RRA_PAD},$self->{FLOAT_EL_SIZE},$self->_packd(\@xff));
1521 15 $idx += $self->{FLOAT_EL_SIZE}*10+$self->{RRA_PAD};
1522 }
1523 # live header
1524 3 substr($header,$idx,2*$self->{LONG_EL_SIZE},pack("$L $L", $self->{rrd}->{last_up},0));
1525 3 $idx+= 2*$self->{LONG_EL_SIZE};
1526 # PDP_PREP
1527 3 ▶ for ($i=0; $i<$self->{rrd}->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"rrd"}{"ds_cnt"}) 12 3 15
1528 substr($header,$idx,30+$self->{PDP_PREP_PAD}+$self->{FLOAT_EL_SIZE},
1529 12 pack("Z".(30+$self->{PDP_PREP_PAD})." $L x".$self->{DIFF_SIZE},$self->{rrd}->{ds}[$i]->{pdp_prep}->{last_ds}, $self->{rrd}->{ds}[$i]->{pdp_prep}->{unkn_sec_cnt}));
1530 12 $idx+=30+$self->{PDP_PREP_PAD}+$self->{FLOAT_EL_SIZE};
1531 12 my @val=($self->{rrd}->{ds}[$i]->{pdp_prep}->{val});
1532 12 substr($header,$idx,$self->{FLOAT_EL_SIZE},$self->_packd(\@val));
1533 12 $idx+= $self->{FLOAT_EL_SIZE}*9;
1534 }
1535 # CDP_PREP
1536 3 my @val; my $ii;
1537 3 ▶ for (my $ii=0; $ii<$self->{rrd}->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $self->{"rrd"}{"rra_cnt"}) 15 3 18
1538 15 ▶ for ($i=0; $i<$self->{rrd}->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"rrd"}{"ds_cnt"}) 60 15 75
1539 # do a bit of code optimisation here
1540 60 ▶ if ($self->{encoding} eq "native-double-simple" || $self->{encoding} eq "native-double-mixed") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'encoding'} eq 'native-double-simple' or $self->{'encoding'} eq 'native-double-mixed') { } 0 60 60
elsif ($self->{'encoding'} eq 'native-single') { } 0 60 60
Condition33%1/3
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
l!l&&r!l&&!r
0060
MC/DC0%0/2
$self->{"encoding"} eq "native-double-simple" or $self->{"encoding"} eq "native-double-mixed"
$self->{"encoding"} eq "native-double-simple" $self->{"encoding"} eq "native-double-mixed"
Truth table33%1/3
A or B
A= $self->{"encoding"} eq "native-double-simple"B= $self->{"encoding"} eq "native-double-mixed"
ABresult
1X1
011
000
1541 0 substr($header,$idx,$self->{CDP_PREP_EL_SIZE}, pack("d $L x".$self->{DIFF_SIZE}." d d d d $L x".$self->{DIFF_SIZE}." $L x".$self->{DIFF_SIZE}." d d",@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}));
1542 0 $idx+=$self->{CDP_PREP_EL_SIZE};
1543 } elsif ($self->{encoding} eq "native-single") {
1544 0 substr($header,$idx,$self->{CDP_PREP_EL_SIZE}, pack("f $L x".$self->{DIFF_SIZE}." f f f f $L x".$self->{DIFF_SIZE}." $L x".$self->{DIFF_SIZE}." f f",@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}));
1545 0 $idx+=$self->{CDP_PREP_EL_SIZE};
1546 } else {
1547 60 substr($header,$idx,$self->{FLOAT_EL_SIZE},$self->_packd([@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[0]]));
1548 60 $idx+=$self->{FLOAT_EL_SIZE};
1549 60 substr($header,$idx,$self->{FLOAT_EL_SIZE},pack("$L x".$self->{DIFF_SIZE},@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[1]));
1550 60 $idx+=$self->{FLOAT_EL_SIZE};
1551 60 substr($header,$idx,4*$self->{FLOAT_EL_SIZE},$self->_packd([@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[2..5]]));
1552 60 $idx+=4*$self->{FLOAT_EL_SIZE};
1553 60 substr($header,$idx,2*$self->{FLOAT_EL_SIZE},pack("$L x".$self->{DIFF_SIZE}." $L x".$self->{DIFF_SIZE},@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[6..7]));
1554 60 $idx+=2*$self->{FLOAT_EL_SIZE};
1555 60 substr($header,$idx,2*$self->{FLOAT_EL_SIZE},$self->_packd([@val=@{$self->{rrd}->{rra}[$ii]->{cdp_prep}[$i]}[8..9]]));
1556 60 $idx+=2*$self->{FLOAT_EL_SIZE};
1557 }
1558 }
1559 }
1560 # RRA PTR
1561 3 ▶ for ($i=0; $i<$self->{rrd}->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"rrd"}{"rra_cnt"}) 15 3 18
1562 15 substr($header,$idx,$self->{LONG_EL_SIZE},pack("$L",$self->{rrd}->{rra}[$i]->{ptr}));
1563 15 $idx+=$self->{LONG_EL_SIZE};
1564 }
1565 #return $header;
1566 3 print $fd $header;
1567 }
1568  
1569 3 ▶sub save {
Subroutine save: called CC=13 SCAR=32.1
Pod documented
1570 # save RRD data to a file
1571 3 my $self=$_[0];
1572
1573 # load RRA data, if not already loaded
1574 3 ▶ if (!defined($self->{rrd}->{dataloaded})) {$self->_loadRRAdata;}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"rrd"}{"dataloaded"}) 1 2 3
1575
1576 3 ▶ if (@_>1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 3 3
elsif (not defined $self->{'file_name'}) { } 0 3 3
1577 # open file
1578 0 $self->{file_name}=$_[1];
1579 } elsif (!defined($self->{file_name})) {
1580 0 croak("Must either supply a filename to use or have a file already opened e.g. via calling open()\n");
1581 }
1582 3 ▶ open $self->{fd}, "+<", $self->{file_name} or open $self->{fd}, ">", $self->{file_name} or croak "Couldn't open file ".$self->{file_name}.": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $self->{"fd"}, "+<", $self->{"file_name"} or CORE::open $self->{"fd"}, ">", $self->{"file_name"} or croak(q[Couldn't open file ] . $self->{"file_name"} . ": $!\n") 3 0 3
Condition33%1/3
CORE::open $self->{"fd"}, "+<", $self->{"file_name"} or CORE::open $self->{"fd"}, ">", $self->{"file_name"}
l!l&&r!l&&!r
300
MC/DC0%0/2
CORE::open $self->{"fd"}, "+<", $self->{"file_name"} or CORE::open $self->{"fd"}, ">", $self->{"file_name"}
CORE::open $self->{"fd"}, "+<", $self->{"file_name"} CORE::open $self->{"fd"}, ">", $self->{"file_name"}
Truth table33%1/3
A or B
A= CORE::open $self->{"fd"}, "+<", $self->{"file_name"}B= CORE::open $self->{"fd"}, ">", $self->{"file_name"}
ABresult
1X1
011
000
1583 3 binmode($self->{fd});
1584 3 my $fd=$self->{fd};
1585  
1586 0 ▶ if (!defined($self->{encoding})) { croak("Current encoding must be defined\n.");}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"encoding"}) 0 3 3
1587 3 my $current_encoding=$self->{encoding};
1588 3 ▶ if (@_>2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 3 3
1589 0 my $encoding=$_[2];
1590 0 ▶ if ($encoding !~ m/^(native-double|native-double-simple|native-double-mixed|portable-double|portable-single)$/) {croak("unknown format ".$encoding."\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoding =~ /^(native-double|native-double-simple|native-double-mixed|portable-double|portable-single)$/) 0 0 0
1591 0 ▶ if ($encoding =~ m/^native-double$/) {$encoding=_native_double();}
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /^native-double$/) 0 0 0
1592 0 $self->{encoding}=$encoding;
1593 }
1594 3 $self->_sizes;
1595  
1596 # output headers
1597 #print $fd $self->getheader();
1598 3 $self->_saveheader($fd);
1599  
1600 # output data
1601 3 my @line; my $i; my $ii;
1602 3 ▶ for ($ii=0; $ii<$self->{rrd}->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $self->{"rrd"}{"rra_cnt"}) 15 3 18
1603 15 ▶ for ($i=0; $i<$self->{rrd}->{rra}[$ii]->{row_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"rrd"}{"rra"}[$ii]{"row_cnt"}) 75 15 90
1604 75 ▶ if ($self->{encoding} ne $current_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"encoding"} ne $current_encoding) 0 75 75
1605 # need to convert binary data encoding
1606 0 @line=$self->_unpackd($self->{rrd}->{rra}[$ii]->{data}[$i],$current_encoding);
1607 0 $self->{rrd}->{rra}[$ii]->{data}[$i] = $self->_packd(\@line);
1608 }
1609 75 print $fd $self->{rrd}->{rra}[$ii]->{data}[$i];
1610 }
1611 }
1612 # done
1613 3 truncate($fd, tell($fd));
1614  
1615 # and exit
1616 3 return 1;
1617 }
1618  
1619 ####
1620 6 ▶sub close {
Subroutine close: called CC=2 SCAR=7.1
Pod documented
1621 # close an open RRD file
1622 6 my ($self) = @_;
1623 6 ▶ if (defined($self->{fd})) { close($self->{fd}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fd"}) 6 0 6
1624 }
1625  
1626 ####
1627  
1628 1 ▶sub create {
Subroutine create: called CC=20 SCAR=31.9
Pod documented
1629 # create a new RRD
1630 1 my ($self, $args_str) = @_; my $rrd=$self->{rrd};
1631  
1632 1 my $last_up=time(); my $pdp_step=300;
1633 1 my $encoding="native-double"; # default to RRDTOOL compatible encoding.
1634 1 my $ret; my $args;
1635 1 ($ret, $args) = _GetOptionsFromString($args_str,
1636 "start|b:i" => \$last_up,
1637 "step|s:i" => \$pdp_step,
1638 "format|f:s" => \$encoding
1639 );
1640 0 ▶ if ($last_up < 3600 * 24 * 365 * 10) { croak("the first entry to the RRD should be after 1980\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($last_up < 315360000) 0 1 1
1641 0 ▶ if ($pdp_step <1) {croak("step size should be no less than one second\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($pdp_step < 1) 0 1 1
1642 0 ▶ if ($encoding !~ m/^(native-double|native-double-simple|native-double-mixed|portable-double|portable-single)$/) {croak("unknown format ".$encoding."\n");}
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoding =~ /^(native-double|native-double-simple|native-double-mixed|portable-double|portable-single)$/) 0 1 1
1643 0 ▶ if ($encoding =~ m/^native-double$/) {$encoding=_native_double();}
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding =~ /^native-double$/) 0 1 1
1644 1 $self->{encoding}=$encoding;
1645 1 $self->_sizes;
1646
1647 1 $rrd->{version}="0003";
1648 1 $rrd->{ds_cnt}=0; $rrd->{rra_cnt}=0; $rrd->{pdp_step}=$pdp_step;
1649 1 $rrd->{last_up}=$last_up;
1650
1651 # now parse the DS and RRA info
1652 1 my $i;
1653 1 my $min; my $max;
1654 1 ▶ for ($i=0; $i<@{$args}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$args;}) 9 1 10
1655 9 ▶ if (${$args}[$i] =~ m/^DS:([a-zA-Z0-9]+):(GAUGE|COUNTER|DERIVE|ABSOLUTE):([0-9]+):(U|[+|-]?[0-9\.]+):(U|[+|-]?[0-9\.]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (do { $args }->[$i] =~ /^DS:([a-zA-Z0-9]+):(GAUGE|COUNTER|DERIVE|ABSOLUTE):([0-9]+):(U|[+|-]?[0-9\.]+):(U|[+|-]?[0-9\.]+)$/) { } 4 5 9
elsif (do { $args }->[$i] =~ /^RRA:(AVERAGE|MAX|MIN|LAST):([0-9\.]+):([0-9]+):([0-9]+)$/) { } 5 0 5
1656 4 my $ds;
1657 4 ▶ $min=$4; if ($min eq "U") {$min=NAN;} # set to NaN
Branch: 1 branch
BranchTrueFalseTotal
if ($min eq "U") 4 0 4
1658 4 ▶ $max=$5; if ($max eq "U") {$max=NAN;} # set to NaN
Branch: 1 branch
BranchTrueFalseTotal
if ($max eq "U") 4 0 4
1659 ($ds->{name}, $ds->{type}, $ds->{hb}, $ds->{min}, $ds->{max},
1660 $ds->{pdp_prep}->{last_ds}, $ds->{pdp_prep}->{unkn_sec_cnt}, $ds->{pdp_prep}->{val}
1661 4 )= ($1,$2,$3,$min,$max,"U", $last_up%$pdp_step, 0.0);
1662 4 $rrd->{ds}[@{$rrd->{ds}}]=$ds;
1663 4 $rrd->{ds_cnt}++;
1664 5 } elsif (${$args}[$i] =~ m/^RRA:(AVERAGE|MAX|MIN|LAST):([0-9\.]+):([0-9]+):([0-9]+)$/) {
1665 5 my $rra;
1666 0 ▶ if ($4<1) { croak("Invalid row count $4\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($4 < 1) 0 5 5
1667 0 ▶ if ($2<0.0 || $2>1.0) { croak("Invalid xff $2: must be between 0 and 1\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($2 < 0 or $2 > 1) 0 5 5
Condition33%1/3
$2 < 0 or $2 > 1
l!l&&r!l&&!r
005
MC/DC0%0/2
$2 < 0 or $2 > 1
$2 < 0 $2 > 1
Truth table33%1/3
A or B
A= $2 < 0B= $2 > 1
ABresult
1X1
011
000
1668 0 ▶ if ($3<1) { croak("Invalid step $3: must be >= 1\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($3 < 1) 0 5 5
1669 5 ($rra->{name}, $rra->{xff}, $rra->{pdp_cnt}, $rra->{row_cnt}, $rra->{ptr}, $rra->{data})=($1,$2,$3,$4,int(rand($4)),undef);
1670 5 $rrd->{rra}[@{$rrd->{rra}}]=$rra;
1671 5 $rrd->{rra_cnt}++;
1672 }
1673 }
1674 0 ▶ if ($rrd->{ds_cnt}<1) {croak("You must define at least one Data Source\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"ds_cnt"} < 1) 0 1 1
1675 0 ▶ if ($rrd->{rra_cnt}<1) {croak("You must define at least one Round Robin Archive\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"rra_cnt"} < 1) 0 1 1
1676
1677 1 my $ii;
1678 1 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 5 1 6
1679 5 ▶ for ($i=0; $i<$rrd->{ds_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"ds_cnt"}) 20 5 25
1680 20 @{$rrd->{rra}[$ii]->{cdp_prep}[$i]} = (NAN,(($last_up-$last_up%$pdp_step)%($pdp_step*$rrd->{rra}[$ii]->{pdp_cnt}))/$pdp_step,0,0,0,0,0,0,0,0);
1681 }
1682 }
1683
1684 # initialise the data
1685 1 my $j;
1686 1 my @empty=((NAN)x$rrd->{ds_cnt});
1687 1 ▶ for ($ii=0; $ii<$rrd->{rra_cnt}; $ii++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii < $rrd->{"rra_cnt"}) 5 1 6
1688 5 ▶ for ($i=0; $i<$rrd->{rra}[$ii]->{row_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra"}[$ii]{"row_cnt"}) 25 5 30
1689 25 $rrd->{rra}[$ii]->{data}[$i]=$self->_packd(\@empty);
1690 }
1691 }
1692 1 $rrd->{dataloaded}=1; # record the fact that the data is now loaded in memory
1693 }
1694  
1695 ####
1696 6 ▶sub open {
Subroutine open: called CC=18 SCAR=32.9
Pod documented
1697 # open an RRD file and read the header; reading of the body of the RRD file (containing the RRA data) is left until actually needed
1698 6 my $self = $_[0]; my $rrd=$self->{rrd};
1699 6 $self->{file_name}=$_[1];
1700
1701 6 ▶ open($self->{fd}, "<", $self->{file_name}) or croak "Couldn't open file ".$self->{file_name}.": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $self->{"fd"}, "<", $self->{"file_name"} or croak(q[Couldn't open file ] . $self->{"file_name"} . ": $!\n") 6 0 6
1702 6 binmode($self->{fd});
1703 6 my $file_len = -s $self->{file_name};
1704  
1705 # check static part of the header (with fixed size)
1706 # header format: {cookie[4], version[5], double float_cookie, ds_cnt, rra_cnt, pdp_step, par[10] (unused array) }
1707 6 read($self->{fd},my $staticheader,16+8*NATIVE_DOUBLE_EL_SIZE);
1708 6 my $file_cookie = unpack("Z4",substr($staticheader,0,4));
1709 0 ▶ if ($file_cookie ne "RRD") { croak("Wrong magic id $file_cookie\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($file_cookie ne "RRD") 0 6 6
1710 6 $rrd->{version}=unpack("Z5",substr($staticheader,4,5));
1711 0 ▶ if ($rrd->{version} ne "0003" && $rrd->{version} ne "0004") { croak("Unsupported RRD version ".$rrd->{version}."\n");}
Branch: 1 branch
BranchTrueFalseTotal
if ($rrd->{"version"} ne "0003" and $rrd->{"version"} ne "0004") 0 6 6
Condition33%1/3
$rrd->{"version"} ne "0003" and $rrd->{"version"} ne "0004"
!ll&&!rl&&r
600
MC/DC0%0/2
$rrd->{"version"} ne "0003" and $rrd->{"version"} ne "0004"
$rrd->{"version"} ne "0003" $rrd->{"version"} ne "0004"
Truth table33%1/3
A and B
A= $rrd->{"version"} ne "0003"B= $rrd->{"version"} ne "0004"
ABresult
0X0
100
111
1712  
1713 # use float cookie to try to figure out the encoding used, taking account of variable byte alignment (e.g. float cookie starts at byte 12 on 32 bits Intel/Linux machines and at byte 16 on 64 bit Intel/Linux machines)
1714 #my ($x, $y, $byte1, $byte2, $byte3, $byte4, $byte5, $byte6, $byte7, $byte8) =unpack("Z4 Z5 x![L!] C C C C C C C C",substr($staticheader,0,length($staticheader)));
1715 #print $byte1, " ", $byte2, " ",$byte3," ", $byte4," ", $byte5," ", $byte6," ", $byte7," ", $byte8,"\n";
1716 6 $self->{encoding}=undef;
1717 6 (my $x, my $y, my $t) =unpack("Z4 Z5 x![L!] d",substr($staticheader,0,length($staticheader)));
1718 6 my $file_floatcookie_native_double_simple = sprintf("%0.6e", $t);
1719 6 ($x, $y, $t) =unpack("Z4 Z5 x![d] d",substr($staticheader,0,length($staticheader)));
1720 6 my $file_floatcookie_native_double_mixed = sprintf("%0.6e", $t);
1721 6 ($t)=$self->_unpackd(substr($staticheader,12,PORTABLE_SINGLE_EL_SIZE),"native-single");
1722 6 my $file_floatcookie_native_single=sprintf("%0.6e",$t);
1723 6 ($t)=$self->_unpackd(substr($staticheader,12,PORTABLE_SINGLE_EL_SIZE),"portable-single");
1724 6 my $file_floatcookie_portable_single=sprintf("%0.6e",$t);
1725 6 ($t)=$self->_unpackd(substr($staticheader,12,PORTABLE_DOUBLE_EL_SIZE),"portable-double");
1726 6 my $file_floatcookie_portable_double=sprintf("%0.6e",$t);
1727 6 my $file_floatcookie_littleendian_single;
1728 my $file_floatcookie_littleendian_double;
1729 6 ▶ if ($PACK_LITTLE_ENDIAN_SUPPORT>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PACK_LITTLE_ENDIAN_SUPPORT > 0) 6 0 6
1730 6 ($t)=$self->_unpackd(substr($staticheader,12,PORTABLE_SINGLE_EL_SIZE),"littleendian-single");
1731 6 $file_floatcookie_littleendian_single=sprintf("%0.6e",$t);
1732 6 ($t)=$self->_unpackd(substr($staticheader,12,PORTABLE_DOUBLE_EL_SIZE),"littleendian-double");
1733 6 $file_floatcookie_littleendian_double=sprintf("%0.6e",$t);
1734 }
1735 6 my $cookie=sprintf("%0.6e",DOUBLE_FLOATCOOKIE);
1736 6 my $singlecookie=sprintf("%0.6e",SINGLE_FLOATCOOKIE);
1737 6 ▶ if ($file_floatcookie_native_double_simple eq $cookie) {
Branch: 7 branches
BranchTrueFalseTotal
if ($file_floatcookie_native_double_simple eq $cookie) { } 0 6 6
elsif ($file_floatcookie_native_double_mixed eq $cookie) { } 0 6 6
elsif ($file_floatcookie_native_single eq $singlecookie) { } 1 5 6
elsif ($PACK_LITTLE_ENDIAN_SUPPORT > 0 and $file_floatcookie_littleendian_double eq $cookie) { } 5 0 5
elsif ($PACK_LITTLE_ENDIAN_SUPPORT > 0 and $file_floatcookie_littleendian_single eq $singlecookie) { } 0 0 0
elsif ($file_floatcookie_portable_single eq $singlecookie) { } 0 0 0
elsif ($file_floatcookie_portable_double eq $cookie) { } 0 0 0
1738 0 $self->{encoding} = "native-double-simple";
1739 } elsif ($file_floatcookie_native_double_mixed eq $cookie ) {
1740 0 $self->{encoding} = "native-double-mixed";
1741 } elsif ($file_floatcookie_native_single eq $singlecookie ) {
1742 1 $self->{encoding} = "native-single";
1743 ▶ } elsif ($PACK_LITTLE_ENDIAN_SUPPORT>0 && $file_floatcookie_littleendian_double eq $cookie) {
Condition33%1/3
$PACK_LITTLE_ENDIAN_SUPPORT > 0 and $file_floatcookie_littleendian_double eq $cookie
!ll&&!rl&&r
005
MC/DC0%0/2
$PACK_LITTLE_ENDIAN_SUPPORT > 0 and $file_floatcookie_littleendian_double eq $cookie
$PACK_LITTLE_ENDIAN_SUPPORT > 0 $file_floatcookie_littleendian_double eq $cookie
Truth table33%1/3
A and B
A= $PACK_LITTLE_ENDIAN_SUPPORT > 0B= $file_floatcookie_littleendian_double eq $cookie
ABresult
0X0
100
111
1744 5 $self->{encoding} = "littleendian-double";
1745 ▶ } elsif ($PACK_LITTLE_ENDIAN_SUPPORT>0 && $file_floatcookie_littleendian_single eq $singlecookie) {
Condition0%0/3
$PACK_LITTLE_ENDIAN_SUPPORT > 0 and $file_floatcookie_littleendian_single eq $singlecookie
!ll&&!rl&&r
000
MC/DC0%0/2
$PACK_LITTLE_ENDIAN_SUPPORT > 0 and $file_floatcookie_littleendian_single eq $singlecookie
$PACK_LITTLE_ENDIAN_SUPPORT > 0 $file_floatcookie_littleendian_single eq $singlecookie
Truth table0%0/3
A and B
A= $PACK_LITTLE_ENDIAN_SUPPORT > 0B= $file_floatcookie_littleendian_single eq $singlecookie
ABresult
0X0
100
111
1746 0 $self->{encoding} = "littleendian-single";
1747 } elsif ($file_floatcookie_portable_single eq $singlecookie) {
1748 0 $self->{encoding} = "portable-single";
1749 } elsif ($file_floatcookie_portable_double eq $cookie) {
1750 0 $self->{encoding} = "portable-double";
1751 } else {
1752 0 croak("This RRD was created on incompatible architecture\n");
1753 }
1754 #print $self->{encoding},"\n";
1755 #$self->{encoding} = "portable-double";
1756 6 $self->_sizes; # now that we know the encoding, calc the sizes of the various elements in the file
1757 6 my $L=$self->_packlongchar;
1758  
1759 # extract info on number of DS's and RRS's, plus the pdp step size
1760 6 ($rrd->{ds_cnt}, $rrd->{rra_cnt}, $rrd->{pdp_step}) =unpack("$L $L $L",substr($staticheader,$self->{OFFSET} +$self->{FLOAT_EL_SIZE},3*$self->{LONG_EL_SIZE}));
1761 #print $self->{encoding}," ",$offset," ",$L," ",$self->{FLOAT_EL_SIZE}," ", $self->{LONG_EL_SIZE}," ",$rrd->{ds_cnt}," ",$rrd->{rra_cnt}," ",$rrd->{pdp_step},"\n";
1762  
1763 # read in the full header now;
1764 6 seek $self->{fd},0,0; # go back to start of the file
1765 6 read($self->{fd},my $header,$self->_get_header_size);
1766 # extract header info into structured arrays
1767 6 my $pos=$self->{DS_DEF_IDX};
1768 6 $self->_extractDSdefs(\$header,$pos);
1769
1770 6 $pos+=$self->{DS_EL_SIZE}*$rrd->{ds_cnt};
1771 6 $self->_extractRRAdefs(\$header,$pos);
1772
1773 6 $pos+=$self->{RRA_DEF_EL_SIZE}*$rrd->{rra_cnt};
1774 6 $rrd->{last_up} = unpack("$L",substr($header,$pos,$self->{LONG_EL_SIZE}));
1775
1776 6 $pos+=$self->{LIVE_HEAD_SIZE};
1777 6 $self->_extractPDPprep(\$header,$pos);
1778
1779 6 $pos+=$self->{PDP_PREP_EL_SIZE}*$rrd->{ds_cnt};
1780 6 $self->_extractCDPprep(\$header,$pos);
1781
1782 6 $pos+=$self->{CDP_PREP_EL_SIZE}*$rrd->{ds_cnt}*$rrd->{rra_cnt};
1783 6 $self->_extractRRAptr(\$header,$pos);
1784
1785 6 $pos+=$self->{RRA_PTR_EL_SIZE}*$rrd->{rra_cnt};
1786
1787 # validate file size
1788 6 my $i; my $row_cnt=0;
1789 6 ▶ for ($i=0; $i<$rrd->{rra_cnt}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rrd->{"rra_cnt"}) 30 6 36
1790 30 $row_cnt+=$rrd->{rra}[$i]->{row_cnt};
1791 }
1792 6 my $correct_len=$self->_get_header_size +$self->{FLOAT_EL_SIZE} * $row_cnt*$rrd->{ds_cnt};
1793 6 ▶ if ($file_len < $correct_len || $file_len > $correct_len+8) { # extra 8 bytes here is to allow for padding on Linux/Intel 64 bit platforms
Branch: 1 branch
BranchTrueFalseTotal
if ($file_len < $correct_len or $file_len > $correct_len + 8) 0 6 6
Condition33%1/3
$file_len < $correct_len or $file_len > $correct_len + 8
l!l&&r!l&&!r
006
MC/DC0%0/2
$file_len < $correct_len or $file_len > $correct_len + 8
$file_len < $correct_len $file_len > $correct_len + 8
Truth table33%1/3
A or B
A= $file_len < $correct_lenB= $file_len > $correct_len + 8
ABresult
1X1
011
000
1794 0 croak($self->{file_name}." size is incorrect (is $file_len bytes but should be $correct_len bytes)");
1795 }
1796 6 $rrd->{dataloaded}=undef; # keep note that data is not loaded yet
1797 6 return $self->{encoding};
1798 }
1799  
1800 7 1;
1801  
1802  
1803 =pod
1804
1805 =head1 NAME
1806
1807 RRD::Editor - Portable, standalone (no need for RRDs.pm) tool to create and edit RRD files.
1808
1809 =head1 SYNOPSIS
1810  
1811 use strict;
1812 use RRD::Editor ();
1813
1814 # Create a new object
1815 my $rrd = RRD::Editor->new();
1816
1817 # Create a new RRD with 3 data sources called bytesIn, bytesOut and
1818 # faultsPerSec and one RRA which stores 1 day worth of data at 5 minute
1819 # intervals (288 data points). The argument format is the same as that used
1820 # by 'rrdtool create', see L<http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html>
1821 $rrd->create("DS:bytesIn:GAUGE:600:U:U DS:bytesOut:GAUGE:600:U:U DS:faultsPerSec:COUNTER:600:U:U RRA:AVERAGE:0.5:1:288")
1822  
1823 # Save RRD to a file
1824 $rrd->save("myfile.rrd");
1825 # The file format to use can also be optionally specified:
1826 # $rrd->save("myfile.rrd","native-double"); # default; non-portable format used by RRDTOOL
1827 # $rrd->save("myfile.rrd","portable-double"); # portable, data stored in double-precision
1828 # $rrd->save("myfile.rrd","portable-single"); # portable, data stored in single-precision
1829  
1830 # Load RRD from a file. Automagically figures out the file format
1831 # (native-double, portable-double etc)
1832 $rrd->open("myfile.rrd");
1833
1834 # Add new data to the RRD for the same 3 data sources bytesIn,
1835 # bytesOut and faultsPerSec. The argument format is the same as that used by
1836 # 'rrdtool update', see L<http://oss.oetiker.ch/rrdtool/doc/rrdupdate.en.html>
1837 $rrd->update("N:10039:389:0.4");
1838
1839 # Show information about an RRD. Output generated is similar to
1840 # 'rrdtool info'.
1841 print $rrd->info();
1842
1843 # XML dump of RRD contents. Output generated is similar to 'rrdtool dump'.
1844 print $rrd->dump();
1845
1846 # Extract data measurements stored in RRAs of type AVERAGE
1847 # The argument format is the same as that used by 'rrdtool fetch' and
1848 # the output generated is also similar, see
1849 # L<http://oss.oetiker.ch/rrdtool/doc/rrdfetch.en.html>
1850 print $rrd->fetch("AVERAGE");
1851
1852 # Get the time when the RRD was last updated (as a unix timestamp)
1853 printf "RRD last updated at %d\n", $rrd->last();
1854  
1855 # Get the measurements added when the RRD was last updated
1856 print $rrd->lastupdate();
1857
1858 # Get the min step size (or resolution) of the RRD. This defaults to 300s unless specified
1859 otherwise when creating an RRD.
1860 print $rrd->minstep()
1861
1862 =head2 Edit Data Sources
1863
1864 # Add a new data-source called bytes. Argument format is the same as $rrd->create().
1865 $rrd->add_DS("DS:bytes:GAUGE:600:U:U");
1866
1867 # Delete the data-source bytesIn
1868 $rrd->delete_DS("bytesIn");
1869
1870 # Get a list of the data-sources names
1871 print $rrd->DS_names();
1872
1873 # Change the name of data-source bytes to be bytes_new
1874 $rrd->rename_DS("bytes", "bytes_new")
1875
1876 # Get the heartbeat value for data-source bytesOut (the max number of seconds that
1877 # may elapse between data measurements)
1878 printf "Heartbeat for DS bytesOut = %d\n", $rrd->DS_heartbeat("bytesOut");
1879  
1880 # Set the heartbeat value for data-source bytesOut to be 1200 secs
1881 $rrd->set_DS_heartbeat("bytesOut",1200);
1882
1883 # Get the type (COUNTER, GAUGE etc) of data-source bytesOut
1884 printf "Type of DS bytesOut = %s\n", $rrd->DS_type("bytesOut");
1885
1886 # Set the type of data-source bytesOut to be COUNTER
1887 $rrd->set_DS_type("bytesOut", "COUNTER");
1888
1889 # Get the minimum value allowed for measurements from data-source bytesOut
1890 printf "Min value of DS bytesOut = %s\n", $rrd->DS_min("bytesOut");
1891  
1892 # Set the minimum value allowed for measurements from data-source bytesOut to be 0
1893 $rrd->set_DS_min("bytesOut",0);
1894
1895 # Get the maximum value allowed for measurements from data-source bytesOut
1896 printf "Max value of DS bytesOut = %s\n", $rrd->DS_max("bytesOut");
1897
1898 # Set the maximum value allowed for measurements from data-source bytesOut to be 100
1899 $rrd->set_DS_max("bytesOut",100);
1900
1901 =head2 Edit RRAs
1902
1903 # Add a new RRA which stores 1 weeks worth of data (336 data points) at 30 minute
1904 # intervals (30 mins = 6 x 5 mins)
1905 $rrd->add_RRA("RRA:AVERAGE:0.5:6:336");
1906  
1907 # RRAs are identified by an index in range 0 .. $rrd->num_RRAs(). The index
1908 # of an RRD can also be found using $rrd->info() or $rrd->dump()
1909 my $rra_idx=1;
1910
1911 # Delete an existing RRA with index $rra_idx.
1912 $rrd->delete_RRA($rra_idx);
1913
1914 # Get the number of rows/data points stored in the RRA with index $rra_idx
1915 $rra_idx=0;
1916 printf "number of rows of RRA %d = %d\n", $rra_idx, $rrd->RRA_numrows($rra_idx);
1917
1918 # Change the number of rows/data points stored in the RRA with index
1919 # $rra_idx to be 600.
1920 $rra->resize_RRA($rra_idx, 600);
1921
1922 # Get the value of bytesIn stored at the 10th row/data-point in the
1923 # RRA with index $rra_idx.
1924 printf "Value of data-source bytesIn at row 10 in RRA %d = %d", $rra_idx, $rra->RRA_el($rra_idx, "bytesIn", 10);
1925
1926 # Set the value of bytesIn at the 10th row/data-point to be 100
1927 $rra->set_RRA_el($rra_idx, "bytesIn", 10, 100);
1928
1929 # Get the xff value for the RRA with index $rra_idx
1930 printf "Xff value of RRA %d = %d\n", $rra_idx, $rra->RRA_xff($rra_idx);
1931  
1932 # Set the xff value to 0.75 for the RRA with index $rra_idx
1933 $rra->RRA_xff($rra_idx,0.75);
1934
1935 # Get the type (AVERAGE, LAST etc) of the RRA with index $rra_idx
1936 print $rrd->RRA_type($rra_idx);
1937
1938 # Get the step (in seconds) of the RRA with index $rra_idx
1939 print $rrd->RRA_step($rra_idx);
1940  
1941  
1942 =head1 DESCRIPTION
1943  
1944 RRD:Editor implements most of the functionality of RRDTOOL, apart from graphing, plus adds some new editing and portability features. It aims to be portable and self-contained (no need for RRDs.pm).
1945
1946 RRD::Editor provides the ability to add/delete DSs and RRAs and to get/set most of the parameters in DSs and RRAs (renaming, resizing etc). It also allows the data values stored in each RRA to be inspected and changed individually. That is, it provides almost complete control over the contents of an RRD.
1947
1948 The RRD files created by RRDTOOL use a binary format (let's call it C<native-double>) that is not portable across platforms. In addition to this file format, RRD:Editor provides two new portable file formats (C<portable-double> and C<portable-single>) that allow the exchange of files. RRD::Editor can freely convert RRD files between these three formats (C<native-double>,C<portable-double> and C<portable-single>).
1949
1950 Notes:
1951  
1952 =over
1953  
1954 =item * times must all be specified as unix timestamps (i.e. -1d, -1w etc don't work, and there is no @ option in rrdupdate).
1955  
1956 =item * there is full support for COUNTER, GAUGE, DERIVE and ABSOLUTE data-source types but the COMPUTE type is only partially supported.
1957  
1958 =item * there is full support for AVERAGE, MIN, MAX, LAST RRA types but the HWPREDICT, MHWPREDICT, SEASONAL etc types are only partially supported).
1959  
1960 =back
1961
1962 =head1 METHODS
1963
1964 =head2 new
1965
1966 my $rrd=new RRD:Editor->new();
1967
1968 Creates a new RRD::Editor object
1969
1970 =head2 create
1971
1972 $rrd->create($args);
1973
1974 The method will create a new RRD with the data-sources and RRAs specified by C<$args>. C<$args> is a string that contains the same sort of command line arguments that would be passed to C<rrdtool create>. The format for C<$args> is:
1975
1976 [--start|-b start time] [--step|-s step] [--format|-f encoding] [DS:ds-name:DST:heartbeat:min:max] [RRA:CF:xff:steps:rows]
1977
1978 where DST may be one of GAUGE, COUNTER, DERIVE, ABSOLUTE and CF may be one of AVERAGE, MIN, MAX, LAST. Possible values for encoding are C<native-double>, C<portable-double>, C<portable-single>. If omitted, defaults to C<native-double> (the non-portable file format used by RRDTOOL). See L<http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html> for further information.
1979
1980 =head2 open
1981
1982 $rrd->open($file_name);
1983
1984 Load the RRD in the file called C<$file_name>. Only the file header is loaded initially, to improve efficiency, with the body of the file subsequently loaded if needed. The file format (C<native-double>, C<portable-double> etc) is detected automagically.
1985  
1986 =head2 save
1987
1988 $rrd->save();
1989 $rrd->save($file_name);
1990 $rrd->save($file_name, $encoding);
1991
1992 Save RRD to a file called $file_name with format specified by C<$encoding>. Possible values for C<$encoding> are C<"native-double">, C<"portable-double">, C<"portable-single">.
1993
1994 If omitted, C<$encoding> defaults to the format of the file specified when calling C<open()>, or to C<"native-double"> if the RRD has just been created using C<create()>. C<native-double> is the non-portable binary format used by RRDTOOL. C<portable-double> is portable across platforms and stores data as double-precision values. C<portable-single> is portable across platforms and stores data as single-precision values (reducing the RRD file size by approximately half). If interested in the gory details, C<portable-double> is just the C<native-double> format used by Intel 32-bit platforms (i.e. little-endian byte ordering, 32 bit integers, 64 bit IEEE 754 doubles, storage aligned to 32 bit boundaries) - an arbitrary choice, but not unreasonable since Intel platforms are probably the most widespread at the moment, and it is also compatible with web tools such as javascriptRRD L<http://javascriptrrd.sourceforge.net/>.
1995
1996 If the RRD was opened using C<open()>, then C<$file_name> is optional and if omitted C<save()> will save the RRD to the same file as it was read from.
1997  
1998 =head2 close
1999
2000 $rrd->close();
2001
2002 Close an RRD file accessed using C<open()> or C<save()>. Calling C<close()> flushes any cached data to disk.
2003  
2004 =head2 info
2005
2006 my $info = $rrd->info();
2007
2008 Returns a string containing information on the DSs and RRAs in the RRD (but not showing the data values stored in the RRAs). Also shows details of the file format (C<native-double>, C<portable-double> etc) if the RRD was read from a file.
2009
2010 =head2 dump
2011
2012 my $dump = $rrd->dump();
2013 my $dump = $rrd->dump($arg);
2014
2015 Returns a string containing the complete contents of the RRD (including data) in XML format. C<$arg> is optional. Possible values are "--no-header" or "-n", which remove the XML header from the output string.
2016
2017 =head2 fetch
2018
2019 my $vals = $rrd->fetch($args);
2020
2021 Returns a string containing a table of measurement data from the RRD. C<$args> is a string that contains the same sort of command line arguments that would be passed to C<rrdtool fetch>. The format for C<$args> is:
2022
2023 CF [--resolution|-r resolution] [--start|-s start] [--end|-e end]
2024
2025 where C<CF> may be one of AVERAGE, MIN, MAX, LAST. See L<http://oss.oetiker.ch/rrdtool/doc/rrdfetch.en.html> for further details.
2026  
2027 =head2 update
2028
2029 $rrd->update($args);
2030
2031 Feeds new data values into the RRD. C<$args> is a string that contains the same sort of command line arguments that would be passed to C<rrdtool update>. The format for C<$args> is:
2032  
2033 [--template:-t ds-name[:ds-name]...] N|timestamp:value[:value...] [timestamp:value[:value...] ...]
2034
2035 See L<http://oss.oetiker.ch/rrdtool/doc/rrdupdate.en.html> for further details.
2036
2037 Since C<update()> is often called repeatedly, in-place updating of RRD files is used where possible for greater efficiency . To understand this, a little knowledge of the RRD file format is needed. RRD files consist of a small header containing details of the DSs and RRAs, and a large body containing the data values stored in the RRAs. Reading the body into memory is relatively costly since it is much larger than the header, and so is only done by RRD::Editor on an "as-needed" basis. So long as the body has not yet been read into memory when C<update()> is called, C<update()> will update the file on disk i.e. without reading in the body. In this case there is no need to call C<save()>. If the body has been loaded into memory when C<update()> is called, then the copy of the data stored in memory will be updated and the file on disk left untouched - a call to C<save()> is then needed to freshen the file stored on disk. Seems complicated, but its actually ok in practice. If all you want to do is efficiently update a file, just use the following formula:
2038
2039 $rrd->open($file_name);
2040 $rrd->update($args);
2041 $rrd->close();
2042
2043 and that's it. If you want to do more, then be sure to call C<save()> when you're done.
2044
2045 =head2 last
2046
2047 my $unixtime = $rrd->last();
2048
2049 Returns the time when the data stored in the RRD was last updated. The time is returned as a unix timestamp. This value should not be confused with the last modified time of the RRD file.
2050  
2051 =head2 set_last
2052
2053 $rrd->set_last($unixtime);
2054
2055 Set the last update time to equal C<$unixtime>. WARNING: Rarely needed, use with caution !
2056  
2057 =head2 lastupdate
2058
2059 my @vals=$rrd->lastupdate();
2060
2061 Return a list containing the data-source values inserted at the most recent update to the RRD
2062
2063 =head2 minstep
2064
2065 my $minstep = $rrd->minstep();
2066
2067 Returns the minimum step size (in seconds) used to store data values in the RRD. RRA data intervals must be integer multiples of this step size. The min step size defaults to 300s when creating an RRD (where it is referred to as the "resolution"). NB: Changing the min step size is hard as it would require resampling all of the stored data, so we leave this "to do".
2068  
2069 =head2 add_DS
2070
2071 $rrd->add_DS($arg);
2072  
2073 Add a new data-source to the RRD. Only one data-source can be added at a time. Details of the data-source to be added are specified by the string C<$arg>. The format of C<$arg> is:
2074
2075 [DS:ds-name:DST:heartbeat:min:max]
2076
2077 where DST may be one of GAUGE, COUNTER, DERIVE, ABSOLUTE i.e. the same format as used for C<create()>.
2078  
2079 =head2 delete_DS
2080
2081 $rrd->delete_DS($ds-name);
2082  
2083 Delete the data-source with name C<$ds-name> from the RRD. WARNING: This will irreversibly delete all of the data stored for the data-source.
2084
2085 =head2 DS_names
2086
2087 my @ds-names = $rrd->DS_names();
2088  
2089 Returns a list containing the names of the data-sources in the RRD.
2090  
2091 =head2 rename_DS
2092  
2093 $rrd->rename_DS($ds-name, $ds-newname);
2094
2095 Change the name of data-source C<$ds-name> to be C<$ds-newname>
2096  
2097 =head2 DS_heartbeat
2098
2099 my $hb= $rrd->DS_heartbeat($ds-name);
2100
2101 Returns the current heartbeat (in seconds) of a data-source. The heartbeat is the max number of seconds that may elapse between data measurements before declaring that data is missing.
2102  
2103 =head2 set_DS_heartbeat
2104
2105 $rrd->set_DS_heartbeat($ds-name,$hb);
2106  
2107 Sets the heartbeat value (in seconds) of data-source C<$ds-name> to be C<$hb>.
2108
2109 =head2 DS_type
2110
2111 my $type = $rrd->DS_type($ds-name);
2112
2113 Returns the type (GAUGE, COUNTER etc) of a data-source.
2114
2115 =head2 set_DS_type
2116  
2117 $rrd->set_DS_type($ds-name, $type);
2118
2119 Sets the type of data-source C<$ds-name> to be C<$type>.
2120  
2121 =head2 DS_min
2122  
2123 my $min = $rrd->DS_min($ds-name);
2124
2125 Returns the minimum allowed for measurements from data-source C<$ds-name>. Measurements below this value are set equal to C<$mi>n when stored in the RRD.
2126
2127 =head2 set_DS_min
2128
2129 $rrd->set_DS_min($ds-name, $min);
2130
2131 Set the minimum value for data-source C<$ds-name> to be C<$min>.
2132
2133 =head2 DS_max
2134
2135 my $max = $rrd->DS_max($ds-name);
2136
2137 Returns the maximum allowed for measurements from data-source C<$ds-name>. Measurements above this value are set equal to C<$max> when stored in the RRD.
2138
2139 =head2 set_DS_max
2140
2141 $rrd->set_DS_max($ds-name, $max);
2142
2143 Set the maximum value for data-source C<$ds-name> to be C<$max>.
2144
2145 =head2 add_RRA
2146
2147 $rrd->add_RRA($arg);
2148  
2149 Add a new RRA to the RRD. Only one RRA can be added at a time. Details of the RRA to be added are specified by the string C<$arg>. The format of C<$arg> is:
2150
2151 [RRA:CF:xff:steps:rows]
2152
2153 where CF may be one of AVERAGE, MIN, MAX, LAST i.e. the same format as used for C<create()>.
2154  
2155 =head2 num_RRAs
2156
2157 my $num_RRAs = $rrd->num_RRAs();
2158
2159 Returns the number of RRAs stored in the RRD. Unfortunately, unlike data-sources, RRAs are not named and so are only identified by an index in the range 0 .. C<num_RRAs()-1>. The index of a specific RRD can be found using C<info()> or C<dump()>.
2160
2161 =head2 delete_RRA
2162
2163 $rrd->delete_RRA($rra_idx);
2164
2165 Delete the RRA with index C<$rra_idx> (see above discussion for how to determine the index of an RRA). WARNING: This will irreversibly delete all of the data stored in the RRA.
2166  
2167 =head2 RRA_numrows
2168  
2169 my $numrows = $rrd->RRA_numrows($rra_idx);
2170
2171 Returns the number of rows in the RRA with index C<$rra_idx>.
2172  
2173 =head2 resize_RRA
2174  
2175 $rra->resize_RRA($rra_idx, $numrows);
2176
2177 Change the number of rows to be C<$numrows> in the RRA with index C<$rra_idx>. WARNING: If C<$numrows> is smaller than the current row size, excess data points will be discarded.
2178  
2179 =head2 RRA_el
2180  
2181 my ($t,$val) = $rra->RRA_el($rra_idx, $ds-name, $row);
2182
2183 Returns the timestamp and the value of data-source C<$ds-name> stored at row C<$row> in the RRA with index C<$rra_idx>. C<$row> must be in the range [0 .. C<RRA_numrows($rra_idx)-1>]. Row 0 corresponds to the oldest data point stored and row C<RRA_numrows($rra_idx)-1> to the most recent data point.
2184  
2185 =head2 set_RRA_el
2186  
2187 $rra->set_RRA_el($rra_idx, $ds-name, $row, $val);
2188
2189 Set the stored value equal to C<$val> for data-source C<$ds-nam>e stored at row C<$row> in the RRA with index C<$rra_idx>.
2190  
2191 =head2 RRA_xff
2192  
2193 my $xff = $rra->RRA_xff($rra_idx);
2194
2195 Returns the xff value for the RRA with index C<$rra_idx>. The xff value defines the proportion of an RRA data interval that may contain UNKNOWN data (i.e. missing data) and still be treated as known. For example, an xff value 0.5 in an RRA with data interval 300 seconds (5 minutes) means that if less than 150s of valid data is available since the last measurement, UNKNOWN will be stored in the RRA for the next data point.
2196  
2197 =head2 set_RRA_xff
2198  
2199 $rra->RRA_xff($rra_idx,$xff);
2200
2201 Sets the xff value to C<$xff> for the RRA with index C<$rra_idx>.
2202
2203 =head2 RRA_step
2204
2205 my $step = $rrd->RRA_step($rra_idx);
2206
2207 Returns the data interval (in seconds) of the RRA with index C<$rra_idx>. NB: Changing the step size is hard as it would require resampling the data stored in the RRA, so we leave this "to do".
2208
2209 =head2 RRA_type
2210
2211 my $type = $rrd->RRA_type($rra_idx);
2212
2213 Returns the type of the RRA with index C<$rra_idx> i.e. AVERAGE, MAX, MIN, LAST etc. NB: Changing the type of an RRA is hard (impossible ?) as the stored data doesn't contain enough information to allow its type to be changed. To change type, its recommended instead to delete the RRA and add a new RRA with the desired type.
2214  
2215 =head1 EXPORTS
2216
2217 You can export the following functions if you do not want to use the object orientated interface:
2218
2219 create
2220 open
2221 save
2222 close
2223 update
2224 info
2225 dump
2226 fetch
2227 last
2228 set_last
2229 lastupdate
2230 minstep
2231 add_RRA
2232 delete_RRA
2233 num_RRAs
2234 RRA_numrows
2235 resize_RRA
2236 RRA_type
2237 RRA_step
2238 RRA_xff
2239 set_RRA_xff
2240 add_DS
2241 delete_DS
2242 DS_names
2243 rename_DS
2244 DS_heartbeat
2245 set_DS_heartbeat
2246 DS_min
2247 set_DS_min
2248 DS_max
2249 set_DS_max
2250 DS_type
2251 set_DS_type
2252  
2253 The tag C<all> is available to easily export everything:
2254
2255 use RRD::Editor qw(:all);
2256
2257 =head1 Portability/Compatibility with RRDTOOL
2258
2259 The RRD::Editor code is portable, and so long as you stick to using the C<portable-double> and C<portable-single> file formats the RRD files generated will also be portable. Portability issues arise when the C<native-double> file format of RRD::Editor is used to store RRDs. This format tries to be compatible with the non-portable binary format used by RRDTOOL, which requires RRD::Editor to figure out nasty low-level details of the platform it is running on (byte ordering, byte alignment, representation used for doubles etc). To date, RRD::Editor and RRDTOOL have been confirmed compatible (i.e. they can read each others RRD files in C<native-double> format) on the following architectures:
2260  
2261 Intel 386 32bit, Intel 686 32bit, AMD64/Intel x86 64bit, Itanium 64bit, Alpha 64bit, MIPS 32bit, MIPSel 32 bit, MIPS 64bit, PowerPC 32bit, ARMv6 (e.g. Raspberry Pi), SPARC 32bit, SPARC 64bit, SH4
2262
2263 Known issues:
2264
2265 On ARMv5 platforms RRD::Editor and RRDTOOL file formats may be only partially compatible (RRD::Editor can read RRDTOOL files, but sometimes not vice-versa depending on ARM config)
2266
2267 For more information on RRD::Editor portability testing, see L<http://www.leith.ie/rrdeditor/>. If your platform is not listed, there is a good chance things will "just work" but double checking that RRDTOOL can read the C<native-double> format RRD files generated by RRD::Editor, and vice-versa, would be a good idea if that's important to you.
2268
2269 =head1 SEE ALSO
2270  
2271 L<rrdtool.pl|http://cpansearch.perl.org/src/DOUGLEITH/RRD-Editor-0.12/scripts/rrdtool.pl> command line interface for RRD::Editor, L<RRD::Simple>, L<RRDTool::OO>, L<http://www.rrdtool.org>
2272
2273 =head1 VERSION
2274
2275 Ver 0.21
2276
2277 =head1 AUTHOR
2278
2279 Doug Leith
2280  
2281 MaxiM Basunov
2282  
2283 =head1 BUGS
2284
2285 Please report any bugs or feature requests to C<bug-rrd-db at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RRD-Editor>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
2286
2287 =head1 COPYRIGHT
2288
2289 Copyright 2014 D.J.Leith.
2290
2291 This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
2292
2293 See http://dev.perl.org/licenses/ for more information.
2294
2295 =cut
2296  
2297  
2298 __END__
↑ Summary