class Oxidized::Fetch

Attributes

model[R]

Public Class Methods

new(opts, &block) click to toggle source

@param [Hash] opts options for Oxidized::Fetch @option opts [String] :host hostname or ip address for Oxidized::Node @option opts [String] :model node model (ios, junos etc) if defined, nodes are not loaded from source @option opts [Fixnum] :timeout oxidized timeout @option opts [String] :username username for login @option opts [String] :passsword password for login @option opts [String] :enable enable password to use @option opts [String] :community community to use for discovery @option opts [String] :protocols protocols to use to connect, default “ssh ,telnet” @option opts [boolean] :verbose extra output, e.g. show command given in output @yieldreturn [self] if called in block, returns self and disconnnects session after exiting block @return [void]

# File lib/oxidized/fetch/fetch.rb, line 29
def initialize opts, &block
  group       = opts.delete :group
  host        = opts.delete :host
  model       = opts.delete :model
  timeout     = opts.delete :timeout
  username    = opts.delete :username
  password    = opts.delete :password
  enable      = opts.delete :enable
  community   = opts.delete :community
  @verbose    = opts.delete :verbose
  CFG.input.default = opts.delete :protocols if opts[:protocols]
  raise InvalidOption, "#{opts} not recognized" unless opts.empty?

  @@oxi ||= false
  if not @@oxi
    Oxidized.mgr = Manager.new
    @@oxi = true
  end

  @node = if model
    Node.new(:name=>host, :group=>group, :model=>model)
  else
    Nodes.new(:node=>host, :group=>group).first
  end
  if not @node
    begin
      require 'corona'
      community ||= Corona::CFG.community
    rescue LoadError
      raise NoNode, 'node not found'
    end
    node = Corona.poll :host=>host, :community=>community
    raise NoNode, 'node not found' unless node
    @node = Node.new :name=>host, :model=>node[:model]
  end
  @node.auth[:username] = username if username
  @node.auth[:password] = password if password
  CFG.vars.enable = enable if enable
  CFG.timeout = timeout if timeout

  status, config = @node.run
  if status == :success
    msg = "update #{@node.name}"
    msg += " from #{@node.from}" if @node.from
    msg += " with message '#{@node.msg}'" if @node.msg
    if @node.output.new.store @node.name, config, :msg => msg, :user => @node.user, :group => @node.group
      STDERR.write "Configuration updated for #{@node.group}/#{@node.name}\n" if @verbose
      exit 1
    end
    STDERR.write "Configuration NOT updated for #{@node.group}/#{@node.name}\n" if @verbose
    exit 0
  else
    STDERR.write "An unexpected error occurred\n" if @verbose
    exit -1
  end
end