Advertisement
Guest User

Example MySQL proxy script for logging to Splunk

a guest
Sep 19th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. local tokenizer = require("proxy.tokenizer")
  2. local auto_config = require("proxy.auto-config")
  3. local commands = require("proxy.commands")
  4.  
  5. math.randomseed(os.time())
  6.  
  7. -- Config
  8. if not proxy.global.config.splunk then
  9.     proxy.global.config.splunk = {
  10.         -- This percent of queries will be Splunk'd, default to 100% (all queries)
  11.         samplerate = 100,
  12.        
  13.         -- This many seconds of delay on the TCP socket  will result in Splunk
  14.         -- being regarded as failed, and all logging to Splunk will be abandoned
  15.         -- until a manual correction is issued
  16.         timeout = 1.0,
  17.  
  18.         -- Will auto switch to 0 if Splunk goes away for timeout seconds
  19.         -- Can be manually switched with: "PROXY SET GLOBAL splunk.splunk_active = [0|1]"
  20.         active = 1,
  21.  
  22.         -- Splunk host as a string
  23.         host = "127.0.0.1",
  24.  
  25.         -- Splunk TCP port as a number
  26.         port = 9332
  27.     }
  28. end
  29.  
  30. function read_query(packet)
  31.     local cmd = commands.parse(packet)
  32.  
  33.     local r = auto_config.handle(cmd)
  34.     if r then return r end
  35.    
  36.     if proxy.global.config.splunk.active == 0 or proxy.global.config.splunk.samplerate < 1 then
  37.         return
  38.     elseif cmd.type == proxy.COM_QUERY and
  39.         (proxy.global.config.splunk.samplerate >= 100 or math.random(0,100) < proxy.global.config.splunk.samplerate) then
  40.         query = packet:sub(2)
  41.         proxy.queries:append(1, packet)
  42.         return proxy.PROXY_SEND_QUERY
  43.     end
  44. end
  45.  
  46. function read_query_result(inj)
  47.     local cmd = commands.parse(inj.query)
  48.  
  49.     if cmd.type == proxy.COM_QUERY then
  50.         local tokens = tokenizer.tokenize(inj.query)
  51.         query = tokenizer.normalize(tokens)
  52.         print("[" .. os.date("%Y-%m-%d %X") .. "] " .. "query=\"" .. trim(query) .. "\",query_time=" .. inj.query_time .. ",response_time=" .. inj.response_time)
  53.     end
  54. end
  55.  
  56. -- Remove 3 leading and 2 trailing chars,
  57. -- the extra space that the tokenizer seems to insert when normalizing
  58. function trim(s)
  59.     return s:sub(3, -2)
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement