mysql-backend.js 3.1 KB

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