Class: Sensucronic
- Inherits:
-
Object
- Object
- Sensucronic
- Includes:
- Mixlib::CLI
- Defined in:
- lib/sensucronic.rb,
lib/sensucronic/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Instance Method Summary collapse
- #cli(argv = ARGV) ⇒ Object
- #dryrun ⇒ Object
- #exitcode ⇒ Object
- #exitsignal ⇒ Object
- #fields ⇒ Object
- #file_socket ⇒ Object
- #issue_report ⇒ Object
- #output ⇒ Object
- #output_status ⇒ Object
- #port ⇒ Object
- #report ⇒ Object
- #report_socket ⇒ Object
- #report_stdout ⇒ Object
- #run ⇒ Object
- #sensu_status ⇒ Object
Instance Attribute Details
#status ⇒ Object (readonly)
Returns the value of attribute status.
47 48 49 |
# File 'lib/sensucronic.rb', line 47 def status @status end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
46 47 48 |
# File 'lib/sensucronic.rb', line 46 def stderr @stderr end |
#stdout ⇒ Object (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) (argv) run issue_report end |
#dryrun ⇒ Object
49 50 51 |
# File 'lib/sensucronic.rb', line 49 def dryrun config[:dryrun] end |
#exitcode ⇒ Object
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 |
#exitsignal ⇒ Object
107 108 109 |
# File 'lib/sensucronic.rb', line 107 def exitsignal status.termsig || status.stopsig # I think stop sig will hang? end |
#fields ⇒ Object
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_socket ⇒ Object
75 76 77 |
# File 'lib/sensucronic.rb', line 75 def file_socket "/dev/tcp/localhost/#{port}" end |
#issue_report ⇒ Object
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 |
#output ⇒ Object
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_status ⇒ Object
97 98 99 |
# File 'lib/sensucronic.rb', line 97 def output_status status.to_s unless status.exited? end |
#port ⇒ Object
53 54 55 |
# File 'lib/sensucronic.rb', line 53 def port config[:port] end |
#report ⇒ Object
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_socket ⇒ Object
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. exit 127 end |
#report_stdout ⇒ Object
71 72 73 |
# File 'lib/sensucronic.rb', line 71 def report_stdout puts JSON.pretty_generate(report) end |
#run ⇒ Object
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., $? end |
#sensu_status ⇒ Object
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 |