Class: Oxidized::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/oxidized/node.rb,
lib/oxidized/node/stats.rb

Defined Under Namespace

Classes: Stats

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ Node

Returns a new instance of Node.



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
# File 'lib/oxidized/node.rb', line 13

def initialize(opt)
  Oxidized.logger.debug 'resolving DNS for %s...' % opt[:name]
  # remove the prefix if an IP Address is provided with one as IPAddr converts it to a network address.
  ip_addr, = opt[:ip].to_s.split("/")
  Oxidized.logger.debug 'IPADDR %s' % ip_addr.to_s
  @name = opt[:name]
  @ip = IPAddr.new(ip_addr).to_s rescue nil
  @ip ||= Resolv.new.getaddress(@name) if Oxidized.config.resolve_dns?
  @ip ||= @name
  @group = opt[:group]
  @model = resolve_model opt
  @input = resolve_input opt
  @output = resolve_output opt
  @auth = resolve_auth opt
  @prompt = resolve_prompt opt
  @vars = opt[:vars]
  @stats = Stats.new
  @retry = 0
  @repo = resolve_repo opt
  @err_type = nil
  @err_reason = nil

  # model instance needs to access node instance
  @model.node = self
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def auth
  @auth
end

#emailObject

Returns the value of attribute email.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def email
  @email
end

#err_reasonObject

Returns the value of attribute err_reason.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def err_reason
  @err_reason
end

#err_typeObject

Returns the value of attribute err_type.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def err_type
  @err_type
end

#fromObject

Returns the value of attribute from.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def from
  @from
end

#groupObject (readonly)

Returns the value of attribute group.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def group
  @group
end

#inputObject (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def input
  @input
end

#ipObject (readonly)

Returns the value of attribute ip.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def ip
  @ip
end

#lastObject

Returns the value of attribute last.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def last
  @last
end

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def model
  @model
end

#msgObject

Returns the value of attribute msg.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def msg
  @msg
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def output
  @output
end

#promptObject (readonly)

Returns the value of attribute prompt.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def prompt
  @prompt
end

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def repo
  @repo
end

#retryObject

Returns the value of attribute retry.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def retry
  @retry
end

#runningObject Also known as: running?

Returns the value of attribute running.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def running
  @running
end

#statsObject

Returns the value of attribute stats.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def stats
  @stats
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/oxidized/node.rb', line 10

def user
  @user
end

#varsObject (readonly)

Returns the value of attribute vars.



9
10
11
# File 'lib/oxidized/node.rb', line 9

def vars
  @vars
end

Instance Method Details

#modifiedObject



141
142
143
# File 'lib/oxidized/node.rb', line 141

def modified
  @stats.update_mtime
end

#resetObject



136
137
138
139
# File 'lib/oxidized/node.rb', line 136

def reset
  @user = @email = @msg = @from = nil
  @retry = 0
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/oxidized/node.rb', line 39

def run
  status, config = :fail, nil
  @input.each do |input|
    # don't try input if model is missing config block, we may need strong config to class_name map
    cfg_name = input.to_s.split('::').last.downcase
    next unless @model.cfg[cfg_name] && (not @model.cfg[cfg_name].empty?)

    @model.input = input = input.new
    if (config = run_input(input))
      Oxidized.logger.debug "lib/oxidized/node.rb: #{input.class.name} ran for #{name} successfully"
      status = :success
      break
    else
      Oxidized.logger.debug "lib/oxidized/node.rb: #{input.class.name} failed for #{name}"
      status = :no_connection
    end
  end
  @model.input = nil
  [status, config]
end

#run_input(input) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/oxidized/node.rb', line 60

def run_input(input)
  rescue_fail = {}
  [input.class::RESCUE_FAIL, input.class.superclass::RESCUE_FAIL].each do |hash|
    hash.each do |level, errors|
      errors.each do |err|
        rescue_fail[err] = level
      end
    end
  end
  begin
    input.connect(self) && input.get
  rescue *rescue_fail.keys => err
    resc = ''
    unless (level = rescue_fail[err.class])
      resc  = err.class.ancestors.find { |e| rescue_fail.has_key?(e) }
      level = rescue_fail[resc]
      resc  = " (rescued #{resc})"
    end
    Oxidized.logger.send(level, '%s raised %s%s with msg "%s"' % [ip, err.class, resc, err.message])
    @err_type = err.class.to_s
    @err_reason = err.message.to_s
    false
  rescue StandardError => e
    crashdir  = Oxidized.config.crash.directory
    crashfile = Oxidized.config.crash.hostnames? ? name : ip.to_s
    FileUtils.mkdir_p(crashdir) unless File.directory?(crashdir)

    File.open File.join(crashdir, crashfile), 'w' do |fh|
      fh.puts Time.now.utc
      fh.puts e.message + ' [' + e.class.to_s + ']'
      fh.puts '-' * 50
      fh.puts e.backtrace
    end
    Oxidized.logger.error '%s raised %s with msg "%s", %s saved' % [ip, e.class, e.message, crashfile]
    @err_type = e.class.to_s
    @err_reason = e.message.to_s
    false
  end
end

#serializeObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/oxidized/node.rb', line 100

def serialize
  h = {
    name:      @name,
    full_name: @name,
    ip:        @ip,
    group:     @group,
    model:     @model.class.to_s,
    last:      nil,
    vars:      @vars,
    mtime:     @stats.mtime
  }
  h[:full_name] = [@group, @name].join('/') if @group
  if @last
    h[:last] = {
      start:  @last.start,
      end:    @last.end,
      status: @last.status,
      time:   @last.time
    }
  end
  h
end