| 1 | package Salvation::AnyNotify::Plugin::Graphite; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Salvation::AnyNotify::Plugin'; | ||
| 7 | |||
| 8 | use URI (); | ||
| 9 | use Socket 'AF_INET', 'AF_INET6'; | ||
| 10 | use AnyEvent (); | ||
| 11 | use Salvation::TC (); | ||
| 12 | use Net::Graphite::Reader (); | ||
| 13 | use Salvation::Method::Signatures; | ||
| 14 | |||
| 15 | our $VERSION = 0.01; | ||
| 16 | |||
| 17 | sub graphite_ttl { 60 } | ||
| 18 | |||
| 19 | method start() { | ||
| 20 | |||
| 21 | $self -> { 'monitor' } = []; | ||
| 22 | } | ||
| 23 | |||
| 24 | method monitor( Str{1,} :plugin!, ArrayRef|HashRef :spec!, Int :interval!, Int :after ) { | ||
| 25 | |||
| 26 | unless( exists $self -> { "monitor:${plugin}" } ) { | ||
| 27 | |||
| 28 | my $object = $self -> load_plugin( | ||
| 29 | infix => 'Monitor', base_name => $plugin, | ||
| 30 | ); | ||
| 31 | |||
| 32 | die( "Graphite has no monitoring available for ${plugin}" ) unless defined $object; | ||
| 33 | |||
| 34 | $self -> { "monitor:${plugin}" } = $object; | ||
| 35 | } | ||
| 36 | |||
| 37 | my @args = (); | ||
| 38 | |||
| 39 | if( ref( $spec ) eq 'HASH' ) { | ||
| 40 | |||
| 41 | @args = %$spec; | ||
| 42 | |||
| 43 | } else { | ||
| 44 | |||
| 45 | @args = @$spec; | ||
| 46 | } | ||
| 47 | |||
| 48 | my $watcher = $self -> { "monitor:${plugin}" } -> add( @args ); | ||
| 49 | |||
| 50 | Salvation::TC -> assert( $watcher, 'CodeRef' ); | ||
| 51 | |||
| 52 | push( @{ $self -> { 'monitor' } }, AnyEvent -> timer( | ||
| 53 | cb => $watcher, | ||
| 54 | interval => $interval, | ||
| 55 | ( defined $after ? ( after => $after ) : () ), | ||
| 56 | ) ); | ||
| 57 | |||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | method query( Str{1,}|ArrayRef[Str{1,}]{1,} :target!, Str{1,} :from!, Str{1,} :to! ) { | ||
| 62 | |||
| 63 | my $now = time(); | ||
| 64 | my $instances = $self -> { 'instances' } //= {}; | ||
| 65 | |||
| 66 | if( | ||
| 67 | exists $instances -> { $$ } | ||
| 68 | && ( ( $instances -> { $$ } -> { 'time' } + $self -> graphite_ttl() ) < $now ) | ||
| 69 | ) { | ||
| 70 | |||
| 71 | delete( $instances -> { $$ } ); | ||
| 72 | } | ||
| 73 | |||
| 74 | unless( exists $instances -> { $$ } ) { | ||
| 75 | |||
| 76 | $instances -> { $$ } = { | ||
| 77 | time => $now, | ||
| 78 | graphite => $self -> get_graphite(), | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $result = eval { $instances -> { $$ } -> { 'graphite' } -> query( | ||
| 83 | target => $target, | ||
| 84 | from => $from, | ||
| 85 | to => $to, | ||
| 86 | ) }; | ||
| 87 | |||
| 88 | if( $@ ) { | ||
| 89 | |||
| 90 | warn $@; | ||
| 91 | delete( $instances -> { $$ } ); | ||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | return $result; | ||
| 96 | } | ||
| 97 | |||
| 98 | method get_graphite() { | ||
| 99 | |||
| 100 | my $hosts = $self -> core() -> config() -> get( 'graphite.hosts' ); | ||
| 101 | |||
| 102 | Salvation::TC -> assert( $hosts, 'ArrayRef[HashRef( | ||
| 103 | Str :host!, | ||
| 104 | Int :port! | ||
| 105 | )]' ); | ||
| 106 | |||
| 107 | foreach my $spec ( @$hosts ) { | ||
| 108 | |||
| 109 | my $uri = URI -> new( $spec -> { 'host' } ); | ||
| 110 | |||
| 111 | unless( $uri -> scheme() ) { | ||
| 112 | |||
| 113 | $uri = URI -> new( 'http://' . $spec -> { 'host' } ); | ||
| 114 | } | ||
| 115 | |||
| 116 | my $scheme = $uri -> scheme(); | ||
| 117 | |||
| 118 | if( | ||
| 119 | ( ( $scheme eq 'http' ) && ( $spec -> { 'port' } == 80 ) ) | ||
| 120 | || ( ( $scheme eq 'https' ) && ( $spec -> { 'port' } == 443 ) ) | ||
| 121 | ) { | ||
| 122 | |||
| 123 | $uri -> port( undef ); | ||
| 124 | |||
| 125 | } else { | ||
| 126 | |||
| 127 | $uri -> port( $spec -> { 'port' } ); | ||
| 128 | } | ||
| 129 | |||
| 130 | foreach my $ai ( Socket::getaddrinfo( $uri -> host(), $uri -> port() ) ) { | ||
| 131 | |||
| 132 | next unless Salvation::TC -> is( $ai, 'HashRef( Int :family! )' ); | ||
| 133 | |||
| 134 | if( | ||
| 135 | ( $ai -> { 'family' } == AF_INET ) | ||
| 136 | || ( $ai -> { 'family' } == AF_INET6 ) | ||
| 137 | ) { | ||
| 138 | |||
| 139 | return Net::Graphite::Reader -> new( | ||
| 140 | uri => $uri -> as_string(), | ||
| 141 | ); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | return undef; | ||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | __END__ |