| 1 | package TheEye::Helper::RabbitMQ; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use Mouse; | ||
| 5 | use LWP::UserAgent; | ||
| 6 | use HTTP::Request; | ||
| 7 | use Test::More; | ||
| 8 | use JSON; | ||
| 9 | |||
| 10 | # ABSTRACT: Graphite plugin for TheEye | ||
| 11 | # | ||
| 12 | our $VERSION = '0.5'; # VERSION | ||
| 13 | |||
| 14 | has 'json' => ( | ||
| 15 | is => 'rw', | ||
| 16 | isa => 'JSON', | ||
| 17 | default => sub { | ||
| 18 | JSON->new->utf8->allow_nonref->allow_blessed->convert_blessed; | ||
| 19 | }, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has 'url' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Str', | ||
| 25 | ); | ||
| 26 | |||
| 27 | has 'realm' => ( | ||
| 28 | is => 'rw', | ||
| 29 | isa => 'Str', | ||
| 30 | ); | ||
| 31 | |||
| 32 | has 'user' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Str', | ||
| 35 | ); | ||
| 36 | |||
| 37 | has 'pass' => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => 'Str', | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | sub get_numbers { | ||
| 44 | my ($self, @ignore) = @_; | ||
| 45 | |||
| 46 | my $ua = LWP::UserAgent->new(); | ||
| 47 | |||
| 48 | my $req = HTTP::Request->new(GET => $self->url . '/api/queues'); | ||
| 49 | $req->authorization_basic($self->user, $self->pass); | ||
| 50 | |||
| 51 | my $res = $ua->request($req); | ||
| 52 | if ($res->is_success) { | ||
| 53 | |||
| 54 | my $json; | ||
| 55 | my $result; | ||
| 56 | eval { $json = $self->json->decode($res->content) }; | ||
| 57 | unless ($@) { | ||
| 58 | foreach my $queue (@{$json}){ | ||
| 59 | my $name = $queue->{vhost} . '/' . $queue->{name}; | ||
| 60 | next if map { $name =~ m/$_/ } @ignore; | ||
| 61 | push( | ||
| 62 | @{$result}, { | ||
| 63 | node => $name, | ||
| 64 | from => time, | ||
| 65 | to => time, | ||
| 66 | resolution => 1, | ||
| 67 | value => $queue->{messages}, | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | return {error => 'Could not parse JSON'} unless $result; | ||
| 72 | return $result; | ||
| 73 | } | ||
| 74 | else { | ||
| 75 | return { error => $res->status_line, }; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | sub byte_to_gb { | ||
| 81 | my ($self, $bytes) = @_; | ||
| 82 | |||
| 83 | return $bytes / 1024 / 1024 / 1024; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | sub byte_to_mb { | ||
| 88 | my ($self, $bytes) = @_; | ||
| 89 | |||
| 90 | return $bytes / 1024 / 1024; | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub test_rabbit { | ||
| 95 | my ($self, $data, $limits) = @_; | ||
| 96 | |||
| 97 | if (ref $data eq 'HASH') { | ||
| 98 | fail("Communication error: " . $data->{error}); | ||
| 99 | } | ||
| 100 | else { | ||
| 101 | foreach my $res (@{$data}) { | ||
| 102 | if (exists $limits->{lower}) { | ||
| 103 | cmp_ok($res->{value}, '>=', $limits->{lower}, | ||
| 104 | "$res->{node} has less than $limits->{lower} $limits->{what} - currently: $res->{value}" | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | if (exists $limits->{upper}) { | ||
| 108 | cmp_ok($res->{value}, '<=', $limits->{upper}, | ||
| 109 | "$res->{node} has more than $limits->{upper} $limits->{what} - currently: $res->{value}" | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | if (exists $limits->{stale}) { | ||
| 113 | cmp_ok($res->{to}, '>=', time - $limits->{stale}, | ||
| 114 | "$res->{node} has stale data (currently $res->{to} seconds old)" | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | __PACKAGE__->meta->make_immutable; | ||
| 123 | |||
| 124 | __END__ |