Guest User

Untitled

a guest
Mar 3rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mysql = require('mysql');
  2.  
  3. var sys = require('sys');
  4.  
  5. var MyMonit = function () {
  6.         this.initialize.apply(this, arguments);
  7. }
  8.  
  9. MyMonit.options = {
  10.         host: 'localhost',
  11.         password: '',
  12.         maxtime: 90
  13. }
  14.  
  15.  
  16. MyMonit.prototype = {
  17.  
  18.         options: {},
  19.  
  20.         intervalPid: null,
  21.  
  22.         connection: null,
  23.  
  24.         listeners: [],
  25.  
  26.         initialize: function (options) {
  27.  
  28.                 this.options = options || MyMonit.options;
  29.  
  30.                 var $this = this;
  31.  
  32.                 this.connection = mysql.createClient({
  33.                         host: this.options.host || 'localhost',
  34.                         port: this.options.port || 3306,
  35.                         user: 'root',
  36.                         password: this.options.password || '',
  37.                 });
  38.  
  39.                 this.addListener(function (action, queries) {
  40.  
  41.                         sys.puts("Se ha hecho " + action + " sobre las siguientes queries:");
  42.  
  43.                         for (var i = 0; i < queries.length; i++) {
  44.                                 sys.puts (queries[i].User + "@" + queries[i].Host + " DB: "+ queries[i].db  + " Time: " + queries[i].Time);
  45.                                 sys.puts (queries[i].Info);
  46.                         }
  47.  
  48.                 });
  49.  
  50.                 setInterval(function () {
  51.                         $this.check.apply($this,[]);
  52.                 }, 1000);
  53.  
  54.         },
  55.  
  56.  
  57.         log: function (action, queries) {
  58.  
  59.                 for (var i =0; i < this.listeners.length; i++) {
  60.                         (function (callback,action,queries) {
  61.                                 setTimeout(function() {
  62.                                         callback(action, queries);
  63.                                 }, 0);
  64.                         })(this.listeners[i],action,queries);
  65.                 }
  66.  
  67.         },
  68.  
  69.         addListener: function (cb) {
  70.                 this.listeners.push(cb);
  71.         },
  72.  
  73.         check: function () {
  74.  
  75.                 var $this = this;
  76.  
  77.                 this.connection.query("show full processlist", function (err, res) {
  78.                         if (err) {
  79.                                 sys.put ("[ERROR] On list processlist: " + err.meesage);
  80.                                 return;
  81.                         }
  82.  
  83.                         var forkill = [];
  84.  
  85.                         for (var i = 0; i < res.length; i++) {
  86.                                 if (res[i].Time > $this.options.maxtime) {
  87.                                         forkill.push(res[i]);
  88.                                 }
  89.                         }
  90.  
  91.                         if (forkill.length) $this.killall(forkill);
  92.  
  93.                 });
  94.  
  95.         },
  96.  
  97.         kill: function (id) {
  98.                 this.connection.query("kill ?",[id]);
  99.         },
  100.  
  101.         killall: function (idlist) {
  102.  
  103.                 for (var i = 0; i < idlist.length; i++) {
  104.                         this.kill(idlist[i].Id);
  105.                 }
  106.  
  107.                 this.log('kill',idlist);
  108.  
  109.         }
  110.  
  111. }
  112.  
  113. module.exports = MyMonit;
Add Comment
Please, Sign In to add comment