|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +# Parameters supported: |
| 4 | +# |
| 5 | +# config |
| 6 | +# autoconf |
| 7 | +# |
| 8 | +# Magic markers: |
| 9 | +#%# family=auto |
| 10 | +#%# capabilities=autoconf |
| 11 | + |
| 12 | +use strict; |
| 13 | +use warnings; |
| 14 | +use LWP; |
| 15 | +use JSON qw/decode_json/; |
| 16 | + |
| 17 | +=head1 NAME |
| 18 | +
|
| 19 | +logstash_jvm_pools_size - A munin plugin that collects jvm pools size stats of your Logstash instances |
| 20 | +
|
| 21 | +=head1 APPLICABLE SYSTEMS |
| 22 | +
|
| 23 | +Logstash |
| 24 | +
|
| 25 | +=head1 CONFIGURATION |
| 26 | +
|
| 27 | + [logstash_*] |
| 28 | + env.host 127.0.0.1 |
| 29 | + env.port 9600 |
| 30 | +
|
| 31 | +=head1 BUGS |
| 32 | +
|
| 33 | +None known so far. If you find any, let me know. |
| 34 | +
|
| 35 | +=head1 AUTHOR |
| 36 | +
|
| 37 | +Original Author: |
| 38 | +Timothy Messier (t0m) - c<< <tim.messier@gmail.com> >> |
| 39 | +Kentaro Yoshida - https://github.com/y-ken |
| 40 | +
|
| 41 | +Modified by Hirokazu MORIKAWA <morikw2@gmail.com> |
| 42 | +
|
| 43 | +=cut |
| 44 | + |
| 45 | +my $host = exists $ENV{'host'} ? $ENV{'host'} : 'localhost'; |
| 46 | +my $port = exists $ENV{'port'} ? $ENV{'port'} : 9600; |
| 47 | + |
| 48 | +my $ua = LWP::UserAgent->new; |
| 49 | +$ua->timeout(10); |
| 50 | + |
| 51 | +sub get_json_from_url { |
| 52 | + my $uri = shift; |
| 53 | + my $res = $ua->get($uri, 'Content-Type' => 'application/json' ); |
| 54 | + Carp::confess($res->code . " for " . $uri) unless $res->is_success; |
| 55 | + my $data = do { local $@; eval { decode_json($res->content) } }; |
| 56 | + die("Could not decode JSON from: " . $res->content) unless $data; |
| 57 | + return $data; |
| 58 | +} |
| 59 | + |
| 60 | +my $data = get_json_from_url("http://$host:$port/_node/stats/jvm"); |
| 61 | +my %out = (young_used => 0, young_peak => 0, survivor_used => 0, survivor_peak => 0, old_used => 0, old_peak => 0); |
| 62 | + |
| 63 | +$out{young_used} += $data->{jvm}{mem}{pools}{young}{used_in_bytes}; |
| 64 | +$out{young_peak} += $data->{jvm}{mem}{pools}{young}{peak_used_in_bytes}; |
| 65 | + |
| 66 | +$out{survivor_used} += $data->{jvm}{mem}{pools}{survivor}{used_in_bytes}; |
| 67 | +$out{survivor_peak} += $data->{jvm}{mem}{pools}{survivor}{peak_used_in_bytes}; |
| 68 | + |
| 69 | +$out{old_used} += $data->{jvm}{mem}{pools}{old}{used_in_bytes}; |
| 70 | +$out{old_peak} += $data->{jvm}{mem}{pools}{old}{peak_used_in_bytes}; |
| 71 | + |
| 72 | +if ($ARGV[0] and $ARGV[0] eq 'config') { |
| 73 | + print "graph_title Logstash (port $port) JVM pools size\n"; |
| 74 | + print "graph_category logstash\n"; |
| 75 | + print "graph_vlabel bytes\n"; |
| 76 | + |
| 77 | + foreach my $name (keys %out) { |
| 78 | + print "$name.label $name\n"."$name.type GAUGE\n"; |
| 79 | + } |
| 80 | +} |
| 81 | +elsif (!$ARGV[0] || $ARGV[0] eq 'autoconf') { |
| 82 | + foreach my $name (keys %out) { |
| 83 | + print "$name.value " . $out{$name} . "\n"; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +exit(0); |
0 commit comments