Class: Sensucronic

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/sensucronic.rb,
lib/sensucronic/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



47
48
49
# File 'lib/sensucronic.rb', line 47

def status
  @status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



46
47
48
# File 'lib/sensucronic.rb', line 46

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



45
46
47
# File 'lib/sensucronic.rb', line 45

def stdout
  @stdout
end

Instance Method Details

#cli(argv = ARGV) ⇒ Object



57
58
59
60
61
# File 'lib/sensucronic.rb', line 57

def cli(argv = ARGV)
  parse_options(argv)
  run
  issue_report
end

#dryrunObject



49
50
51
# File 'lib/sensucronic.rb', line 49

def dryrun
  config[:dryrun]
end

#exitcodeObject



111
112
113
114
# File 'lib/sensucronic.rb', line 111

def exitcode
  # be like the bash on signals
  status.exited? ? status.exitstatus : 128 + exitsignal
end

#exitsignalObject



107
108
109
# File 'lib/sensucronic.rb', line 107

def exitsignal
  status.termsig || status.stopsig # I think stop sig will hang?
end

#fieldsObject



125
126
127
128
129
# 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_socketObject



75
76
77
# File 'lib/sensucronic.rb', line 75

def file_socket
  "/dev/tcp/localhost/#{port}"
end

#issue_reportObject



63
64
65
66
67
68
69
# File 'lib/sensucronic.rb', line 63

def issue_report
  if dryrun
    report_stdout
  else
    report_socket
  end
end

#outputObject



101
102
103
104
105
# File 'lib/sensucronic.rb', line 101

def output
  [stdout, stderr, output_status]
    .select {|msg| msg && msg != "" }
    .join("\n");
end

#output_statusObject



97
98
99
# File 'lib/sensucronic.rb', line 97

def output_status
  status.to_s unless status.exited?
end

#portObject



53
54
55
# File 'lib/sensucronic.rb', line 53

def port
  config[:port]
end

#reportObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# 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_socketObject



79
80
81
82
83
84
85
86
87
88
89
# 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_stdoutObject



71
72
73
# File 'lib/sensucronic.rb', line 71

def report_stdout
  puts JSON.pretty_generate(report)
end

#runObject



91
92
93
94
95
# 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_statusObject



116
117
118
119
120
121
122
123
# File 'lib/sensucronic.rb', line 116

def sensu_status
  case exitcode
  when 0..2
    exitcode
  else
    3
  end
end