Class: Naginata::Status
- Inherits:
-
Object
- Object
- Naginata::Status
- Extended by:
- Forwardable
- Defined in:
- lib/naginata/status.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
(also: #nagios)
Returns the value of attribute hostname.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
-
.build(string, nagios_server_hostname) ⇒ Naginata::Status
Create a new instance from raw status.dat string.
-
.cache_dir ⇒ String
Return a directory path for status.dat caching.
-
.find(nagios_server_hostname) ⇒ Naginata::Status
Find a instance from cache by nagios server hostname.
Instance Method Summary collapse
-
#decorate ⇒ StatusDecorator
Returns decorator instance.
-
#path ⇒ String
Path of marshal dumped status file.
- #purge_cache ⇒ Object
-
#save ⇒ Naginata::Status, false
Write status.dat into local disk.
Instance Attribute Details
#hostname ⇒ Object Also known as: nagios
Returns the value of attribute hostname.
10 11 12 |
# File 'lib/naginata/status.rb', line 10 def hostname @hostname end |
#status ⇒ Object
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
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_dir ⇒ String
Return a 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
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
#decorate ⇒ StatusDecorator
Returns decorator instance
43 44 45 |
# File 'lib/naginata/status.rb', line 43 def decorate @decorator ||= StatusDecorator.new(self) end |
#path ⇒ 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_cache ⇒ Object
26 27 28 29 30 |
# File 'lib/naginata/status.rb', line 26 def purge_cache if File.exist?(path) FileUtils.rm path end end |
#save ⇒ Naginata::Status, false
Write status.dat into local disk
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 |