Sha256: 5642404396fba7f0337418ef8db0e6f99a01bd4733f493c739decb937126bf8e

Contents?: true

Size: 1.85 KB

Versions: 11

Compression:

Stored size: 1.85 KB

Contents

#encoding: utf-8

module Riemann
  class Babbler

    def helper_error(msg = 'Unknown helper error')
      report({
               :service => plugin.service,
               :state => 'critical',
               :description => msg
      })
    end

    def plugin_timeout
      ( plugin.interval * 2 ).to_f/3
    end

    # хэлпер для парса stdout+stderr и exit status
    def shell(*cmd)
      exit_status=nil
      err=nil
      out=nil
      begin
        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
        }
      rescue => e
        helper_error "#{e.class} #{e}\n#{e.backtrace.join "\n"}"
      end
      if exit_status.to_i > 0
        err = err.chomp if err
        helper_error(err)
      elsif out
        return out.strip
      else
        # статус 0, вывода stdout нет
        ''
      end
    end

    def tcp_port_aviable?(ip, port)
      begin
        Timeout::timeout(plugin_timeout) do
          begin
            s = TCPSocket.new(ip, port)
            s.close
            return true
          rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
            return false
          end
        end
      rescue Timeout::Error
      end
      return false
    end

    # http rest
    def rest_get(url)
      begin
        Timeout::timeout(plugin_timeout) do
          begin
            RestClient.get url
          rescue
            helper_error("Get from url: #{url} failed")
          end
        end
      rescue Timeout::Error
        helper_error("Get from url: #{url}, timeout error")
      end
    end

    # unix timestamp
    def unixnow
      Time.now.to_i
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
riemann-babbler-1.0.7.9 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.8 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.7 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.5 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.4 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.1.0pre2 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.3 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.2 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7.1 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.7 lib/riemann/babbler/support/plugin_helpers.rb
riemann-babbler-1.0.6 lib/riemann/babbler/support/plugin_helpers.rb