| 1 | package Daemonise::Plugin::Riemann; | ||
| 2 | |||
| 3 | use Mouse::Role; | ||
| 4 | |||
| 5 | # ABSTRACT: Daemonise Riemann plugin | ||
| 6 | |||
| 7 | use Riemann::Client; | ||
| 8 | use Scalar::Util qw(looks_like_number); | ||
| 9 | use Carp; | ||
| 10 | |||
| 11 | |||
| 12 | has 'riemann_host' => ( | ||
| 13 | is => 'rw', | ||
| 14 | isa => 'Str', | ||
| 15 | lazy => 1, | ||
| 16 | default => sub { 'localhost' }, | ||
| 17 | ); | ||
| 18 | |||
| 19 | |||
| 20 | has 'riemann_port' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Int', | ||
| 23 | lazy => 1, | ||
| 24 | default => sub { 5555 }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | |||
| 28 | has 'riemann_proto' => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => 'Str', | ||
| 31 | lazy => 1, | ||
| 32 | default => sub { 'tcp' }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | |||
| 36 | has 'riemann' => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'Riemann::Client', | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | after 'configure' => sub { | ||
| 43 | my ($self, $reconfig) = @_; | ||
| 44 | |||
| 45 | if ($reconfig and exists $self->riemann->{transport}->{socket}) { | ||
| 46 | $self->log("closing riemann socket") if $self->debug; | ||
| 47 | $self->riemann->transport->clear_socket; | ||
| 48 | } | ||
| 49 | |||
| 50 | $self->log("configuring Riemann plugin") if $self->debug; | ||
| 51 | |||
| 52 | if (ref($self->config->{riemann}) eq 'HASH') { | ||
| 53 | foreach my $conf_key ('host', 'port', 'proto') { | ||
| 54 | my $attr = "riemann_" . $conf_key; | ||
| 55 | $self->$attr($self->config->{riemann}->{$conf_key}) | ||
| 56 | if defined $self->config->{riemann}->{$conf_key}; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | $self->riemann( | ||
| 61 | Riemann::Client->new( | ||
| 62 | host => $self->riemann_host, | ||
| 63 | port => $self->riemann_port, | ||
| 64 | proto => $self->riemann_proto, | ||
| 65 | )); | ||
| 66 | |||
| 67 | return; | ||
| 68 | }; | ||
| 69 | |||
| 70 | |||
| 71 | sub graph { | ||
| 72 | my ($self, $service, $state, $metric, $desc, $tags) = @_; | ||
| 73 | |||
| 74 | unless (ref \$service eq 'SCALAR' | ||
| 75 | and defined $service | ||
| 76 | and ref \$state eq 'SCALAR' | ||
| 77 | and defined $state | ||
| 78 | and ref \$metric eq 'SCALAR' | ||
| 79 | and defined $metric) | ||
| 80 | { | ||
| 81 | carp 'missing or wrong type of a mandatory argument! ' | ||
| 82 | . 'usage: $d->graph("$service", "$state", $metric, "$desc")'; | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | unless (looks_like_number($metric)) { | ||
| 87 | carp "metric has to be an integer or float"; | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | if ($desc and ref(\$desc) ne 'SCALAR') { | ||
| 92 | carp "description must be of SCALAR type, ignoring"; | ||
| 93 | undef $desc; | ||
| 94 | } | ||
| 95 | |||
| 96 | if ($self->debug) { | ||
| 97 | $self->log("[riemann] $service.$state: $metric"); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | # $self->async and return; | ||
| 102 | |||
| 103 | eval { | ||
| 104 | $self->riemann( | ||
| 105 | Riemann::Client->new( | ||
| 106 | host => $self->riemann_host, | ||
| 107 | port => $self->riemann_port, | ||
| 108 | proto => $self->riemann_proto, | ||
| 109 | )) unless defined $self->riemann; | ||
| 110 | |||
| 111 | $self->riemann->send({ | ||
| 112 | host => $self->hostname, | ||
| 113 | service => $service, | ||
| 114 | state => $state, | ||
| 115 | metric => $metric, | ||
| 116 | defined $desc ? (description => $desc) : (), | ||
| 117 | ref $tags eq 'ARRAY' ? (tags => $tags) : (), | ||
| 118 | }); | ||
| 119 | }; | ||
| 120 | carp "sending metric failed: $@" if $@; | ||
| 121 | |||
| 122 | # exit; # async | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ |