Sha256: 1d33ac72a9f86053e7762cb942dbabd574ca629b9aa20d8e6afb23438c5adc45

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Corona
  class DB
    require 'sequel'
    require 'sqlite3'
    def initialize
      @db = Sequel.sqlite(CFG.db, :max_connections => 1, :pool_timeout => 60)
      create_table #unless @db.table_exists?(:devices)
      require_relative 'model'
    end

    def add record
      record[:first_seen] = record[:last_seen] = Time.now.utc
      record[:active] = true
      Log.debug "adding: #{record}"
      Device.new(record).save
    end

    def update record, where
      record[:last_seen] = Time.now.utc
      record[:active] = true
      Log.debug "updating (where: #{where}): #{record}"
      Device[where.first.to_sym => where.last].update record
    end

    def old ip, oid_sysName
      ip       = Device[:ip => ip]
      sysName  = Device[:oid_sysName => oid_sysName]
      [ip, sysName]
    end

    private
    
    def create_table
      @db.create_table? :devices do
        primary_key :id
        String      :ip
        String      :ptr
        String      :model
        String      :oid_ifDescr
        Boolean     :active
        Time        :first_seen
        Time        :last_seen
        String      :oid_sysName
        String      :oid_sysLocation
        String      :oid_sysDescr
        String      :oid_sysObjectID
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
corona-0.2.0 lib/corona/db/db.rb