continuum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
4
+ doc/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ruby-1.9.2-p136@continuum
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in continuum.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ continuum (0.0.1)
5
+ hugs
6
+ rake
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ autotest-standalone (4.5.5)
12
+ fakeweb (1.3.0)
13
+ hugs (2.5.2)
14
+ multipart-post (~> 1.0.1)
15
+ net-http-persistent (~> 1.4.1)
16
+ nokogiri (~> 1.4.4)
17
+ yajl-ruby (~> 0.7.9)
18
+ multipart-post (1.0.1)
19
+ net-http-persistent (1.4.1)
20
+ nokogiri (1.4.4)
21
+ rake (0.8.7)
22
+ vcr (1.5.0)
23
+ yajl-ruby (0.7.9)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ autotest-standalone
30
+ continuum!
31
+ fakeweb
32
+ vcr (= 1.5.0)
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Continuum
2
+ A Ruby gem which integrates with the OpenTSDB API
3
+
4
+ http://opentsdb.net/http-api.html
5
+
6
+ # About OpenTSDB
7
+ OpenTSDB is a distributed, scalable Time Series Database (TSDB) written on top of HBase. OpenTSDB was written to address a common need: store, index and serve metrics collected from computer systems (network gear, operating systems, applications) at a large scale, and make this data easily accessible and graphable.
8
+
9
+ # About Continuum
10
+ Continuum integrates with the OpenTSDB API so that Ruby Applications can access the metrics written to OpenTSDB instances. Support is planned for writing metrics into an OpenTSDB instance. So that you can easily integrate with Ruby applications.
11
+
12
+ # Usage
13
+
14
+ > client = Continuum::Client.new '10.0.0.1', 4242
15
+ > client.aggregators
16
+ => ["min", "sum", "max", "avg"]
17
+
18
+ > client.logs.first
19
+ => "1305212010\tINFO\tNew I/O server boss #1 ([id: 0x7d8a8ce2, /0:0:0:0:0:0:0:0:4242])\tnet.opentsdb.tsd.ConnectionManager\t[id: 0x33f98d58, /10.0.0.2:63832 => /10.0.0.1:4242] CONNECTED: /10.0.0.2:63832"
20
+
21
+ > client.query(
22
+ :format => :json,
23
+ :start => '2h-ago',
24
+ :m => ['sum:rate:proc.net.bytes', 'sum:rate:proc.stat.cpu']
25
+ )
26
+ => {"plotted"=>701, "points"=>1961, "etags"=>[["direction"], ["type"]], "timing"=>370}
27
+
28
+ > client.query(
29
+ :format => :png,
30
+ :start => (Time.now - 7200),
31
+ :m => ['sum:rate:proc.net.bytes', 'sum:rate:proc.stat.cpu']
32
+ )
33
+ => # A PNG binary.
34
+
35
+ > client.query(
36
+ :format => :ascii,
37
+ :start => (Time.now - 7200),
38
+ :m => ['sum:rate:proc.net.bytes', 'sum:rate:proc.stat.cpu']
39
+ )
40
+ > data.split("\n").first
41
+ => "proc.net.bytes 1305211753 563002.2 iface=eth0 host=i-007"
42
+
43
+
44
+ # Todo
45
+ * The rest of the Read API
46
+ * The write API
47
+
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler'
2
+
3
+ require "rake/testtask"
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << "./lib" << "./test"
6
+ test.pattern = "test/**/*_test.rb"
7
+ test.verbose = true
8
+ end
9
+
10
+ Bundler::GemHelper.install_tasks
11
+
12
+ task :irb do
13
+ exec 'irb -I.:lib -rubygems -rcontinuum'
14
+ end
15
+
16
+ task :autotest do
17
+ exec 'autotest -b'
18
+ end
19
+
20
+ task :default= => :test
data/continuum.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "continuum/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "continuum"
7
+ s.version = Continuum::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Josh Kleinpeter"]
10
+ s.email = ["josh@kleinpeter.org"]
11
+ s.homepage = ""
12
+ s.summary = %q{A Ruby gem that interfaces with OpenTSDB.}
13
+ s.description = %q{A Ruby gem that interfaces with OpenTSDB.}
14
+
15
+ s.rubyforge_project = "continuum"
16
+ s.add_dependency 'hugs'
17
+ s.add_dependency 'rake'
18
+ s.add_development_dependency 'fakeweb'
19
+ s.add_development_dependency 'vcr', '1.5.0'
20
+ s.add_development_dependency 'autotest-standalone'
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,170 @@
1
+ module Continuum
2
+ # Create an instance of the client to interface with the OpenTSDB API (http://opentsdb.net/http-api.html)
3
+ class Client
4
+ # Create an connection to a specific OpenTSDB instance
5
+ #
6
+ # *Params:*
7
+ #
8
+ # * host is the host IP (defaults to localhost)
9
+ # * port is the host's port (defaults to 4242)
10
+ #
11
+ # *Returns:*
12
+ #
13
+ # A client to play with
14
+ def initialize host = '127.0.0.1', port = 4242
15
+ @host = host
16
+ @port = port
17
+ @client = Hugs::Client.new(
18
+ :host => @host,
19
+ :port => @port,
20
+ :scheme => 'http',
21
+ :type => :none
22
+ )
23
+ end
24
+
25
+ # Lists the supported aggregators by this instance
26
+ #
27
+ # *Returns:*
28
+ #
29
+ # An array of aggregators.
30
+ def aggregators
31
+ response = @client.get '/aggregators?json=true'
32
+ JSON.parse response.body
33
+ end
34
+
35
+ # Lists an array of log lines. By default, OpenTSDB returns 1024 lines.
36
+ # You can't modify that number via the API.
37
+ #
38
+ # *Returns:*
39
+ #
40
+ # An array of log lines.
41
+ def logs
42
+ response = @client.get '/logs?json=true'
43
+ JSON.parse response.body
44
+ end
45
+
46
+ # Queries the instance for a graph. 3 (useful) formats are supported:
47
+ #
48
+ # * ASCII returns data that is suitable for graphing or otherwise interpreting on the client
49
+ # * JSON returns meta data for the query
50
+ # * PNG returns a PNG image you can render on the client
51
+ #
52
+ #
53
+ # Params (See http://opentsdb.net/http-api.html#/q_Parameters for more information):
54
+ #
55
+ #
56
+ # options a hash which may include the following keys:
57
+ #
58
+ # * format (one of json, ascii, png), defaults to json.
59
+ # * start The query's start date. (required)
60
+ # * end The query's end date.
61
+ # * m The query itself. (required, may be an array)
62
+ # * o Rendering options.
63
+ # * wxh The dimensions of the graph.
64
+ # * yrange The range of the left Y axis.
65
+ # * y2range The range of the right Y axis.
66
+ # * ylabel Label for the left Y axis.
67
+ # * y2label Label for the right Y axis.
68
+ # * yformat Format string for the left Y axis.
69
+ # * y2format Format string for the right Y axis.
70
+ # * ylog Enables log scale for the left Y axis.
71
+ # * y2log Enables log scale for the right Y axis.
72
+ # * key Options for the key (legend) of the graph.
73
+ # * nokey Removes the key (legend) from the graph.
74
+ # * nocache Forces TSD to ignore cache and fetch results from HBase.
75
+ #
76
+ # The syntax for metrics (m) (square brackets indicate an optional part):
77
+ #
78
+ # AGG:[interval-AGG:][rate:]metric[{tag1=value1[,tag2=value2...]}]
79
+ def query options = {}
80
+ format = options.delete(:format) || options.delete('format') || 'json'
81
+ options[format.to_sym] = true
82
+ params = query_params(options, [:start, :m])
83
+ response = @client.get "/q?#{params}"
84
+
85
+ if format.to_sym == :json
86
+ JSON.parse response.body
87
+ else
88
+ response.body
89
+ end
90
+ end
91
+
92
+ # Stats about the OpenTSDB server itself.
93
+ #
94
+ # Returns:
95
+ # An array of stats.
96
+ def stats
97
+ response = @client.get '/stats?json'
98
+ JSON.parse response.body
99
+ end
100
+
101
+ # Returns suggestions for metric or tag names.
102
+ #
103
+ # Params:
104
+ # * query: the string to search for
105
+ # * type: the type of item to search for (defaults to metrics)
106
+ # Type can be one of the following:
107
+ # * metrics: Provide suggestions for metric names.
108
+ # * tagk: Provide suggestions for tag names.
109
+ # * tagv: Provide suggestions for tag values.
110
+ #
111
+ # Returns:
112
+ # An array of suggestions
113
+ def suggest query, type = 'metrics'
114
+ response = @client.get "/suggest?q=#{query}&type=#{type}"
115
+ JSON.parse response.body
116
+ end
117
+
118
+ # Format
119
+ # put <metric> <tisse> <value> host=<hostname>
120
+ # put proc.loadavg.5m 1305308654 0.01 host=i-00000106
121
+ def metric name, value
122
+ message = "put #{name} #{Time.now.to_i} #{value} host=#{Socket.gethostname}"
123
+
124
+ socket = TCPSocket.new @host, @port
125
+ socket.write message
126
+ socket.close
127
+
128
+ message
129
+ end
130
+
131
+ # Returns the version of OpenTSDB
132
+ #
133
+ # Returns
134
+ # An array with the version information
135
+ def version
136
+ response = @client.get '/version?json'
137
+ JSON.parse response.body
138
+ end
139
+
140
+ # Parses a query param hash into a query string as expected by OpenTSDB
141
+ # *Params:*
142
+ # * params the parameters to parse into a query string
143
+ # * requirements: any required parameters
144
+ # *Returns:*
145
+ # A query string
146
+ # Raises:
147
+ # ArgumentError if a required parameter is missing
148
+ def query_params params = {}, requirements = []
149
+ query = []
150
+
151
+ requirements.each do |req|
152
+ unless params.keys.include?(req.to_sym) || params.keys.include?(req.to_s)
153
+ raise ArgumentError.new("#{req} is a required parameter.")
154
+ end
155
+ end
156
+
157
+ params.each_pair do |k,v|
158
+ if v.respond_to? :each
159
+ v.each do |subv|
160
+ query << "#{k}=#{subv}"
161
+ end
162
+ else
163
+ v = v.strftime('%Y/%m/%d-%H:%M:%S') if v.respond_to? :strftime
164
+ query << "#{k}=#{v}"
165
+ end
166
+ end
167
+ query.join '&'
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,4 @@
1
+ module Continuum
2
+ # This is the current version. Awesome!
3
+ VERSION = "0.0.1"
4
+ end
data/lib/continuum.rb ADDED
@@ -0,0 +1,51 @@
1
+ require 'hugs'
2
+ require 'json'
3
+ require 'socket'
4
+ require 'continuum/client'
5
+
6
+ ## Continuum
7
+ #A Ruby gem which integrates with the OpenTSDB API
8
+ #
9
+ #http://opentsdb.net/http-api.html
10
+ #
11
+ ## About OpenTSDB
12
+ #OpenTSDB is a distributed, scalable Time Series Database (TSDB) written on top of HBase. OpenTSDB was written to address a common need: store, index and serve metrics collected from computer systems (network gear, operating systems, applications) at a large scale, and make this data easily accessible and graphable.
13
+ #
14
+ ## About Continuum
15
+ #Continuum integrates with the OpenTSDB API so that Ruby Applications can access the metrics written to OpenTSDB instances. Support is planned for writing metrics into an OpenTSDB instance. So that you can easily integrate with Ruby applications.
16
+ #
17
+ ## Usage
18
+ #
19
+ ##
20
+ # > client = Continuum::Client.new '10.0.0.1', 4242
21
+ # > client.aggregators
22
+ # => ["min", "sum", "max", "avg"]
23
+ #
24
+ # > client.logs.first
25
+ # => "1305212010\tINFO\tNew I/O server boss #1 ([id: 0x7d8a8ce2, /0:0:0:0:0:0:0:0:4242])\tnet.opentsdb.tsd.ConnectionManager\t[id: 0x33f98d58, /10.0.0.2:63832 => /10.0.0.1:4242] CONNECTED: /10.0.0.2:63832"
26
+ #
27
+ # > client.query(
28
+ # :format => :json,
29
+ # :start => '2h-ago',
30
+ # :m => ['sum:rate:proc.net.bytes', 'sum:rate:proc.stat.cpu']
31
+ # )
32
+ # => {"plotted"=>701, "points"=>1961, "etags"=>[["direction"], ["type"]], "timing"=>370}
33
+ #
34
+ # > client.query(
35
+ # :format => :png,
36
+ # :start => (Time.now - 7200),
37
+ # :m => ['sum:rate:proc.net.bytes', 'sum:rate:proc.stat.cpu']
38
+ # )
39
+ # => # A PNG binary.
40
+ #
41
+ # > client.query(
42
+ # :format => :ascii,
43
+ # :start => (Time.now - 7200),
44
+ # :m => ['sum:rate:proc.net.bytes', 'sum:rate:proc.stat.cpu']
45
+ # )
46
+ # > data.split("\n").first
47
+ # => "proc.net.bytes 1305211753 563002.2 iface=eth0 host=i-007"
48
+ #
49
+ #
50
+ module Continuum
51
+ end
@@ -0,0 +1,24 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :get
5
+ uri: http://10.3.172.58:4242/aggregators?json=true
6
+ body:
7
+ headers:
8
+ accept:
9
+ - application/json
10
+ connection:
11
+ - keep-alive
12
+ keep-alive:
13
+ - 30
14
+ response: !ruby/struct:VCR::Response
15
+ status: !ruby/struct:VCR::ResponseStatus
16
+ code: 200
17
+ message: OK
18
+ headers:
19
+ content-type:
20
+ - application/json
21
+ content-length:
22
+ - "25"
23
+ body: "[\"min\",\"sum\",\"max\",\"avg\"]"
24
+ http_version: "1.1"