YATG/Store/Memcached.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 83 CC 3 SCAR 19.5
CC
3
Cov
0%
store
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 7.0
1 package YATG::Store::Memcached;
2 {
3 $YATG::Store::Memcached::VERSION = '5.140510';
4 }
5  
6 use strict;
7 use warnings FATAL => 'all';
8  
9 use Cache::Memcached;
10  
11 sub store {
12 my ($config, undef, $results) = @_;
13  
14 die "Must specify list of cache server(s)\n"
15 unless ref $config->{cache_memcached}->{servers} eq 'ARRAY'
16 and scalar @{$config->{cache_memcached}->{servers}} > 0;
17  
18 # get connection to Memcached server
19 my $m = eval {
20 Cache::Memcached->new( $config->{cache_memcached} )
21 } or die "yatg: FATAL: memcached initialization failed: $@\n";
22  
23 # results look like this:
24 # $results->{host}->{leaf}->{port} = {value}
25 my $TTL = $config->{yatg}->{interval} || 300;
26 $TTL += (int (20 / 100 * $TTL)); # 20 p/c breathing space for splayed storage
27 # $TTL = 2 * $TTL; # see if this helps nagios
28  
29 eval { $m->set('yatg_devices', [keys %$results], $TTL) }
30 or warn "yatg: failed to store 'yatg_devices' to memcached\n";
31  
32 # send results
33 foreach my $device (keys %$results) {
34 foreach my $leaf (keys %{$results->{$device}}) {
35 eval { $m->set("ports_for:$device",
36 [keys %{$results->{$device}->{$leaf}}], $TTL) }
37 or warn "yatg: failed to store 'ports_for:$device' to memcached\n";
38  
39 foreach my $port (keys %{$results->{$device}->{$leaf}}) {
40  
41 (my $key = join ':', $device, $leaf, $port) =~ s/\s/_/g;
42 my $val = $results->{$device}->{$leaf}->{$port} || 0;
43  
44 if (grep m/^diff$/, @{$config->{yatg}->{oids}->{$leaf}}) {
45 my $oval = $m->get($key) || '0:0';
46 my (undef, $old) = split ':', $oval;
47 $old ||= $val;
48 $val = "$old:$val";
49 }
50  
51 eval { $m->set($key, $val, $TTL) }
52 or warn "yatg: failed to store '$key' to memcached\n";
53 } # port
54 } # leaf
55 } # device
56  
57 return 1;
58 }
59  
60 1;
61  
62 # ABSTRACT: Back-end module to store polled data to a Memcached
63  
64  
65 __END__