metzoo 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/metzoo.rb +163 -0
  2. metadata +45 -0
data/lib/metzoo.rb ADDED
@@ -0,0 +1,163 @@
1
+ #encoding: UTF-8
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ # Interface to communicate with metzoo services
7
+ #
8
+ # To set the prouction environment (default):
9
+ # Metzoo::API.uri = "http://api.metzoo.com/"
10
+ #
11
+ # To set the staging environment:
12
+ # Metzoo::API.uri = "http://staging-metzoo01.dev.edrans.net:8081/"
13
+ #
14
+ # To initialize a customer instance just pass your customer key:
15
+ # customer = Metzoo::Customer.new('xxx-xxx....')
16
+ #
17
+ # To create a new agent or get an existing:
18
+ # agent = customer.create_agent("host name...")
19
+ #
20
+ # To create a metric for an agent or just send data to new metric name:
21
+ # metric = agent.create_metric(["metric name 1..", ""metric name 2.."", ...])
22
+ #
23
+ # To send data for a create a metric for an agent:
24
+ # agent = customer.create_agent("host name...")
25
+ #
26
+ # Minimum example:
27
+ # require 'metzoo'
28
+ #
29
+ # customer = Metzoo::Customer.new "xxx-xxx...."
30
+ # agent = customer.create_agent "host.local"
31
+ # agent.send_data "random.value": Random.rand(), "fix.value": 3.14
32
+ #
33
+ module Metzoo
34
+
35
+ # Ruby SDK version
36
+ VERSION = '0.1.3'
37
+
38
+ # Raised on Metzoo exceptions
39
+ class MetzooException < Exception; end
40
+
41
+ # Customer access
42
+ #
43
+ class Customer
44
+
45
+ # Customer uniq identification
46
+ attr_reader :key
47
+
48
+ # Initialize a customer using the customer_key
49
+ #
50
+ # Attributes
51
+ # * +customer_key+ - Customer uniq key
52
+ #
53
+ def initialize(customer_key)
54
+ @key = customer_key
55
+ end
56
+
57
+ # Create a new agent from a customer or get if exsits
58
+ #
59
+ # Attributes
60
+ # * +host_name+ - A string with the agent name
61
+ #
62
+ def create_agent(host_name)
63
+ result_code, result_data = API.call("/agents", body: {host_name: host_name, customer_key: @key})
64
+ if [200,201].include? result_code
65
+ Agent.new result_data["agentKey"] || result_data.gsub(/"/, "")
66
+ else
67
+ raise MetzooException.new result_data
68
+ end
69
+ end
70
+ end
71
+
72
+ # Agent access
73
+ #
74
+ class Agent
75
+ # Agent uniq identification
76
+ attr_reader :key
77
+
78
+ # Initialize an agent using the agent_key
79
+ #
80
+ # Attributes
81
+ # * +agent_key+ - Agent uniq key
82
+ #
83
+ def initialize(agent_key)
84
+ @key = agent_key
85
+ end
86
+
87
+ # Create a new metric from an agent
88
+ # If the metric exists no error returns
89
+ #
90
+ # Attributes
91
+ # * +series+ - A string contain one metric name or an array contain metric names
92
+ #
93
+ def create_metric(series)
94
+ series= [series].flatten.map{|serie| serie.is_a?(String) ? Hash[[:name, :unit].zip(serie.split(":"))] : serie}
95
+ API.call("/metrics", header: {"Agent-Key" => @key}, body: series)
96
+ end
97
+
98
+ # Send data to agent metrics
99
+ #
100
+ # Multiple metrics for one agent can be sended
101
+ #
102
+ # Attributes
103
+ # * +data+ - hash with metrics and values
104
+ # You can send multiple metrics in a key-value format.
105
+ # You can send the user time as :t attributte. If this attributte not exists the SDK will send your local time
106
+ #
107
+ # Example:
108
+ # agent.send_data t: Time.now, "random.value": Random.rand(100), "fix.value": 3.1416
109
+ #
110
+ def send_data(data)
111
+ data = Hash[data.map{|k, v| [k.to_s, v]}]
112
+ data["t"] = data["t"].utc.to_i if data["t"].is_a?(Time)
113
+ data["t"] = Time.now.utc.to_i if !data["t"]
114
+ API.call("/metrics/data?register=true", header: {"Agent-Key" => @key}, body: data)
115
+ end
116
+ end
117
+
118
+ # Metzoo API access
119
+ #
120
+ class API
121
+ # The environment end-point URI address.
122
+ @@api = URI('http://api.metzoo.com/')
123
+
124
+ # Set the environment end-point URI address
125
+ def self.uri=(uri)
126
+ @@api = URI(uri)
127
+ end
128
+
129
+ def self.call(service, params = {})
130
+ headers = build_header(params[:header])
131
+ body = params[:body].to_json if params[:body]
132
+
133
+ http = Net::HTTP.new(@@api.host, @@api.port)
134
+ req = Net::HTTP::Post.new(service, headers)
135
+ req.body = body
136
+ res = http.request(req)
137
+
138
+ [res.code.to_i, (JSON.parse(res.body) rescue res.body)]
139
+ end
140
+
141
+ private
142
+
143
+ def self.build_header(params = {})
144
+ {
145
+ "Content-Type" => "application/json",
146
+ "User-Agent" => "Metzoo-Ruby-Client/#{Metzoo::VERSION}"
147
+ }.merge(params || {})
148
+ end
149
+ end
150
+
151
+ end
152
+
153
+ # require 'metzoo'
154
+ #
155
+ # # Metzoo::API.uri = "http://staging-metzoo01.dev.edrans.net:8081/"
156
+ #
157
+ # customer = Metzoo::Customer.new "2f9397a8-d339-4061-b9f2-cd54505d5c3f"
158
+ # agent = customer.create_agent "host001.domain.com"
159
+ #
160
+ # agent = Metzoo::Agent.new "e22f3ebf-fa03-50c3-79cd-7064d1010a78"
161
+ # agent.create_metric ["cpu.low", "cpu.high"]
162
+ # agent.send_data "cpu.low" => 0.28, "cpu.high" => 0.65
163
+ # agent.send_data "t" => Time.now, "cpu.low" => 0.28, "cpu.high" => 0.65
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metzoo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Edrans development teem
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-10-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Metzoo agent for Ruby
15
+ email: tech-dev@edrans.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/metzoo.rb
21
+ homepage: http://www.metzoo.com
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.23.2
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Metzoo agent for Ruby
45
+ test_files: []