App-Ylastic-CostAgent

 view release on metacpan or  search on metacpan

bin/ylastic-costagent  view on Meta::CPAN

  facility  => $opts->get_syslog ? $opts->get_syslog : undef,
  to_file   => $opts->get_logpath ? 1 : undef,
  log_pid   => 0,
  debug     => $opts->get_debug,
});

my $app = App::Ylastic::CostAgent->new(
  config_file => $cf,
  logger      => $logger,
  dir         => $opts->get_dir,
  upload      => ! $opts->get_dry_run,
);

exit $app->run;



=pod

=head1 NAME

bin/ylastic-costagent  view on Meta::CPAN


version 0.006

=head1 SYNOPSIS

   ylastic-costagent [OPTIONS] <CONFIG_FILE>
 
   --dir|-d      Directory to hold downloads (default is tempdir)
   --syslog|-s   Syslogd facility for logging (default is none)
   --logpath|-l  Directory to hold a logfile (default is no logfile)
   --dry-run|-n  Download, but don't upload (default is false)
   --debug       Log with extra detail (default is false)
   --help|h      Show summary of options

=head1 DESCRIPTION

This program downloads your Amazon Web Services usage data and uploads it to
your Ylastic account for cost analysis.  It should be run regularly from a cron
job to ensure up-to-date usage and spending history.

=for Pod::Coverage method_names_here

=head1 CONFIGURATION

=head2 Installation

Install L<App::Ylastic::CostAgent> using your regular CPAN client or

lib/App/Ylastic/CostAgent.pm  view on Meta::CPAN

use Time::Piece;
use Time::Piece::Month;
use WWW::Mechanize;

use Object::Tiny qw(
  accounts
  config_file
  dir
  logger
  mech
  upload
  ylastic_id
);

my %URL = (
  ylastic_service_list  => "http://ylastic.com/cost_services.list",
  ylastic_upload_form   => "http://ylastic.com/usage_upload.html",
  aws_usage_report_form => "https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=usage-report",
);

#--------------------------------------------------------------------------#
# new -- constructor
#
# Parameters for new:
#   * config_file -- path to a .ini style configuration file (required)
#   * dir -- a directory to hold downloaded data (defaults to a tempdir)
#   * logger -- a Log::Dispatchouli object (defaults to a null logger)
#   * upload -- whether to upload data to Ylastic (default is false)
#--------------------------------------------------------------------------#

sub new {
  my $class = shift;
  my $self = $class->SUPER::new( @_ );

  croak __PACKAGE__ . " requires a valid 'config_file' argument\n"
    unless $self->config_file && -r $self->config_file;

  $self->{logger} ||= Log::Dispatchouli->new({ident => __PACKAGE__, to_self => 1});
  $self->{dir} ||= File::Temp::tempdir();
  $self->_parse_config;

  return $self;
}

#--------------------------------------------------------------------------#
# run -- downloads and possibly uploads data for all accounts from config
#--------------------------------------------------------------------------#

sub run {
  my $self = shift;

  for my $account ( @{ $self->accounts } ) {
    my $zipfile = $self->_download_usage( $account );
    $self->_upload_usage( $account, $zipfile )
      if $self->upload;
  }

  return 0;
}

#--------------------------------------------------------------------------#
# private
#--------------------------------------------------------------------------#

sub _do_aws_login {

lib/App/Ylastic/CostAgent.pm  view on Meta::CPAN

  my $list = $self->mech->get($URL{ylastic_service_list})->decoded_content;
  chomp $list;
  return $self->{services} = [split q{,}, $list];
}

sub _start_date {
  state $start_date = Time::Piece::Month->new("2010-01-01")->start;
  return $start_date;
}

sub _upload_usage {
  my ($self, $account, $zipfile) = @_;
  $self->_initialize_mech;
  $self->mech->get($URL{ylastic_upload_form});
  $self->mech->submit_form(
    form_name => 'upload',
    fields => {
      file1 => $zipfile,
    }
  );
  $self->logger->log(["Uploaded usage reports to Ylastic for account %s",$account->[0]]);
  return;
}

1;

lib/App/Ylastic/CostAgent.pm  view on Meta::CPAN

This module contains the internal routines for LE<lt>ylastic-costagentE<gt>.  Please
see that for end-user documentation.

=for Pod::Coverage new
run
accounts
config_file
dir
logger
mech
upload
ylastic_id

=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders

=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests by email to C<bug-app-ylastic-costagent at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=App-Ylastic-CostAgent>. You will be automatically notified of any



( run in 1.598 second using v1.01-cache-2.11-cpan-b16cb0d3907 )