class Oxidized::HTTP

Public Class Methods

new() click to toggle source
Calls superclass method Oxidized::Input::CLI::new
# File lib/oxidized/source/http.rb, line 3
def initialize
  @cfg = Oxidized.config.source.http
  super
end

Public Instance Methods

cmd(callback_or_string) click to toggle source
# File lib/oxidized/input/http.rb, line 32
def cmd(callback_or_string)
  return cmd_cb callback_or_string if callback_or_string.is_a?(Proc)

  cmd_str callback_or_string
end
cmd_cb(callback) click to toggle source
# File lib/oxidized/input/http.rb, line 38
def cmd_cb(callback)
  instance_exec(&callback)
end
cmd_str(string) click to toggle source
# File lib/oxidized/input/http.rb, line 42
def cmd_str(string)
  path = string % { password: @node.auth[:password] }
  get_http path
end
connect(node) click to toggle source
# File lib/oxidized/input/http.rb, line 9
def connect(node)
  @node = node
  @secure = false
  @username = nil
  @password = nil
  @headers = {}
  @log = File.open(Oxidized::Config::Log + "/#{@node.ip}-http", "w") if Oxidized.config.input.debug?
  @node.model.cfg["http"].each { |cb| instance_exec(&cb) }

  return true unless @main_page && defined?(login)

  begin
    require "mechanize"
  rescue LoadError
    raise OxidizedError, "mechanize not found: sudo gem install mechanize"
  end

  @m = Mechanize.new
  url = URI::HTTP.build host: @node.ip, path: @main_page
  @m_page = @m.get(url.to_s)
  login
end
load(node_want = nil) click to toggle source
# File lib/oxidized/source/http.rb, line 19
def load(node_want = nil)
  nodes = []
  data = JSON.parse(read_http(node_want))
  data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
  data.each do |node|
    next if node.empty?

    # map node parameters
    keys = {}
    @cfg.map.each do |key, want_position|
      keys[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
    end
    keys[:model] = map_model keys[:model] if keys.has_key? :model

    # map node specific vars
    vars = {}
    @cfg.vars_map.each do |key, want_position|
      vars[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
    end
    keys[:vars] = vars unless vars.empty?

    nodes << keys
  end
  nodes
end
setup() click to toggle source
# File lib/oxidized/source/http.rb, line 8
def setup
  return unless @cfg.url.empty?

  raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config'
end

Private Instance Methods

disconnect() click to toggle source
# File lib/oxidized/input/http.rb, line 68
def disconnect
  @log.close if Oxidized.config.input.debug?
end
get_http(path) click to toggle source
# File lib/oxidized/input/http.rb, line 49
def get_http(path)
  schema = @secure ? "https://" : "http://"
  uri = URI("#{schema}#{@node.ip}#{path}")
  req = Net::HTTP::Get.new(uri)
  req.basic_auth @username, @password unless @username.nil?
  @headers.each do |header, value|
    req.add_field(header, value)
  end
  ssl_verify = Oxidized.config.input.http.ssl_verify? ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https", verify_mode: ssl_verify) do |http|
    http.request(req)
  end
  res.body
end
log(str) click to toggle source
# File lib/oxidized/input/http.rb, line 64
def log(str)
  @log&.write(str)
end
read_http(node_want) click to toggle source
# File lib/oxidized/source/http.rb, line 58
def read_http(node_want)
  uri = URI.parse(@cfg.url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @cfg.secure

  # map headers
  headers = {}
  @cfg.headers.each do |header, value|
    headers[header] = value
  end

  req_uri = uri.request_uri
  req_uri = "#{req_uri}/#{node_want}" if node_want
  request = Net::HTTP::Get.new(req_uri, headers)
  request.basic_auth(@cfg.user, @cfg.pass) if @cfg.user? && @cfg.pass?
  http.request(request).body
end
string_navigate(object, wants) click to toggle source
# File lib/oxidized/source/http.rb, line 47
def string_navigate(object, wants)
  wants = wants.split(".").map do |want|
    head, match, _tail = want.partition(/\[\d+\]/)
    match.empty? ? head : [head, match[1..-2].to_i]
  end
  wants.flatten.each do |want|
    object = object[want] if object.respond_to? :each
  end
  object
end