Sha256: 7d9882f75e233768b517c15ccb313997bf3ac783cd7fa4ce5cf5fe04731b8cc8

Contents?: true

Size: 1.2 KB

Versions: 12

Compression:

Stored size: 1.2 KB

Contents

module ZabbixRubyClient
  class Store

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

    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

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

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

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

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

    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

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
zabbix-ruby-client-0.1.3 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.1.2 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.1.1 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.1.0 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.23 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.22 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.21 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.20 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.19 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.18 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.17 lib/zabbix-ruby-client/store.rb
zabbix-ruby-client-0.0.16 lib/zabbix-ruby-client/store.rb