Class: SphinxDataAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/sphinx.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SphinxDataAccessor

Returns a new instance of SphinxDataAccessor.



14
15
16
17
18
19
20
21
# File 'lib/logstash/filters/sphinx.rb', line 14

def initialize(config)

  @redis_user_conn = Redis.new(:host => config["redis_host"], :port => config["redis_port"], :db => config["redis_user_db"])
  @redis_record_conn = Redis.new(:host => config["redis_host"], :port => config["redis_port"], :db => config["redis_record_db"])
  @redis_host_conn = Redis.new(:host => config["redis_host"], :port => config["redis_port"], :db => config["redis_host_db"])
  @pg_conn = ConnectionPool::Wrapper.new(size: 8, timeout: 3) { PG::connect(:host => config["pg_host"], :user => config["pg_user"], :password => config["pg_password"], :dbname => config["pg_dbname"]) }

end

Instance Method Details

#add_host(user_id, hostname) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/logstash/filters/sphinx.rb', line 50

def add_host(user_id, hostname)

  # check redis cache first

  host = get_host_from_redis(user_id, hostname)
  return if host

  # create a host entry if not in the database

  begin
    host = create_host(user_id, hostname)
    insert_host_into_pg(host)

    set_host_in_redis(user_id, hostname, host)

  rescue => e
    puts e.message

  end



end

#get_record(md5) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/logstash/filters/sphinx.rb', line 74

def get_record(md5)


  # check the redis cache

  record = get_record_from_redis(md5)
  if record

    # Return it only if the record contains reputation meta data.

    # We learn this by checking the existence of the 'reputation_timestamp' key

    # which is only set by the backend after checking with VT (or other data source)

    if record["reputation_timestamp"]
      puts "#{md5}: Cache hit with data"
      return record
    else
      puts "#{md5}: Cache hit with no data"
      return nil
    end

  end


  # we couldn't find it in the cache. Check the db

  record = get_record_from_pg(md5)

  if record


    # Return it only if the record contains reputation meta data.

    # We learn this by checking the existence of the 'reputation_timestamp' key

    # which is only set by the backend after checking with VT (or other data source)

    if record["reputation_timestamp"]
      puts "#{md5}: DB hit with data"

      # cache it in redis

      set_record_in_redis(md5, record)
      return record

    else
      puts "#{md5}: DB hit with no data"

      empty_record = create_new_record(md5)
      set_record_in_redis(md5, empty_record)
      return nil
    end

  else

    puts "#{md5}: NO hit. Inserting a new record into DB and Cache"

    # Insert this md5 entry into the reference_hash table with a blank reputation timestamp.

    # This way the backend can update this entry accordingly

    record = create_new_record(md5)
    insert_record_into_pg(record)

    # Insert this into

    set_record_in_redis(md5, record)

    # NOTE: this new record is not returned to the user as it contains no reputation meta data.

    return nil

  end

  return nil

end

#get_user(access_id, access_key) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logstash/filters/sphinx.rb', line 24

def get_user(access_id, access_key)

  # sanity check for user inputs

  if access_id.nil? || access_key.nil?
    return nil
  end

  if access_id.strip == '' || access_key == ''
    return nil
  end

  # check redis first

  user = get_user_from_redis(access_id, access_key)
  return user if user

  user = get_user_from_pg(access_id, access_key)

  if user
    set_user_in_redis(access_id, access_key, user)
  end

  return user
end