3 var url = require('url');
\r
4 var http = require('http');
\r
5 var util = require('util');
\r
8 var netdata = require('netdata');
\r
10 var example_chart = {
\r
11 id: 'id', // the unique id of the chart
\r
12 name: 'name', // the name of the chart
\r
13 title: 'title', // the title of the chart
\r
14 units: 'units', // the units of the chart dimensions
\r
15 family: 'family', // the family of the chart
\r
16 category: 'category', // the category of the chart
\r
17 type: netdata.chartTypes.line, // the type of the chart
\r
18 priority: 0, // the priority relative to others in the same family and category
\r
19 update_every: 1, // the expected update frequency of the chart
\r
22 id: 'dim1', // the unique id of the dimension
\r
23 name: 'name', // the name of the dimension
\r
24 algorithm: netdata.chartAlgorithms.absolute, // the id of the netdata algorithm
\r
25 multiplier: 1, // the multiplier
\r
26 divisor: 1, // the divisor
\r
27 hidden: false, // is hidden (boolean)
\r
30 id: 'dim2', // the unique id of the dimension
\r
31 name: 'name', // the name of the dimension
\r
32 algorithm: 'absolute', // the id of the netdata algorithm
\r
33 multiplier: 1, // the multiplier
\r
34 divisor: 1, // the divisor
\r
35 hidden: false, // is hidden (boolean)
\r
37 // add as many dimensions as needed
\r
44 filename: __filename,
\r
50 incremental: 'incremental',
\r
51 absolute: 'absolute',
\r
52 percentage_of_absolute_row: 'percentage-of-absolute-row',
\r
53 percentage_of_incremental_row: 'percentage-of-incremental-row'
\r
62 services: new Array(),
\r
63 modules_configuring: 0,
\r
66 stringify: function(obj) {
\r
67 return util.inspect(obj, {depth: 10});
\r
70 // show debug info, if debug is enabled
\r
71 debug: function(msg) {
\r
72 if(this.options.DEBUG === true) {
\r
73 var now = new Date();
\r
74 console.error(now.toString() + ': ' + netdata.options.filename + ': DEBUG: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
\r
79 error: function(msg) {
\r
80 var now = new Date();
\r
81 console.error(now.toString() + ': ' + netdata.options.filename + ': ERROR: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
\r
84 // send data to netdata
\r
85 send: function(msg) {
\r
86 console.log(msg.toString());
\r
89 serviceAdd: function(service) {
\r
90 if(service.added !== true) {
\r
91 service.updates = 0;
\r
92 service.enabled = true;
\r
93 service.added = true;
\r
94 service.running = false;
\r
95 service.started = 0;
\r
97 service._current_chart = null; // the current chart we work on
\r
98 service._queue = '';
\r
99 service.queue = function(txt) {
\r
100 this._queue += txt + '\n';
\r
103 service._send_chart_to_netdata = function(chart) {
\r
104 // internal function to send a chart to netdata
\r
105 this.queue('CHART "' + chart.id + '" "' + chart.name + '" "' + chart.title + '" "' + chart.units + '" "' + chart.family + '" "' + chart.category + '" "' + chart.type + '" ' + chart.priority.toString() + ' ' + chart.update_every.toString());
\r
107 for(var dim in chart.dimensions) {
\r
108 var d = chart.dimensions[dim];
\r
110 this.queue('DIMENSION "' + d.id + '" "' + d.name + '" "' + d.algorithm + '" ' + d.multiplier.toString() + ' ' + d.divisor.toString() + ' ' + ((d.hidden === true)?'hidden':'').toString());
\r
112 d._updated = false;
\r
115 chart._created = true;
\r
116 chart._updated = false;
\r
119 // begin data collection for a chart
\r
120 service.begin = function(chart) {
\r
121 if(this._current_chart !== null && this._current_chart !== chart) {
\r
122 netdata.error('Called begin() for chart ' + chart.id + ' while chart ' + this._current_chart.id + ' is still open. Closing it.');
\r
126 if(typeof(chart.id) === 'undefined' || netdata.charts[chart.id] != chart) {
\r
127 netdata.error('Called begin() for chart ' + chart.id + ' that is not mine. Where did you find it? Ignoring it.');
\r
131 if(netdata.options.DEBUG === true) netdata.debug('setting current chart to ' + chart.id);
\r
132 this._current_chart = chart;
\r
133 this._current_chart._began = true;
\r
135 if(this._current_chart._dimensions_count !== 0) {
\r
136 if(this._current_chart._created === false || this._current_chart._updated === true)
\r
137 this._send_chart_to_netdata(this._current_chart);
\r
139 var now = this.ended;
\r
140 this.queue('BEGIN ' + this._current_chart.id + ' ' + ((this._current_chart._last_updated > 0)?((now - this._current_chart._last_updated) * 1000):'').toString());
\r
142 // else netdata.error('Called begin() for chart ' + chart.id + ' which is empty.');
\r
144 this._current_chart._last_updated = now;
\r
145 this._current_chart._began = true;
\r
146 this._current_chart._counter++;
\r
151 // set a collected value for a chart
\r
152 // we do most things on the first value we attempt to set
\r
153 service.set = function(dimension, value) {
\r
154 if(this._current_chart === null) {
\r
155 netdata.error('Called set(' + dimension + ', ' + value + ') without an open chart.');
\r
159 if(typeof(this._current_chart.dimensions[dimension]) === 'undefined') {
\r
160 netdata.error('Called set(' + dimension + ', ' + value + ') but dimension "' + dimension + '" does not exist in chart "' + this._current_chart.id + '".');
\r
164 if(this._current_chart._dimensions_count !== 0)
\r
165 this.queue('SET ' + dimension + ' = ' + value);
\r
170 // end data collection for the current chart - after calling begin()
\r
171 service.end = function() {
\r
172 if(this._current_chart !== null && this._current_chart._began === false) {
\r
173 netdata.error('Called end() without an open chart.');
\r
177 if(this._current_chart._dimensions_count !== 0) {
\r
179 netdata.send(this._queue);
\r
183 this._current_chart._began = false;
\r
184 if(netdata.options.DEBUG === true) netdata.debug('committed chart ' + this._current_chart.id);
\r
185 this._current_chart = null;
\r
189 // discard the collected values for the current chart - after calling begin()
\r
190 service.flush = function() {
\r
191 if(this._current_chart === null || this._current_chart._began === false) {
\r
192 netdata.error('Called flush() without an open chart.');
\r
197 this._current_chart._began = false;
\r
198 this._current_chart = null;
\r
202 // create a netdata chart
\r
203 service.chart = function(id, chart) {
\r
204 if(typeof(netdata.charts[id]) === 'undefined') {
\r
205 netdata.charts[id] = {
\r
211 _dimensions_count: 0,
\r
214 title: 'untitled chart',
\r
218 type: netdata.chartTypes.line,
\r
220 update_every: netdata.options.update_every,
\r
225 var c = netdata.charts[id];
\r
227 if(typeof(chart.name) !== 'undefined' && chart.name !== c.name) {
\r
228 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its name');
\r
229 c.name = chart.name;
\r
233 if(typeof(chart.title) !== 'undefined' && chart.title !== c.title) {
\r
234 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its title');
\r
235 c.title = chart.title;
\r
239 if(typeof(chart.units) !== 'undefined' && chart.units !== c.units) {
\r
240 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its units');
\r
241 c.units = chart.units;
\r
245 if(typeof(chart.family) !== 'undefined' && chart.family !== c.family) {
\r
246 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its family');
\r
247 c.family = chart.family;
\r
251 if(typeof(chart.category) !== 'undefined' && chart.category !== c.category) {
\r
252 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its category');
\r
253 c.category = chart.category;
\r
257 if(typeof(chart.type) !== 'undefined' && chart.type !== c.type) {
\r
258 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its type');
\r
259 c.type = chart.type;
\r
263 if(typeof(chart.priority) !== 'undefined' && chart.priority !== c.priority) {
\r
264 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its priority');
\r
265 c.priority = chart.priority;
\r
269 if(typeof(chart.update_every) !== 'undefined' && chart.update_every !== c.update_every) {
\r
270 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its update_every');
\r
271 c.update_every = chart.update_every;
\r
275 if(typeof(chart.dimensions) !== 'undefined') {
\r
276 for(var x in chart.dimensions) {
\r
277 if(typeof(c.dimensions[x]) === 'undefined') {
\r
278 c._dimensions_count++;
\r
280 c.dimensions[x] = {
\r
283 id: x, // the unique id of the dimension
\r
284 name: x, // the name of the dimension
\r
285 algorithm: netdata.chartAlgorithms.absolute, // the id of the netdata algorithm
\r
286 multiplier: 1, // the multiplier
\r
287 divisor: 1, // the divisor
\r
288 hidden: false, // is hidden (boolean)
\r
291 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' created dimension ' + x);
\r
295 var dim = chart.dimensions[x];
\r
296 var d = c.dimensions[x];
\r
298 if(typeof(dim.name) !== 'undefined' && d.name !== dim.name) {
\r
299 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its name');
\r
304 if(typeof(dim.algorithm) !== 'undefined' && d.algorithm !== dim.algorithm) {
\r
305 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its algorithm from ' + d.algorithm + ' to ' + dim.algorithm);
\r
306 d.algorithm = dim.algorithm;
\r
310 if(typeof(dim.multiplier) !== 'undefined' && d.multiplier !== dim.multiplier) {
\r
311 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its multiplier');
\r
312 d.multiplier = dim.multiplier;
\r
316 if(typeof(dim.divisor) !== 'undefined' && d.divisor !== dim.divisor) {
\r
317 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its divisor');
\r
318 d.divisor = dim.divisor;
\r
322 if(typeof(dim.hidden) !== 'undefined' && d.hidden !== dim.hidden) {
\r
323 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its hidden status');
\r
324 d.hidden = dim.hidden;
\r
328 if(d._updated) c._updated = true;
\r
332 if(netdata.options.DEBUG === true) netdata.debug(netdata.charts);
\r
333 return netdata.charts[id];
\r
336 this.services.push(service);
\r
337 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': service added.');
\r
341 serviceRun: function(service) {
\r
342 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': starting data collection...');
\r
343 service.running = true;
\r
344 service.started = new Date().getTime();
\r
347 service.module.update(service, function() {
\r
348 service.ended = new Date().getTime();
\r
349 service.duration = service.ended - service.started;
\r
350 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': data collection ended in ' + service.duration.toString() + ' ms.');
\r
351 service.running = false;
\r
355 runAllServices: function() {
\r
356 if(netdata.options.DEBUG === true) netdata.debug('runAllServices()');
\r
358 var now = new Date().getTime();
\r
359 var len = netdata.services.length;
\r
361 var service = netdata.services[len];
\r
363 if(service.enabled === false || service.running === true) continue;
\r
364 if(now - service.ended < service.update_every) continue;
\r
366 netdata.serviceRun(service);
\r
369 setTimeout(netdata.runAllServices, 100);
\r
372 start: function() {
\r
373 if(netdata.options.DEBUG === true) this.debug('started, services:');
\r
375 if(this.services.length === 0) {
\r
376 this.disableNodePlugin();
\r
379 else this.runAllServices();
\r
382 // disable the whole node.js plugin
\r
383 disableNodePlugin: function() {
\r
384 this.send('DISABLE');
\r
388 requestFromParams: function(protocol, hostname, port, path, method) {
\r
390 protocol: protocol,
\r
391 hostname: hostname,
\r
397 'Content-Type': 'application/x-www-form-urlencoded',
\r
398 'Connection': 'keep-alive'
\r
400 agent: new http.Agent({
\r
402 keepAliveMsecs: netdata.options.update_every,
\r
403 maxSockets: 2, // it must be 2 to work
\r
409 requestFromURL: function(a_url) {
\r
410 var u = url.parse(a_url);
\r
411 return netdata.requestFromParams(u.protocol, u.hostname, u.port, u.path, 'GET');
\r
414 processResponse: function(service, data, callback) {
\r
415 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': processing response...');
\r
417 callback(service, data);
\r
419 service.module.running--;
\r
420 if(service.module.running <= 0) {
\r
421 service.module.running = 0;
\r
423 // check if we run under configure
\r
424 if(service.module.configure_callback !== null) {
\r
425 if(netdata.options.DEBUG === true) this.debug(service.module.name + ': configuration finish callback called from processResponse().');
\r
426 var ccallback = service.module.configure_callback;
\r
427 service.module.configure_callback = null;
\r
433 getResponse: function(service, response, callback) {
\r
434 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': got response...');
\r
438 response.setEncoding('utf8');
\r
440 if(response.statusCode !== 200) {
\r
441 if(end === false) {
\r
442 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': got HTTP code ' + response.statusCode + ', failed to get data.');
\r
444 netdata.processResponse(service, null, callback);
\r
448 response.on('data', function(chunk) {
\r
449 if(end === false) data += chunk;
\r
452 response.on('error', function() {
\r
453 if(end === false) {
\r
454 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': Read error, failed to get data.');
\r
456 netdata.processResponse(service, null, callback);
\r
460 response.on('end', function() {
\r
461 if(end === false) {
\r
462 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': read completed.');
\r
464 netdata.processResponse(service, data, callback);
\r
469 serviceExecute: function(service, callback) {
\r
470 service.module.running++;
\r
472 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': making request: ' + netdata.stringify(service.request));
\r
473 var req = http.request(service.request, function(response) {
\r
474 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': request done.');
\r
475 netdata.getResponse(service, response, callback);
\r
478 req.on('error', function(e) {
\r
479 netdata.error(service.module.name + ': ' + service.name + ': failed to make request: ' + netdata.stringify(service.request) + ', message: ' + e.message);
\r
480 netdata.processResponse(service, null, callback);
\r
483 // write data to request body
\r
484 if(typeof service.postData !== 'undefined' && service.request.method === 'POST')
\r
485 req.write(service.postData);
\r
490 configure: function(module, config, callback) {
\r
491 if(netdata.options.DEBUG === true) this.debug(module.name + ': configuring (update_every: ' + this.options.update_every + ')...');
\r
493 module.running = 0;
\r
494 module.update_every = this.options.update_every;
\r
496 if(typeof config.update_every !== 'undefined')
\r
497 module.update_every = config.update_every * 1000;
\r
499 module.enable_autodetect = (config.enable_autodetect)?true:false;
\r
501 if(typeof(callback) === 'function')
\r
502 module.configure_callback = callback;
\r
504 module.configure_callback = null;
\r
506 var added = module.configure(config);
\r
508 if(netdata.options.DEBUG === true) this.debug(module.name + ': configured, reporting ' + added + ' eligible services.');
\r
510 if(module.configure_callback !== null && added === 0) {
\r
511 if(netdata.options.DEBUG === true) this.debug(module.name + ': configuration finish callback called from configure().');
\r
512 module.configure_callback = null;
\r
521 if(netdata.options.DEBUG === true) netdata.debug('loaded netdata from: ' + __filename);
\r
522 module.exports = netdata;
\r
525 var test1 = netdata.chart('test1', { name: 'test name', dimensions: { dim1: {}}});
\r
526 netdata.begin(test1);
\r
527 netdata.set('dim1', 1);
\r
529 netdata.begin(test1);
\r
530 netdata.set('dim1', 2);
\r
532 netdata.begin(test1);
\r
533 netdata.set('dim1', 3);
\r
535 netdata.begin(test1);
\r
536 netdata.set('dim1', 4);
\r