YATG/Store/Memcached.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 72 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__
66 =pod
67  
68 =head1 NAME
69  
70 YATG::Store::Memcached - Back-end module to store polled data to a Memcached
71  
72 =head1 VERSION
73  
74 version 5.140510
75  
76 =head1 DESCRIPTION
77  
78 This module implements part of a callback handler used to store SNMP data into
79 a memcached service. It will be faster than storing to disk, and so is
80 recommended if you do not require historical data.
81  
82 The module will die if it cannot connect to your memcached server, so see
83 below for the configuration guidelines. Note that all example keys here use
84 the namespace prefix of C<yatg:> although this is configurable.
85  
86 One data structure is passed in, which represents a set of results for a set of
87 polled OIDs on some devices. It looks a bit like this:
88  
89 $results->{$device}->{$leaf}->{$port} = {value}
90  
91 In your memcached server, a few well-known keys store lists of polled devices
92 and so on, to help you bootstrap to find stored results.
93  
94 The key C<yatg:yatg_devices> will contain an array reference containing all
95 device IPs provided in the results data.
96  
97 Further, each key of the form C<yatg:ports_for:$device> will contain an array
98 reference containing all ports polled on that device. The port name is not
99 munged in any way. The "port" entity might in fact just be an index value, or
100 C<1> if this OID is not Interface Indexed.
101  
102 Finally, the result of a poll is stored in memcached with a key of the
103 following format:
104  
105 yatg:$device:$leaf:$port
106  
107 Note that the C<$leaf> is the SNMP leaf name and not the OID. That key will be
108 munged to remove whitespace, as that is not permitted in memcached keys.
109  
110 All of the above values are stored with a TTL of the polling interval as
111 gathered from the main C<yatg_updater> configuration.
112  
113 With all this information it is possible to write a script to find all the
114 data stored in the memcache using the two lookup tables and then retrieving
115 the desired keys. There is an example of this in the C<examples/> folder of
116 this distribution, called C<check_interfaces>. It is a Nagios2 check script.
117  
118 =head1 REQUIREMENTS
119  
120 Install the following additonal module to use this plugin:
121  
122 =over 4
123  
124 =item *
125  
126 L<Cache::Memcached>
127  
128 =back
129  
130 =head1 CONFIGURATION
131  
132 In the main C<yatg_updater> configuration, you must provide details of the
133 location of your memcached server. Follow the example (C<yatg.yml>) file in
134 this distribution. Remember you can override the namespace used from the
135 default of C<yatg:>, like so:
136  
137 cache_memcached:
138 namespace: 'my_space:'
139  
140 =head1 SEE ALSO
141  
142 =over 4
143  
144 =item L<Cache::Memcached>
145  
146 =back
147  
148 =head1 AUTHOR
149  
150 Oliver Gorwits <oliver@cpan.org>
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 This software is copyright (c) 2014 by University of Oxford.
155  
156 This is free software; you can redistribute it and/or modify it under
157 the same terms as the Perl 5 programming language system itself.
158  
159 =cut
160