├── .gitignore ├── package.json ├── tests └── test.js ├── LICENSE ├── README.md └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | tests/conf.json 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Tim Atkinson (http://timisbusy.com)", 3 | "name": "amqp-stats", 4 | "description": "Interface for RabbitMQ Management statistics. http://www.rabbitmq.com/management.html", 5 | "version": "0.0.14", 6 | "repository": { 7 | "url": "https://github.com/timisbusy/node-amqp-stats" 8 | }, 9 | "main": "index.js", 10 | "engines": { 11 | "node": "~0.6.0" 12 | }, 13 | "license": "MIT", 14 | "dependencies": { 15 | "request": "~2.69.0", 16 | "qs": "1.2.1" 17 | }, 18 | "devDependencies": {} 19 | } 20 | -------------------------------------------------------------------------------- /tests/test.js: -------------------------------------------------------------------------------- 1 | var Stats = require('../index.js'); 2 | var conf = require('./conf.json'); 3 | 4 | var stats = new Stats(conf); // defaults to localhost, be sure you have mgmt plugin installed: http://www.rabbitmq.com/management.html 5 | 6 | stats.whoami(function (err, res, data) { 7 | if(err) { throw err; } 8 | console.log('data: ', data); 9 | }); 10 | 11 | stats.queues(function(err, res, data){ 12 | if (err) { throw err; } 13 | console.log('data: ', data); 14 | data.forEach(function (queue) { 15 | if(queue.message_stats){ 16 | console.log('name: ', queue.name); 17 | console.log('message_stats: ', queue.message_stats.deliver_get); 18 | } 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | (The MIT License) 3 | 4 | Copyright (c) 2016 Tim Atkinson 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of 7 | this software and associated documentation files (the "Software"), to deal in 8 | the Software without restriction, including without limitation the rights to 9 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 10 | the Software, and to permit persons to whom the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 18 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 19 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 20 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Node.js Interface for RabbitMQ Management Statistics 2 | 3 | ### This package creates an easy interface for getting statistics from a RabbitMQ instance with the management plugin installed. Read more about the management plugin here: 4 | 5 | - http://www.rabbitmq.com/management.html 6 | - http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v2_8_1/priv/www/api/index.html 7 | 8 | ## Required 9 | 10 | You will need: 11 | 12 | 1. An instance of RabbitMQ (running locally or in the cloud) 13 | 2. The Management Plugin 14 | 15 | ## Installation 16 | 17 | npm install amqp-stats 18 | 19 | ## Usage 20 | 21 | Require the amqp-stats package: 22 | 23 | var AMQPStats = require('amqp-stats'); 24 | 25 | Instantiate and provide authentication details (defaults to standard setup for a local instance). 26 | 27 | var stats = new AMQPStats({ 28 | username: "AMQP_USERNAME", // default: guest 29 | password: "AMQP_PASSWORD", // default: guest 30 | hostname: "AMQP_HOSTNAME", // default: localhost:55672 31 | protocol: "HTTP_OR_HTTPS" // default: http 32 | }); 33 | 34 | NOTE: for RabbitMQ instances running on heroku, the hostname should look something like: 35 | 36 | heroku.srs.rabbitmq.com/rabbitmq/sdaewywqh 37 | 38 | From there you can use the stats instance to get data about your system's usage: 39 | 40 | stats.overview(function(err, res, data){ 41 | if (err) { throw err; } 42 | console.log('data: ', data); 43 | }); 44 | 45 | This will return a list of your queues with lots of data about their rate of use, total messages sent, etc: 46 | 47 | stats.queues(callback); 48 | 49 | Note that when you lack admin privileges (on heroku instances for example) you may not be able to get at certain parts of this API. You can check your status with: 50 | 51 | stats.whoami(callback); 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var request = require('request'), 2 | qs = require('qs'); 3 | 4 | function AmqpStats (_options) { 5 | var options = _options || {}; 6 | this.hostname = options.hostname || 'localhost:55672'; 7 | this.username = options.username || 'guest'; 8 | this.password = options.password || 'guest'; 9 | this.protocol = options.protocol || 'http'; 10 | }; 11 | 12 | // Overview 13 | 14 | AmqpStats.prototype.overview = function getOverview (callback) { 15 | this.sendRequest('GET', 'overview/', {}, callback); 16 | }; 17 | 18 | // Nodes 19 | 20 | AmqpStats.prototype.nodes = function getNodes (callback) { 21 | this.sendRequest('GET', 'nodes/', {}, callback); 22 | }; 23 | 24 | AmqpStats.prototype.getNode = function getNode (name, callback) { 25 | this.sendRequest('GET', 'nodes/' + encodeURIComponent(name) + '/', {}, callback); 26 | }; 27 | 28 | // Extensions 29 | 30 | AmqpStats.prototype.extensions = function getExtensions (callback) { 31 | this.sendRequest('GET', 'extensions/', {}, callback); 32 | }; 33 | 34 | AmqpStats.prototype.definitions = function getDefinitions (callback) { 35 | this.sendRequest('GET', 'definitions/', {}, callback); 36 | }; 37 | 38 | // Connections 39 | 40 | AmqpStats.prototype.connections = function getConnections (callback) { 41 | this.sendRequest('GET', 'connections/', {}, callback); 42 | }; 43 | 44 | AmqpStats.prototype.getConnection = function getConnection (name, callback) { 45 | this.sendRequest('GET', 'connections/' + encodeURIComponent(name) + '/', {}, callback); 46 | }; 47 | 48 | // Channels 49 | 50 | AmqpStats.prototype.channels = function getChannels (options, callback) { 51 | if(typeof options === 'function'){ 52 | callback = options; 53 | options = {}; 54 | } 55 | this.sendRequest('GET', 'channels/', options, callback); 56 | }; 57 | 58 | AmqpStats.prototype.getChannel = function getChannel (name, callback) { 59 | this.sendRequest('GET', 'channels/' + encodeURIComponent(name) + '/', {}, callback); 60 | }; 61 | 62 | // Exchanges 63 | 64 | AmqpStats.prototype.exchanges = function getExchanges (options, callback) { 65 | if(typeof options === 'function'){ 66 | callback = options; 67 | options = {}; 68 | } 69 | this.sendRequest('GET', 'exchanges/', options, callback); 70 | }; 71 | 72 | AmqpStats.prototype.getExchangesForVHost = function getExchangesForVHost (vhost, callback) { 73 | this.sendRequest('GET', 'exchanges/' + encodeURIComponent(vhost) + '/', {}, callback); 74 | }; 75 | 76 | AmqpStats.prototype.getExchange = function getExchange (vhost, name, callback) { 77 | this.sendRequest('GET', 'exchanges/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(name) + '/', {}, callback); 78 | }; 79 | 80 | AmqpStats.prototype.getBindingsWithSource = function getBindingsWithSource (vhost, name, callback) { 81 | this.sendRequest('GET', 'exchanges/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(name) + '/bindings/source/', {}, callback); 82 | }; 83 | 84 | AmqpStats.prototype.getBindingsWithDestination = function getBindingsWithDestination (vhost, name, callback) { 85 | this.sendRequest('GET', 'exchanges/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(name) + '/bindings/destination/', {}, callback); 86 | }; 87 | 88 | // Queues 89 | 90 | AmqpStats.prototype.queues = function getQueues (options, callback) { 91 | if(typeof options === 'function'){ 92 | callback = options; 93 | options = {}; 94 | } 95 | this.sendRequest('GET', 'queues/', options, callback); 96 | }; 97 | 98 | AmqpStats.prototype.getQueuesForVHost = function getQueuesForVHost (vhost, callback) { 99 | this.sendRequest('GET', 'queues/' + encodeURIComponent(vhost) + '/', {}, callback); 100 | }; 101 | 102 | AmqpStats.prototype.getQueue = function getQueue (vhost, name, callback) { 103 | this.sendRequest('GET', 'queues/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(name) + '/', {}, callback); 104 | }; 105 | 106 | AmqpStats.prototype.getBindingsForQueue = function getBindingsForQueue (vhost, name, callback) { 107 | this.sendRequest('GET', 'queues/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(name) + '/bindings/', {}, callback); 108 | }; 109 | 110 | // Bindings 111 | 112 | AmqpStats.prototype.bindings = function getBindings (options, callback) { 113 | if(typeof options === 'function'){ 114 | callback = options; 115 | options = {}; 116 | } 117 | this.sendRequest('GET', 'bindings/', options, callback); 118 | }; 119 | 120 | AmqpStats.prototype.getBindingsForVHost = function getBindingsForVHost (vhost, callback) { 121 | this.sendRequest('GET', 'bindings/' + encodeURIComponent(vhost) + '/', {}, callback); 122 | }; 123 | 124 | AmqpStats.prototype.getBindingsForExchangeAndQueue = function getBindingsForExchangeAndQueue (vhost, exchange, queue, callback) { 125 | this.sendRequest('GET', 'queues/' + encodeURIComponent(vhost) + '/e/' + encodeURIComponent(exchange) + '/q/' + encodeURIComponent(queue) + '/', {}, callback); 126 | }; 127 | 128 | // Virtual Hosts 129 | 130 | AmqpStats.prototype.vhosts = function getVHosts (callback) { 131 | this.sendRequest('GET', 'vhosts/', {}, callback); 132 | }; 133 | 134 | AmqpStats.prototype.getVHost = function getVHost (name, callback) { 135 | this.sendRequest('GET', 'vhosts/' + encodeURIComponent(name) + '/', {}, callback); 136 | }; 137 | 138 | AmqpStats.prototype.getVHostPermissions = function getVHostPermissions (name, callback) { 139 | this.sendRequest('GET', 'vhosts/' + encodeURIComponent(name) + '/permissions/', {}, callback); 140 | }; 141 | 142 | // Users 143 | 144 | AmqpStats.prototype.users = function getUsers (callback) { 145 | this.sendRequest('GET', 'users/', {}, callback); 146 | }; 147 | 148 | AmqpStats.prototype.getUser = function getUser (name, callback) { 149 | this.sendRequest('GET', 'users/' + encodeURIComponent(name) + '/', {}, callback); 150 | }; 151 | 152 | AmqpStats.prototype.getUserPermissions = function getVHostPermissions (name, callback) { 153 | this.sendRequest('GET', 'users/' + encodeURIComponent(name) + '/permissions/', {}, callback); 154 | }; 155 | 156 | // Who Am I? 157 | 158 | AmqpStats.prototype.whoami = function whoami (callback) { 159 | this.sendRequest('GET', 'whoami/', {}, callback); 160 | }; 161 | 162 | // Permissions 163 | 164 | AmqpStats.prototype.permissions = function getPermissions (callback) { 165 | this.sendRequest('GET', 'permissions/', {}, callback); 166 | }; 167 | 168 | AmqpStats.prototype.getPermissionsForUserOnVHost = function getPermissionsForUserOnVHost (vhost, name, callback) { 169 | this.sendRequest('GET', '/permissions/' + encodeURIComponent(vhost) + '/' + encodeURIComponent(name) + '/', {}, callback); 170 | }; 171 | 172 | // Aliveness 173 | 174 | AmqpStats.prototype.alive = function alivenessTest (vhost, callback) { 175 | this.sendRequest('GET', 'aliveness-test/' + encodeURIComponent(vhost) + '/', {}, callback); 176 | }; 177 | 178 | // Utility used by all other calls. Can also be used seperately to make any API call not specified above. 179 | 180 | AmqpStats.prototype.sendRequest = function sendRequest (method, path, params, callback) { 181 | request({ 182 | method: method, 183 | url: this.protocol + "://" + this.username + ":" + this.password + "@" + this.hostname + "/api/" + path + qs.stringify(params), 184 | body: qs.stringify(params), 185 | form: true 186 | }, function(err, res, data){ 187 | //console.log(err); 188 | //console.log(res.statusCode); 189 | //console.log('data: ', data); 190 | if (err) { 191 | callback(err); 192 | } else if (res.statusCode > 200) { 193 | callback(new Error("Status code: "+ res.statusCode)); 194 | } else if (data === "Not found.") { 195 | callback(new Error("Undefined.")); 196 | } else { 197 | data = JSON.parse(data); 198 | callback(null, res, data); 199 | } 200 | }); 201 | } 202 | 203 | module.exports = AmqpStats; 204 | --------------------------------------------------------------------------------