YATG/Retrieve/Disk.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 91 CC 8 SCAR 37.4
CC
8
Cov
0%
retrieve
72.0
BEGIN
2.0
BEGIN
2.0
CRAP 42.0
1 package YATG::Retrieve::Disk;
2 {
3 $YATG::Retrieve::Disk::VERSION = '5.140510';
4 }
5  
6 use strict;
7 use warnings FATAL => 'all';
8  
9 use Time::Local;
10 use Tie::File::FixedRecLen;
11 use Fcntl 'O_RDONLY';
12 use integer;
13  
14 sub retrieve {
15 my ($config, $device, $port, $leaf, $start, $end, $step) = @_;
16 my $root = $config->{yatg}->{disk_root};
17  
18 $port ||= 1;
19 $port =~ s/[^A-Za-z0-9]/./g; # just in case, re-squash port chars
20  
21 my @files = grep {m/\d{4}-\d\d-\d\d_\d\d-\d\d-\d\dZ?,\d+$/}
22 glob("$root/$device/$leaf/$port/*");
23 die "File not found at '$root/$device/$leaf/$port'\n"
24 if scalar @files == 0;
25  
26 my $filename = (sort @files)[-1];
27 die "File '$filename' empty!\n" unless -s $filename;
28  
29 my @store;
30 tie @store, 'Tie::File::FixedRecLen', $filename,
31 mode => O_RDONLY, record_length => 20;
32 die "Failed to Tie::File::FixedRecLen file at '$filename'\n"
33 if !tied @store;
34  
35 $filename =~ s#$root/$device/$leaf/$port/##;
36 $filename =~ m/^(\d{4})-(\d\d)-(\d\d)_(\d\d)-(\d\d)-(\d\d)(Z?),(\d+)$/;
37  
38 my $base_time = $7 ?
39 timegm($6,$5,$4,$3,$2-1,$1-1900) :
40 timelocal($6,$5,$4,$3,$2-1,$1-1900);
41 my $interval = $8;
42 my $top_time = $base_time + ($#store * $interval);
43  
44 $step ||= $interval;
45 $start = ($start - ($start % $step));
46 $end = ($end - ($end % $step));
47 my $nudge = ($step / $interval);
48  
49 my $num_steps = (($end - $start) / $step);
50 my @data = map {'0'} (0 .. ($num_steps - 1)); # fenceposts!
51  
52 my $data_offset_start =
53 (($start < $base_time) ? (($base_time - $start) / $step) : 0);
54  
55 my $store_offset_start =
56 (($start > $base_time) ? ((($start - $base_time) / $interval)) : 0);
57  
58 my @store_pad = map {$_} ($store_offset_start .. $#store);
59  
60 while (my @chunk = splice @store_pad,0,$nudge) {
61 $data[$data_offset_start] = $store[ $chunk[-1] ] || 0;
62 ++$data_offset_start;
63 last if $data_offset_start > $#data;
64 }
65  
66 untie @store;
67 return \@data;
68 }
69  
70 1;
71  
72 # ABSTRACT: Retrieve a set of data stored by YATG::Store::Disk
73  
74  
75 __END__
76 =pod
77  
78 =head1 NAME
79  
80 YATG::Retrieve::Disk - Retrieve a set of data stored by YATG::Store::Disk
81  
82 =head1 VERSION
83  
84 version 5.140510
85  
86 =head1 DESCRIPTION
87  
88 You can load this module to retrieve a set of data which has previously been
89 stored by YATG::Store::Disk. An implementation of this process is given in the
90 CGI bundled with this distribution, which displays results of SNMP polls.
91  
92 For more information on the data storage format, see L<YATG::Store::Disk>.
93  
94 =head1 REQUIREMENTS
95  
96 Install the following additional modules to use this plugin:
97  
98 =over 4
99  
100 =item *
101  
102 L<Tile::File::FixedRecLen>
103  
104 =item *
105  
106 L<integer>
107  
108 =item *
109  
110 L<Time::Local>
111  
112 =item *
113  
114 L<Fcntl>
115  
116 =back
117  
118 =head1 USAGE
119  
120 There is one subroutine, C<retrieve()> which takes seven parameters:
121  
122 =over 4
123  
124 =item C<config>
125  
126 Load up your C<yatg_updater> config using the following idiom, and pass the
127 result in this parameter (assuming the file is named C<yatg.yml>):
128  
129 use YATG::Config;
130 YATG::Config->Defaults->{'no_validation'} = 1;
131
132 my $yatg_conf = YATG::Config->parse('yatg.yml')
133 || die 'failed to load yatg config';
134
135 # now pass in $yatg_conf
136  
137 =item C<device>
138  
139 This should be the IP address of a device.
140  
141 =item C<port>
142  
143 This should be the port name, for example C<GigabitEthernet5/1>.
144  
145 =item C<leaf>
146  
147 This should be the SNMP leaf name (and not the OID), for example
148 C<ifHCInOctets>.
149  
150 =item C<start>
151  
152 This should be a UNIX timestamp (i.e. seconds since the epoch) representing
153 the start point for retrieved results.
154  
155 =item C<end>
156  
157 This should be a UNIX timestamp (i.e. seconds since the epoch) representing
158 the end point for retrieved results.
159  
160 =item C<step>
161  
162 This parameter is optional. If not specified, the filename of the result set
163 encodes the polling interval which will then be used for the returned results
164 interval (i.e. one data point per C<step> seconds returned).
165  
166 Alternatively you can pass a number of seconds in this parameter and the
167 module will do its best to provide one data point per C<step> seconds in the
168 returned results.
169  
170 =back
171  
172 The subroutine will die if it encounters difficulty opening the data file or
173 extracting results from it, so use an C<eval{};> construct or similar.
174  
175 Otherwise, the return value is an array reference of results corresponding to
176 the data points you requested with start, end and step.
177  
178 =head1 SEE ALSO
179  
180 =over 4
181  
182 =item L<Tie::File::FixedRecLen>
183  
184 =back
185  
186 =head1 AUTHOR
187  
188 Oliver Gorwits <oliver@cpan.org>
189  
190 =head1 COPYRIGHT AND LICENSE
191  
192 This software is copyright (c) 2014 by University of Oxford.
193  
194 This is free software; you can redistribute it and/or modify it under
195 the same terms as the Perl 5 programming language system itself.
196  
197 =cut
198