Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 120 lines (101 sloc) 2.854 kb
#!/usr/bin/env ruby
require "hastur"
require "chronic"
require "trollop"
opts = Trollop::options do
banner <<EOS
hastur is a command-line program to send Hastur metrics.
Usage:
hastur [options] <type> [<name> [<value>]]
Examples:
hastur counter things.to.do 4 --labels app=MyApp type=todo
hastur heartbeat slow.cron.job
hastur mark script.ran.after.failed.job --labels env=development activity=debugging
hastur gauge old.gauge 37.1 --time "3 months ago Saturday at 5pm"
Options:
EOS
opt :time, "Timestamp to send", :type => String
opt :labels, "Labels to send", :type => :strings, :multi => true
opt :print, "Print the call args", :type => :boolean, :default => false
end
Trollop::die "you must give a type!" if ARGV.empty?
type = ARGV.shift.downcase
# Args:
# - mark: name, value, timestamp, labels
# - counter: name, increment, timestamp, labels
# - gauge: name, value, timestamp, labels
# - event: name, subject, body, attn, timestamp, labels
# - heartbeat: name, value, timeout, timestamp, labels
TYPES = {
"mark" => {
:name => true,
:value => :maybe,
},
"gauge" => {
:name => true,
:value => true,
},
"counter" => {
:name => true,
:value => :maybe,
},
"heartbeat" => {
:name => :maybe,
:value => :maybe,
:timeout => :maybe,
}
}
TYPE = TYPES[type]
Trollop::die "Type must be one of: #{TYPES.keys.join(', ')}" unless TYPE
#
# Convert a value to an appropriate Ruby value (either Fixnum, Float, or String).
#
# @param [String] value_string The string value to convert
# @return The resulting value
#
def convert_to_ruby_value(value_string)
case value_string
when /^-?\d*\.\d+$/ then value_string.to_f
when /^-?\d+$/ then value_string.to_f
else value_string
end
end
#
# Try to get an argument by name if this message type supports it.
#
def try_get_arg(arg_name, message_type)
# Doesn't allow this arg type? Return quietly.
return unless TYPE[arg_name]
if ARGV.size > 0
# If the arg is here and TYPE[arg_name] is true or maybe, use it.
convert_to_ruby_value(ARGV.shift)
elsif TYPE[arg_name] == :maybe
nil
else
Trollop::die "You must give a #{arg_name} for a metric of type #{type}"
end
end
##############################
# Build the argument list
##############################
args = [type]
args << try_get_arg(:name, type)
args << try_get_arg(:value, type)
# TODO(noah): add timeout for heartbeat
# Time is next to last
time = Time.now
if opts[:time]
time = Chronic.parse opts[:time]
end
args << time
# Labels is last
labels = {}
if opts[:labels]
opts[:labels].flatten.each do |item|
name, value = item.split("=")
labels[name] = convert_to_ruby_value(value)
end
end
args << labels
puts "Hastur.send *#{args.inspect}" if opts[:print]
Hastur.send(*args)
Jump to Line
Something went wrong with that request. Please try again.