summaryrefslogtreecommitdiff
path: root/lib/oxidized/input/http.rb
blob: 5ce1e2b1866c9599537f6b04604972320d4d0ecf (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
module Oxidized
  require_relative "cli"

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

  class HTTP < Input
    include Input::CLI

    def connect node
      @node = node
      @m    = Mechanize.new
      @log  = File.open(Oxidized::Config::Log + "/#{@node.ip}-http", "w") if Oxidized.config.input.debug?

      @node.model.cfg["http"].each { |cb| instance_exec(&cb) }

      url = URI::HTTP.build host: @node.ip, path: @main_page
      @m_page = @m.get(url.to_s)
      login
    end

    def cmd callback
      instance_exec(&callback)
    end

    private

    def log str
      @log.write(str) if @log
    end

    def disconnect
      @log.close if Oxidized.config.input.debug?
    end
  end
end