mysql-backend.js 4.2 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` (`timestamp`,`name`,`value`) values(" + time_stamp + ",'" + counter +"'," + value + ") on duplicate key update 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. console.log("trying to execute : " + sqlQuerries[i]);
  73. connection.query(sqlQuerries[i], function(err, rows) {
  74. if(!err) {
  75. console.log("Query succesfully executed");
  76. }
  77. else {
  78. //TODO : add better error handling code
  79. console.log("Error while executing sql query");
  80. }
  81. connection.end(function(err) {
  82. if(err){
  83. console.log("There was an error while trying to close DB connection");
  84. //Let's make sure that socket is destroyed
  85. connection.destroy();
  86. }
  87. });
  88. });
  89. }
  90. }
  91. });
  92. }
  93. /**
  94. *
  95. * @param error
  96. * @param backend_name
  97. * @param stat_name
  98. * @param stat_value
  99. */
  100. StatdMySQLBackend.prototype.onStatus = function(error, backend_name, stat_name, stat_value) {
  101. }
  102. exports.init = function(startupTime, config, events) {
  103. var instance = new StatdMySQLBackend(startupTime, config, events);
  104. return true;
  105. };
  106. /*
  107. * Backend example : repeater.js
  108. *
  109. var util = require('util'),
  110. dgram = require('dgram');
  111. function RepeaterBackend(startupTime, config, emitter){
  112. var self = this;
  113. this.config = config.repeater || [];
  114. this.sock = dgram.createSocket('udp6');
  115. // attach
  116. emitter.on('packet', function(packet, rinfo) { self.process(packet, rinfo); });
  117. };
  118. RepeaterBackend.prototype.process = function(packet, rinfo) {
  119. var self = this;
  120. hosts = self.config;
  121. for(var i=0; i<hosts.length; i++) {
  122. self.sock.send(packet,0,packet.length,hosts[i].port,hosts[i].host,
  123. function(err,bytes) {
  124. if (err) {
  125. console.log(err);
  126. }
  127. });
  128. }
  129. };
  130. exports.init = function(startupTime, config, events) {
  131. var instance = new RepeaterBackend(startupTime, config, events);
  132. return true;
  133. };
  134. */