fluent-plugin-webhook-mackerel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4a0840319dbdd39467c451e079f45996603ffd2
4
+ data.tar.gz: 4fc2b3b2c49d93a9cbe3d142b8457cc0c1974275
5
+ SHA512:
6
+ metadata.gz: 18c33789894abebb57c4941360a09f48fc32283b4cafb8b113abdc97bf1a538a21452bc53b98b724c2f00dbbd87cab5b1c198d4dfef950559edd00fdc71916e8
7
+ data.tar.gz: b60702519a5ed03986bc0ae7901612589555c460b8c51742a5a2abd614ba846e5cbff19af198e627ef84220e45b2af66476f683ab8f6dc6a134f753a99a188f0
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-webhook-mackerel.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Songmu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Fluent::Plugin::Webhook::Mackerel
2
+
3
+ fluentd input plugin for incoming webhook from Mackerel.
4
+
5
+ ## Installation
6
+
7
+ $ gem install fluent-plugin-webhook-mackerel
8
+
9
+ ## Usage
10
+
11
+ ```
12
+ <source>
13
+ type webhook_mackerel
14
+ tag mackerel
15
+
16
+ # optional (values are default)
17
+ bind 0.0.0.0
18
+ port 3838
19
+ mount /
20
+ </source>
21
+
22
+ <match mackerel.*>
23
+ type stdout
24
+ </match>
25
+
26
+ <match mackerel.alert>
27
+ type slack
28
+ </match>
29
+ ```
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it ( https://github.com/mackerelio/fluent-plugin-webhook-mackerel/fork )
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/test_*.rb'
6
+ test.verbose = true
7
+ end
8
+
9
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "fluent-plugin-webhook-mackerel"
6
+ spec.version = File.read(File.expand_path('../VERSION', __FILE__))
7
+ spec.authors = ["mackerelio"]
8
+ spec.email = ["mackerel-developers@mackerel.io"]
9
+ spec.summary = %q{fluentd input plugin for receiving Mackerel webhook}
10
+ spec.description = %q{fluentd input plugin for receiving Mackerel webhook}
11
+ spec.homepage = "https://mackerel.com/mackerelio/fluent-plugin-webhook-mackerel"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_runtime_dependency "fluentd", "~> 0"
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,58 @@
1
+ require "thread"
2
+ require "json"
3
+ require 'fluent/process'
4
+
5
+ module Fluent
6
+ class WebhookMackerelInput < Input
7
+ include DetachProcessMixin
8
+
9
+ Plugin.register_input('webhook_mackerel', self)
10
+
11
+ config_param :tag, :string
12
+ config_param :bind, :string, :default => "0.0.0.0"
13
+ config_param :port, :integer, :default => 3838
14
+ config_param :mount, :string, :default => "/"
15
+
16
+ def start
17
+ @thread = Thread.new(&method(:run))
18
+ end
19
+
20
+ def shutdown
21
+ @server.shutdown
22
+ Thread.kill(@thread)
23
+ end
24
+
25
+ def run
26
+ @server = WEBrick::HTTPServer.new(
27
+ :BindAddress => @bind,
28
+ :Port => @port,
29
+ )
30
+ $log.debug "Listen on http://#{@bind}:#{@port}#{@mount}"
31
+ @server.mount_proc(@mount) do |req, res|
32
+ begin
33
+ $log.debug req.header
34
+
35
+ if req.request_method != "POST"
36
+ res.status = 405
37
+ else
38
+ payload = JSON.parse(req.body)
39
+ process(payload)
40
+ res.status = 204
41
+ end
42
+ rescue => e
43
+ $log.error e.inspect
44
+ $log.error e.backtrace.join("\n")
45
+ res.status = 400
46
+ end
47
+ end
48
+ @server.start
49
+ end
50
+
51
+ def process(payload)
52
+ event = payload.delete "event"
53
+ payload[:event] = event
54
+ $log.info "tag: #{@tag.dup}.#{event}, payload:#{payload}"
55
+ Engine.emit("#{@tag.dup}.#{event}", Engine.now, payload)
56
+ end
57
+ end
58
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+
15
+ require 'fluent/test'
16
+ require 'fluent/plugin/in_webhook_mackerel'
17
+
18
+ def unused_port
19
+ s = TCPServer.open(0)
20
+ port = s.addr[1]
21
+ s.close
22
+ port
23
+ end
24
+
@@ -0,0 +1,85 @@
1
+ require 'helper'
2
+ require 'net/http'
3
+
4
+ class MackerelWebhookInputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ PORT = unused_port
10
+ CONFIG = %[
11
+ port #{PORT}
12
+ tag mwebhook
13
+ ]
14
+
15
+ def create_driver(conf=CONFIG, tag='test')
16
+ Fluent::Test::OutputTestDriver.new(Fluent::WebhookMackerelInput, tag).configure(conf)
17
+ end
18
+
19
+ def test_configure
20
+ assert_raise(Fluent::ConfigError) {
21
+ d = create_driver('')
22
+ }
23
+ d = create_driver
24
+ assert_equal 'mwebhook', d.instance.tag
25
+ assert_equal PORT, d.instance.port
26
+ assert_equal '/', d.instance.mount
27
+ assert_equal '0.0.0.0', d.instance.bind
28
+ end
29
+
30
+ def test_basic
31
+ d = create_driver
32
+
33
+ time = Time.parse("2011-01-02 13:14:15 UTC").to_i
34
+ Fluent::Engine.now = time
35
+
36
+ d.expect_emit "mwebhook.alert", time, {
37
+ :event => 'alert',
38
+ 'host' => {
39
+ 'id' => 'abcabc'
40
+ },
41
+ 'alert' => {
42
+ 'url' => 'http://',
43
+ },
44
+ }
45
+
46
+ payload = {
47
+ 'event' => 'alert',
48
+ 'host' => {
49
+ 'id' => 'abcabc'
50
+ },
51
+ 'alert' => {
52
+ 'url' => 'http://',
53
+ },
54
+ }
55
+
56
+ d.run do
57
+ d.expected_emits.each {|tag, time, record|
58
+ res = post("/", payload.to_json)
59
+ assert_equal "204", res.code
60
+ }
61
+ end
62
+ end
63
+
64
+ def test_405
65
+ create_driver.run do
66
+ http = Net::HTTP.new("127.0.0.1", PORT)
67
+ req = Net::HTTP::Get.new('/')
68
+ res = http.request(req)
69
+
70
+ assert_equal "405", res.code
71
+ end
72
+ end
73
+
74
+ def post(path, params, header = {})
75
+ http = Net::HTTP.new("127.0.0.1", PORT)
76
+ req = Net::HTTP::Post.new(path, header)
77
+ if params.is_a?(String)
78
+ req.body = params
79
+ else
80
+ req.set_form_data(params)
81
+ end
82
+ http.request(req)
83
+ end
84
+
85
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-webhook-mackerel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mackerelio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: fluentd input plugin for receiving Mackerel webhook
56
+ email:
57
+ - mackerel-developers@mackerel.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - VERSION
68
+ - fluent-plugin-webhook-mackerel.gemspec
69
+ - lib/fluent/plugin/in_webhook_mackerel.rb
70
+ - test/helper.rb
71
+ - test/plugin/test_in_webhook_mackerel.rb
72
+ homepage: https://mackerel.com/mackerelio/fluent-plugin-webhook-mackerel
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.2
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: fluentd input plugin for receiving Mackerel webhook
96
+ test_files:
97
+ - test/helper.rb
98
+ - test/plugin/test_in_webhook_mackerel.rb