mysql-backend.js 4.1 KB

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