mysql-backend.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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', function(time_stamp, metrics) { self.onFlush(time_stamp, metrics); } );
  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 host : ");
  40. var connection = _mysql.createConnection(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. });
  49. }
  50. /**
  51. *
  52. * @param error
  53. * @param backend_name
  54. * @param stat_name
  55. * @param stat_value
  56. */
  57. StatdMySQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
  58. }
  59. exports.init = function(startupTime, config, events) {
  60. var instance = new StatdMySQLBackend(startupTime, config, events);
  61. return true;
  62. };
  63. /*
  64. * Backend example : repeater.js
  65. *
  66. var util = require('util'),
  67. dgram = require('dgram');
  68. function RepeaterBackend(startupTime, config, emitter){
  69. var self = this;
  70. this.config = config.repeater || [];
  71. this.sock = dgram.createSocket('udp6');
  72. // attach
  73. emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
  74. };
  75. RepeaterBackend.prototype.process = function(packet, rinfo) {
  76. var self = this;
  77. hosts = self.config;
  78. for(var i=0; i<hosts.length; i++) {
  79. self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
  80. function(err,bytes) {
  81. if (err) {
  82. console.log(err);
  83. }
  84. });
  85. }
  86. };
  87. exports.init = function(startupTime, config, events) {
  88. var instance = new RepeaterBackend(startupTime, config, events);
  89. return true;
  90. };
  91. */