Last active
January 28, 2016 18:54
-
-
Save cbankston/725ae6d09b7707df35f2 to your computer and use it in GitHub Desktop.
node mysql processlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('uncaughtException', function (err) { | |
console.error(err.stack); | |
process.exit(1); | |
}) | |
var Promise = require("bluebird"); | |
var knex = require('knex')({ | |
client: 'mysql', | |
connection: { | |
host : 'example.org', | |
user : 'some_user', | |
password : 'some_pass', | |
database : 'some_db', | |
charset : 'utf8' | |
} | |
}); | |
function log_processlist() { | |
knex.raw('SHOW FULL PROCESSLIST').spread(function(processlist) { | |
return processlist; | |
}).map(function(item) { | |
return { | |
// id: item.Id, | |
time: item.Time, | |
state: item.State, | |
command: item.Command, | |
db: item.db, | |
info: item.Info, | |
}; | |
}).filter(function(item) { | |
return item.command == 'Query' && item.time > 1; | |
}).each(function(item) { | |
console.log( JSON.stringify(item) ); | |
}).then(function(items) { | |
if (items.length) { | |
console.log(''); | |
} | |
}); | |
} | |
setInterval(log_processlist, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment