mysql-backend.js 2.8 KB

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