summaryrefslogtreecommitdiff
path: root/lib/oxidized/source/http.rb
blob: 0c3c7fd3f2d5eb9c7fec5c95d184ef27d3b8d21f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module Oxidized
  class HTTP < Source
    def initialize
      @cfg = Oxidized.config.source.http
      super
    end

    def setup
      if @cfg.url.empty?
        raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config'
      end
    end

    require "net/http"
    require "uri"
    require "json"

    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

    private

    def string_navigate object, wants
      wants.split(".").map do |want|
        head, match, _tail = want.partition(/\[\d+\]/)
        match.empty? ? head : [head, match[1..-2].to_i]
      end.flatten.each do |want|
        object = object[want] if object.respond_to? :each
      end
      object
    end

    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
      if node_want
        req_uri = "#{req_uri}/#{node_want}"
      end
      request = Net::HTTP::Get.new(req_uri, headers)
      if (@cfg.user? && @cfg.pass?)
        request.basic_auth(@cfg.user, @cfg.pass)
      end
      http.request(request).body
    end
  end
end