Class: Hostmon::Models::Host

Inherits:
Base
  • Object
show all
Defined in:
lib/hostmon/models/host.rb

Constant Summary collapse

OK =
"OK"
FAIL =
"FAIL"
EXEC_FAIL =
"EXEC_FAIL"
@@clusters =
Set.new
@@saved_reports =
1
@@checks =
Set.new
@@check_map =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], all, each, find

Constructor Details

#initialize(h, c) ⇒ Host

Returns a new instance of Host.



108
109
110
111
112
113
114
# File 'lib/hostmon/models/host.rb', line 108

def initialize(h, c)
  # needed to disambiguate
  super(h+'.'+c)
  @hostname = h
  @cluster = c
  @report_time = nil
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



58
59
60
# File 'lib/hostmon/models/host.rb', line 58

def checks
  @checks
end

#clusterObject (readonly)

Returns the value of attribute cluster.



58
59
60
# File 'lib/hostmon/models/host.rb', line 58

def cluster
  @cluster
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



58
59
60
# File 'lib/hostmon/models/host.rb', line 58

def hostname
  @hostname
end

#reportObject (readonly)

Returns the value of attribute report.



58
59
60
# File 'lib/hostmon/models/host.rb', line 58

def report
  @report
end

#report_timeObject

Returns the value of attribute report_time.



59
60
61
# File 'lib/hostmon/models/host.rb', line 59

def report_time
  @report_time
end

#reportsObject

Returns the value of attribute reports.



59
60
61
# File 'lib/hostmon/models/host.rb', line 59

def reports
  @reports
end

#status_mapObject (readonly)

Returns the value of attribute status_map.



58
59
60
# File 'lib/hostmon/models/host.rb', line 58

def status_map
  @status_map
end

Class Method Details

.absorb(report) ⇒ Object

process and update (or create) a host and return it fixme saved_reports unbounded fixme special case for 0 (only store in memory)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/hostmon/models/host.rb', line 164

def self.absorb(report)
  h = report.host
  c = report.cluster
  unless host = find_by_name_and_cluster(h, c)
    host = new(h, c)
    puts "new host #{h}"
  end
  host.reports ||= Hash.new {|hsh,k| hsh[k] = Array.new}

  # fixme slightly offensive
  if host.reports[report.name].size < @@saved_reports
    host.reports[report.name] << report
    host.reports[report.name].sort! {|a,b| a.timestamp <=> b.timestamp }
    report.write
  elsif report.timestamp < host.reports[report.name].first.timestamp
    return host
  else
    pp host.reports[report.name].size
    pp @@saved_reports
    host.reports[report.name] << report
    host.reports[report.name].sort! {|a,b| a.timestamp <=> b.timestamp }
    while host.reports[report.name].size > @@saved_reports
      shift = host.reports[report.name].shift
      shift.delete
    end
    report.write
  end
  ####################

  time = report.timestamp
  if (!host.report_time) || (host.report_time < time)
    host.report_time = time
  end
  @@clusters << host.cluster
  host
end

.check_mapObject



37
38
39
# File 'lib/hostmon/models/host.rb', line 37

def check_map
  @@check_map
end

.clustersObject

attr_*



26
27
28
# File 'lib/hostmon/models/host.rb', line 26

def clusters # attr_*
  @@clusters
end

.find_by_cluster(cluster) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/hostmon/models/host.rb', line 49

def find_by_cluster(cluster)
  ret = []
  Host.each do |name, host|
    ret << host if host.cluster == cluster
  end
  return ret
end

.find_by_name_and_cluster(name, cluster) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/hostmon/models/host.rb', line 41

def find_by_name_and_cluster(name, cluster)
  Host.each do |host_name, host|
    next unless host.name == name
    return host if host.cluster == cluster
  end
  return nil
end

.hosts_failing(check) ⇒ Object



29
30
31
# File 'lib/hostmon/models/host.rb', line 29

def hosts_failing(check)
  @@check_map[check][FAIL]||Set.new
end

.hosts_succeeding(check) ⇒ Object



33
34
35
# File 'lib/hostmon/models/host.rb', line 33

def hosts_succeeding(check)
  @@check_map[check][OK]||Set.new
end

.saved_reportsObject



15
16
17
# File 'lib/hostmon/models/host.rb', line 15

def self.saved_reports
  @@saved_reports
end

.saved_reports=(i) ⇒ Object



18
19
20
# File 'lib/hostmon/models/host.rb', line 18

def self.saved_reports= (i)
  @@saved_reports = i
end

.statusesObject



8
9
10
# File 'lib/hostmon/models/host.rb', line 8

def self.statuses
  [OK, FAIL, EXEC_FAIL]
end

Instance Method Details

#<=>(other) ⇒ Object

fixme @params is always empty right now



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hostmon/models/host.rb', line 134

def <=>(other)
  if @params[:host_sort] == "builtin"
    return key <=> other.key
  elsif @params[:host_sort] == "numeric"
    regexp = /\d+/
    match = @name.match(regexp)
    match2 = other.name.match(regexp)
    if match.pre_match != match2.pre_match
      return match.pre_match <=> match2.pre_match
    else
      return match[0].to_i <=> match2[0].to_i
    end
  else
    # http://www.bofh.org.uk/2007/12/16/comprehensible-sorting-in-ruby
    sensible = lambda do |k|
      k.sname.split(
             /((?:(?:^|\s)[-+])?(?:\.\d+|\d+(?:\.\d+?(?:[eE]\d+)?(?:$|(?![eE\.])))?))/ms
             ).map { |v| Float(v) rescue v.downcase }
    end
    return sensible.call(self) <=> sensible.call(other)
  end
end

#==(other) ⇒ Object



124
125
126
# File 'lib/hostmon/models/host.rb', line 124

def ==(other)
  key == other.key
end

#check_reports(check_name) ⇒ Object



201
202
203
# File 'lib/hostmon/models/host.rb', line 201

def check_reports(check_name)
  reports[check_name]
end

#checks_failingObject

return reports for checks that failed to execute properly



89
90
91
92
93
94
95
96
97
# File 'lib/hostmon/models/host.rb', line 89

def checks_failing
  failing = Set.new
  @reports.each do |n, r|
    if r.last.check_failed?
      failing << r.last
    end
  end
  failing.sort
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/hostmon/models/host.rb', line 120

def eql?(other)
  key == other.key
end

#failingObject

return reports for checks that the host fails



78
79
80
81
82
83
84
85
86
# File 'lib/hostmon/models/host.rb', line 78

def failing
  failing_reports = Set.new
  @reports.each do |n, r|
    if r.last.failed?
      failing_reports << r.last
    end
  end
  failing_reports.sort
end

#hashObject



157
158
159
# File 'lib/hostmon/models/host.rb', line 157

def hash
  key.hash
end

#inspectObject

temporary



206
207
208
# File 'lib/hostmon/models/host.rb', line 206

def inspect
  @hostname
end

#keyObject



116
117
118
# File 'lib/hostmon/models/host.rb', line 116

def key
  "#{@cluster}#{@name}"
end

#nameObject



104
105
106
# File 'lib/hostmon/models/host.rb', line 104

def name
  @hostname
end

#snameObject

fixme this is just silly



129
130
131
# File 'lib/hostmon/models/host.rb', line 129

def sname
  @hostname + "." + @cluster
end

#succeedingObject

return reports for checks that the host passes



67
68
69
70
71
72
73
74
75
# File 'lib/hostmon/models/host.rb', line 67

def succeeding
  ok_reports = Set.new
  @reports.each do |n, r|
    if r.last.ok?
      ok_reports << r.last
    end
  end
  ok_reports.sort
end

#to_sObject

hacks



100
101
102
# File 'lib/hostmon/models/host.rb', line 100

def to_s
  @hostname
end