class Sensucronic

Constants

VERSION

Attributes

status[R]
stderr[R]
stdout[R]

Public Instance Methods

cli(argv = ARGV) click to toggle source
# File lib/sensucronic.rb, line 57
def cli(argv = ARGV)
  parse_options(argv)
  run
  issue_report
end
dryrun() click to toggle source
# File lib/sensucronic.rb, line 49
def dryrun
  config[:dryrun]
end
exitcode() click to toggle source
# File lib/sensucronic.rb, line 111
def exitcode
  # be like the bash on signals
  status.exited? ? status.exitstatus : 128 + exitsignal
end
exitsignal() click to toggle source
# File lib/sensucronic.rb, line 107
def exitsignal
  status.termsig || status.stopsig # I think stop sig will hang?
end
fields() click to toggle source
# File lib/sensucronic.rb, line 125
def fields
  config[:field]
    .map { |f| f.split(/:\s*/,2) }
    .each_with_object({}) { |(k,v), h| h[k.to_sym] = v }
end
file_socket() click to toggle source
# File lib/sensucronic.rb, line 75
def file_socket
  "/dev/tcp/localhost/#{port}"
end
issue_report() click to toggle source
# File lib/sensucronic.rb, line 63
def issue_report
  if dryrun
    report_stdout
  else
    report_socket
  end
end
output() click to toggle source
# File lib/sensucronic.rb, line 101
def output
  [stdout, stderr, output_status]
    .select {|msg| msg && msg != "" }
    .join("\n");
end
output_status() click to toggle source
# File lib/sensucronic.rb, line 97
def output_status
  status.to_s unless status.exited?
end
port() click to toggle source
# File lib/sensucronic.rb, line 53
def port
  config[:port]
end
report() click to toggle source
# File lib/sensucronic.rb, line 131
def report
  {
    command:  Shellwords.shelljoin(cli_arguments),
    output:   output,
    status:   sensu_status,
    exitcode: exitcode,
    agent:    self.class.to_s.downcase,
  }.tap do |r|
    r[:exitsignal] = status.termsig  if status.termsig
    r[:source]     = config[:source] if config[:source]
    r.update(fields) { |k,o,n| o }
  end
end
report_socket() click to toggle source
# File lib/sensucronic.rb, line 79
def report_socket
  # TODO if needed actually connect via tcp to the
  # client input socket.
  File.open(file_socket, 'w') do |sock|
    sock.write(report.to_json)
  end
rescue Errno::ENOENT => e
  STDERR.puts "failed to submit to sensu client input socket"
  STDERR.puts e.message
  exit 127
end
report_stdout() click to toggle source
# File lib/sensucronic.rb, line 71
def report_stdout
  puts JSON.pretty_generate(report)
end
run() click to toggle source
# File lib/sensucronic.rb, line 91
def run
  @stdout, @stderr, @status = Open3.capture3(*cli_arguments)
rescue Errno::ENOENT => e
  @stdout, @stderr, @status = e.class, e.message, $?
end
sensu_status() click to toggle source
# File lib/sensucronic.rb, line 116
def sensu_status
  case exitcode
  when 0..2
    exitcode
  else
    3
  end
end