Class: ARII::SQLDetector

Inherits:
Detector show all
Defined in:
lib/arii/sqldetector.rb

Overview

SQLDetector

Detec changes in SQL databases. MySQL support only.

Instance Attribute Summary

Attributes inherited from Detector

#agent, #content, #identifier, #objects, #payloads, #templates

Instance Method Summary collapse

Methods inherited from Detector

#checkup, #initialize

Constructor Details

This class inherits a constructor from ARII::Detector

Instance Method Details

#detect(object) ⇒ Object

Detect the changes



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
# File 'lib/arii/sqldetector.rb', line 19

def detect object
  ARII::Config.log.debug(self.class.name) { "Monitoring #{object[:host]}" }
  begin
    case object[:server]
    when 'mysql'
      @client = Mysql2::Client.new(:host => object[:host], :username => object[:username], :password => object[:password], :database => object[:database])
      @client.query(object[:query]).each(:symbolize_keys => false) do |row|
        unless object[:cache].nil? then
          @response = Cashier.verify row[object[:cache]], object, row, object[:seed]
        else
          @response = Cashier.verify row["id"], object, row, object[:seed]
        end

        # Process ARII cache response
        @cache = JSON.parse(@response, {:symbolize_names => true})
        unless @cache[:templates].nil? then
          @cache[:templates].each do |t|
            @templates.push t
          end
        end
        # The actual processing
        #
        if @cache[:cache][:status] == 100 then
          ARII::Config.log.info(self.class.name) { "Not on cache, generating payload" }
          # add row data to payload from selectors (key => key, value => column name)
          payload = Hash.new
          object[:selectors].each do |selector|
            selector.each do |k, v|
              payload[k] = row[v]
            end
          end
          # add payload object to payloads list
          @payloads.push payload
        end
      end
    when 'mssql'
      @client = TinyTds::Client.new username: object[:username], password: object[:password], host: object[:host], database: object[:database], port: object[:port], timeout: 60
      @results = @client.execute(@agent[:payload][:query])
      @results.each(:symbolize_keys => false) do |row|
        unless object[:cache].nil? then
          @response = Cashier.verify row[object[:cache]], object, row, object[:seed]
        else
          @response = Cashier.verify row["id"], object, row, object[:seed]
        end

        # Process ARII cache response
        @cache = JSON.parse(@response, {:symbolize_names => true})
        unless @cache[:templates].nil? then
          @cache[:templates].each do |t|
            @templates.push t
          end
        end
        # The actual processing
        #
        if @cache[:cache][:status] == 100 then
          ARII::Config.log.info(self.class.name) { "Not on cache, generating payload" }
          # add row data to payload from selectors (key => key, value => column name)
          payload = Hash.new
          object[:selectors].each do |selector|
            selector.each do |k, v|
              payload[k] = row[v]
            end
          end
          # add payload object to payloads list
          @payloads.push payload
        end
      end
    when 'postgresql'
      client = PG::Connection.new(:host => object[:host], :user => object[:username], :password => object[:password], :dbname => object[:database])
      client.exec(object[:query]).each do |row|
        unless object[:cache].nil? then
          @response = Cashier.verify row[object[:cache]], object, row, object[:seed]
        else
          @response = Cashier.verify row["id"], object, row, object[:seed]
        end

        # Process ARII cache response
        @cache = JSON.parse(@response, {:symbolize_names => true})
        unless @cache[:templates].nil? then
          @cache[:templates].each do |t|
            @templates.push t
          end
        end
        # The actual processing
        #
        if @cache[:cache][:status] == 100 then
          ARII::Config.log.info(self.class.name) { "Not on cache, generating payload" }
          # add row data to payload from selectors (key => key, value => column name)
          payload = Hash.new
          object[:selectors].each do |selector|
            selector.each do |k, v|
              payload[k] = row[v]
            end
          end
          # add payload object to payloads list
          @payloads.push payload
        end
      end
    end
  rescue Exception => e
    ARII::Config.log.error(self.class.name) { "Processing error: #{e}" }
  end
  @cache[:templates]
end