TheEye/Helper/Graphite.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 101 CC 10 SCAR 30.9
CC
10
Cov
0%
get_numbers
42.0
test_graphite
30.0
map_services
2.0
CRAP 22.0
1 package TheEye::Helper::Graphite;
2  
3 use 5.010;
4 use Mouse;
5 use LWP::UserAgent;
6 use HTTP::Request;
7 use Test::More;
8  
9 # ABSTRACT: Graphite plugin for TheEye
10 #
11 our $VERSION = '0.5'; # VERSION
12  
13 has 'url' => (
14 is => 'rw',
15 isa => 'Str',
16 );
17  
18 has 'postfix' => (
19 is => 'rw',
20 isa => 'Str',
21 default => '&rawData=true&from=-15min',
22 );
23  
24 has 'user' => (
25 is => 'rw',
26 isa => 'Str',
27 );
28  
29 has 'pass' => (
30 is => 'rw',
31 isa => 'Str',
32 );
33  
34  
35 sub map_services {
36 my ($self, $name) = @_;
37  
38 my $hash = {
39 df_root => {
40 path => '.df-root.df_complex-free.value',
41 convert => 'byte_to_gb'
42 },
43 mem => {
44 path => '.memory.memory-free.value',
45 convert => 'byte_to_mb',
46 },
47 load => {
48 path => '.load.load.shortterm',
49 },
50 rabbit_msg => {
51 path => '.messages',
52 drill_down => 3,
53 },
54 };
55  
56 return $hash->{$name};
57 }
58  
59  
60 sub get_numbers {
61 my ($self, $host, $service) = @_;
62  
63 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
64 my $svc = $self->map_services($service);
65 my $url = $self->url . $host . $svc->{path} . $self->postfix;
66 my $req = HTTP::Request->new(GET => $url );
67 if($self->user){
68 $req->authorization_basic($self->user, $self->pass);
69 }
70  
71 my $res = $ua->request($req);
72 if ($res->is_success) {
73 my $res_txt = $res->content;
74 if(exists $svc->{drill_down}){
75 foreach my $i (1 .. $svc->{drill_down}){
76 $host .= '.*';
77 my $res2 = $ua->get($self->url . $host . $svc->{path} . $self->postfix);
78 $res_txt .= $res2->content;
79 }
80 }
81 my $result;
82 foreach (split(/\n/, $res_txt)) {
83 my ($def, $vals) = split(/\|/, $_);
84 my @values = split(/,/, $vals);
85 my ($node, $from, $to, $resolution) = split(/,/, $def);
86 my $val = pop(@values);
87 $val = pop(@values)
88 if $val =~ m{none}i; # make sure we got something
89 if (exists $svc->{convert}) {
90 my $conv = $svc->{convert};
91 $val = $self->$conv($val);
92 }
93 push(
94 @{$result}, {
95 node => $node,
96 from => $from,
97 to => $to,
98 resolution => $resolution,
99 value => $val,
100 });
101 }
102 return $result;
103 }
104 else {
105 return { error => $res->status_line, };
106 }
107 }
108  
109  
110 sub byte_to_gb {
111 my ($self, $bytes) = @_;
112  
113 return $bytes / 1024 / 1024 / 1024;
114 }
115  
116  
117 sub byte_to_mb {
118 my ($self, $bytes) = @_;
119  
120 return $bytes / 1024 / 1024;
121 }
122  
123  
124 sub test_graphite {
125 my($self, $data, $limits) = @_;
126  
127 if (ref $data eq 'HASH') {
128 fail("Communication error: " . $data->{error});
129 }
130 else {
131 foreach my $res (@{$data}) {
132 if(exists $limits->{lower}){
133 cmp_ok($res->{value}, '>=', $limits->{lower},
134 "$res->{node} has less than $limits->{lower} $limits->{what} - currently: $res->{value}");
135 }
136 if(exists $limits->{upper}){
137 cmp_ok($res->{value}, '<=', $limits->{upper},
138 "$res->{node} has more than $limits->{upper} $limits->{what} - currently: $res->{value}");
139 }
140 if(exists $limits->{stale}){
141 cmp_ok($res->{to}, '>=', time - $limits->{stale},
142 "$res->{node} has stale data (currently $res->{to} seconds old)"
143 );
144 }
145 }
146 }
147  
148 }
149  
150 __PACKAGE__->meta->make_immutable;
151  
152 __END__