librato-metrics-taps 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,42 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+
14
+ # jeweler generated
15
+ pkg
16
+
17
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
18
+ #
19
+ # * Create a file at ~/.gitignore
20
+ # * Include files you want ignored
21
+ # * Run: git config --global core.excludesfile ~/.gitignore
22
+ #
23
+ # After doing this, these files will be ignored in all your git projects,
24
+ # saving you from having to 'pollute' every project you touch with them
25
+ #
26
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
27
+ #
28
+ # For MacOS:
29
+ #
30
+ #.DS_Store
31
+ #
32
+ # For TextMate
33
+ #*.tmproj
34
+ #tmtags
35
+ #
36
+ # For emacs:
37
+ #*~
38
+ #\#*
39
+ #.\#*
40
+ #
41
+ # For vim:
42
+ #*.swp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ librato-metrics-taps (0.3.6)
5
+ jmx4r (= 0.1.4)
6
+ jruby-openssl (= 0.8.5)
7
+ librato-metrics (~> 1.0.4)
8
+ trollop (= 1.16.2)
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ aggregate (0.2.2)
14
+ bouncy-castle-java (1.5.0147)
15
+ faraday (0.8.7)
16
+ multipart-post (~> 1.1)
17
+ jmx4r (0.1.4)
18
+ jruby-openssl (0.8.5)
19
+ bouncy-castle-java (>= 1.5.0147)
20
+ json (1.7.3)
21
+ json (1.7.3-java)
22
+ librato-metrics (1.0.4)
23
+ aggregate (~> 0.2.2)
24
+ faraday (~> 0.7)
25
+ multi_json
26
+ multi_json (1.7.2)
27
+ multipart-post (1.2.0)
28
+ rake (10.0.3)
29
+ rdoc (3.12)
30
+ json (~> 1.4)
31
+ shoulda (3.0.1)
32
+ shoulda-context (~> 1.0.0)
33
+ shoulda-matchers (~> 1.0.0)
34
+ shoulda-context (1.0.0)
35
+ shoulda-matchers (1.0.0)
36
+ trollop (1.16.2)
37
+
38
+ PLATFORMS
39
+ java
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ librato-metrics-taps!
44
+ rake
45
+ rdoc (~> 3.12)
46
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Mike Heffner
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.
@@ -0,0 +1,145 @@
1
+ # librato-metrics-taps
2
+
3
+ Collection of helper scripts and library routines to tap into external
4
+ metric sources and pump those metrics into Librato's Metric Service.
5
+
6
+ ## librato-metrics-tap-jmxbeans
7
+
8
+ The *JMX Beans* tap script will connect to a JMX service and pull
9
+ monitoring attributes from a configured set of MBeans. The values of
10
+ these MBean attributes are pumped into Librato's Metric Service.
11
+
12
+ The usage for librato-metrics-tap-jmxbeans is:
13
+
14
+ ```
15
+ Usage: librato-metrics-tap-jmxbeans <options>
16
+
17
+ Options:
18
+ --jmx-host, -j <s>: JMX Hostname (default: localhost)
19
+ --jmx-port, -m <i>: JMX Port (default: 8080)
20
+ --jmx-username, -x <s>: JMX Username
21
+ --jmx-password, -w <s>: JMX Password
22
+ --email, -e <s>: Metrics Email address
23
+ --token, -t <s>: Metrics API token
24
+ --source, -s <s>: Optional source name
25
+ --prefix, -z <s>: Optional prefix name
26
+ --measure-time, -a <i>: Optional time for measurements
27
+ --metrics-url, -r <s>: Metrics URL (default:
28
+ https://metrics-api.librato.com)
29
+ --interval, -i <i>: Run as a daemon and poll every N seconds
30
+ --ignore-missing, -g: Ignore missing beans/attributes
31
+ --data-file-full, -d <s>: YAML file defining beans & attribute names
32
+ --data-file-attributes, -f <s>: YAML file of bean attributes
33
+ --bean-name, -b <s>: Bean name to match against or lookup
34
+ --publish, -p: Publish Bean Attributes to Librato Metrics
35
+ --match-beans, -c: Output beans that match --bean-name regexp
36
+ --version, -v: Print version and exit
37
+ --help, -h: Show this message
38
+ ```
39
+
40
+ You can either specify a full bean and attribute definition file or
41
+ you can specify the bean name(s) separately on the commandline and
42
+ specify the bean attributes from the config file.
43
+
44
+ ### Full Bean and Attribute Definition
45
+
46
+ Use the `--data-file-full` option to specify the path to a YAML file
47
+ defining the full list of beans and attributes to retreive. An example
48
+ of such a configuration file is:
49
+
50
+ ```
51
+ ---
52
+ org.apache.cassandra.internal:type=AntiEntropyStage:
53
+ org.apache.cassandra.internal:type=FlushSorter:
54
+ ActiveCount:
55
+ CompletedTasks: counter
56
+ PendingTasks:
57
+ ```
58
+
59
+ For example, this will grab all the attributes of the bean name
60
+ `org.apache.cassandra.internal:type=AntiEntropyStage` and will push
61
+ each of those attributes as metrics.
62
+
63
+ If the file contains a list of attributes for a bean, it will restrict
64
+ the attributs to the ones listed. For example, for the bean
65
+ `org.apache.cassandra.internal:type=FlushSorter`, it will limit the
66
+ bean attributes to *ActiveCount*, *CompletedTasks* and *PendingTasks*.
67
+
68
+ By default, attributes are published as **gauges** to the Librato
69
+ Metrics service. If instead you would like to publish the attribute as
70
+ a counter, set the attribute name to the value 'counter'.
71
+
72
+ An example full bean definition file is included for Cassandra under
73
+ the `examples/cassandra` directory in the top-level of this gem. For
74
+ example, to publish the thread JMX beans from Cassandra 0.8.1:
75
+
76
+ ```
77
+ librato-metrics-tap-jmxbeans --email "$EMAIL" --token "$TOKEN" \
78
+ --publish \
79
+ --source "$SOURCE_NAME" \
80
+ --jmx-host "$JMX_HOST" --jmx-port "$JMX_PORT" \
81
+ --data-file-full examples/cassandra/tpstats-0_8_1.yaml \
82
+ --measure-time "$MEASURE_TIME"
83
+ ```
84
+
85
+ ### Bean attributes separate from beans
86
+
87
+ Oftentimes it is useful to publish the same attributes from multiple
88
+ beans that all export the same attribute names. In this case you can
89
+ use the `--data-file-attributes` option to set a YAML file defining
90
+ the bean attributes to publish. You can then set the bean names using
91
+ the `--bean-name` option.
92
+
93
+ For example, a bean attribute definition file may look like:
94
+
95
+ ```
96
+ ---
97
+ LiveDiskSpaceUsed:
98
+ LiveSSTableCount:
99
+ MemtableColumnsCount: counter
100
+ ```
101
+
102
+ If this file were used in combination with the option `--bean-name
103
+ org.apache.cassandra.db:type=ColumnFamilies,keyspace=MyKeyspace,columnfamily=mycf`,
104
+ then the attributes `LiveDiskSpaceUsed`, `LiveSSTableCount` and
105
+ `MemtableColumnsCount` for the bean representing the Cassandra
106
+ columnfamily *mycf* in the keyspace *MyKeyspace* would be published.
107
+
108
+ The `--bean-name` can also be a wildcard that will be expanded to all
109
+ matching beans. For example, the option `--bean-name
110
+ 'org.apache.cassandra.db:type=ColumnFamilies,keyspace=MyKeyspace,columnfamily=*'`
111
+ would publish the same attributes for all column families in the
112
+ keyspace *MyKeyspace*.
113
+
114
+ Attributes are always published as *gauges* to the Librato Metrics
115
+ service. To publish them as a counter, set their value to 'counter'.
116
+
117
+ An example bean attribute definition file is included for Cassandra
118
+ under the `examples/cassandra` directory in the top-level of this
119
+ gem. For example, to publish the CF stats for all column families in
120
+ the keyspace *MyKeyspace*:
121
+
122
+ ```
123
+ librato-metrics-tap-jmxbeans --email "$EMAIL" --token "$TOKEN" \
124
+ --publish --source "$SOURCE_NAME" \
125
+ --jmx-host "$JMX_HOST" --jmx-port "$JMX_PORT" \
126
+ --bean-name 'org.apache.cassandra.db:type=ColumnFamilies,keyspace=MyKeyspace,columnfamily=*' \
127
+ --data-file-attributes examples/cassandra/cfstats-0_8_1.yaml \
128
+ --measure-time "$MEASURE_TIME"
129
+ ```
130
+
131
+ ## Contributing to librato-metrics-taps
132
+
133
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
134
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
135
+ * Fork the project
136
+ * Start a feature/bugfix branch
137
+ * Commit and push until you are happy with your contribution
138
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
139
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
140
+
141
+ ## Copyright
142
+
143
+ Copyright (c) 2011 Librato, Inc. See LICENSE.txt for
144
+ further details.
145
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.6
@@ -0,0 +1,209 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml'
4
+
5
+ require File.join(File.dirname(__FILE__), '../lib/librato/metrics/taps')
6
+ include Librato::Metrics
7
+
8
+ def err(msg)
9
+ $stderr.puts msg
10
+ exit 1
11
+ end
12
+
13
+ def get_beans(bean_name)
14
+ beans = Taps::JMX::match_beans(bean_name)
15
+ unless beans && beans.length > 0
16
+ err "No beans match: #{bean_name}"
17
+ end
18
+ beans
19
+ end
20
+
21
+ def match_beans(opts)
22
+ ret = Taps::JMX::connect!(opts[:jmx_host], opts[:jmx_port])
23
+ unless ret
24
+ err "Failed to connect to JMX endpoint"
25
+ end
26
+
27
+ unless opts[:bean_name]
28
+ err "Must specify --bean-name when matching bean names"
29
+ end
30
+
31
+ beans = get_beans(opts[:bean_name])
32
+ puts beans.join("\n")
33
+ end
34
+
35
+ def publish_beans(publisher, beans, opts)
36
+ (counters, gauges) = Taps::JMX::retrieve(beans, opts[:ignore_missing])
37
+
38
+ r = publisher.post(counters, gauges, opts)
39
+ unless r
40
+ $stderr.puts "ERROR: Failed to publish metrics!"
41
+ end
42
+ return r
43
+ end
44
+
45
+ opts = Trollop::options do
46
+ version "Version: #{Taps::version}"
47
+ banner <<EOS
48
+ Librato Metrics Tap for JMX Beans
49
+ Version: #{Taps::version}
50
+
51
+ Usage: librato-metrics-tap-jmxbeans <options>
52
+
53
+ Options:
54
+ EOS
55
+ # JMX End point
56
+ opt :jmx_host, "JMX Hostname", :short => "-j", :default => "localhost"
57
+ opt :jmx_port, "JMX Port", :short => "-m", :default => 8080
58
+ opt :jmx_username, "JMX Username", :short => "-x", :type => :string
59
+ opt :jmx_password, "JMX Password", :short => "-w", :type => :string
60
+
61
+ opt :email, "Metrics Email address", :short => "-e", :type => :string
62
+ opt :token, "Metrics API token", :short => "-t", :type => :string
63
+ opt :source, "Optional source name", :short => "-s", :type => :string
64
+ opt :prefix, "Optional prefix name", :short => "-z", :type => :string
65
+
66
+ opt :measure_time, "Optional time for measurements", :short => "-a", :type => :int
67
+ opt :metrics_url, "Metrics URL", :short => "-r", :default => 'https://metrics-api.librato.com'
68
+
69
+ opt :interval, "Run as a daemon and poll every N seconds", :short => "-i", :type => :int
70
+
71
+ opt :ignore_missing, "Ignore missing beans/attributes", :short => "-g"
72
+
73
+ # Bean collection options
74
+ opt :data_file_full, "YAML file defining beans & attribute names", :short => "-d", :type => :string
75
+ opt :data_file_attributes, "YAML file of bean attributes", :short => "-f", :type => :string
76
+ opt :bean_name, "Bean name to match against or lookup", :short => "-b", :type => :string
77
+
78
+ # Commands
79
+ opt :publish, "Publish Bean Attributes to Librato Metrics", :short => "-p"
80
+ opt :match_beans, "Output beans that match --bean-name regexp", :short => "-c"
81
+
82
+ end
83
+
84
+ if opts[:publish] && opts[:match_beans]
85
+ err "Only specify one command"
86
+ end
87
+
88
+ unless opts[:publish] || opts[:match_beans]
89
+ err "Must specify a command"
90
+ end
91
+
92
+ if opts[:publish]
93
+ unless opts[:email]
94
+ err "Must provide email to publish"
95
+ end
96
+
97
+ unless opts[:token]
98
+ err "Must provide token to publish"
99
+ end
100
+ end
101
+
102
+ if opts[:match_beans]
103
+ match_beans(opts)
104
+ exit 0
105
+ end
106
+
107
+ #
108
+ # Publishing. Read beans + attributes then push to Metrics
109
+ #
110
+
111
+ publisher = Taps::Publisher.new(opts)
112
+
113
+ ret = Taps::JMX::connect!(opts[:jmx_host], opts[:jmx_port], opts[:jmx_username], opts[:jmx_password])
114
+ unless ret
115
+ err "Failed to connect to JMX endpoint"
116
+ end
117
+
118
+ # Load full definition
119
+ #
120
+ if opts[:data_file_full]
121
+ filename = opts[:data_file_full]
122
+ begin
123
+ beanf = File.open(filename, "r")
124
+ rescue => err
125
+ puts "Failed to open bean file #{filename}: #{err.message}"
126
+ exit 1
127
+ end
128
+
129
+ begin
130
+ beans = YAML::load(beanf)
131
+ rescue => err
132
+ puts "Failed to parse #{filename}: #{err.message}"
133
+ exit 1
134
+ end
135
+ beanf.close
136
+ elsif opts[:bean_name] && opts[:data_file_attributes]
137
+ # Load attributes from file
138
+ #
139
+ filename = opts[:data_file_attributes]
140
+ begin
141
+ beanf = File.open(filename, "r")
142
+ rescue => err
143
+ puts "Failed to open attributes file #{filename}: #{err.message}"
144
+ exit 1
145
+ end
146
+
147
+ begin
148
+ attrs = YAML::load(beanf)
149
+ rescue => err
150
+ puts "Failed to parse #{filename}: #{err.message}"
151
+ exit 1
152
+ end
153
+ beanf.close
154
+
155
+ beans = {}
156
+ beannames = get_beans(opts[:bean_name])
157
+ beannames.each do |name|
158
+ beans[name] = attrs
159
+ end
160
+ else
161
+ err "Must specify --data-file-full or --data-file-attributes"
162
+ end
163
+
164
+ unless opts[:interval]
165
+ r = publish_beans(publisher, beans, opts)
166
+ exit(r ? 0 : 1)
167
+ end
168
+
169
+ # If --interval has been specified, broadcast every interval
170
+ # seconds. We wait for interval seconds each time to ensure we
171
+ # broadcast on the interval
172
+ #
173
+ # We use a random stagger to ensure that we are measuring and
174
+ # publishing our metrics at a random point within the period. This
175
+ # ensures that multiple entities are not measuring and reporting
176
+ # at the same exact points in time.
177
+
178
+ interval = opts[:interval]
179
+ stagger = rand(interval)
180
+ begin
181
+ t = Time.now.tv_sec
182
+
183
+ # Floor the time to the current interval
184
+ t2 = (t / interval) * interval
185
+
186
+ # Offset by the stagger
187
+ t2 += stagger
188
+
189
+ # If our stagger is < interval/2, it is possible that we
190
+ # went back in time. In that case, simply skip another interval
191
+ #
192
+ if t2 <= t
193
+ t2 += interval
194
+ end
195
+
196
+ sleep (t2 - t)
197
+ t = Time.now
198
+
199
+ # We report our measure time as the nearest interval
200
+ tsecs = ((t.tv_sec + (interval / 2)) / interval) * interval
201
+
202
+ publish_beans(publisher, beans, opts.merge(:measure_time => tsecs))
203
+ end while true
204
+
205
+ exit 1
206
+
207
+ # Local Variables:
208
+ # mode: ruby
209
+ # End: