mysql-backend.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  11. * Backend Constructor
  12. *
  13. * @param startupTime
  14. * @param config
  15. * @param emmiter
  16. */
  17. function StatdMySQLBackend(startupTime, config, emitter) {
  18. var self = this;
  19. this.config = config.mysql || {};
  20. // Verifying that the config file contains enough information for this backend to work
  21. if(!this.config.host || !this.config.database || !this.config.user) {
  22. console.log("You need to specify at least host, port, database and user for this mysql backend");
  23. process.exit(-1);
  24. }
  25. // Default port for mysql is 3306, if unset in conf file, we set it here to default
  26. if(!this.config.port) {
  27. this.config.port = 3306;
  28. }
  29. // Attach events
  30. emitter.on('flush', self.onFlush );
  31. emitter.on('status', self.onStatus );
  32. }
  33. /**
  34. *
  35. * @param time_stamp
  36. * @param metrics
  37. */
  38. StatdMySQLBackend.prototype.onFlush = function(time_stamp, metrics) {
  39. console.log("onFlush event Recieved");
  40. var connection = _mysql.createConnection(this.config);
  41. connection.query('SELECT 1', function(err, rows) {
  42. if(!err) {
  43. console.log("DB connected");
  44. }
  45. else {
  46. console.log("there was an error while trying to connect to DB, please check");
  47. }
  48. // connected! (unless `err` is set)
  49. });
  50. }
  51. /**
  52. *
  53. * @param error
  54. * @param backend_name
  55. * @param stat_name
  56. * @param stat_value
  57. */
  58. StatdMySQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
  59. console.log("onStatus event Recieved");
  60. }
  61. exports.init = function(startupTime, config, events) {
  62. var instance = new StatdMySQLBackend(startupTime, config, events);
  63. return true;
  64. };
  65. /*
  66. * Backend example : repeater.js
  67. *
  68. var util = require('util'),
  69. dgram = require('dgram');
  70. function RepeaterBackend(startupTime, config, emitter){
  71. var self = this;
  72. this.config = config.repeater || [];
  73. this.sock = dgram.createSocket('udp6');
  74. // attach
  75. emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
  76. };
  77. RepeaterBackend.prototype.process = function(packet, rinfo) {
  78. var self = this;
  79. hosts = self.config;
  80. for(var i=0; i<hosts.length; i++) {
  81. self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
  82. function(err,bytes) {
  83. if (err) {
  84. console.log(err);
  85. }
  86. });
  87. }
  88. };
  89. exports.init = function(startupTime, config, events) {
  90. var instance = new RepeaterBackend(startupTime, config, events);
  91. return true;
  92. };
  93. */