MySQL Proxy tutorial - Measuring bandwidth by user

Developed In: Lua — Contributed by: Giuseppe Maxia

This script shows how to measure client traffic by user.

MySQL Proxy is a simple program that sits between your client and MySQL server that can monitor, analyze or transform their communication. See http://forge.mysql.com/wiki/MySQL_Proxy for more information.


Giuseppe Maxia
Lua
  1. ---
  2. -- user_bandwidth.lua
  3. --
  4. -- measures bandwidth by user
  5. --
  6. proxy.global.bandwidth = proxy.global.bandwidth or {}
  7.  
  8. local session_user
  9.  
  10. function read_auth( auth )
  11. session_user = auth.username
  12. proxy.global.bandwidth[session_user] =
  13. proxy.global.bandwidth[session_user] or 0
  14. end
  15.  
  16. function read_query (packet )
  17. -- just to show how we can block a user query
  18. -- when the quota has been exceeded
  19. if proxy.global.bandwidth[session_user] > 10000
  20. and session_user ~= 'root'
  21. then
  22. return error_result('you have exceeded your query quota')
  23. end
  24. proxy.global.bandwidth[session_user ] =
  25. proxy.global.bandwidth[session_user] + packet:len()
  26. proxy.queries:append(1, packet )
  27. return proxy.PROXY_SEND_QUERY
  28. end
  29.  
  30. function read_query_result(inj)
  31. local fields = inj.resultset.fields
  32. local rows = inj.resultset.rows
  33. if fields then
  34. for i = 1, #fields do
  35. proxy.global.bandwidth[session_user] =
  36. proxy.global.bandwidth[session_user] +
  37. (fields[i] and fields[i].name:len() or 0)
  38. end
  39. if rows then
  40. for row in rows do
  41. for i = 1, #fields do
  42. proxy.global.bandwidth[session_user] =
  43. proxy.global.bandwidth[session_user] +
  44. (row[i] and row[i]:len() or 0)
  45. end
  46. end
  47. end
  48. end
  49. print (session_user .. ' -> ' ..
  50. proxy.global.bandwidth[session_user])
  51. end
  52.  
  53. function error_result (msg)
  54. proxy.response = {
  55. type = proxy.MYSQLD_PACKET_ERR,
  56. errmsg = msg,
  57. errcode = 7777,
  58. sqlstate = 'X7777',
  59. }
  60. return proxy.PROXY_SEND_RESULT
  61. end
  62.  

Current Tags

You must be logged in to tag this tool

Congresses thrive in these situations. With experience in over 30 countries and more than 500 events, marcus evans Congresses understand how to bring high level business decision makers together in order to foster lasting and successful business relationships in a variety of industries. Accredited High School Diploma AND GED test AND Get GED Online

nah kalo sekarang sudah diapprove mas…mungkin yang kmrn2 terlalu banyak yang daftar jadi overload…hehehehe… online homeschooling AND Online Diploma

I tried this and ran into a couple issues my self wondering i there was any more informational on the web on it thanks 6S

Votes

  • Rated 5.00 out of 5
Rated 5.00 out of 5 with 2 votes cast.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment