-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweak.pm
1642 lines (1229 loc) · 44.2 KB
/
Tweak.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package RRD::Tweak;
use strict;
use warnings;
use Carp;
use base 'DynaLoader';
our $VERSION = '1.00';
bootstrap RRD::Tweak;
# Internal object structure:
# $self->{ds} is array of DS definitions
# $self->{rra} is array of RRA definitions
# $self->{cdp_prep}[$rra][$ds] is a hash of intermediate data
# $self->{cdp_data}[$rra][$row][$ds] is a data element
# (last row corresponds to the newest data)
=head1 NAME
RRD::Tweak - RRD file manipulation
=cut
=head1 SYNOPSIS
use RRD::Tweak;
my $rrd = RRD::Tweak->new();
$rrd->load_file($filename1);
my $rrd_info = $rrd->info();
$rrd->del_ds(5);
$rrd->add_ds({name => 'InErrors',
type=> 'COUNTER',
heartbeat => 755});
$rrd->modify_ds(2, {max => 1000});
$rrd->add_rra({cf => 'MAX',
xff => 0.77,
steps => 12,
rows => 10000}) ;
$rrd->modify_rra(6, {rows => 3000});
$rrd->save_file($filename2);
This is a module for manipulating the structure of RRDtool files. It can
read a file, alter its DS and RRA structure, and save a new file. It
also allows creating new empty RRD files in memory or on the disk.
The file read/write operations are implemented in native C. The module
links with librrd, so the librrd library and its header files are
required for building the RRD::Tweak module. The module is tested with
RRDtool versions 1.3 and 1.4. As the RRD file format is architecture
dependent, RRD::Tweak can only read files which are created by RRDtool
in the same processor architecture.
=head1 METHODS
=head2 new
my $rrd = RRD::Tweak->new();
Creates a new RRD::Tweak object
=cut
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->{'errmsg'} = '';
$self->_set_empty(1);
return $self;
}
=head2 is_empty
$status = $rrd->is_empty();
Returns true value if this RRD::Tweak object contains no data. The
object can be empty due to new() or clean() objects.
=cut
sub is_empty {
my $self = shift;
return $self->{'is_empty'};
}
sub _set_empty {
my $self = shift;
my $val = shift;
$self->{'is_empty'} = $val;
return;
}
=head2 validate
$status = $rrd->validate();
Validates the contents of an RRD::Tweak object and returns false if the
data is inconsistent. In case of failed validation, $rrd->errmsg()
returns a human-readable explanation of the failure.
=cut
# DS types supported
my %valid_ds_types =
('GAUGE' => 1,
'COUNTER' => 1,
'DERIVE' => 1,
'ABSOLUTE' => 1,
'COMPUTE' => 1);
# CF names and corresponding required attributes
my %cf_names_and_rra_attributes =
('AVERAGE' => ['xff'],
'MIN' => ['xff'],
'MAX' => ['xff'],
'LAST' => ['xff'],
'HWPREDICT' => ['hw_alpha', 'hw_beta', 'dependent_rra_idx'],
'MHWPREDICT' => ['hw_alpha', 'hw_beta', 'dependent_rra_idx'],
'DEVPREDICT' => ['dependent_rra_idx'],
'SEASONAL' => ['seasonal_gamma', 'seasonal_smooth_idx',
'dependent_rra_idx'],
'DEVSEASONAL' => ['seasonal_gamma', 'seasonal_smooth_idx',
'dependent_rra_idx'],
'FAILURES' => ['delta_pos', 'delta_neg', 'window_len',
'failure_threshold', 'dependent_rra_idx'],
);
# required cdp_prep attributes for each CF
my %cdp_prep_attributes =
('AVERAGE' => ['value', 'unknown_datapoints'],
'MIN' => ['value', 'unknown_datapoints'],
'MAX' => ['value', 'unknown_datapoints'],
'LAST' => ['value', 'unknown_datapoints'],
'HWPREDICT' => ['intercept', 'last_intercept', 'slope', 'last_slope',
'null_count', 'last_null_count'],
'MHWPREDICT' => ['intercept', 'last_intercept', 'slope', 'last_slope',
'null_count', 'last_null_count'],
'DEVPREDICT' => [],
'SEASONAL' => ['seasonal', 'last_seasonal', 'init_flag'],
'DEVSEASONAL' => ['seasonal', 'last_seasonal', 'init_flag'],
'FAILURES' => ['history'],
);
# CF names with special treatment
my %hw_rra_name =
('HWPREDICT' => 1,
'MHWPREDICT' => 1,
'DEVPREDICT' => 1,
'SEASONAL' => 1,
'DEVSEASONAL' => 1,
'FAILURES' => 1,
);
sub validate {
my $self = shift;
if( $self->is_empty() ) {
$self->_set_errmsg('This is an empty RRD::Tweak object');
return 0;
}
# validate positive numbers
foreach my $key ('pdp_step', 'last_up') {
if( not defined($self->{$key}) ) {
$self->_set_errmsg('$self->{' . $key . '} is undefined');
return 0;
}
if( not eval {$self->{$key} > 0}) {
$self->_set_errmsg('$self->{' . $key .
'} is not a positive number');
return 0;
}
}
# validate the presence of arrays
foreach my $key ('ds', 'rra', 'cdp_prep', 'cdp_data') {
if( not defined($self->{$key}) ) {
$self->_set_errmsg('$self->{' . $key . '} is undefined');
return 0;
}
if( ref($self->{$key}) ne 'ARRAY' ) {
$self->_set_errmsg('$self->{' . $key .
'} is not an ARRAY');
return 0;
}
}
# Check that we have a positive number of DS'es
my $n_ds = scalar(@{$self->{'ds'}});
if( $n_ds == 0 ) {
$self->_set_errmsg('no datasources are defined in RRD');
return 0;
}
# validate each DS definition
for( my $ds=0; $ds < $n_ds; $ds++ ) {
my $r = $self->{'ds'}[$ds];
# validate strings
foreach my $key ('name', 'type', 'last_ds') {
if( not defined($r->{$key}) ) {
$self->_set_errmsg('$self->{ds}[' . $ds .
']{' . $key . '} is undefined');
return 0;
}
if( $r->{$key} eq '' ) {
$self->_set_errmsg('$self->{ds}[' . $ds .
']{' . $key . '} is empty');
return 0;
}
}
# check if the type is valid
if( not $valid_ds_types{$r->{'type'}} ) {
$self->_set_errmsg('$self->{ds}[' . $ds .
']{type} has invalid value: "' . $r->{'type'} .
'"');
return 0;
}
# validate numbers
my @number_keys = ('scratch_value', 'unknown_sec');
if( $r->{'type'} ne 'COMPUTE' ) {
push(@number_keys, 'hb', 'min', 'max');
} else {
# COMPUTE is not currently supported by Tweak.xs because RPN
# processing methods are not exported by librrd
push(@number_keys, 'rpn');
}
foreach my $key (@number_keys) {
if( not defined($r->{$key}) ) {
$self->_set_errmsg('$self->{ds}[' . $ds .
']{' . $key . '} is undefined');
return 0;
}
if( $r->{$key} !~ /^-?nan$/io and
$r->{$key} !~ /^[0-9e+\-.]+$/io ) {
$self->_set_errmsg('$self->{ds}[' . $ds .
']{' . $key . '} is not a number');
return 0;
}
}
}
# Check that we have a positive number of RRA's
my $n_rra = scalar(@{$self->{'rra'}});
if( $n_rra == 0 ) {
$self->_set_errmsg('no round-robin arrays are defined in RRD');
return 0;
}
# validate RRA definitions
for( my $rra=0; $rra < $n_rra; $rra++) {
my $r = $self->{'rra'}[$rra];
if( ref($r) ne 'HASH' ) {
$self->_set_errmsg('$self->{rra}[' . $rra . '] is not a HASH');
return 0;
}
my $cf = $r->{cf};
if( not defined($cf) ) {
$self->_set_errmsg('$self->{rra}[' . $rra . ']{cf} is undefined');
return 0;
}
if( not defined($cf_names_and_rra_attributes{$cf}) ) {
$self->_set_errmsg('Unknown CF name in $self->{rra}[' . $rra .
']{cf}: ' . $cf);
return 0;
}
my $pdp_per_row = $r->{'pdp_per_row'};
if( not defined($pdp_per_row) ) {
$self->_set_errmsg('$self->{rra}[' . $rra .
']{pdp_per_row} is undefined');
return 0;
}
if( 0 + $pdp_per_row <= 0 ) {
$self->_set_errmsg('$self->{rra}[' . $rra .
']{pdp_per_row} is not a positive integer');
return 0;
}
if( ref($r->{'cdp_prep_defaults'}) ne 'HASH' ) {
$self->_set_errmsg('$self->{rra}[' . $rra .
']{cdp_prep_defaults} is undefined');
return 0;
}
foreach my $key (@{$cf_names_and_rra_attributes{$cf}}) {
if( not defined($r->{$key}) ) {
$self->_set_errmsg('$self->{rra}[' . $rra . ']{' . $key .
'} is undefined');
return 0;
}
}
}
# Check the sizes of cdp_prep and cdp_data arrays
if( scalar(@{$self->{'cdp_prep'}}) != $n_rra ) {
$self->_set_errmsg('Wrong size of $self->{cdp_prep} array. ' .
'Expected: ' . $n_rra . ', is: ' .
scalar(@{$self->{'cdp_prep'}}));
return 0;
}
if( scalar(@{$self->{'cdp_data'}}) != $n_rra ) {
$self->_set_errmsg('Wrong size of $self->{cdp_data} array. ' .
'Expected: ' . $n_rra . ', is: ' .
scalar(@{$self->{'cdp_data'}}));
return 0;
}
# validate cdp_prep
for( my $rra=0; $rra < $n_rra; $rra++) {
if( ref($self->{'cdp_prep'}[$rra]) ne 'ARRAY' ) {
$self->_set_errmsg('$self->{cdp_prep}[' . $rra .
'] is not an ARRAY');
return 0;
}
my $cf = $self->{'rra'}[$rra]{cf};
for( my $ds=0; $ds < $n_ds; $ds++ ) {
my $r = $self->{'cdp_prep'}[$rra][$ds];
if( ref($r) ne 'HASH' ) {
$self->_set_errmsg('$self->{cdp_prep}[' . $rra .
'][' . $ds . '] is not an HASH');
return 0;
}
foreach my $key (@{$cdp_prep_attributes{$cf}}) {
if( not defined($r->{$key}) ) {
$self->_set_errmsg
('$self->{cdp_prep}[' . $rra .
'][' . $ds . ']{' . $key . '} is undefined');
return 0;
}
}
if( $cf eq 'FAILURES' ) {
if( ref($r->{'history'}) ne 'ARRAY' ) {
$self->_set_errmsg
('$self->{cdp_prep}[' . $rra .
'][' . $ds . ']{history} is not an ARRAY');
return 0;
}
# in rrd_format.h: MAX_FAILURES_WINDOW_LEN=28
if( scalar(@{$r->{'history'}}) > 28 ) {
$self->_set_errmsg
('$self->{cdp_prep}[' . $rra .
'][' . $ds . ']{history} is a too large array');
return 0;
}
}
}
}
# validate cdp_data
for( my $rra=0; $rra < $n_rra; $rra++) {
my $rra_data = $self->{'cdp_data'}[$rra];
if( ref($rra_data) ne 'ARRAY' ) {
$self->_set_errmsg('$self->{cdp_data}[' . $rra .
'] is not an ARRAY');
return 0;
}
my $rra_len = scalar(@{$rra_data});
if( $rra_len == 0 ) {
$self->_set_errmsg('$self->{cdp_data}[' . $rra .
'] is an empty array');
return 0;
}
for( my $row=0; $row < $rra_len; $row++ ) {
my $row_data = $rra_data->[$row];
if( ref($row_data) ne 'ARRAY' ) {
$self->_set_errmsg('$self->{cdp_data}[' . $rra .
'][' . $row . '] is not an ARRAY');
return 0;
}
my $row_len = scalar(@{$row_data});
if( $row_len != $n_ds ) {
$self->_set_errmsg('$self->{cdp_data}[' . $rra .
'][' . $row . '] array has wrong size. ' .
'Expected: ' . $n_ds . ', found: ' .
$row_len);
return 0;
}
for( my $ds=0; $ds < $n_ds; $ds++ ) {
if( not defined($row_data->[$ds]) ) {
$self->_set_errmsg('$self->{cdp_prep}[' . $rra .
'][' . $ds . '][' . $ds .
'] is undefined');
return 0;
}
}
}
}
return 1;
}
=head2 errmsg
$msg = $rrd->errmsg();
Returns a text string explaining the details if $rrd->validate() failed.
=cut
sub errmsg {
my $self = shift;
return $self->{'errmsg'};
}
sub _set_errmsg {
my $self = shift;
my $msg = shift;
$self->{'errmsg'} = $msg;
return;
}
=head2 load_file
$rrd->load_file($filename);
Reads the RRD file and stores its whole content in the RRD::Tweak object
=cut
sub load_file {
my $self = shift;
my $filename = shift;
croak('load_file() requires a filename') unless defined($filename);
# the native method is defined in Tweak.xs and uses librrd methods
$self->_load_file($filename);
$self->_set_empty(0);
# populate $self->{'rra'}[$rra]->{'cdp_prep_defaults'}
my $n_rra = scalar(@{$self->{'rra'}});
for( my $rra=0; $rra < $n_rra; $rra++) {
$self->_default_cdp_prep_attributes($rra);
}
if( not $self->validate() ) {
croak('load_file prodiced an invalid RRD::Tweak object: ' .
$self->errmsg());
}
return;
}
=head2 save_file
$rrd->save_file($filename);
Creates a new RRD file from the contents of the RRD::Tweak object. If
the file already exists, it's truncated and overwritten.
=cut
sub save_file {
my $self = shift;
my $filename = shift;
croak('save_file() requires a filename') unless defined($filename);
if( not $self->validate() ) {
croak('Cannot run save_file because RRD::Tweak object is invalid: ' .
$self->errmsg());
}
# the native method is defined in Tweak.xs and uses librrd methods
$self->_save_file($filename);
return;
}
=head2 clean
$rrd->clean();
The method empties the RRD::Tweak object to what it was right after new().
=cut
sub clean {
my $self = shift;
delete $self->{'pdp_step'};
delete $self->{'last_up'};
delete $self->{'ds'};
delete $self->{'rra'};
delete $self->{'cdp_prep'};
delete $self->{'cdp_data'};
$self->{'errmsg'} = '';
$self->_set_empty(1);
return;
}
=head2 create
$rrd->create({step => 300,
start => time(),
ds => [{name => 'InOctets',
type=> 'COUNTER',
heartbeat => 600},
{name => 'OutOctets',
type => 'COUNTER',
heartbeat => 600},
{name => 'Load',
type => 'GAUGE',
heartbeat => 800,
min => 0,
max => 255}],
rra => [{cf => 'AVERAGE',
xff => 0.5,
steps => 1,
rows => 2016},
{cf => 'AVERAGE',
xff => 0.25,
steps => 12,
rows => 768},
{cf => 'MAX',
xff => 0.25,
steps => 12,
rows => 768}]});
The method initializes the RRD::Tweak object with new RRD data as
specified by the arguments. The arguments are presented in a hash
reference with the following keys and values: C<step>, defining the
minumum RRA resolution (default is 300 seconds); C<start> in seconds
from epoch (default is "time() - 10"); C<ds> pointing to an array that
defines the datasources; C<rra> pointing to an array with RRA
definitions.
C<start> and C<lastupdate> are synonyms.
Each datasource definition is a hash with the following arguments:
C<name>, C<type>, C<heartbeat>, C<min> (default: "nan"), C<max>
(default: "nan"). The COMPUTE datasource type is currently not supported.
Each RRA definition is a hash with arguments: C<cf> defines the
consolidation function; C<steps> defines how many minimal steps are
aggregated by this RRA; C<rows> defines the size of the RRA.
For AVERAGE, MIN, MAX, and LAST consolidation functions, C<xff> is required.
The a subset of the following attributes is required for each RRA that
is related to the Holt-Winters Forecasting: C<hw_alpha>, C<hw_beta>,
C<dependent_rra_idx>, C<dependent_rra_idx>, C<seasonal_gamma>,
C<seasonal_smooth_idx>, C<delta_pos>, C<delta_neg>, C<window_len>,
C<failure_threshold>, C<dependent_rra_idx>. Also C<smoothing_window> is
supported for RRD files of version 4.
See also I<rrdcreate> manual page of RRDTool for more details.
=cut
sub create {
my $self = shift;
my $arg = shift;
if( not $self->is_empty() ) {
croak('create() requies an empty RRD::Tweak object');
}
if( ref($arg) ne 'HASH' ) {
croak('create() requies a hashref as argument');
}
if( ref($arg->{'ds'}) ne 'ARRAY' ) {
croak('create() requires "ds" array in the argument');
}
my $n_ds = scalar(@{$arg->{'ds'}});
if( $n_ds == 0 ) {
croak('create(): "ds" is an empty array');
}
if( ref($arg->{rra}) ne 'ARRAY' ) {
croak('create() requires "rra" array in the argument');
}
my $n_rra = scalar(@{$arg->{'rra'}});
if( $n_rra == 0 ) {
croak('create(): "rra" is an empty array');
}
my $pdp_step = $arg->{'step'};
$pdp_step = 300 unless defined($pdp_step);
$self->{'pdp_step'} = $pdp_step;
my $last_up = $arg->{'start'};
if( not defined($last_up) ) {
$last_up = $arg->{'lastupdate'};
}
elsif( defined($arg->{'lastupdate'}) ) {
croak('create(): both "start" and "lastupdate" are defined');
}
$last_up = (time() - 10) unless defined($last_up);
$self->{'last_up'} = $last_up;
$self->{'ds'} = [];
$self->{'rra'} = [];
$self->{'cdp_prep'} = [];
$self->{'cdp_data'} = [];
# process DS definitions
for( my $ds=0; $ds < $n_ds; $ds++ ) {
my $r = $arg->{'ds'}[$ds];
if( ref($r) ne 'HASH' ) {
croak('create(): $arg->{ds}[' . $ds .
'] is not a HASH');
}
$self->add_ds($r);
}
# process RRA definitions
for( my $rra=0; $rra < $n_rra; $rra++) {
my $r = $arg->{'rra'}[$rra];
if( ref($r) ne 'HASH' ) {
croak('create(): $arg->{rra}[' . $rra .
'] is not a HASH');
}
$self->add_rra($r);
}
$self->_set_empty(0);
return;
}
# For a newly created RRA, this method
# populates $self->{'rra'}[$rra]->{'cdp_prep_defaults'}
# see details in rrd_create.c
sub _default_cdp_prep_attributes {
my $self = shift;
my $rra = shift;
my $cf = $self->{'rra'}[$rra]->{'cf'};
my $cdp_prep_attr = {};
if( grep {$cf eq $_} qw/AVERAGE MIN MAX LAST/ ) {
my $pdp_step = $self->{'pdp_step'};
my $last_up = $self->{'last_up'};
my $pdp_per_row = $self->{'rra'}[$rra]->{'pdp_per_row'};
my $unknown_sec = $last_up % $pdp_step;
$cdp_prep_attr->{'value'} = 'nan';
$cdp_prep_attr->{'unknown_datapoints'} =
(($last_up - $unknown_sec) % ($pdp_step * $pdp_per_row)) /
$pdp_step;
}
elsif( grep {$cf eq $_} qw/HWPREDICT MHWPREDICT/ ) {
$cdp_prep_attr->{'intercept'} = 'nan';
$cdp_prep_attr->{'last_intercept'} = 'nan';
$cdp_prep_attr->{'slope'} = 'nan';
$cdp_prep_attr->{'last_slope'} = 'nan';
$cdp_prep_attr->{'null_count'} = 1;
$cdp_prep_attr->{'last_null_count'} = 1;
}
elsif( grep {$cf eq $_} qw/SEASONAL DEVSEASONAL/ ) {
$cdp_prep_attr->{'seasonal'} = 'nan';
$cdp_prep_attr->{'last_seasonal'} = 'nan';
$cdp_prep_attr->{'init_flag'} = 1;
}
elsif( $cf eq 'FAILURES' ) {
my $history = [];
my $window_len = $self->{'rra'}[$rra]->{'window_len'};
for( my $i=0; $i < $window_len; $i++ ) {
push(@{$history}, 0);
}
$cdp_prep_attr->{'history'} = $history;
}
$self->{'rra'}[$rra]->{'cdp_prep_defaults'} = $cdp_prep_attr;
return;
}
sub _validate_ds_name {
my $self = shift;
my $ds_name = shift;
if( length($ds_name) > 19 ) {
croak('DS name is too long: "' . $ds_name . '"');
}
if( $ds_name !~ /^[0-9a-zA-Z_-]+$/o ) {
croak('DS has invalid characters: "' . $ds_name . '"');
}
return;
}
# check name uniqueness
sub _check_unique_ds_name {
my $self = shift;
my $ds_name = shift;
foreach my $ds_def (@{$self->{'ds'}}) {
if( $ds_name eq $ds_def->{'name'} ) {
croak('A DS named "' . $ds_name .
'" already exists in this RRD::Tweak object');
}
}
return;
}
=head2 add_ds
$rrd->add_ds({name => 'InOctets',
type=> 'COUNTER',
heartbeat => 600});
The method takes a hash reference as an argument and extends the
RRD::Tweak object by adding a new datasource. The new datasource is
appended to the array of other datasources. The name should be unique,
otherwise the method croaks. The hash keys and values are the same as
the DS attributes in create() method.
add_ds() should only be called on an RRD::Tweak object with some data in
it. The data can be initialized by create() or load_file().
=cut
sub add_ds {
my $self = shift;
my $arg = shift;
if( ref($arg) ne 'HASH' ) {
croak('add_ds() requies a hashref as argument');
}
if( ref($self->{'ds'}) ne 'ARRAY' or
ref($self->{'rra'}) ne 'ARRAY' or
ref($self->{'cdp_prep'}) ne 'ARRAY' or
ref($self->{'cdp_data'}) ne 'ARRAY' ) {
croak('add_ds() is called on an unitialized RRD::Tweak object');
}
my $ds_attr = {};
foreach my $key ('name', 'type') {
if( not defined($arg->{$key}) ) {
croak('add_ds(): $arg->{' . $key . '} is undefined');
}
if( $arg->{$key} eq '' ) {
croak('add_ds(): $arg->{' . $key . '} is empty');
}
$ds_attr->{$key} = $arg->{$key};
}
$self->_validate_ds_name($arg->{'name'});
$self->_check_unique_ds_name($arg->{'name'});
if( not $valid_ds_types{$arg->{'type'}} ) {
croak('add_ds(): $arg->{type} has invalid value: "' .
$arg->{'type'} . '"');
}
if( $arg->{'type'} eq 'COMPUTE' ) {
croak('add_ds(): DS type COMPUTE is currently unsupported');
}
else {
my $hb = $arg->{'heartbeat'};
if( not defined($hb) ) {
croak('add_ds(): $arg->{heartbeat} is undefined');
}
$ds_attr->{'hb'} = int($hb);
foreach my $key ('min', 'max') {
my $val = $arg->{$key};
if( defined($val) ) {
if( $val eq 'U' ) {
$val = 'nan';
}
}
else {
$val = 'nan';
}
$ds_attr->{$key} = $val;
}
}
# Values as defined in rrd_create.c
$ds_attr->{'last_ds'} = 'U';
$ds_attr->{'scratch_value'} = '0.0';
$ds_attr->{'unknown_sec'} = $self->{'last_up'} % $self->{'pdp_step'};
# add to the list of DS definitions
push(@{$self->{'ds'}}, $ds_attr);
# update cdp_prep
my $n_rra = scalar(@{$self->{'rra'}});
for( my $rra=0; $rra < $n_rra; $rra++) {
my $cdp_prep_attr = $self->{'rra'}[$rra]->{'cdp_prep_defaults'};
if( not defined($cdp_prep_attr) ) {
croak('add_ds(): $self->{rra}[' . $rra .
']->{cdp_prep_defaults} is undefined');
}
# duplicate cdp_prep attributes for the new DS
my $attr = {};
while(my($key, $value) = each %{$cdp_prep_attr}) {
$attr->{$key} = $value;
}
# if this is the first DS, $self->{'cdp_prep'}[$rra] is
# not initialized yet
if( not defined($self->{'cdp_prep'}[$rra]) ) {
$self->{'cdp_prep'}[$rra] = [];
}
push(@{$self->{'cdp_prep'}[$rra]}, $attr);
}
# update cdp_data
for( my $rra=0; $rra < $n_rra; $rra++) {
my $rra_data = $self->{'cdp_data'}[$rra];
my $rra_len = scalar(@{$rra_data});
for( my $row=0; $row < $rra_len; $row++ ) {
push(@{$rra_data->[$row]}, 'nan');
}
}
return;
}
=head2 del_ds
$rrd->del_ds($ds_index);
The method removes a datasource from a given index. The indexing starts
from 0.
=cut
sub del_ds {
my $self = shift;
my $del_ds_index = shift;
my $n_ds = scalar(@{$self->{'ds'}});
if( $del_ds_index < 0 or $del_ds_index >= $n_ds ) {
croak('del_ds(): DS index is outside of allowed range: ' .
$del_ds_index);
}
splice(@{$self->{'ds'}}, $del_ds_index, 1);
# update cdp_prep and cdp_data
my $n_rra = scalar(@{$self->{'rra'}});
for( my $rra=0; $rra < $n_rra; $rra++) {
splice(@{$self->{'cdp_prep'}[$rra]}, $del_ds_index, 1);
my $rra_data = $self->{'cdp_data'}[$rra];
my $rra_len = scalar(@{$rra_data});
for( my $row=0; $row < $rra_len; $row++ ) {
splice(@{$rra_data->[$row]}, $del_ds_index, 1);
}
}
return;
}
=head2 modify_ds
$rrd->modify_ds($ds_index, {heartbeat => 700});
The method takes the DS index and a hash reference with DS parameters
that need to be modified. All DS parameters described for the create()
method are supported.
=cut
sub modify_ds {
my $self = shift;
my $mod_ds_index = shift;
my $arg = shift;
my $n_ds = scalar(@{$self->{'ds'}});
if( $mod_ds_index < 0 or $mod_ds_index >= $n_ds ) {
croak('modify_ds(): DS index is outside of allowed range: ' .
$mod_ds_index);
}
my $ds_attr = $self->{'ds'}[$mod_ds_index];
if( exists $arg->{'name'} and
$arg->{'name'} ne $ds_attr->{'name'} )
{
$self->_validate_ds_name($arg->{'name'});
$self->_check_unique_ds_name($arg->{'name'});
$ds_attr->{'name'} = $arg->{'name'};
}
if( exists($arg->{'type'}) and
$arg->{'type'} ne $ds_attr->{'type'} )
{
if( not $valid_ds_types{$arg->{'type'}} ) {
croak('modify_ds(): $arg->{type} has invalid value: "' .
$arg->{'type'} . '"');
}
if( $arg->{'type'} eq 'COMPUTE' ) {
croak('modify_ds(): DS type COMPUTE is currently unsupported');
}
$ds_attr->{'type'} = $arg->{'type'};
}
# when we start supporting COMPUTE datasources, need also to process
# the type changing more correctly: a new type may require new
# attributes.
if( $ds_attr->{'type'} ne 'COMPUTE' ) {
if( exists($arg->{'heartbeat'}) and
int($arg->{'heartbeat'}) != $ds_attr->{'hb'} ) {
$ds_attr->{'hb'} = int($arg->{'heartbeat'});
}
foreach my $key ('min', 'max') {
my $val = $arg->{$key};
if( defined($val) ) {
if( $val eq 'U' ) {
$val = 'nan';
}
if( $val =~ /^-?nan$/i ) {
if( $ds_attr->{$key} !~ /^-?nan$/io ) {
$ds_attr->{$key} = 'nan';
}
}
elsif( $val != $ds_attr->{$key} ) {
$ds_attr->{$key} = $val;