mysql-backend.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.handleCounters = function(_counters, time_stamp) {
  49. var queries = [];
  50. var value = 0;
  51. for(var counter in _counters) {
  52. value = counters[counter];
  53. if(value === 0) {
  54. continue;
  55. }
  56. else {
  57. queries.push("insert into statistics values(" + time_stamp + ",'" + counter +"'," + value + ") on duplicate key value = value + " + value + ", timestamp = " + time_stamp);
  58. }
  59. }
  60. }
  61. StatdMySQLBackend.prototype.executeQuery = function(sqlQuerries) {
  62. // Let's create a connection to the DB server
  63. var connection = _mysql.createConnection(this.config);
  64. connection.connect(function(err){
  65. if(err){
  66. console.log("There was an error while trying to connect to DB, please check");
  67. }
  68. else {
  69. for(var sql in sqlQuerries){
  70. connection.query(sql, function(err, rows) {
  71. if(!err) {
  72. console.log("Query succesfully executed");
  73. }
  74. else {
  75. //TODO : add better error handling code
  76. console.log("Error while executing sql query : " + sql);
  77. }
  78. });
  79. }
  80. }
  81. }
  82. });
  83. connection.end(function(err) {
  84. if(err){
  85. console.log("There was an error while trying to close DB connection");
  86. //Let's make sure that socket is destroyed
  87. connection.destroy();
  88. }
  89. });
  90. }
  91. /**
  92. *
  93. * @param error
  94. * @param backend_name
  95. * @param stat_name
  96. * @param stat_value
  97. */
  98. StatdMySQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
  99. }
  100. exports.init = function(startupTime, config, events) {
  101. var instance = new StatdMySQLBackend(startupTime, config, events);
  102. return true;
  103. };
  104. /*
  105. * Backend example : repeater.js
  106. *
  107. var util = require('util'),
  108. dgram = require('dgram');
  109. function RepeaterBackend(startupTime, config, emitter){
  110. var self = this;
  111. this.config = config.repeater || [];
  112. this.sock = dgram.createSocket('udp6');
  113. // attach
  114. emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
  115. };
  116. RepeaterBackend.prototype.process = function(packet, rinfo) {
  117. var self = this;
  118. hosts = self.config;
  119. for(var i=0; i<hosts.length; i++) {
  120. self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
  121. function(err,bytes) {
  122. if (err) {
  123. console.log(err);
  124. }
  125. });
  126. }
  127. };
  128. exports.init = function(startupTime, config, events) {
  129. var instance = new RepeaterBackend(startupTime, config, events);
  130. return true;
  131. };
  132. */