| 1 | #!/usr/local/bin/perl | ||
| 2 | # PODNAME: es-graphite-dynamic.pl | ||
| 3 | # ABSTRACT: Dynamically gather metrics and send to graphite | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use App::ElasticSearch::Utilities qw(es_connect); | ||
| 8 | use App::ElasticSearch::Utilities::Metrics; | ||
| 9 | use CLI::Helpers qw(:all); | ||
| 10 | use Getopt::Long qw(:config no_ignore_case no_ignore_case_always); | ||
| 11 | use IO::Socket::INET; | ||
| 12 | use Pod::Usage; | ||
| 13 | use Ref::Util qw(is_hashref is_arrayref); | ||
| 14 | |||
| 15 | #------------------------------------------------------------------------# | ||
| 16 | # Argument Collection | ||
| 17 | my %opt; | ||
| 18 | GetOptions(\%opt, | ||
| 19 | 'ignore=s', | ||
| 20 | 'carbon-base=s', | ||
| 21 | 'carbon-proto=s', | ||
| 22 | 'carbon-server=s', | ||
| 23 | 'carbon-port=i', | ||
| 24 | 'prefix=s', | ||
| 25 | 'no-prefix', | ||
| 26 | 'help|h', | ||
| 27 | 'manual|m', | ||
| 28 | ); | ||
| 29 | |||
| 30 | #------------------------------------------------------------------------# | ||
| 31 | # Documentations! | ||
| 32 | pod2usage(1) if $opt{help}; | ||
| 33 | pod2usage(-exitstatus => 0, -verbose => 2) if $opt{manual}; | ||
| 34 | |||
| 35 | #------------------------------------------------------------------------# | ||
| 36 | # Argument Sanitazation | ||
| 37 | |||
| 38 | |||
| 39 | # Ignore uninteresting metrics | ||
| 40 | my @_IGNORE = (); | ||
| 41 | push @_IGNORE, split(/,/, $opt{ignore}) if exists $opt{ignore}; | ||
| 42 | my %_IGNORE = map { $_ => 1 } @_IGNORE; | ||
| 43 | # Merge options into config | ||
| 44 | my %cfg = ( | ||
| 45 | 'carbon-proto' => 'tcp', | ||
| 46 | 'carbon-base' => 'general.es', | ||
| 47 | %opt, | ||
| 48 | ); | ||
| 49 | |||
| 50 | #------------------------------------------------------------------------# | ||
| 51 | # Globals | ||
| 52 | my $TIME = time; | ||
| 53 | my $Fetcher = App::ElasticSearch::Utilities::Metrics->new( | ||
| 54 | connection => es_connect(), | ||
| 55 | %_IGNORE ? ( | ||
| 56 | ignore => [ sort keys %_IGNORE ] | ||
| 57 | ) : (), | ||
| 58 | ); | ||
| 59 | |||
| 60 | #------------------------------------------------------------------------# | ||
| 61 | # Carbon Socket Creation | ||
| 62 | my $carbon_socket; | ||
| 63 | if( exists $cfg{'carbon-server'} and length $cfg{'carbon-server'} ) { | ||
| 64 | my %valid_protos = ( tcp => 1, udp => 1 ); | ||
| 65 | die "invalid protocol specified: $cfg{'carbon-proto'}\n" unless exists $valid_protos{$cfg{'carbon-proto'}}; | ||
| 66 | $carbon_socket = IO::Socket::INET->new( | ||
| 67 | PeerAddr => $cfg{'carbon-server'}, | ||
| 68 | PeerPort => $cfg{'carbon-port'} || 2003, | ||
| 69 | Proto => $cfg{'carbon-proto'}, | ||
| 70 | ); | ||
| 71 | die "unable to connect to carbon server: $!" unless defined $carbon_socket && $carbon_socket->connected; | ||
| 72 | } | ||
| 73 | |||
| 74 | #------------------------------------------------------------------------# | ||
| 75 | # Collect and Decode the Cluster Statistics | ||
| 76 | my @metrics = sort map { "$_->{key} $_->{value}" } @{ $Fetcher->get_metrics }; | ||
| 77 | if( !@metrics ) { | ||
| 78 | output({color=>'red'}, "Error retrieving metrics"); | ||
| 79 | exit 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | #------------------------------------------------------------------------# | ||
| 83 | # Send output to appropriate channels | ||
| 84 | for ( @metrics ) { | ||
| 85 | # Format | ||
| 86 | my $prefix = exists $cfg{prefix} ? $cfg{prefix} : join('.', $cfg{'carbon-base'}, $Fetcher->node_details->{name}); | ||
| 87 | s/^/$prefix./ unless $cfg{'no-prefix'}; | ||
| 88 | s/$/ $TIME\n/; | ||
| 89 | |||
| 90 | # Send the Data | ||
| 91 | if( defined $carbon_socket && $carbon_socket->connected) { | ||
| 92 | $carbon_socket->send( $_ ); | ||
| 93 | verbose($_); | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | output({data=>1},$_); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | __END__ |