Creator.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary Creator/DayPDP.pm »
1 package RRDTool::Creator ;
2  
3 # ============================================
4 #
5 # Jacquelin Charbonnel - CNRS/LAREMA
6 #
7 # $Id: Creator.pm 410 2008-03-11 21:45:09Z jaclin $
8 #
9 # ----
10 #
11 # A generic abstract creator for round robin databases (RRD)
12 #
13 # ----
14 # $LastChangedDate: 2008-03-11 22:45:09 +0100 (Tue, 11 Mar 2008) $
15 # $LastChangedRevision: 410 $
16 # $LastChangedBy: jaclin $
17 # $URL: https://svn.math.cnrs.fr/jaclin/src/pm/RRDTool-Creator/Creator.pm $
18 #
19 # ============================================
20  
21 require Exporter ;
22 @ISA = qw(Exporter);
23 @EXPORT=qw() ;
24  
25 2 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 0 ▶use RRDTool::OO ;
Subroutine BEGIN: called CC=1 SCAR=2.6
27 use strict ;
28 use warnings ;
29  
30 our $VERSION = "1.0" ; # $LastChangedRevision: 410 $
31 $Carp::CarpLevel = 1;
32  
33 my $InSeconds = {
34 "s" => 1
35 , "mn" => 60
36 , "h" => 60*60
37 , "d" => 60*60*24
38 , "w" => 60*60*24*7
39 , "m" => 60*60*24*30 # supposed 30 days
40 , "q" => 60*60*24*30*3 # 3 months
41 , "y" => 60*60*24*30*12 # 12 months
42 } ;
43  
44 sub _getkey
45 {
46 my ($key) = @_ ;
47 $Carp::CarpLevel = 2 ;
48
49 my @keys = grep /^$key/i,keys %$InSeconds ;
50 croak "unknown '$key' unit" if scalar(@keys)==0 ;
51 croak "ambigous '$key' unit (".join(",",@keys)." ?)" if scalar(@keys)>1 ;
52 return lc($keys[0]) ;
53 }
54  
55 sub _inSeconds
56 {
57 my($duration,$allowed) = @_ ;
58 $Carp::CarpLevel = 2 ;
59
60 my ($num,$unit) = $duration=~/^\s*(\d+)\s*([a-zA-Z]+)\s*$/ or return undef ;
61 my %allowed = map { $_ => 1 } @$allowed ;
62
63 croak "unit '$unit' not allowed" unless exists $allowed{lc($unit)} ;
64 my $key = _getkey($unit) ;
65  
66 return $num * $InSeconds->{$key} ;
67 }
68  
69 #--
70 sub _new
71 {
72 my($type,$units,%h) = @_ ;
73  
74 %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
75 for my $k ("step")
76 {
77 croak "argument '$k' is missing" unless exists $h{lc($k)} ;
78 }
79 my $step = $h{"step"} ;
80 my $s_step = _inSeconds($step,$units) or croak "bad format for step '$step'" ;
81
82 my $this = {
83 "step" => $s_step
84 , "DS" => []
85 , "RRA" => []
86 } ;
87  
88 bless $this,$type ;
89 return $this ;
90 }
91  
92 #-------------------------------
93 sub _set_filename
94 {
95 my($this,$filename) = @_ ;
96 $this->{"filename"} = $filename ;
97 }
98
99 #-------------------------------
100 sub add
101 {
102 my($this,%h) = @_ ;
103  
104 %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
105  
106 # normalisation
107 if (exists $h{"cfunc"}) { $h{"cf"} = $h{"cfunc"} ; delete $h{"cfunc"} ; }
108  
109 push(@{$this->{"CF"}},$h{"cf"}) if exists $h{"cf"} ;
110 }
111  
112 #-------------------------------
113  
114 sub add_RRA
115 {
116 my($this,%h) = @_ ;
117  
118 %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
119
120 for my $k ("duration")
121 {
122 croak "'$k' argument is missing" unless exists $h{lc($k)} ;
123 }
124  
125 my $duration = $h{"duration"} ;
126 croak "possible duration are : "
127 .join(",",keys %{$this->{"allowed_RRA_duration"}})
128 unless exists $this->{"allowed_RRA_duration"}{$duration} ;
129
130 push(@{$this->{"RRA"}},\%h) ;
131 }
132  
133 #-------------------------------
134 sub add_data_source { add_DS(@_) ; }
135 sub add_DS
136 {
137 my($this,%h) = @_ ;
138  
139 %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
140
141 # normalisation
142 if (exists $h{"name"}) { $h{"ds_name"} = $h{"name"} ; delete $h{"name"} ; }
143 if (exists $h{"type"}) { $h{"dst"} = $h{"type"} ; delete $h{"type"} ; }
144
145 $h{"min"} = "U" unless exists $h{"min"} ;
146 $h{"max"} = "U" unless exists $h{"max"} ;
147 $h{"heartbeat"} = 2*$this->{"step"} unless exists $h{"heartbeat"} ;
148
149 for my $k ("ds_name","DST")
150 {
151 croak "'$k' argument is missing" unless exists $h{lc($k)} ;
152 }
153  
154 push(@{$this->{"DS"}},\%h) ;
155 }
156  
157 #-------------------------------
158 sub add_compute_DS
159 {
160 my($this,%h) = @_ ;
161  
162 %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
163
164 # normalisation
165 if (exists $h{"name"}) { $h{"ds_name"} = $h{"name"} ; delete $h{"name"} ; }
166
167 for my $k ("ds_name","rpn_expression")
168 {
169 croak "'$k' argument is missing" unless exists $h{lc($k)} ;
170 }
171  
172 push(@{$this->{"DS"}},\%h) ;
173 }
174  
175 #-------------------------------
176 sub compile
177 {
178 my($this) = @_ ;
179  
180 my $rrd = RRDTool::OO->new("file" => $this->{"filename"});
181 my @arg = () ;
182 push(@arg,("step" => $this->{"step"})) ;
183
184 for my $ds (@{$this->{"DS"}})
185 {
186 push(@arg,("data_source",
187 {
188 "name" => $ds->{"ds_name"}
189 , "type" => $ds->{"dst"}
190 , "heartbeat" => $ds->{"heartbeat"}
191 , "min" => $ds->{"min"}
192 , "max" => $ds->{"max"}
193 }
194 )
195 )
196 }
197  
198 push(@arg,("archive",
199 {
200 "cfunc" => "LAST"
201 , "cpoints" => 1
202 , "rows" => $this->{"rows"}
203 }
204 )) ;
205  
206 for my $cpoint (@{$this->{"RRA"}})
207 {
208 for my $cfunc (@{$this->{"CF"}})
209 {
210 push(@arg,("archive",
211 {
212 "cfunc" => $cfunc
213 , "cpoints" => $InSeconds->{substr($cpoint->{"duration"},0,1)}/($this->{"step"}*$this->{"rows"})
214 , "rows" => $this->{"rows"}
215 }
216 )
217 )
218 }
219 }
220 $this->{"OO_create_arg"} = \@arg ;
221 return @arg ;
222 }
223  
224 #-------------------------------
225 sub create
226 {
227 my($this,%h) = @_ ;
228  
229 %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
230
231 # normalisation
232 if (exists $h{"file"}) { $h{"filename"} = $h{"file"} ; delete $h{"file"} ; }
233  
234 $this->{"filename"} = $h{"filename"} if exists $h{"filename"} ;
235 $this->{"OO_create_arg"} = $h{"OO_create_arg"} if exists $h{"OO_create_arg"} ;
236
237 $this->compile() unless defined $this->{"OO_create_arg"} ;
238 my $rrd = RRDTool::OO->new("file" => $h{"filename"});
239 $rrd->create(@{$this->{"OO_create_arg"}}) ;
240 }
241
242 __END__
↑ Summary Creator/DayPDP.pm »