metric_collect 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDlmODAwNDYzZWU0YzQwZTJiMmQxZWUxNDI5OWY4NzI3ZmMzNTM5MA==
5
+ data.tar.gz: !binary |-
6
+ ZjE5Yzk1YzNhMWFlMGUxYzVkZmZhMDA3YmIzNGJhZDRlNmVmY2Y3Mw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MTU0YTkwOTg2YTUwMjE5YjQxY2U5ZTA4MWFjOWZmY2NhZWNiNjE2ZTkwYmNk
10
+ ZTYzYWMxNDIwMGQ0YjA3ZDYxNTQzNjIxYWI2NTU4ZTg0OWJjOGNlMWNiM2Zm
11
+ YTBmNDViYTE3YTA4NGE4ZTgyODBlMDkwMGVhZjBiOThmZWY4NmU=
12
+ data.tar.gz: !binary |-
13
+ OWY2NDRkOTMxYTBlZWJiYjk4YzQ1YjM4Yjk2ZGE3NzZmN2Q4MTA3NGFjMTk4
14
+ YWFjYTliOGJkMWM2ZDM5ODVkZTNjMjMxYjhiNjcyYzVmZDdkZWIwM2I4ZTg2
15
+ MWFmOTVjMDQxZWJhZjIzNThmZjlkNDU1OTY0ZTgyNjllYWZiN2U=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in metric_collect.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ Metric Collect
2
+ ==============
3
+
4
+ This repository contains the code for metric_collect. This is intended to be used to help collect metrics either from a central server or in a distributed fashion by giving an easy to use DSL for graphite, statsd, & e-mail. Other backends can easily be added.
5
+
6
+ Installation
7
+ ============
8
+
9
+ To use simply clone from github (https://github.com/moserke/metric_collect.git)
10
+
11
+ Usage
12
+ =====
13
+
14
+ Run metric_collect from ./bin passing it the fully qualified path to your config.rb file
15
+
16
+ Config
17
+ ======
18
+
19
+ The config file is used to set some general parameters.
20
+
21
+ Parameters
22
+ ----------
23
+
24
+ - graphite_server: Name of graphite server (default: localhost)
25
+ - graphite_port: Port of graphite server (default: 2003)
26
+ - statsd_server: Name of statsd server (default: localhost)
27
+ - statsd_port: Port of statsd server (default: 8125)
28
+ - email_server: SMTP server to send to (default: nil)
29
+ - email_from: Who to send the e-mail from (default: nil)
30
+ - metrics_directory: Directory to look for metric definition ruby files (default: nil)
31
+
32
+ Back Ends
33
+ =========
34
+
35
+ statsd
36
+ ------
37
+
38
+ This is the metric type that will send to a statsd backend. Make sure to include statsd_server & statsd_port in config.rb. Default is localhost and 8125
39
+
40
+ ### Attributes
41
+
42
+ - name: Name of the metric (default: name of the metric)
43
+ - value: Value of the metric (default: nil)
44
+ - key: The key path to use in dot notation (default: metric_collect)
45
+ - type: Is a symbol defining what type of statsd metric to send (:counter, :timer, :gauge, :set) (default: :gauge)
46
+
47
+ graphite
48
+ --------
49
+
50
+ This is the metric type that will send to a graphite backend. Make sure to include graphite_server & graphite_port in config.rb. Default is localhost and 2003
51
+
52
+ ### Attributes
53
+
54
+ - name: Name of the metric (default: name of the metric)
55
+ - value: Value of the metric (default: nil)
56
+ - key: The key path to use in dot notation (default: metric_collect)
57
+
58
+
59
+ email
60
+ -----
61
+
62
+ This is the metric type that will send to an smtp server. Make sure to include email_server and email_from in your config.rb. There are no defaults for these.
63
+
64
+ ### Attributes
65
+
66
+ - name: Name of the metric (default: name of the metric)
67
+ - value: Value of the metric (default: nil)
68
+ - subject: Subject of the e-mail (default: Message From Metric Collect)
69
+ - to: Who to send the e-mail to (default: nil)
70
+ - body: Body of the message to send (default: nil)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/lazy-opt.rb ADDED
@@ -0,0 +1,55 @@
1
+ class LazyOpt
2
+ require 'optparse'
3
+
4
+ def initialize(script, options={})
5
+ @options = options
6
+ @script = script
7
+ end
8
+
9
+ def add(name, short, long, description, optional=true, default=nil)
10
+ @options[name] = {
11
+ short: short,
12
+ long: long,
13
+ description: description,
14
+ default: default,
15
+ optional: optional,
16
+ value: nil
17
+ }
18
+ end
19
+
20
+ def [](option)
21
+ @options[option][:value]
22
+ end
23
+
24
+ def parse!
25
+ banner = "usage: #{@script} "
26
+ @options.each do |option, config|
27
+ if config[:optional]
28
+ banner << "[--#{config[:long]} #{config[:long].upcase}] "
29
+ else
30
+ banner << "--#{config[:long]} #{config[:long].upcase} "
31
+ end
32
+ end
33
+
34
+ OptionParser.new do |opts|
35
+ opts.banner = banner
36
+
37
+ @options.each do |option, config|
38
+ description = "#{config[:description]}"
39
+ description << " (#{config[:default]})" if config[:default]
40
+ description << " MANDATORY" unless config[:default]
41
+ opts.on("-#{config[:short]}", "--#{config[:long]} #{config[:long].upcase}", description) do |opt|
42
+ @options[option][:value] = opt
43
+ end
44
+ end
45
+ end.parse!
46
+
47
+ @options.each do |option, config|
48
+ raise OptionParser::MissingArgument, "--#{option}" if (config[:value].nil? && !config[:optional])
49
+ end
50
+
51
+ @options.each do |option, config|
52
+ config[:value] = config[:value] ? config[:value] : config[:default]
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "#{File.dirname(__FILE__)}/lazy-opt"
4
+
5
+ opts = LazyOpt.new(__FILE__)
6
+ opts.add(:metric, "m", "metric", "Metric definition to send", true)
7
+ opts.add(:config, "c", "config", "Path to configuration file", false)
8
+ opts.parse!
9
+
10
+ require 'metric_collect'
11
+
12
+ opts[:metric] ? MetricCollect.new.run(opts[:config], opts[:metric]) : MetricCollect.new.run(opts[:config])
data/config.rb ADDED
@@ -0,0 +1,10 @@
1
+ graphite_server "localhost"
2
+ graphite_port "2003"
3
+
4
+ statsd_server "localhost"
5
+ statsd_port "8125"
6
+
7
+ email_server "mailserver.company.com"
8
+ email_from "do-not-reply@company.com"
9
+
10
+ metrics_directory "/Users/x3v7/git/tmp/metrics"
data/example_config.rb ADDED
@@ -0,0 +1,10 @@
1
+ graphite_server "localhost"
2
+ graphite_port "2003"
3
+
4
+ statsd_server "localhost"
5
+ statsd_port "8125"
6
+
7
+ email_server "mailserver.company.com"
8
+ email_from "do-not-reply@company.com"
9
+
10
+ metrics_directory "/usr/share/metric_collect/metrics"
@@ -0,0 +1,25 @@
1
+ #Find the number of running processes and report to graphite, statsd, & e-mail the list!
2
+
3
+ processes = `ps aux`
4
+
5
+ statsd :num_of_processes do
6
+ key "my_computer.os"
7
+ value "#{processes.lines.count}"
8
+ type :gauge
9
+ end
10
+
11
+ graphite :num_of_processes do
12
+ key "my_computer.os"
13
+ value "#{processes.lines.count}"
14
+ end
15
+
16
+ statsd :num_of_processes do
17
+ subject "my_computer.os"
18
+ value "#{processes.lines.count}"
19
+ to "example@company.com"
20
+ body <<-EOF
21
+ List of running processes on my_computer:
22
+
23
+ #{processes}
24
+ EOF
25
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'net/smtp'
19
+
20
+ class MetricCollect
21
+ class Backend
22
+ class Email
23
+ def initialize(name)
24
+ @params = Hash.new
25
+ @params[:name] = name
26
+ @params[:value] = nil
27
+ @params[:subject] = 'Message from Metrics Collect'
28
+ @params[:body] = nil
29
+ @params[:to] = nil
30
+ end
31
+
32
+ def method_missing(method_symbol, *args, &block)
33
+ @params[method_symbol]
34
+ end
35
+
36
+ attr_accessor :params
37
+
38
+ def process_metric(config)
39
+ name = @params[:name]
40
+ value = @params[:value]
41
+ subject = @params[:subject]
42
+ body = @params[:body]
43
+ to = @params[:to]
44
+ @config = config.params
45
+
46
+ send_email(name, value, to, subject, body)
47
+ end
48
+
49
+ private
50
+ def send_email(name, value, to, subject, body)
51
+ msg = <<-EOF
52
+ From: <#{@config[:email_from]}>
53
+ To: <#{to}>
54
+ Subject: #{subject}
55
+
56
+ #{body}
57
+ EOF
58
+
59
+ Net::SMTP.start(@config[:email_server]) do |smtp|
60
+ smtp.send_message msg, @config[:email_from], to
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,70 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'socket'
19
+
20
+ class MetricCollect
21
+ class Backend
22
+ class Graphite
23
+ def initialize(name)
24
+ @params = Hash.new
25
+ @params[:name] = name
26
+ @params[:value] = nil
27
+ @params[:key] = "metric_collect"
28
+ end
29
+
30
+ def method_missing(method_symbol, *args, &block)
31
+ @params[method_symbol]
32
+ end
33
+
34
+ attr_accessor :params
35
+
36
+ def process_metric(config)
37
+ @config = config.params
38
+ name = @params[:name]
39
+ value = @params[:value]
40
+ key = @params[:key]
41
+
42
+ report("#{key}.#{name}", value)
43
+ end
44
+
45
+ private
46
+ def socket
47
+ host = @config[:graphite_server]
48
+ port = @config[:graphit_port]
49
+
50
+ return @socket if @socket && !@socket.closed?
51
+ @socket = TCPSocket.new(@config[:graphite_server], @config[:graphite_port])
52
+ end
53
+
54
+ def report(key, value, time = Time.now)
55
+ begin
56
+ socket.write("#{key} #{value.to_f} #{time.to_i}\n")
57
+ rescue Errno::EPIPE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED
58
+ @socket = nil
59
+ nil
60
+ end
61
+ end
62
+
63
+ def close_socket
64
+ @socket.close if @socket
65
+ @socket = nil
66
+ end
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,113 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'socket'
19
+
20
+ class MetricCollect
21
+ class Backend
22
+ class Statsd
23
+ def initialize(name)
24
+ @params = Hash.new
25
+ @params[:name] = name
26
+ @params[:value] = nil
27
+ @params[:key] = "metric_collect"
28
+ @params[:type] = :gauge
29
+ end
30
+
31
+ def method_missing(method_symbol, *args, &block)
32
+ @params[method_symbol]
33
+ end
34
+
35
+ attr_accessor :params
36
+
37
+ def process_metric(config)
38
+ @config = config.params
39
+ name = @params[:name]
40
+ value = @params[:value]
41
+ key = @params[:key]
42
+ type = @params[:type]
43
+
44
+ case type
45
+ when :counter
46
+ counters("#{key}.#{name}", value)
47
+ when :timer
48
+ timers("#{key}.#{name}", value)
49
+ when :gauge
50
+ gauges("#{key}.#{name}", value)
51
+ when :set
52
+ sets("#{key}.#{name}", value)
53
+ else
54
+ MetricCollect::Log.fatal("#{type.to_s} is not a valid statsd type!")
55
+ end
56
+ end
57
+
58
+ private
59
+ def timers(stats, time, sample_rate=1)
60
+ update_stats(stats, time, sample_rate, 'ms')
61
+ end
62
+
63
+ def counters(stats, value, sample_rate=1)
64
+ update_stats(stats, value, sample_rate)
65
+ end
66
+
67
+ def gauges(stats, value, sample_rate=1)
68
+ update_stats(stats, value, sample_rate, 'g')
69
+ end
70
+
71
+ def sets(stats, value, sample_rate=1)
72
+ update_stats(stats, value, sample_rate, 's')
73
+ end
74
+
75
+ def update_stats(stats, delta=1, sample_rate=1, metric='c')
76
+ stats = [stats].flatten
77
+
78
+ data = {}
79
+ stats.each do |stat|
80
+ data[stat] = "%s|%s" % [delta, metric]
81
+ end
82
+
83
+ send(data, sample_rate)
84
+ end
85
+
86
+ def send(data, sample_rate=1)
87
+ begin
88
+ host = @config[:statsd_server]
89
+ port = @config[:statsd_port]
90
+
91
+ sampled_data = {}
92
+ if sample_rate < 1
93
+ if rand <= sample_rate
94
+ data.each_pair do |stat, val|
95
+ sampled_data[stat] = "%s|@%s" % [val, sample_rate]
96
+ end
97
+ end
98
+ else
99
+ sampled_data = data
100
+ end
101
+
102
+ udp = UDPSocket.new
103
+ sampled_data.each_pair do |stat, val|
104
+ send_data = "%s:%s" % [stat, val]
105
+ udp.send send_data, 0, host, port
106
+ end
107
+ rescue => e
108
+ puts e.message
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,94 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'metric_collect/version'
19
+ require 'metric_collect/config'
20
+ require 'metric_collect/metric'
21
+ require 'metric_collect/log'
22
+ require 'backend/statsd'
23
+ require 'backend/graphite'
24
+ require 'backend/email'
25
+
26
+ class MetricCollect
27
+ def run(config_file, metric=nil)
28
+ unless config_file
29
+ puts "Config file not specified!!"
30
+ exit
31
+ end
32
+ @config_file = config_file
33
+
34
+ load_config_from_file
35
+ load_logger
36
+
37
+ unless @config.params[:metrics_directory]
38
+ MetricCollect::Log.fatal("You must set a metrics_directory in your config.rb")
39
+ end
40
+
41
+ load_metric_definitions
42
+ load_dynamic_definitions(metric) if metric
43
+ send_all_metrics
44
+
45
+ MetricCollect::Log.info("All metrics processed successfully!")
46
+ end
47
+
48
+ def load_logger
49
+ MetricCollect::Log.new(@config.params[:log_file]) if @config.params[:log_file]
50
+ end
51
+
52
+ def load_dynamic_definitions(metric)
53
+ new_metric = MetricCollect::Metric.new("CommandLine")
54
+ begin
55
+ MetricCollect::Log.info("Loading metric[#{new_metric.name}]")
56
+ new_metric.instance_eval(metric)
57
+ rescue => error
58
+ MetricCollect::Log.fatal("Error loading metric[#{new_metric.name}]", error)
59
+ end
60
+ @metrics << new_metric
61
+ end
62
+
63
+ def load_metric_definitions
64
+ unless File.exists?(@config.params[:metrics_directory])
65
+ MetricCollect::Log.fatal("Metrics directory supplied does not exist!")
66
+ end
67
+ metric_files = Dir["#{@config.params[:metrics_directory]}/*.rb"]
68
+ @metrics = Array.new
69
+
70
+ metric_files.each do |filename|
71
+ new_metric = MetricCollect::Metric.new(File.basename(filename, ".rb"))
72
+ begin
73
+ MetricCollect::Log.info("Loading metric[#{new_metric.name}]")
74
+ new_metric.instance_eval(IO.read(filename))
75
+ rescue => error
76
+ MetricCollect::Log.fatal("Error loading metric[#{new_metric.name}]", error)
77
+ end
78
+ @metrics << new_metric
79
+ end
80
+ end
81
+
82
+ def load_config_from_file
83
+ puts "#{MetricCollect::Log.build_time}|INFO|Loading config file #{@config_file}"
84
+ @config = MetricCollect::Config.new
85
+ @config.instance_eval(IO.read(@config_file))
86
+ end
87
+
88
+ def send_all_metrics
89
+ @metrics.each do |metric|
90
+ MetricCollect::Log.info("Processing metric[#{metric.name}]")
91
+ metric.send(@config)
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,39 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class MetricCollect
19
+ class Config
20
+ def initialize()
21
+ @params = Hash.new
22
+ @params[:graphite_server] = "localhost"
23
+ @params[:graphite_port] = "2003"
24
+ @params[:statsd_server] = "localhost"
25
+ @params[:statsd_port] = "8125"
26
+ @params[:metrics_directory] = nil
27
+ @params[:email_server] = nil
28
+ @params[:email_from] = nil
29
+ @params[:log_file] = nil
30
+ end
31
+
32
+ def method_missing(method_symbol, *args, &block)
33
+ @params[method_symbol] = args[0]
34
+ block.call if block
35
+ end
36
+
37
+ attr_reader :params
38
+ end
39
+ end
@@ -0,0 +1,61 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class MetricCollect
19
+ class Log
20
+ def initialize(log_file=nil)
21
+ @log_file = log_file
22
+ end
23
+
24
+ def self.info(msg)
25
+ Log.write(msg, "INFO")
26
+ end
27
+
28
+ def self.warn(msg)
29
+ Log.write(msg, "WARN")
30
+ end
31
+
32
+ def self.fatal(msg, error=nil)
33
+ Log.write(msg, "FATAL")
34
+ raise error if error
35
+ exit
36
+ end
37
+
38
+ def self.build_time
39
+ time = Time.now
40
+ year = "%04d" % time.year
41
+ month = "%02d" % time.month
42
+ day = "%02d" % time.day
43
+ hour = "%02d" % time.hour
44
+ min = "%02d" % time.min
45
+ sec = "%02d" % time.sec
46
+
47
+ "#{year}-#{month}-#{day} #{hour}:#{min}:#{sec}"
48
+ end
49
+
50
+ private
51
+ def self.write(msg, level)
52
+ if @log_file
53
+ File.open(@log_file, 'w') do |log|
54
+ log.write("#{build_time}|#{level}|#{msg}")
55
+ end
56
+ else
57
+ puts "#{build_time}|#{level}|#{msg}"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,60 @@
1
+ #
2
+ # Author:: Kevin Moser (<kevin.moser@nordstrom.com>)
3
+ #
4
+ # Copyright:: 2012, Nordstrom, Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ class MetricCollect
19
+ class Metric
20
+ attr_reader :backends
21
+ attr_accessor :name
22
+
23
+ def initialize(name=nil)
24
+ @backends = Array.new
25
+ @name = name
26
+ end
27
+
28
+ def method_missing(method_symbol, *args, &block)
29
+ case method_symbol
30
+ when :statsd
31
+ @new_backend = MetricCollect::Backend::Statsd.new(args[0])
32
+ when :graphite
33
+ @new_backend = MetricCollect::Backend::Graphite.new(args[0])
34
+ when :email
35
+ @new_backend = MetricCollect::Backend::Email.new(args[0])
36
+ else
37
+ if @new_backend.params.has_key?(method_symbol)
38
+ @new_backend.params[method_symbol] = args[0]
39
+ else
40
+ raise ArgumentError.new("No argument for #{@new_backend.class} named #{method_symbol.to_s}")
41
+ end
42
+ return
43
+ end
44
+
45
+ begin
46
+ yield if block
47
+ rescue => error
48
+ MetricCollect::Log.fatal("Error loading value[#{name}::#{@new_backend.name}]", error)
49
+ end
50
+ @backends << @new_backend
51
+ end
52
+
53
+ def send(config)
54
+ @backends.each do |backend|
55
+ MetricCollect::Log.info("Processing value[#{name}::#{backend.name}] sending key: #{backend.key} value: #{backend.value} to #{backend.class}")
56
+ backend.process_metric(config)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ class MetricCollect
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'metric_collect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "metric_collect"
8
+ spec.version = MetricCollect::VERSION
9
+ spec.authors = ["Kevin Moser"]
10
+ spec.email = ["kevin.moser@nordstrom.com"]
11
+ spec.description = %q{Ship metrics from Ruby scripts}
12
+ spec.summary = %q{Ship metrics from Ruby scripts}
13
+ spec.homepage = "https://github.com/Nordstrom/metric_collect"
14
+ spec.license = "Apache License, Version 2.0"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetricCollect do
4
+ it 'should have a version number' do
5
+ MetricCollect::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'should do something useful' do
9
+ false.should be_true
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'metric_collect'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metric_collect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Moser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: Ship metrics from Ruby scripts
56
+ email:
57
+ - kevin.moser@nordstrom.com
58
+ executables:
59
+ - lazy-opt.rb
60
+ - metric_collect
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - .rspec
66
+ - .travis.yml
67
+ - Gemfile
68
+ - LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - bin/lazy-opt.rb
72
+ - bin/metric_collect
73
+ - config.rb
74
+ - example_config.rb
75
+ - example_metrics/running_processes.rb
76
+ - lib/backend/email.rb
77
+ - lib/backend/graphite.rb
78
+ - lib/backend/statsd.rb
79
+ - lib/metric_collect.rb
80
+ - lib/metric_collect/config.rb
81
+ - lib/metric_collect/log.rb
82
+ - lib/metric_collect/metric.rb
83
+ - lib/metric_collect/version.rb
84
+ - metric_collect.gemspec
85
+ - spec/metric_collect_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/Nordstrom/metric_collect
88
+ licenses:
89
+ - Apache License, Version 2.0
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ship metrics from Ruby scripts
111
+ test_files:
112
+ - spec/metric_collect_spec.rb
113
+ - spec/spec_helper.rb