-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.rb
40 lines (34 loc) · 954 Bytes
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'socket'
require 'opentsdb/logging'
module OpenTSDB
class Client
include Logging
attr_reader :connection
def initialize(options = {})
begin
hostname = options[:hostname] || 'localhost'
port = options[:port] || 4242
@connection = TCPSocket.new(hostname, port)
rescue
raise "Unable to connect or invalid connection data"
end
end
def to_command( options )
timestamp = options[:timestamp].to_i
metric_name = options[:metric]
value = options[:value].to_f
tags = options[:tags].map{|k,v| "#{k}=#{v}"}.join(" ")
"put #{metric_name} #{timestamp} #{value} #{tags}"
end
def build_command(input)
if input.kind_of?(Array)
input.collect { |unit| to_command(unit) }.join("\n")
else
to_command(input)
end.chomp
end
def put(options = {})
@connection.puts(build_command(options))
end
end
end