class Sensu::Plugin::Metric::CLI

Public Instance Methods

to_dogstatsd(*args) click to toggle source

Outputs metrics using the DogStatsd datagram format

@param args [Array<String, Int>] list of arguments @note the argument order should be:

`metric_name`: Mandatory, name for the metric,
`value`: Mandatory, metric value
`type`: Optional, metric type- `c` for counter, `g` for gauge, `ms` for timer, `h` for histogram, `s` for set
`tags`: Optional, a comma separated key:value string `tag1:value1,tag2:value2`

@return [String] formated metric data

# File lib/sensu-plugin/metric/cli.rb, line 68
def to_dogstatsd(*args)
  return if args.join.empty?
  if args[0].is_a?(Exception) || args[1].nil?
    puts args[0].to_s
  else
    type = args[2] || 'kv'
    tags = args[3] ? "##{args[3]}" : nil
    puts [args[0..1].join(':'), type, tags].compact.join('|')
  end
end
to_graphite(*args) click to toggle source

Outputs metrics using the Statsd datagram format

@param args [Array<String, Int>] list of arguments @note the argument order should be:

`metric_path`: Mandatory, name for the metric,
`value`: Mandatory, metric value
`timestamp`: Optional, unix timestamp, defaults to current time

@return [String] formated metric data

# File lib/sensu-plugin/metric/cli.rb, line 31
def to_graphite(*args)
  return if args.join.empty?
  if args[0].is_a?(Exception) || args[1].nil?
    puts args[0].to_s
  else
    args[2] ||= Time.now.to_i
    puts args[0..2].join("\s")
  end
end
to_influxdb(*args) click to toggle source

Outputs metrics using the InfluxDB line protocol format

@param args [Array<String, Int>] list of arguments @note the argument order should be:

`measurement_name`: Mandatory, name for the InfluxDB measurement,
`fields`: Mandatory, either an integer or a comma separated key=value string `field1=value1,field2=value2`
`tags`: Optional, a comma separated key=value string `tag1=value1,tag2=value2`
`timestamp`: Optional, unix timestamp, defaults to current time

@return [String] formated metric data

# File lib/sensu-plugin/metric/cli.rb, line 88
def to_influxdb(*args)
  return if args.join.empty?
  if args[0].is_a?(Exception) || args[1].nil?
    puts args[0].to_s
  else
    fields = if args[1].is_a?(Integer)
               "value=#{args[1]}"
             else
               args[1]
             end
    measurement = [args[0], args[2]].compact.join(',')
    ts = args[3] || Time.now.to_i
    puts [measurement, fields, ts].join(' ')
  end
end
to_json(obj = nil) click to toggle source

Outputs metrics using raw json format

@param obj [Hash] there is no strict expectation from the provided object @return [String] formated metric data

# File lib/sensu-plugin/metric/cli.rb, line 14
def to_json(obj = nil)
  if obj.is_a?(String) || obj.is_a?(Exception)
    puts obj.to_s
  elsif obj.is_a?(Hash)
    obj['timestamp'] ||= Time.now.to_i
    puts ::JSON.generate(obj)
  end
end
to_statsd(*args) click to toggle source

Outputs metrics using the Statsd datagram format

@param args [Array<String, Int>] list of arguments @note the argument order should be:

`metric_name`: Mandatory, name for the metric,
`value`: Mandatory, metric value
`type`: Optional, metric type- `c` for counter, `g` for gauge, `ms` for timer, `s` for set

@return [String] formated metric data

# File lib/sensu-plugin/metric/cli.rb, line 49
def to_statsd(*args)
  return if args.join.empty?
  if args[0].is_a?(Exception) || args[1].nil?
    puts args[0].to_s
  else
    type = args[2] || 'kv'
    puts [args[0..1].join(':'), type].join('|')
  end
end