mysql-backend.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. var self = this;
  41. var counters = metrics['counters'];
  42. var timers = metrics['timers'];
  43. var gauges = metrics['gauges'];
  44. var sets = metrics['sets'];
  45. var pctThreshold = metrics['pctThreshold'];
  46. self.executeQuery("toto");
  47. }
  48. StatdMySQLBackend.prototype.executeQuery = function(sql) {
  49. var connection = _mysql.createConnection(this.config);
  50. connection.query('SELECT 1', function(err, rows) {
  51. if(!err) {
  52. console.log("DB connected");
  53. }
  54. else {
  55. console.log("There was an error while trying to connect to DB, please check");
  56. }
  57. });
  58. connection.end(function(err) {
  59. if(err){
  60. console.log("There was an error while trying to close DB connection");
  61. }
  62. });
  63. }
  64. /**
  65. *
  66. * @param error
  67. * @param backend_name
  68. * @param stat_name
  69. * @param stat_value
  70. */
  71. StatdMySQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
  72. }
  73. exports.init = function(startupTime, config, events) {
  74. var instance = new StatdMySQLBackend(startupTime, config, events);
  75. return true;
  76. };
  77. /*
  78. * Backend example : repeater.js
  79. *
  80. var util = require('util'),
  81. dgram = require('dgram');
  82. function RepeaterBackend(startupTime, config, emitter){
  83. var self = this;
  84. this.config = config.repeater || [];
  85. this.sock = dgram.createSocket('udp6');
  86. // attach
  87. emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
  88. };
  89. RepeaterBackend.prototype.process = function(packet, rinfo) {
  90. var self = this;
  91. hosts = self.config;
  92. for(var i=0; i<hosts.length; i++) {
  93. self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
  94. function(err,bytes) {
  95. if (err) {
  96. console.log(err);
  97. }
  98. });
  99. }
  100. };
  101. exports.init = function(startupTime, config, events) {
  102. var instance = new RepeaterBackend(startupTime, config, events);
  103. return true;
  104. };
  105. */