recognizer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Sean Porter
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.org ADDED
File without changes
data/bin/recognizer ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE__) + '/../lib/')
4
+
5
+ require "rubygems"
6
+ require "recognizer"
7
+
8
+ Recognizer.run
@@ -0,0 +1,41 @@
1
+ require "rubygems"
2
+ require "thread"
3
+ require "bunny"
4
+
5
+ module Recognizer
6
+ class AMQP
7
+ def initialize(thread_queue, options)
8
+ unless thread_queue && options.is_a?(Hash)
9
+ raise "You must provide a thread queue and options"
10
+ end
11
+ amqp = Bunny.new(options[:amqp])
12
+ amqp.start
13
+ queue = amqp.queue("recognizer")
14
+ exchange = amqp.exchange("graphite", :type => :topic, :durable => true)
15
+ queue.bind(exchange, :key => "*")
16
+ Thread.abort_on_exception = true
17
+ consumer = Thread.new do
18
+ puts "Awaiting the metrics with impatience ..."
19
+ queue.subscribe do |message|
20
+ payload = message[:payload]
21
+ begin
22
+ metrics = JSON.parse(payload)
23
+ if metrics.is_a?(Array)
24
+ metrics.each do |metric|
25
+ if metric.split(" ").count == 3
26
+ thread_queue.push(metric)
27
+ end
28
+ end
29
+ end
30
+ rescue JSON::ParserError
31
+ metric = payload
32
+ if metric.split(" ").count == 3
33
+ thread_queue.push(metric)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ consumer.join
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,28 @@
1
+ require "rubygems"
2
+ require "mixlib/cli"
3
+
4
+ module Recognizer
5
+ class CLI
6
+ include Mixlib::CLI
7
+
8
+ option :config_file,
9
+ :short => "-c CONFIG",
10
+ :long => "--config CONFIG",
11
+ :default => File.join(File.dirname(__FILE__), "..", "..", "config.json"),
12
+ :description => "The config file path"
13
+
14
+ option :help,
15
+ :short => "-h",
16
+ :long => "--help",
17
+ :description => "Show this message",
18
+ :on => :tail,
19
+ :boolean => true,
20
+ :show_options => true,
21
+ :exit => 0
22
+
23
+ def read
24
+ parse_options
25
+ config
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ require "rubygems"
2
+ require "json"
3
+
4
+ require File.join(File.dirname(__FILE__), 'patches', 'hash')
5
+
6
+ module Recognizer
7
+ class Config
8
+ def initialize(options={})
9
+ unless options[:config_file]
10
+ raise "Missing config file path"
11
+ end
12
+ if File.readable?(options[:config_file])
13
+ config_file_contents = File.open(options[:config_file], 'r').read
14
+ begin
15
+ @config = JSON.parse(config_file_contents)
16
+ rescue JSON::ParserError => error
17
+ raise "Config file must be valid JSON: #{error}"
18
+ end
19
+ else
20
+ raise "Config file does not exist or is not readable: #{options[:config_file]}"
21
+ end
22
+ end
23
+
24
+ def read
25
+ @config.symbolize_keys
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ require "rubygems"
2
+ require "thread"
3
+ require "librato/metrics"
4
+
5
+ module Recognizer
6
+ class Librato
7
+ def initialize(thread_queue, options)
8
+ unless thread_queue && options.is_a?(Hash)
9
+ raise "You must provide a thread queue and options"
10
+ end
11
+ unless options[:librato][:email] && options[:librato][:api_key]
12
+ raise "You must provide a Librato Metrics account email and API key"
13
+ end
14
+ ::Librato::Metrics.authenticate(options[:librato][:email], options[:librato][:api_key])
15
+ librato = ::Librato::Metrics::Queue.new
16
+ mutex = Mutex.new
17
+ Thread.abort_on_exception = true
18
+ Thread.new do
19
+ loop do
20
+ sleep(options[:librato][:flush_interval] || 10)
21
+ unless librato.queued.empty?
22
+ puts "Attempting to flush metrics to Librato"
23
+ mutex.synchronize do
24
+ librato.submit
25
+ end
26
+ puts "Successfully flushed metrics to Librato"
27
+ end
28
+ end
29
+ end
30
+ Thread.new do
31
+ loop do
32
+ graphite_formated = thread_queue.pop
33
+ puts "Adding metric to queue: #{graphite_formated}"
34
+ metric = graphite_formated.split(" ")
35
+ mutex.synchronize do
36
+ librato.add metric[0].to_sym => {:value => metric[1].to_f, :measure_time => metric[2].to_i}
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ class Hash
2
+ def symbolize_keys(item=self)
3
+ case item
4
+ when Array
5
+ item.map do |i|
6
+ symbolize_keys(i)
7
+ end
8
+ when Hash
9
+ Hash[
10
+ item.map do |key, value|
11
+ new_key = key.is_a?(String) ? key.to_sym : key
12
+ new_value = symbolize_keys(value)
13
+ [new_key, new_value]
14
+ end
15
+ ]
16
+ else
17
+ item
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Recognizer
2
+ VERSION = "0.0.1"
3
+ end
data/lib/recognizer.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "recognizer/version"
2
+ require "recognizer/cli"
3
+ require "recognizer/config"
4
+ require "recognizer/librato"
5
+ require "recognizer/amqp"
6
+
7
+ module Recognizer
8
+ def self.run
9
+ cli = Recognizer::CLI.new
10
+ cli_options = cli.read
11
+ config = Recognizer::Config.new(cli_options)
12
+ config_options = config.read
13
+ thread_queue = Queue.new
14
+ Recognizer::Librato.new(thread_queue, config_options)
15
+ Recognizer::AMQP.new(thread_queue, config_options)
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "recognizer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "recognizer"
7
+ s.version = Recognizer::VERSION
8
+ s.authors = ["Sean Porter"]
9
+ s.email = ["portertech@gmail.com"]
10
+ s.homepage = "https://github.com/portertech/recognizer"
11
+ s.summary = ""
12
+ s.description = ""
13
+
14
+ s.rubyforge_project = "recognizer"
15
+
16
+ s.files = Dir.glob("{bin,lib}/**/*") + %w[recognizer.gemspec README.org MIT-LICENSE.txt]
17
+ s.executables = Dir.glob("bin/**/*").map { |file| File.basename(file) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency("librato-metrics", "0.2.2")
21
+ s.add_dependency("bunny", "0.7.8")
22
+ s.add_dependency('mixlib-cli', '>= 1.1.0')
23
+ s.add_dependency("json")
24
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recognizer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sean Porter
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-29 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: librato-metrics
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 2
33
+ - 2
34
+ version: 0.2.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bunny
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 0
48
+ - 7
49
+ - 8
50
+ version: 0.7.8
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: mixlib-cli
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 19
62
+ segments:
63
+ - 1
64
+ - 1
65
+ - 0
66
+ version: 1.1.0
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :runtime
82
+ version_requirements: *id004
83
+ description: ""
84
+ email:
85
+ - portertech@gmail.com
86
+ executables:
87
+ - recognizer
88
+ extensions: []
89
+
90
+ extra_rdoc_files: []
91
+
92
+ files:
93
+ - bin/recognizer
94
+ - lib/recognizer/amqp.rb
95
+ - lib/recognizer/cli.rb
96
+ - lib/recognizer/config.rb
97
+ - lib/recognizer/librato.rb
98
+ - lib/recognizer/patches/hash.rb
99
+ - lib/recognizer/version.rb
100
+ - lib/recognizer.rb
101
+ - recognizer.gemspec
102
+ - README.org
103
+ - MIT-LICENSE.txt
104
+ has_rdoc: true
105
+ homepage: https://github.com/portertech/recognizer
106
+ licenses: []
107
+
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project: recognizer
134
+ rubygems_version: 1.3.7
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: ""
138
+ test_files: []
139
+