Class: Naginata::Status

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/naginata/status.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostnameObject Also known as: nagios

Returns the value of attribute hostname.



10
11
12
# File 'lib/naginata/status.rb', line 10

def hostname
  @hostname
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/naginata/status.rb', line 10

def status
  @status
end

Class Method Details

.build(string, nagios_server_hostname) ⇒ Naginata::Status

Create a new instance from raw status.dat string

Parameters:

  • string (String)

    raw status.dat text

  • nagios_server_hostname (String)

    hostname to identify status.data data

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/naginata/status.rb', line 52

def self.build(string, nagios_server_hostname)
  instance = new
  instance.hostname = nagios_server_hostname

  # @Note NagiosAnalizer::Status.new requires filename, so here
  # needs to write text data into tempfile.
  Tempfile.open(["naginata-#{nagios_server_hostname}", ".status.dat"]) do |temp|
    temp.sync = true
    temp.write string
    instance.status = ::NagiosAnalyzer::Status.new(temp.path, include_ok: true)
    temp.close!
  end
  instance
end

.cache_dirString

Return a directory path for status.dat caching

Returns:

  • (String)

    directory path for status.dat caching



70
71
72
73
74
75
# File 'lib/naginata/status.rb', line 70

def self.cache_dir
  dir = (ENV['HOME'] && Dir.exist?(ENV['HOME'])) ? ENV['HOME'] : Dir.pwd
  dir = File.join(dir, '.naginata/cache/status')
  FileUtils.mkdir_p dir
  return dir
end

.find(nagios_server_hostname) ⇒ Naginata::Status

Find a instance from cache by nagios server hostname

Returns:



80
81
82
83
84
85
86
87
# File 'lib/naginata/status.rb', line 80

def self.find(nagios_server_hostname)
  instance = new
  instance.hostname = nagios_server_hostname
  if File.exist?(instance.path)
    File.open(instance.path) { |f| instance.status = Marshal.load(f) }
    return instance
  end
end

Instance Method Details

#decorateStatusDecorator

Returns decorator instance

Returns:



43
44
45
# File 'lib/naginata/status.rb', line 43

def decorate
  @decorator ||= StatusDecorator.new(self)
end

#pathString

Path of marshal dumped status file

Returns:

  • (String)

    path of marshal dumped status file



35
36
37
38
# File 'lib/naginata/status.rb', line 35

def path
  raise "@hostname must be set" if hostname.nil?
  @path ||= File.join(self.class.cache_dir, "#{hostname}.status.dat")
end

#purge_cacheObject



26
27
28
29
30
# File 'lib/naginata/status.rb', line 26

def purge_cache
  if File.exist?(path)
    FileUtils.rm path
  end
end

#saveNaginata::Status, false

Write status.dat into local disk

Returns:



17
18
19
20
21
22
23
24
# File 'lib/naginata/status.rb', line 17

def save
  return false if status.nil?
  File.open(path, "w") do |f|
    f.sync = true
    Marshal.dump(status, f)
  end
  self
end