Class: Hostmon::Models::Report

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

Constant Summary collapse

@@report_base =
""
@@status_map =
{
  :CHECK_OK => 0b000000,
  :CHECK_FAILED => 0b000001,
  :FAILED_MISSING_PRELUDE => 0b000100,
  :FAILED_STDERR_NONEMPTY => 0b001000,
  :FAILED_BAD_EXIT_STATUS => 0b010000,
  :FAILED_SKIPPED => 0b100000
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #[], all, each, find, #to_s

Constructor Details

#initialize(hsh) ⇒ Report

Returns a new instance of Report.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hostmon/models/report.rb', line 22

def initialize (hsh)
  @name = hsh[:name]
  @status = hsh[:status]
  @timestamp = Time.parse(hsh[:timestamp])
  @exit_status = hsh[:exit_status]
  @stdout = hsh[:stdout]
  @stderr = hsh[:stderr]
  @host = hsh[:host]
  @cluster = hsh[:cluster]
  h = Host.absorb(self)
  Check.absorb(name, h)
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



20
21
22
# File 'lib/hostmon/models/report.rb', line 20

def cluster
  @cluster
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



19
20
21
# File 'lib/hostmon/models/report.rb', line 19

def exit_status
  @exit_status
end

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/hostmon/models/report.rb', line 20

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/hostmon/models/report.rb', line 19

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



19
20
21
# File 'lib/hostmon/models/report.rb', line 19

def status
  @status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



19
20
21
# File 'lib/hostmon/models/report.rb', line 19

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



19
20
21
# File 'lib/hostmon/models/report.rb', line 19

def stdout
  @stdout
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



19
20
21
# File 'lib/hostmon/models/report.rb', line 19

def timestamp
  @timestamp
end

Class Method Details

.report_base=(dir) ⇒ Object



7
8
9
# File 'lib/hostmon/models/report.rb', line 7

def self.report_base= (dir)
  @@report_base = dir
end

Instance Method Details

#bodyObject

fixme rewrite to stdout stderr etc



52
53
54
# File 'lib/hostmon/models/report.rb', line 52

def body
  CGI::escapeHTML(@stdout)
end

#check_failed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/hostmon/models/report.rb', line 47

def check_failed?
  int_status > 1
end

#deleteObject



98
99
100
101
102
103
104
105
# File 'lib/hostmon/models/report.rb', line 98

def delete
  if File.exist?(report_fname)
    puts "delete #{report_fname}"
    FileUtils.rm(report_fname)
  else
    puts "report #{report_fname} not on disk"
  end
end

#failed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/hostmon/models/report.rb', line 43

def failed?
  int_status == 1
end

#hostlint_idObject

this is actually a property of a host



57
58
59
# File 'lib/hostmon/models/report.rb', line 57

def hostlint_id
  Digest::SHA1.hexdigest("#{@name}#{@host}#{@cluster}")[0..7]
end

#int_statusObject



35
36
37
# File 'lib/hostmon/models/report.rb', line 35

def int_status
  @status.map {|x| @@status_map[x]}.inject(:|)
end

#ok?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/hostmon/models/report.rb', line 39

def ok?
  int_status == 0
end

#report_dirObject



75
76
77
# File 'lib/hostmon/models/report.rb', line 75

def report_dir
  File.join(@@report_base, @name.split("."), @cluster, @host)
end

#report_fnameObject



79
80
81
82
# File 'lib/hostmon/models/report.rb', line 79

def report_fname
  File.join(report_dir,
            "report_#{@timestamp.strftime("%F-%T")}.yaml")
end

#to_yamlObject

dammit



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hostmon/models/report.rb', line 62

def to_yaml
  {
    :name => @name,
    :status => @status,
    :timestamp => @timestamp.to_s,
    :exit_status => @exit_status,
    :stdout => @stdout,
    :stderr => @stderr,
    :host => @host,
    :cluster => @cluster
  }.to_yaml
end

#writeObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hostmon/models/report.rb', line 84

def write
  if File.exist?(report_fname)
    puts "#{report_fname} already exists"
  else
    unless File.directory?(report_dir)
      FileUtils.mkdir_p(report_dir)
    end
    puts "write #{report_fname}"
    File.open(report_fname, "w") do |f|
      f.puts(to_yaml)
    end
  end
end