Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Last active February 26, 2016 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazeburo/25e0c232892412fe3a17 to your computer and use it in GitHub Desktop.
Save kazeburo/25e0c232892412fe3a17 to your computer and use it in GitHub Desktop.
$ perl stats_elb.pl --region us-west-2 --elb ProdElb | mkr throw --service production
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use Getopt::Long;
sub get_healthy {
my ($region,$name) = @_;
open(my $pipe, '-|',
'aws','elb','describe-instance-health',
'--region', $region,
'--load-balancer-name', $name
) or die $!;
my $buf = "";
while ( <$pipe> ) {
$buf .= $_;
}
my $data = JSON::decode_json($buf) or die "failed to decode json";
my $total = scalar @{$data->{InstanceStates}};
my $in_service = grep { $_->{State} eq "InService" } @{$data->{InstanceStates}};
my $out_of_service = grep { $_->{State} eq "OutOfService" } @{$data->{InstanceStates}};
my $unknown = grep { $_->{State} eq "Unknown" } @{$data->{InstanceStates}};
my $time = time;
my @ret;
push @ret,["custom.elb-stats-${name}-instances.InService", $in_service];
push @ret,["custom.elb-stats-${name}-instances.OutOfService", $out_of_service];
push @ret,["custom.elb-stats-${name}-instances.Unknown", $unknown];
push @ret,["custom.elb-stats-${name}-instances-rate.InService", $total > 0 ? $in_service / $total * 100 : 0];
push @ret,["custom.elb-stats-${name}-instances-rate.OutOfService", $total > 0 ? $out_of_service / $total * 100: 0];
push @ret,["custom.elb-stats-${name}-instances-rate.Unknown", $total > 0 ? $unknown / $total *100 : 0];
return @ret;
}
sub dt {
my $shift = shift || 0;
my @lt = gmtime(time - $shift);
sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$lt[5]+1900,$lt[4]+1,$lt[3],$lt[2],$lt[1],$lt[0]);
}
sub get_point {
my ($region, $name, $metrics, $statistics) = @_;
#aws cloudwatch get-metric-statistics --namespace AWS/ELB --dimensions Name=LoadBalancerName,Value=production-pascal --metric-name Latency --start-time $(date -u -d '2 minutes ago' +%Y-%m-%dT%TZ) --end-time $(date -u +%Y-%m-%dT%TZ) --period 60 --statistics Average
open(my $pipe, '-|',
'aws','cloudwatch','get-metric-statistics',
'--region', $region,
'--namespace', 'AWS/ELB',
'--dimensions', 'Name=LoadBalancerName,Value='.$name,
'--metric-name', $metrics,
'--start-time', dt(120),
'--end-time', dt(),
'--period', 60,
'--statistics', $statistics
) or die $!;
my $buf = "";
while ( <$pipe> ) {
$buf .= $_;
}
my $data = JSON::decode_json($buf) or die "failed to decode json";
my @points = sort { $a->{Timestamp} cmp $b->{Timestamp} } @{$data->{Datapoints}};
return 0 if @points == 0;
return $points[0]->{$statistics};
}
sub get_stats {
my ($region, $name) = @_;
my @ret;
push @ret,["custom.elb-stats-${name}-latency.second", get_point($region, $name, 'Latency', 'Average')];
push @ret,["custom.elb-stats-${name}-request.count", get_point($region, $name, 'RequestCount', 'Sum')];
push @ret,["custom.elb-stats-${name}-elb-error.5xx", get_point($region, $name, 'HTTPCode_ELB_5XX', 'Sum')];
push @ret,["custom.elb-stats-${name}-elb-error.4xx", get_point($region, $name, 'HTTPCode_ELB_4XX', 'Sum')];
return @ret;
}
my $region = 'ap-northeast-1';
Getopt::Long::Configure ("no_ignore_case");
my $r = GetOptions(
'region=s' => \$region,
'elb=s' => \my $name,
"h|help" => \my $help,
);
if ( !$r || $help || !defined $name) {
print qq!usage: $0 --region ap-northeast-1 --elb elb_name\n!;
exit($help ? 0 : 1);
}
my @ret;
push @ret, get_healthy($region, $name);
push @ret, get_stats($region, $name);
my $time = time();
foreach (@ret) {
print join("\t", @{$_}, $time)."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment