class Bipbip::Plugin::Command

Attributes

schema[RW]

Public Instance Methods

metrics_schema() click to toggle source
# File lib/bipbip/plugin/command.rb, line 8
def metrics_schema
  @schema ||= find_schema
end
monitor() click to toggle source
# File lib/bipbip/plugin/command.rb, line 12
def monitor
  Hash[command_output.map { |metric, value| [metric, metric_value(value)] }]
end

Private Instance Methods

command_output() click to toggle source
# File lib/bipbip/plugin/command.rb, line 40
def command_output
  JSON.parse(exec_command)
end
detect_operation_mode(value) click to toggle source
# File lib/bipbip/plugin/command.rb, line 36
def detect_operation_mode(value)
  { true => :advanced, false => :simple }.fetch(value.is_a?(Hash))
end
exec_command() click to toggle source
# File lib/bipbip/plugin/command.rb, line 44
def exec_command
  command = config['command'].to_s

  output_stdout = output_stderr = exit_code = nil
  Open3.popen3(command) do |_stdin, stdout, stderr, wait_thr|
    output_stdout = stdout.read.chomp
    output_stderr = stderr.read.chomp
    exit_code = wait_thr.value
  end

  unless exit_code.success?
    message = ['Command execution failed:', command]
    message.push 'STDOUT:', output_stdout unless output_stdout.empty?
    message.push 'STDERR:', output_stderr unless output_stderr.empty?
    raise message.join("\n")
  end

  output_stdout
end
find_schema() click to toggle source
# File lib/bipbip/plugin/command.rb, line 25
def find_schema
  command_output.map do |metric, value|
    case detect_operation_mode(value)
    when :simple
      { name: metric.to_s, type: 'gauge' }
    when :advanced
      { name: metric.to_s, type: value['type'], unit: value['unit'] }
    end
  end
end
metric_value(value) click to toggle source
# File lib/bipbip/plugin/command.rb, line 18
def metric_value(value)
  value = value['value'] if detect_operation_mode(value) == :advanced
  value = 1 if value == 'true' || value == true
  value = 0 if value == 'false' || value == false
  value
end