From d233e9cbb86967a72fa0691b257e6ff23be07890 Mon Sep 17 00:00:00 2001 From: Jonathan Lee Date: Tue, 8 Feb 2011 15:26:46 -0500 Subject: [PATCH] Fix bad 'this' reference in updateFromEventEmitter Add an example of using jmxstat.jar --- examples/graphjmx.ex.js | 47 +++++++++++++++++++++++++++++++++++++++++ lib/reporting/report.js | 13 ++++++------ nodeload.js | 3 ++- 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 examples/graphjmx.ex.js diff --git a/examples/graphjmx.ex.js b/examples/graphjmx.ex.js new file mode 100644 index 0000000..437e04e --- /dev/null +++ b/examples/graphjmx.ex.js @@ -0,0 +1,47 @@ +/*jslint forin:true */ + +var assert = require('assert'), + child_process = require('child_process'), + reporting = require('../lib/reporting'), + REPORT_MANAGER = reporting.REPORT_MANAGER; + +REPORT_MANAGER.setLogFile('.reporting.test-output.html'); + +var hostAndPort = 'localhost:9999', + refreshInterval = 2; + +var report = REPORT_MANAGER.addReport('JMX'), + memory = report.getChart('Memory'), + cpu = report.getChart('CPU'); + +var jmx = reporting.spawnAndMonitor( + /HeapMemoryUsage.max=(.*) HeapMemoryUsage.committed=(.*) HeapMemoryUsage.used=(.*) SystemLoadAverage=([0-9\.]*)/, + ['max', 'committed', 'used', 'loadavg'], + 'java', [ + '-jar', 'jmxstat/jmxstat.jar', + hostAndPort, + 'java.lang:type=Memory[HeapMemoryUsage.max,HeapMemoryUsage.committed,HeapMemoryUsage.used]', + 'java.lang:type=OperatingSystem[SystemLoadAverage]', + refreshInterval + ] + ), + iostat = reporting.spawnAndMonitor( + / +[^ ]+ +[^ ]+ +[^ ]+ +([^ ]+) +([^ ]+) +[^ ]+ +[^ ]+ +[^ ]+ +[^ ]+/, + ['user', 'system'], + 'iostat', [refreshInterval] + ); + +jmx.stderr.on('data', function (data) { + console.log(data.toString()); +}); + +jmx.on('exit', function (code) { + if (code !== 0) { console.log('JMX monitor died with code ' + code); } + process.exit(code); +}); + +jmx.on('data', function(data) { + memory.put({max: data.max/1024, committed: data.committed/1024, used: data.used/1024}); +}); + +cpu.updateFromEventEmitter(iostat, ['user', 'system']); diff --git a/lib/reporting/report.js b/lib/reporting/report.js index 3585a2c..660255f 100644 --- a/lib/reporting/report.js +++ b/lib/reporting/report.js @@ -113,13 +113,14 @@ Chart.prototype = { */ updateFromEventEmitter: function(eventEmitter, fields, event) { - eventEmitter.on(event || 'data', function(data) { - var row = {}; - fields.forEach(function(i) { - if (data[i] !== undefined) { row[i] = data[i]; } + var self = this; + eventEmitter.on(event || 'data', function(data) { + var row = {}; + fields.forEach(function(i) { + if (data[i] !== undefined) { row[i] = data[i]; } + }); + self.put(row); }); - this.put(row); - }); } }; diff --git a/nodeload.js b/nodeload.js index 20691a1..74a2df0 100755 --- a/nodeload.js +++ b/nodeload.js @@ -99,7 +99,8 @@ if(callback){callback(data?fn(data):fn);} else{return data?fn(data):fn;}}};exports.create=template.create.bind(template);var BUILD_AS_SINGLE_FILE;if(!BUILD_AS_SINGLE_FILE){var util=require('../util');var querystring=require('querystring');var LogFile=require('../stats').LogFile;var template=require('./template');var config=require('../config');var REPORT_SUMMARY_TEMPLATE=require('./summary.tpl.js').REPORT_SUMMARY_TEMPLATE;var NODELOAD_CONFIG=config.NODELOAD_CONFIG;var START=NODELOAD_CONFIG.START;var DYGRAPH_SOURCE=require('./dygraph.tpl.js').DYGRAPH_SOURCE;} var Chart,timeFromStart;var Report=exports.Report=function(name){this.name=name;this.uid=util.uid();this.summary={};this.charts={};};Report.prototype={getChart:function(name){if(!this.charts[name]){this.charts[name]=new Chart(name);} return this.charts[name];},updateFromMonitor:function(monitor){monitor.on('update',this.doUpdateFromMonitor_.bind(this,monitor,''));return this;},updateFromMonitorGroup:function(monitorGroup){var self=this;monitorGroup.on('update',function(){util.forEach(monitorGroup.monitors,function(monitorname,monitor){self.doUpdateFromMonitor_(monitor,monitorname);});});return self;},doUpdateFromMonitor_:function(monitor,monitorname){var self=this;monitorname=monitorname?monitorname+' ':'';util.forEach(monitor.stats,function(statname,stat){util.forEach(stat.summary(),function(name,val){self.summary[self.name+' '+monitorname+statname+' '+name]=val;});if(monitor.interval[statname]){self.getChart(monitorname+statname).put(monitor.interval[statname].summary());}});}};var Chart=exports.Chart=function(name){this.name=name;this.uid=util.uid();this.columns=["time"];this.rows=[[timeFromStart()]];};Chart.prototype={put:function(data){var self=this,row=[timeFromStart()];util.forEach(data,function(column,val){var col=self.columns.indexOf(column);if(col<0){col=self.columns.length;self.columns.push(column);self.rows[0].push(0);} -row[col]=val;});self.rows.push(row);},updateFromEventEmitter:function(eventEmitter,fields,event){eventEmitter.on(event||'data',function(data){var row={};fields.forEach(function(i){if(data[i]!==undefined){row[i]=data[i];}});this.put(row);});}};var ReportGroup=exports.ReportGroup=function(){this.reports=[];this.logNameOrObject='results-'+START.getTime()+'.html';};ReportGroup.prototype={addReport:function(report){report=(typeof report==='string')?new Report(report):report;this.reports.push(report);return report;},setLogFile:function(logNameOrObject){this.logNameOrObject=logNameOrObject;},setLoggingEnabled:function(enabled){clearTimeout(this.loggingTimeoutId);if(enabled){this.logger=this.logger||(typeof this.logNameOrObject==='string')?new LogFile(this.logNameOrObject):this.logNameOrObject;this.loggingTimeoutId=setTimeout(this.writeToLog_.bind(this),this.refreshIntervalMs);}else if(this.logger){this.logger.close();this.logger=null;} +row[col]=val;});self.rows.push(row);},updateFromEventEmitter:function(eventEmitter,fields,event){var self=this;eventEmitter.on(event||'data',function(data){var row={};fields.forEach(function(i){if(data[i]!==undefined){row[i]=data[i];}});console.log(row) +self.put(row);});}};var ReportGroup=exports.ReportGroup=function(){this.reports=[];this.logNameOrObject='results-'+START.getTime()+'.html';};ReportGroup.prototype={addReport:function(report){report=(typeof report==='string')?new Report(report):report;this.reports.push(report);return report;},setLogFile:function(logNameOrObject){this.logNameOrObject=logNameOrObject;},setLoggingEnabled:function(enabled){clearTimeout(this.loggingTimeoutId);if(enabled){this.logger=this.logger||(typeof this.logNameOrObject==='string')?new LogFile(this.logNameOrObject):this.logNameOrObject;this.loggingTimeoutId=setTimeout(this.writeToLog_.bind(this),this.refreshIntervalMs);}else if(this.logger){this.logger.close();this.logger=null;} return this;},reset:function(){this.reports={};},getHtml:function(){var self=this,t=template.create(REPORT_SUMMARY_TEMPLATE);return t({DYGRAPH_SOURCE:DYGRAPH_SOURCE,querystring:querystring,refreshPeriodMs:self.refreshIntervalMs,reports:self.reports});},writeToLog_:function(){this.loggingTimeoutId=setTimeout(this.writeToLog_.bind(this),this.refreshIntervalMs);this.logger.clear(this.getHtml());}};function timeFromStart(){return(Math.floor((new Date().getTime()-START)/600)/100);} var BUILD_AS_SINGLE_FILE;if(!BUILD_AS_SINGLE_FILE){var ReportGroup=require('./report').ReportGroup;var config=require('../config');var NODELOAD_CONFIG=config.NODELOAD_CONFIG;var HTTP_SERVER=require('../http').HTTP_SERVER;} var REPORT_MANAGER=exports.REPORT_MANAGER=new ReportGroup();NODELOAD_CONFIG.on('apply',function(){REPORT_MANAGER.refreshIntervalMs=REPORT_MANAGER.refreshIntervalMs||NODELOAD_CONFIG.AJAX_REFRESH_INTERVAL_MS;REPORT_MANAGER.setLoggingEnabled(NODELOAD_CONFIG.LOGS_ENABLED);});HTTP_SERVER.addRoute('^/$',function(url,req,res){var html=REPORT_MANAGER.getHtml();res.writeHead(200,{"Content-Type":"text/html","Content-Length":html.length});res.write(html);res.end();});HTTP_SERVER.addRoute('^/reports$',function(url,req,res){var json=JSON.stringify(REPORT_MANAGER.reports);res.writeHead(200,{"Content-Type":"application/json","Content-Length":json.length});res.write(json);res.end();});var BUILD_AS_SINGLE_FILE;if(!BUILD_AS_SINGLE_FILE){var child_process=require('child_process');}