Class: ZabbixRubyClient::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix-ruby-client/store.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir, server, task, keepdata = false) ⇒ Store

Returns a new instance of Store.



4
5
6
7
8
9
10
# File 'lib/zabbix-ruby-client/store.rb', line 4

def initialize(dir, server, task, keepdata=false)
  @dir = dir
  @server = server
  @task = task
  @keepdata = keepdata
  @basename = "#{@server}-#{@task}-data"
end

Instance Method Details

#datafileObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/zabbix-ruby-client/store.rb', line 12

def datafile
  @datafile ||= if @keepdata
    unless Dir.exists? File.join(@dir, Time.now.strftime("%Y-%m-%d"))
      FileUtils.mkdir File.join(@dir, Time.now.strftime("%Y-%m-%d"))
    end
    File.join(@dir, Time.now.strftime("%Y-%m-%d"),"#{@basename}_"+Time.now.strftime("%H%M%S"))
  else
    File.join(@dir, @basename)
  end
end

#keepdata(file) ⇒ Object



27
28
29
# File 'lib/zabbix-ruby-client/store.rb', line 27

def keepdata(file)
  FileUtils.mv(file, pendingfile)
end

#pending_contentObject



35
36
37
38
39
40
41
42
# File 'lib/zabbix-ruby-client/store.rb', line 35

def pending_content
  pending = ""
  if File.exists? pendingfile
    pending = File.open(pendingfile,'r').read
    File.delete(pendingfile)
  end
  pending
end

#pendingfileObject



31
32
33
# File 'lib/zabbix-ruby-client/store.rb', line 31

def pendingfile
  @pendingfile ||= File.join(@dir, "#{@server}-pending")
end

#record(data) ⇒ Object



23
24
25
# File 'lib/zabbix-ruby-client/store.rb', line 23

def record(data)
  write(data, datafile, pending_content)
end

#write(data, file, prepend = "") ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/zabbix-ruby-client/store.rb', line 44

def write(data, file, prepend = "")
  File.open(file, "w") do |f|
    f.write(prepend)
    data.each do |d|
      f.puts d
    end
  end
  file
end