mysql-backend.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. // NodeJS Statsd MySQL Backend 1.0
  3. // ------------------------------------------------------------------------------
  4. //
  5. // Authors: Nicolas FRADIN, Damien PACAUD
  6. // Date: 31/10/2012
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////////
  9. var _mysql = require('mysql'),
  10. _mysql_config = { pool_size: 5 }
  11. _options = {}
  12. /**
  13. * Backend Constructor
  14. *
  15. * @param startupTime
  16. * @param config
  17. * @param emmiter
  18. */
  19. function StatdMySQLBackend(startupTime, config, emitter) {
  20. var self = this;
  21. this.config = config.mysql || {};
  22. // Verifying that the config file contains enough information for this backend to work
  23. if(this.config.host === "undefined" || this.config.port === "undefined" || this.config.database === "undefined" || this.config.user === "undefined") {
  24. console.log("You need to specify at least Host, Port and Database for the mysql backend");
  25. return;
  26. }
  27. // Attach events
  28. emitter.on('flush', self.onFlush );
  29. emitter.on('status', self.onStatus );
  30. }
  31. /**
  32. *
  33. * @param time_stamp
  34. * @param metrics
  35. */
  36. StatdMySQLBackend.prototype.onFlush = function(time_stamp, metrics) {
  37. console.log("onFlush event Recieved");
  38. }
  39. /**
  40. *
  41. * @param error
  42. * @param backend_name
  43. * @param stat_name
  44. * @param stat_value
  45. */
  46. StatdMySQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
  47. console.log("onStatus event Recieved");
  48. }
  49. exports.init = function(startupTime, config, events) {
  50. var instance = new StatdMySQLBackend(startupTime, config, events);
  51. return true;
  52. };
  53. /*
  54. * Backend example : repeater.js
  55. *
  56. var util = require('util'),
  57. dgram = require('dgram');
  58. function RepeaterBackend(startupTime, config, emitter){
  59. var self = this;
  60. this.config = config.repeater || [];
  61. this.sock = dgram.createSocket('udp6');
  62. // attach
  63. emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
  64. };
  65. RepeaterBackend.prototype.process = function(packet, rinfo) {
  66. var self = this;
  67. hosts = self.config;
  68. for(var i=0; i<hosts.length; i++) {
  69. self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
  70. function(err,bytes) {
  71. if (err) {
  72. console.log(err);
  73. }
  74. });
  75. }
  76. };
  77. exports.init = function(startupTime, config, events) {
  78. var instance = new RepeaterBackend(startupTime, config, events);
  79. return true;
  80. };
  81. */