Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mysql = require('mysql');
- var sys = require('sys');
- var MyMonit = function () {
- this.initialize.apply(this, arguments);
- }
- MyMonit.options = {
- host: 'localhost',
- password: '',
- maxtime: 90
- }
- MyMonit.prototype = {
- options: {},
- intervalPid: null,
- connection: null,
- listeners: [],
- initialize: function (options) {
- this.options = options || MyMonit.options;
- var $this = this;
- this.connection = mysql.createClient({
- host: this.options.host || 'localhost',
- port: this.options.port || 3306,
- user: 'root',
- password: this.options.password || '',
- });
- this.addListener(function (action, queries) {
- sys.puts("Se ha hecho " + action + " sobre las siguientes queries:");
- for (var i = 0; i < queries.length; i++) {
- sys.puts (queries[i].User + "@" + queries[i].Host + " DB: "+ queries[i].db + " Time: " + queries[i].Time);
- sys.puts (queries[i].Info);
- }
- });
- setInterval(function () {
- $this.check.apply($this,[]);
- }, 1000);
- },
- log: function (action, queries) {
- for (var i =0; i < this.listeners.length; i++) {
- (function (callback,action,queries) {
- setTimeout(function() {
- callback(action, queries);
- }, 0);
- })(this.listeners[i],action,queries);
- }
- },
- addListener: function (cb) {
- this.listeners.push(cb);
- },
- check: function () {
- var $this = this;
- this.connection.query("show full processlist", function (err, res) {
- if (err) {
- sys.put ("[ERROR] On list processlist: " + err.meesage);
- return;
- }
- var forkill = [];
- for (var i = 0; i < res.length; i++) {
- if (res[i].Time > $this.options.maxtime) {
- forkill.push(res[i]);
- }
- }
- if (forkill.length) $this.killall(forkill);
- });
- },
- kill: function (id) {
- this.connection.query("kill ?",[id]);
- },
- killall: function (idlist) {
- for (var i = 0; i < idlist.length; i++) {
- this.kill(idlist[i].Id);
- }
- this.log('kill',idlist);
- }
- }
- module.exports = MyMonit;
Add Comment
Please, Sign In to add comment