ec2-usage-report 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +55 -0
  2. data/lib/ec2-usage-report.rb +65 -0
  3. metadata +79 -0
data/README ADDED
@@ -0,0 +1,55 @@
1
+ = ec2-usage-report
2
+
3
+ == Description
4
+
5
+ ec2-usage-report is a library for acquiring usage report of EC2.
6
+
7
+ == Source Code
8
+
9
+ https://bitbucket.org/winebarrel/ec2-usage-report
10
+
11
+ == Install
12
+
13
+ gem install ec2-usage-report
14
+
15
+ == Example
16
+
17
+ require 'ec2-usage-report'
18
+ require 'yaml'
19
+
20
+ EMAIL_ADDREDD = 'sgwr_dts@yahoo.co.jp'
21
+ PASSWORD = '...'
22
+
23
+ today = Time.now
24
+ _3_days_ago = today - (24 * 60 * 60) * 3
25
+
26
+ ec2_usage_report = EC2UsageReport.new(EMAIL_ADDREDD, PASSWORD)
27
+
28
+ rows = ec2_usage_report.fetch(:start => _3_days_ago, :end => today, :periodType => :days, :operation => 'RunInstances')
29
+
30
+ puts YAML.dump(rows)
31
+
32
+ # (output example)
33
+ # ---
34
+ # - - Service
35
+ # - Operation
36
+ # - UsageType
37
+ # - Resource
38
+ # - StartTime
39
+ # - EndTime
40
+ # - UsageValue
41
+ # - - AmazonEC2
42
+ # - RunInstances
43
+ # - DataTransfer-In-Bytes
44
+ # - ""
45
+ # - 12/11/12 00:00:00
46
+ # - 12/12/12 00:00:00
47
+ # - "405550"
48
+ # - - AmazonEC2
49
+ # - RunInstances
50
+ # - DataTransfer-Out-Bytes
51
+ # - ""
52
+ # - 12/11/12 00:00:00
53
+ # - 12/12/12 00:00:00
54
+ # - "737666"
55
+ # ...
@@ -0,0 +1,65 @@
1
+ require 'csv'
2
+ require 'time'
3
+ require 'mechanize'
4
+
5
+ class EC2UsageReport
6
+ def initialize(email, password)
7
+ @email = email
8
+ @password = password
9
+ end
10
+
11
+ def fetch(options = {})
12
+ options[:end] ||= Time.now
13
+ options[:start] ||= (options[:end] - 7 * 24 * 60 * 60)
14
+ options[:end] = Time.parse(options[:end].to_s)
15
+ options[:start] = Time.parse(options[:start].to_s)
16
+
17
+ options[:periodType] ||= :days
18
+
19
+ agent = Mechanize.new
20
+ agent.user_agent_alias = 'Windows IE 7'
21
+
22
+ page = agent.get('https://aws-portal.amazon.com/gp/aws/developer/account/usage-report.html?productCode=AmazonEC2')
23
+
24
+ agent.page.form_with(:name => 'signIn') do |f|
25
+ f.field_with(:name => 'email').value = @email
26
+ f.field_with(:name => 'password').value = @password
27
+ f.click_button
28
+ end
29
+
30
+ agent.page.form_with(:name => 'usageReportForm') do |f|
31
+ select(f, :timePeriod, 'aws-portal-custom-date-range')
32
+
33
+ select_time(f, 'start', options.delete(:start))
34
+ select_time(f, 'end', options.delete(:end))
35
+
36
+ options.each do |name, value|
37
+ select(f, name, value)
38
+ end
39
+
40
+ f.click_button(f.button_with(:name => 'download-usage-report-csv'))
41
+ end
42
+
43
+ csv_rows = agent.page.kind_of?(Mechanize::File) ? CSV.parse((agent.page.body || '').strip) : nil
44
+
45
+ if csv_rows
46
+ csv_rows = csv_rows.map do |row|
47
+ row.map {|i| i.to_s.strip }
48
+ end
49
+ end
50
+
51
+ return csv_rows
52
+ end # fetch
53
+
54
+ private
55
+
56
+ def select(form, name, value)
57
+ form.field_with(:name => name.to_s).option_with(:value => value.to_s).select
58
+ end
59
+
60
+ def select_time(form, prefix, time)
61
+ select(form, "#{prefix}Month", time.mon)
62
+ select(form, "#{prefix}Day", time.day)
63
+ select(form, "#{prefix}Year", time.year)
64
+ end
65
+ end # EC2UsageReport
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2-usage-report
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - winebarrel
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-12-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mechanize
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description:
35
+ email: sgwr_dts@yahoo.co.jp
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - README
44
+ - lib/ec2-usage-report.rb
45
+ homepage: https://bitbucket.org/winebarrel/ec2-usage-report
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.24
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: ec2-usage-report is a library for acquiring usage report of EC2.
78
+ test_files: []
79
+