Sha256: dc2815de446f3a57e4aeef8aae90ee989b63d6ef0033fb0d627a7b4308724fb7

Contents?: true

Size: 899 Bytes

Versions: 51

Compression:

Stored size: 899 Bytes

Contents

#encoding: utf-8

require 'open3'

module Riemann
  module Babbler
    module Plugins
      module Helpers

        # helper: stdout+stderr и exit status
        def shell(*cmd)
          exit_status=nil
          err        =nil
          out        =nil
          Timeout::timeout(plugin.timeout) {
            Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
              err = stderr.gets(nil)
              out = stdout.gets(nil)
              [stdin, stdout, stderr].each { |stream| stream.send('close') }
              exit_status = wait_thread.value
            end
          }
          if exit_status.to_i > 0
            err = err.chomp if err
            raise 'Error while running shell: ' + err.to_s
          elsif out
            return out.strip
          else
            # exit status 0, no stdout
            ''
          end
        end

      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
riemann-babbler-2.0.0pre11 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre10 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre9 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre8 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre7 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre6 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre5 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre4 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre3 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre2 lib/riemann/babbler/plugins/helpers/shell.rb
riemann-babbler-2.0.0pre1 lib/riemann/babbler/plugins/helpers/shell.rb