fluent-plugin-ec2-metadata 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 622067156fe604a54288fc1c8a3322c96a1b2927
4
- data.tar.gz: efb9a9f3c6415ae0899d8d90e60811ac0bb67918
3
+ metadata.gz: e775b4cdc0695590d02f3933c3ae964da6075b2a
4
+ data.tar.gz: c00a2053b3cefd22f1526486eb380a5807a4b05e
5
5
  SHA512:
6
- metadata.gz: cbeb6506a22bb9e54ba7e27910d6c7f018ccd9f95f4166e32396aea2299117d5cdd85b4db8f9d7942cd2477a6dbfa7108aad8445d8fe35a698c0a662977217ee
7
- data.tar.gz: bfc3acf3973c78dfb32a4a16602283546f605a941a24e7c5fd599e891a5dd027aa173597329a471326dc931d74b5a913e3cc9ef39a13066b68147691eb8aa6ac
6
+ metadata.gz: 6a1bc7aef728a460b519d695641f1683dd6b738bbc66ba948df8b3a21aca1d0b79e951ee6d9f0577773c078995f6a276bc76fa524bcfc80d2a33e2163f63d80d
7
+ data.tar.gz: 778cc3e7bf5233862971030ee5023264fc7f42057199ab296b5d091469eebd3452a971accdb25826ee825dbbb75cda7d677f0bc479461818daa454d84bb2fa4b
data/README.md CHANGED
@@ -12,11 +12,18 @@ Use RubyGems:
12
12
  Example:
13
13
 
14
14
  <match foo.**>
15
- type ec2-metadata
16
- output_tag ${instance_id}.${tag}
15
+ type ec2_metadata
16
+
17
+ aws_key_id YOUR_AWS_KEY_ID
18
+ aws_sec_key YOUR_AWS_SECRET/KEY
17
19
 
20
+ output_tag ${instance_id}.${tag}
18
21
  <record>
19
- hostname ${instance_id}
22
+ hostname ${tagset_name}
23
+ instance_id ${instance_id}
24
+ instance_type ${instance_type}
25
+ az ${availability_zone}
26
+ vpc_id ${vpc_id}
20
27
  </record>
21
28
  </match>
22
29
 
@@ -30,18 +37,31 @@ then output becomes as below (indented):
30
37
 
31
38
  ```js
32
39
  i-28b5ee77.foo.bar {
33
- "hostname" : "i-28b5ee77",
34
- "message" : "hello ec2!"
40
+ "hostname" : "web0001",
41
+ "instance_id" : "i-28b5ee77",
42
+ "instance_type" : "m1.large",
43
+ "az" : "us-west-1b",
44
+ "vpc_id" : "vpc-25dab194",
45
+ "message" : "hello ec2!"
35
46
  }
36
47
  ```
37
48
 
38
49
  ### Placeholders
39
50
 
40
- The following placeholders are available:
51
+ The following placeholders are always available:
41
52
 
42
- * ${instance_id} instance id
43
53
  * ${tag} input tag
44
54
  * ${tag_parts} input tag splitted by '.'. you can use it like `${tag_parts[0]}` or `${tag_parts[-1]}`
55
+ * ${instance_id} instance id
56
+ * ${instance_type} instance type
57
+ * ${availability_zone} availability zone
58
+ * ${region} region
59
+
60
+ The followings are available when you define `aws_key_id` and `aws_sec_key`:
61
+
62
+ * ${vpc_id} vpc id
63
+ * ${subnet_id} subnet id
64
+ * ${tagset_xxx} EC2 tag (e.g. tagset_name is replaced by the value of Key = Name)
45
65
 
46
66
  ## Contributing
47
67
 
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-ec2-metadata"
7
- spec.version = "0.0.2"
7
+ spec.version = "0.0.3"
8
8
  spec.authors = ["SAKAMOTO Takumi"]
9
9
  spec.email = ["takumi.saka@gmail.com"]
10
- spec.description = %q{Output plugin to add ec2 metadata into messages}
11
- spec.summary = %q{Output plugin to add ec2 metadata into messages}
10
+ spec.description = %q{Fluentd output plugin to add ec2 metadata fields to a event record}
11
+ spec.summary = %q{Fluentd output plugin to add ec2 metadata fields to a event record}
12
12
  spec.homepage = "https://github.com/takus/fluent-plugin-ec2-metadata"
13
13
  spec.license = "APLv2"
14
14
 
@@ -19,4 +19,5 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_development_dependency "rake"
21
21
  spec.add_runtime_dependency "fluentd"
22
+ spec.add_runtime_dependency "aws-sdk"
22
23
  end
@@ -7,7 +7,10 @@ module Fluent
7
7
  require 'net/http'
8
8
  end
9
9
 
10
- config_param :output_tag, :string
10
+ config_param :output_tag, :string
11
+
12
+ config_param :aws_key_id, :string, :default => ENV['AWS_ACCESS_KEY_ID']
13
+ config_param :aws_sec_key, :string, :default => ENV['AWS_SECRET_ACCESS_KEY']
11
14
 
12
15
  def configure(conf)
13
16
  super
@@ -25,7 +28,27 @@ module Fluent
25
28
 
26
29
  # get ec2 metadata
27
30
  @ec2_metadata = {}
28
- @ec2_metadata['instance_id'] = get_instance_id()
31
+ @ec2_metadata['instance_id'] = get_metadata('instance-id')
32
+ @ec2_metadata['instance_type'] = get_metadata('instance-type')
33
+ @ec2_metadata['availability_zone'] = get_metadata('placement/availability-zone')
34
+ @ec2_metadata['region'] = @ec2_metadata['availability_zone'].chop
35
+
36
+ if @aws_key_id and @aws_sec_key then
37
+ require 'aws-sdk'
38
+
39
+ AWS.config(access_key_id: @aws_key_id, secret_access_key: @aws_sec_key, region: @ec2_metadata['region'])
40
+
41
+ response = AWS.ec2.client.describe_instances( { :instance_ids => [ @ec2_metadata['instance_id'] ] })
42
+ instance = response[:reservation_set].first[:instances_set].first
43
+ raise Fluent::ConfigError, "ec2-metadata: failed to get instance data #{response.pretty_inspect}" if instance.nil?
44
+
45
+ @ec2_metadata['vpc_id'] = instance[:vpc_id]
46
+ @ec2_metadata['subnet_id'] = instance[:subnet_id]
47
+
48
+ instance[:tag_set].each { |tag|
49
+ @ec2_metadata["tagset_#{tag[:key].downcase}"] = tag[:value]
50
+ }
51
+ end
29
52
  end
30
53
 
31
54
  def emit(tag, es, chain)
@@ -41,10 +64,9 @@ module Fluent
41
64
 
42
65
  private
43
66
 
44
- def get_instance_id
45
- res = Net::HTTP.get_response("169.254.169.254", "/latest/meta-data/instance-id")
46
- raise Fluent::ConfigError, "ec2-metadata: failed to get instance-id" unless res.is_a?(Net::HTTPSuccess)
47
- raise Fluent::ConfigError, "ec2-metadata: invalid instance-id #{res.body}" unless res.body =~ /^i-\h{8}$/
67
+ def get_metadata(f)
68
+ res = Net::HTTP.get_response("169.254.169.254", "/latest/meta-data/#{f}")
69
+ raise Fluent::ConfigError, "ec2-metadata: failed to get #{f}" unless res.is_a?(Net::HTTPSuccess)
48
70
  res.body
49
71
  end
50
72
 
@@ -83,8 +105,7 @@ module Fluent
83
105
  end
84
106
 
85
107
  def expand(str)
86
- str.gsub(/(\${[a-z_]+(\[-?[0-9]+\])?}|__[A-Z_]+__)/) {
87
- $log.warn "ec2-metadata: unknown placeholder `#{$1}` found in a tag `#{tag}`" unless @placeholders.include?($1)
108
+ str.gsub(/(\${[a-z_]+(\[-?[0-9]+\])?}|__[A-Z_]+__)/) { $log.warn "ec2-metadata: unknown placeholder `#{$1}` found in a tag `#{tag}`" unless @placeholders.include?($1)
88
109
  @placeholders[$1]
89
110
  }
90
111
  end
@@ -9,6 +9,7 @@ class EC2MetadataOutputTest < Test::Unit::TestCase
9
9
  output_tag ${instance_id}.${tag}
10
10
  <record>
11
11
  instance_id ${instance_id}
12
+ az ${availability_zone}
12
13
  </record>
13
14
  ]
14
15
 
@@ -20,6 +21,10 @@ class EC2MetadataOutputTest < Test::Unit::TestCase
20
21
  Net::HTTP.get_response('169.254.169.254', '/latest/meta-data/instance-id').body
21
22
  end
22
23
 
24
+ def get_avaivality_zone
25
+ Net::HTTP.get_response('169.254.169.254', '/latest/meta-data/placement/availability-zone').body
26
+ end
27
+
23
28
  def test_emit
24
29
  d = create_driver(CONFIG, 'foo.bar')
25
30
 
@@ -29,13 +34,14 @@ class EC2MetadataOutputTest < Test::Unit::TestCase
29
34
  end
30
35
 
31
36
  instance_id = get_instance_id
37
+ availability_zone = get_avaivality_zone
32
38
 
33
39
  # tag
34
40
  assert_equal "#{instance_id}.foo.bar", d.emits[0][0]
35
41
  assert_equal "#{instance_id}.foo.bar", d.emits[1][0]
36
42
 
37
43
  # record
38
- mapped = { 'instance_id' => instance_id }
44
+ mapped = { 'instance_id' => instance_id, 'az' => availability_zone }
39
45
  assert_equal [
40
46
  {"a" => 1}.merge(mapped),
41
47
  {"a" => 2}.merge(mapped),
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-ec2-metadata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - SAKAMOTO Takumi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-17 00:00:00.000000000 Z
11
+ date: 2014-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,7 +38,21 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Output plugin to add ec2 metadata into messages
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Fluentd output plugin to add ec2 metadata fields to a event record
42
56
  email:
43
57
  - takumi.saka@gmail.com
44
58
  executables: []
@@ -78,7 +92,7 @@ rubyforge_project:
78
92
  rubygems_version: 2.0.3
79
93
  signing_key:
80
94
  specification_version: 4
81
- summary: Output plugin to add ec2 metadata into messages
95
+ summary: Fluentd output plugin to add ec2 metadata fields to a event record
82
96
  test_files:
83
97
  - test/helper.rb
84
98
  - test/plugin/test_out_ec2_metadata.rb