Class: Sensu::Plugin::Metric::CLI

Inherits:
CLI
  • Object
show all
Defined in:
lib/sensu-plugin/metric/cli.rb

Direct Known Subclasses

Dogstatsd, Generic, Graphite, Influxdb, JSON, Statsd

Defined Under Namespace

Classes: Dogstatsd, Generic, Graphite, Influxdb, JSON, Statsd

Instance Attribute Summary

Attributes inherited from CLI

#argv

Instance Method Summary collapse

Methods inherited from CLI

#initialize, method_added, #output, #run

Constructor Details

This class inherits a constructor from Sensu::Plugin::CLI

Instance Method Details

#to_dogstatsd(*args) ⇒ String

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`

Outputs metrics using the DogStatsd datagram format

Parameters:

  • args (Array<String, Int>)

    list of arguments

Returns:

  • (String)

    formated metric data



68
69
70
71
72
73
74
75
76
77
# 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) ⇒ String

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

Outputs metrics using the Statsd datagram format

Parameters:

  • args (Array<String, Int>)

    list of arguments

Returns:

  • (String)

    formated metric data



31
32
33
34
35
36
37
38
39
# 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) ⇒ String

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

Outputs metrics using the InfluxDB line protocol format

Parameters:

  • args (Array<String, Int>)

    list of arguments

Returns:

  • (String)

    formated metric data



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ String

Outputs metrics using raw json format

Parameters:

  • obj (Hash) (defaults to: nil)

    there is no strict expectation from the provided object

Returns:

  • (String)

    formated metric data



14
15
16
17
18
19
20
21
# 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) ⇒ String

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

Outputs metrics using the Statsd datagram format

Parameters:

  • args (Array<String, Int>)

    list of arguments

Returns:

  • (String)

    formated metric data



49
50
51
52
53
54
55
56
57
# 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