Riemann/Query.pm

filter: Statementstmt 75.6% 112 / 148 Branchbran 56.9% 33 / 58 Conditioncond 42.8% 6 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 4 totaltotal 67.4% 168 / 249 CC 40 SCAR 35.3
CC
40
Cov
69%
my_get
65.8
_get_riemann_attribute
12.0
_dispatch
3.3
CRAP 34.3
↑ Summary Riemann.pm »
1 package Collectd::Plugins::Riemann::Query;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Collectd qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Collectd::Plugins::Common qw(recurse_config);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Riemann::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #use DDP {
11 # deparse => 1,
12 # class => {
13 # expand => 'all'
14 # }
15 #};
16  
17 3 my %opt = (
18 Server => "127.0.0.1",
19 Port => 5555,
20 Protocol => 'TCP',
21 Type => 'gauge',
22 PluginInstance => '',
23 TypeInstance => '',
24 PluginFrom => 'plugin',
25 PluginInstanceFrom => 'plugin_instance',
26 TypeFrom => 'ds_type',
27 TypeInstanceFrom => 'type_instance',
28 );
29  
30 =head1 NAME
31  
32 Collectd::Plugins::Riemann::Query - Collectd plugin for querying Riemann Events
33  
34 =head1 SYNOPSIS
35  
36 To be used with L<Collectd>.
37  
38 =over 8
39  
40 =item From the collectd configfile
41  
42 <LoadPlugin "perl">
43 Globals true
44 </LoadPlugin>
45  
46 <Plugin "perl">
47 BaseName "Collectd::Plugins"
48 LoadPlugin "Riemann::Query"
49 <Plugin "Riemann::Query">
50 Host myriemann
51 Port 5555
52 Protocol TCP
53 # Static plugin metadata
54 <Plugin foo>
55 Query "tagged \"foo\" and service =~ \"bar%\""
56 Plugin foo
57 PluginInstance bar
58 Type gauge
59 </Plugin>
60 # plugin metadata from riemann attributes
61 <Plugin>
62 Query "tagged \"aggregation\""
63 PluginFrom plugin
64 PluginInstanceFrom plugin_instance
65 TypeFrom ds_type
66 TypeInstanceFrom type_instance
67 </Plugin>
68 </Plugin>
69 </Plugin>
70  
71 =back
72
73 =head1 Root block configuration options
74  
75 =over 8
76  
77 =item Host STRING
78  
79 riemann host to query. defaults to localhost
80  
81 =item Port STRING
82  
83 riemann port to query. defaults to 5555
84  
85 =item Protocol STRING
86  
87 defaults to TCP
88  
89 =back
90  
91 =head1 Plugin block configuration options
92  
93 =over 8
94  
95 =item Query STRING
96  
97 Riemann Query. Mandatory
98  
99 =item Host STRING
100  
101 Static host part of collectd plugin. If unset, the host part of the riemann event will be used instead.
102  
103 =item PluginFrom/TypeFrom/PluginInstanceFrom/TypeInstanceFrom STRING
104  
105 Dynamic plugin metadata: riemann attribute to be used to set corresponding collectd metadata. service and host are also possible. Defaults to plugin, type, plugin_instance and type_instance respectively.
106  
107 =item Plugin/Type/PluginInstance/TypeInstance STRING
108  
109 Will be used instead if no *From counterpart is used or found in riemann event. Can be used as a fallback. Default for Type is gauge and for Plugin is riemann service of the event.
110  
111 =back
112  
113 =head1 SUBROUTINES
114  
115 Please refer to the L<Collectd> documentation.
116 Or C<man collectd-perl>
117  
118 =head1 FILES
119  
120 /etc/collectd.conf
121 /etc/collectd.d/
122  
123 =head1 SEE ALSO
124  
125 Collectd, collectd-perl, collectd
126  
127 =cut
128  
129 3 my $plugin_name = "Riemann::Query";
130 3 my $r;
131  
132 3 plugin_register(TYPE_CONFIG, $plugin_name, 'my_config');
133 3 plugin_register(TYPE_READ, $plugin_name, 'my_get');
134 3 plugin_register(TYPE_INIT, $plugin_name, 'my_init');
135  
136 3 ▶sub my_init {
Subroutine my_init: called CC=1 SCAR=0.0
Pod undocumented
137 3 1;
138 }
139  
140 56 ▶sub my_log {
Subroutine my_log: called CC=1 SCAR=0.0
Pod undocumented
141 56 plugin_log shift @_, join " ", "plugin=".$plugin_name, @_;
142 }
143  
144 3 ▶sub my_config {
Subroutine my_config: called CC=2 SCAR=7.1
Pod undocumented
145 3 my (undef,$o) = recurse_config($_[0]);
146 3 ▶ _validate_config($o) or return;
Branch: 1 branch
BranchTrueFalseTotal
_validate_config($o) or return 3 0 3
147 3 %opt = (%opt,%$o);
148 }
149  
150 3 ▶sub my_get {
Subroutine my_get: called CC=27 SCAR=41.9
Pod undocumented
151 3 ▶ unless (ref $r eq "Riemann::Client") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $r eq "Riemann::Client") 3 0 3
152 3 my_log(LOG_DEBUG, "get: initializing riemann client");
153 $r = Riemann::Client->new(
154 host => $opt{Host},
155 port => $opt{Port},
156 proto => $opt{Protocol},
157 )
158 3 }
159 3 my_log(LOG_DEBUG, "get: fetching data");
160  
161  
162 #$VAR8 = [
163 # {
164 # 'PluginInstance' => 'bar',
165 # 'Type' => 'gauge',
166 # 'Query' => 'tagged "foo" and service =~ "bar%"'
167 # },
168 # {
169 # 'TypeFrom' => 'ds_type',
170 # 'PluginInstanceFrom' => 'plugin_instance',
171 # 'Query' => 'tagged "aggregation"',
172 # 'TypeInstanceFrom' => 'type_instance',
173 # 'PluginFrom' => 'plugin'
174 # }
175 # ];
176  
177 3 my @Plugins;
178 3 ▶ if (ref $opt{Plugin} eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $opt{'Plugin'} eq 'ARRAY') { } 0 3 3
elsif (ref $opt{'Plugin'} eq 'HASH') { } 3 0 3
179 0 @Plugins = @{$opt{Plugin}}
180 } elsif ( ref $opt{Plugin} eq "HASH") {
181 @Plugins = ($opt{Plugin})
182 3 } else {
183 0 my_log(LOG_ERR, "get: internal configuration problem: 'Plugin' must be hash or array");
184 return
185 0 }
186 3 my $pi = -1;
187 3 PLUGIN: for my $Plugin (@Plugins) {
188 3 $pi++;
189 3 my $res;
190 3 my $query = $Plugin -> {Query};
191 3 ▶ unless (defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query) 0 3 3
192 0 my_log(LOG_ERR, "get: no query defined for plugin[$pi]. ignoring");
193 next PLUGIN
194 0 }
195 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
196 3 $res = $r -> query($Plugin -> {Query});
197 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 my_log(LOG_ERR, "get: problem fetching data for query `$query`", $_);
199 0 return;
200 3 };
201 3 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 3 3
202 0 my_log(LOG_ERR, "get: empty message for query `$query`");
203 next PLUGIN
204 0 }
205 3 my $events = $res -> {events};
206 3 ▶ unless ($events) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($events) 0 3 3
207 0 my_log(LOG_INFO, "get: query `$query` returned no events");
208 next PLUGIN
209 0 }
210 3 ▶ unless (ref $events eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $events eq "ARRAY") 0 3 3
211 0 my_log(LOG_ERR, "get: events not array for query `$query`");
212 0 return;
213 }
214 3 for my $event (@$events) {
215 6 ▶ my $host = $Plugin->{Host} || $event -> {host} || "nil";
Condition40%2/5
$Plugin->{'Host'} || $event->{'host'} || 'nil'
l!l
60
$Plugin->{"Host"} or $event->{"host"}
l!l&&r!l&&!r
060
MC/DC0%0/2
$Plugin->{'Host'} || $event->{'host'} || 'nil'
$Plugin->{"Host"} $event->{"host"}
Truth table33%1/3
A or B
A= $Plugin->{"Host"}B= $event->{"host"}
ABresult
1X1
011
000
216 6 _sanitize($host);
217 6 my %plugin;
218 6 for my $k (qw(Type TypeInstance Plugin PluginInstance)) {
219 24 ▶ if (exists($Plugin->{"${k}From"})) { # metadata from riemann
Branch: 4 branches
BranchTrueFalseTotal
if (exists $$Plugin{"${k}From"}) { } 8 16 24
elsif (exists $Plugin->{$k}) { } 4 12 16
elsif (defined $opt{"${k}From"}) { } 12 0 12
elsif (defined $opt{$k}) { } 0 0 0
220 8 my $ik = "${k}From";
221 8 my $attr = _get_riemann_attribute($event,$Plugin->{$ik});
222 8 ▶ if (defined($attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) { } 3 5 8
223 3 my_log(LOG_DEBUG, "Inferring `$k` using `$ik=$attr` option value for query `$query`.");
224 3 $plugin{_plug2cb($k)} = $attr
225 } else {
226 5 my_log(LOG_DEBUG, "Not inferring `$k` using `$ik` option value for query `$query`.");
227 }
228 } elsif (exists($Plugin->{$k})) { # static metadata
229 4 my $ik = $k;
230 4 my $v = $Plugin->{$ik};
231 4 my_log(LOG_DEBUG, "Inferring `$k` using `$ik=$v` option value for query `$query`.");
232 4 $plugin{_plug2cb($k)} = $v;
233 } elsif (defined($opt{"${k}From"})) {
234 12 my $ik = "${k}From";
235 12 my $attr = _get_riemann_attribute($event,$opt{$ik});
236 12 ▶ if (defined($attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) { } 4 8 12
237 4 my_log(LOG_DEBUG, "Inferring `$k` using default `$ik=$attr` option value for query `$query`.");
238 4 ▶ $plugin{_plug2cb($k)} = $attr if defined($attr);
Branch: 1 branch
BranchTrueFalseTotal
if defined $attr 4 0 4
239 } else {
240 8 my_log(LOG_DEBUG, "Not inferring `$k` using default `$ik` option value for query `$query`.");
241 }
242 } elsif (defined($opt{$k})) {
243 0 my $ik = $k;
244 0 my $v = $opt{$ik};
245 0 my_log(LOG_DEBUG, "Inferring `$k` using default `$ik=$v` option value for query `$query`.");
246 0 $plugin{_plug2cb($k)} = $v;
247 } else {
248 0 my_log(LOG_INFO, "failed to infer `${k}` for query `$query`. Will ignore query results");
249 next PLUGIN
250 0 }
251 }
252 6 for my $k (qw/Plugin Type/) {
253 11 ▶ unless (defined $plugin{_plug2cb($k)}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $plugin{_plug2cb($k)}) 3 8 11
254 3 my_log(LOG_INFO, "Key `$k` is empty for query `$query`. Will ignore query results");
255 next PLUGIN
256 3 }
257 }
258 3 my $ttl = $event -> {ttl};
259 3 my $interval = plugin_get_interval();
260 3 ▶ if ($ttl && $interval gt $ttl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttl and $interval gt $ttl) 0 3 3
Condition33%1/3
$ttl and $interval gt $ttl
!ll&&!rl&&r
300
MC/DC0%0/2
$ttl and $interval gt $ttl
$ttl $interval gt $ttl
Truth table33%1/3
A and B
A= $ttlB= $interval gt $ttl
ABresult
0X0
100
111
261 0 my_log(LOG_INFO, "TTL ($ttl) for event returned by query `$query` is smaller than collectd interval ($interval)");
262 }
263 3 my $metric;
264 3 ▶ if (exists $event -> {metric_d}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $event->{'metric_d'}) { } 0 3 3
elsif (exists $event->{'metric_f'}) { } 3 0 3
elsif (exists $event->{'metric_sint64'}) { } 0 0 0
265 $metric = $event -> {metric_d}
266 0 } elsif (exists $event -> {metric_f}) {
267 $metric = $event -> {metric_f}
268 3 } elsif (exists $event -> {metric_sint64}) {
269 $metric = $event -> {metric_sint64}
270 0 } else {
271 0 my $p_s = join(',',%plugin);
272 0 my_log(LOG_INFO, "get: event `$p_s` for query `$query` has no metric: ignoring");
273 next PLUGIN
274 0 }
275 3 _dispatch($host,\%plugin,$metric);
276 }
277 }
278 3 1;
279 }
280  
281 3 ▶sub _validate_config {
Subroutine _validate_config: called CC=2 SCAR=9.2
282 3 my $o = shift;
283 3 ▶ unless (exists($o->{Plugin})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $o->{"Plugin"}) 0 3 3
284 0 my_log(LOG_ERR, "missing 'Plugin' block in configuration");
285 return
286 0 }
287
288 }
289  
290 6 ▶sub _sanitize ($) {
Subroutine _sanitize: called CC=1 SCAR=0.0
291 6 map { s/ /_/g } @_;
292 }
293  
294 20 ▶sub _get_riemann_attribute ($$) {
Subroutine _get_riemann_attribute: called CC=9 SCAR=24.8
295 20 my ($evt, $key) = @_;
296 20 ▶ unless ($evt -> isa('Event')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($evt->isa("Event")) 0 20 20
297 0 my_log(LOG_ERR, "_get_riemann_attribute event is garbled");
298 return
299 0 }
300 20 ▶ unless ($key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 20 20
301 0 my_log(LOG_ERR, "_get_riemann_attribute arg2 empty");
302 return
303 0 }
304 20 ▶ if ($key eq 'service' or $key eq 'host') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'service' or $key eq 'host') { } 0 20 20
Condition33%1/3
$key eq "service" or $key eq "host"
l!l&&r!l&&!r
0020
MC/DC0%0/2
$key eq "service" or $key eq "host"
$key eq "service" $key eq "host"
Truth table33%1/3
A or B
A= $key eq "service"B= $key eq "host"
ABresult
1X1
011
000
305 0 return $evt -> {$key};
306 } else {
307 20 my $attributes = $evt -> {attributes};
308 20 ▶ if ($attributes && ref $attributes eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes and ref $attributes eq 'ARRAY') { } 10 10 20
Condition66%2/3
$attributes and ref $attributes eq "ARRAY"
!ll&&!rl&&r
10010
MC/DC50%1/2
$attributes and ref $attributes eq "ARRAY"
$attributes ref $attributes eq "ARRAY"
Truth table66%2/3
A and B
A= $attributesB= ref $attributes eq "ARRAY"
ABresult
0X0
100
111
309 10 for my $attr (@$attributes) {
310 23 ▶ if ($attr -> {key} eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"key"} eq $key) 7 16 23
311 return $attr -> {value}
312 7 }
313 }
314 } else {
315 10 my_log(LOG_DEBUG, "_get_riemann_attribute no attributes for event");
316 }
317 13 my_log(LOG_DEBUG, "_get_riemann_attribute attribute `$key` not found for event");
318 }
319 return
320 13 }
321  
322 3 ▶sub _dispatch ($$$) {
Subroutine _dispatch: called CC=3 SCAR=12.0
323 3 my $host = shift;
324 3 my $p = shift;
325 3 my %plugin = %$p;
326 3 my $metric = shift;
327 3 $plugin{host} = $host;
328 3 $plugin{values} = [ $metric ];
329 3 my $ret = plugin_dispatch_values(\%plugin);
330 3 ▶ unless ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 0 3 3
331 0 my $p_s = join(',',%plugin);
332 0 ▶ my_log(LOG_INFO, "dispatch error: `$p_s`") unless ($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
333 }
334 3 return $ret;
335 }
336  
337 22 ▶sub _plug2cb {
Subroutine _plug2cb: called CC=2 SCAR=7.4
338 22 my $p = shift;
339 22 my %plugin_cb_mapping = (
340 Plugin => 'plugin',
341 PluginInstance => 'plugin_instance',
342 Type => 'type',
343 TypeInstance => 'type_instance'
344 );
345 22 ▶ if (exists($plugin_cb_mapping{$p})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $plugin_cb_mapping{$p}) { } 22 0 22
346 22 $plugin_cb_mapping{$p};
347 } else {
348 undef
349 0 }
350 }
351  
352 3 1;
353  
↑ Summary Riemann.pm »