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