3 var url = require('url');
4 var http = require('http');
5 var util = require('util');
8 var netdata = require('netdata');
11 id: 'id', // the unique id of the chart
12 name: 'name', // the name of the chart
13 title: 'title', // the title of the chart
14 units: 'units', // the units of the chart dimensions
15 family: 'family', // the family of the chart
16 context: 'context', // the context of the chart
17 type: netdata.chartTypes.line, // the type of the chart
18 priority: 0, // the priority relative to others in the same family
19 update_every: 1, // the expected update frequency of the chart
22 id: 'dim1', // the unique id of the dimension
23 name: 'name', // the name of the dimension
24 algorithm: netdata.chartAlgorithms.absolute, // the id of the netdata algorithm
25 multiplier: 1, // the multiplier
26 divisor: 1, // the divisor
27 hidden: false, // is hidden (boolean)
30 id: 'dim2', // the unique id of the dimension
31 name: 'name', // the name of the dimension
32 algorithm: 'absolute', // the id of the netdata algorithm
33 multiplier: 1, // the multiplier
34 divisor: 1, // the divisor
35 hidden: false, // is hidden (boolean)
37 // add as many dimensions as needed
50 incremental: 'incremental',
52 percentage_of_absolute_row: 'percentage-of-absolute-row',
53 percentage_of_incremental_row: 'percentage-of-incremental-row'
62 services: new Array(),
63 modules_configuring: 0,
71 process: function(service, callback) {
72 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': making ' + this.name + ' request: ' + netdata.stringify(service.request));
74 var req = http.request(service.request, function(response) {
75 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': got server response...');
79 response.setEncoding('utf8');
81 if(response.statusCode !== 200) {
83 service.error('Got HTTP code ' + response.statusCode + ', failed to get data.');
89 response.on('data', function(chunk) {
90 if(end === false) data += chunk;
93 response.on('error', function() {
95 service.error(': Read error, failed to get data.');
101 response.on('end', function() {
103 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': read completed.');
110 req.on('error', function(e) {
111 service.error('Failed to make request: ' + netdata.stringify(service.request) + ', message: ' + e.message);
115 // write data to request body
116 if(typeof service.postData !== 'undefined' && service.request.method === 'POST') {
117 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': posting data: ' + service.postData);
118 req.write(service.postData);
126 stringify: function(obj) {
127 return util.inspect(obj, {depth: 10});
130 // show debug info, if debug is enabled
131 debug: function(msg) {
132 if(this.options.DEBUG === true) {
133 var now = new Date();
134 console.error(now.toString() + ': ' + netdata.options.filename + ': DEBUG: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
139 error: function(msg) {
140 var now = new Date();
141 console.error(now.toString() + ': ' + netdata.options.filename + ': ERROR: ' + ((typeof(msg) === 'object')?netdata.stringify(msg):msg).toString());
144 // send data to netdata
145 send: function(msg) {
146 console.log(msg.toString());
149 service: function(service) {
150 if(typeof service === 'undefined')
153 var now = new Date().getTime();
155 service._current_chart = null; // the current chart we work on
156 service._queue = ''; // data to be sent to netdata
158 service.error_reported = false; // error log flood control
160 service.added = false; // added to netdata.services
161 service.enabled = true;
163 service.running = false;
167 if(typeof service.module === 'undefined') {
168 service.module = { name: 'not-defined-module' };
169 service.error('Attempted to create service without a module.');
170 service.enabled = false;
173 if(typeof service.name === 'undefined') {
174 service.name = 'unnamed@' + service.module.name + '/' + now;
177 if(typeof service.processor === 'undefined')
178 service.processor = netdata.processors.http;
180 if(typeof service.update_every === 'undefined')
181 service.update_every = service.module.update_every;
183 if(typeof service.update_every === 'undefined')
184 service.update_every = netdata.options.update_every;
186 if(service.update_every < netdata.options.update_every)
187 service.update_every = netdata.options.update_every;
190 service.next_run = now - (now % (service.update_every * 1000));
192 service.commit = function() {
193 if(this.added !== true) {
196 var now = new Date().getTime();
197 while( this.next_run < now )
198 this.next_run += (this.update_every * 1000);
200 netdata.services.push(this);
201 if(netdata.options.DEBUG === true) netdata.debug(this.module.name + ': ' + this.name + ': service committed.');
205 service.execute = function(callback) {
206 if(service.enabled === false) {
211 this.module.active++;
213 this.started = new Date().getTime();
216 if(netdata.options.DEBUG === true)
217 netdata.debug(this.module.name + ': ' + this.name + ': making ' + this.processor.name + ' request: ' + netdata.stringify(this));
219 this.processor.process(this, function(response) {
220 service.ended = new Date().getTime();
221 service.duration = service.ended - service.started;
223 if(typeof response === 'undefined')
226 if(response !== null)
227 service.errorClear();
229 if(netdata.options.DEBUG === true)
230 netdata.debug(service.module.name + ': ' + service.name + ': processing ' + service.processor.name + ' response (received in ' + (service.ended - service.started).toString() + ' ms)');
232 callback(service, response);
234 service.running = false;
235 service.module.active--;
236 if(service.module.active < 0) {
237 service.module.active = 0;
238 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': active module counter below zero.');
241 if(service.module.active === 0) {
242 // check if we run under configure
243 if(service.module.configure_callback !== null) {
244 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': configuration finish callback called from processResponse().');
245 var ccallback = service.module.configure_callback;
246 service.module.configure_callback = null;
253 service.update = function() {
254 if(netdata.options.DEBUG === true) netdata.debug(this.module.name + ': ' + this.name + ': starting data collection...');
256 this.module.update(this, function() {
257 if(netdata.options.DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': data collection ended in ' + service.duration.toString() + ' ms.');
261 service.error = function(message) {
262 if(this.error_reported === false) {
263 netdata.error(this.module.name + ': ' + this.name + ': ' + message);
264 this.error_reported = true;
266 else if(netdata.options.DEBUG === true)
267 netdata.debug(this.module.name + ': ' + this.name + ': ' + message);
270 service.errorClear = function() {
271 this.error_reported = false;
274 service.queue = function(txt) {
275 this._queue += txt + '\n';
278 service._send_chart_to_netdata = function(chart) {
279 // internal function to send a chart to netdata
280 this.queue('CHART "' + chart.id + '" "' + chart.name + '" "' + chart.title + '" "' + chart.units + '" "' + chart.family + '" "' + chart.context + '" "' + chart.type + '" ' + chart.priority.toString() + ' ' + chart.update_every.toString());
282 for(var dim in chart.dimensions) {
283 var d = chart.dimensions[dim];
285 this.queue('DIMENSION "' + d.id + '" "' + d.name + '" "' + d.algorithm + '" ' + d.multiplier.toString() + ' ' + d.divisor.toString() + ' ' + ((d.hidden === true)?'hidden':'').toString());
290 chart._created = true;
291 chart._updated = false;
294 // begin data collection for a chart
295 service.begin = function(chart) {
296 if(this._current_chart !== null && this._current_chart !== chart) {
297 this.error('Called begin() for chart ' + chart.id + ' while chart ' + this._current_chart.id + ' is still open. Closing it.');
301 if(typeof(chart.id) === 'undefined' || netdata.charts[chart.id] != chart) {
302 this.error('Called begin() for chart ' + chart.id + ' that is not mine. Where did you find it? Ignoring it.');
306 if(netdata.options.DEBUG === true) netdata.debug('setting current chart to ' + chart.id);
307 this._current_chart = chart;
308 this._current_chart._began = true;
310 if(this._current_chart._dimensions_count !== 0) {
311 if(this._current_chart._created === false || this._current_chart._updated === true)
312 this._send_chart_to_netdata(this._current_chart);
314 var now = this.ended;
315 this.queue('BEGIN ' + this._current_chart.id + ' ' + ((this._current_chart._last_updated > 0)?((now - this._current_chart._last_updated) * 1000):'').toString());
317 // else this.error('Called begin() for chart ' + chart.id + ' which is empty.');
319 this._current_chart._last_updated = now;
320 this._current_chart._began = true;
321 this._current_chart._counter++;
326 // set a collected value for a chart
327 // we do most things on the first value we attempt to set
328 service.set = function(dimension, value) {
329 if(this._current_chart === null) {
330 this.error('Called set(' + dimension + ', ' + value + ') without an open chart.');
334 if(typeof(this._current_chart.dimensions[dimension]) === 'undefined') {
335 this.error('Called set(' + dimension + ', ' + value + ') but dimension "' + dimension + '" does not exist in chart "' + this._current_chart.id + '".');
339 if(typeof value === 'undefined' || value === null)
342 if(this._current_chart._dimensions_count !== 0) {
343 if (value instanceof Buffer)
344 this.queue('SET ' + dimension + ' = 0x' + value.toString('hex'));
346 this.queue('SET ' + dimension + ' = ' + value.toString());
352 // end data collection for the current chart - after calling begin()
353 service.end = function() {
354 if(this._current_chart !== null && this._current_chart._began === false) {
355 this.error('Called end() without an open chart.');
359 if(this._current_chart._dimensions_count !== 0) {
361 netdata.send(this._queue);
365 this._current_chart._began = false;
366 if(netdata.options.DEBUG === true) netdata.debug('sent chart ' + this._current_chart.id);
367 this._current_chart = null;
371 // discard the collected values for the current chart - after calling begin()
372 service.flush = function() {
373 if(this._current_chart === null || this._current_chart._began === false) {
374 this.error('Called flush() without an open chart.');
379 this._current_chart._began = false;
380 this._current_chart = null;
384 // create a netdata chart
385 service.chart = function(id, chart) {
386 if(typeof(netdata.charts[id]) === 'undefined') {
387 netdata.charts[id] = {
393 _dimensions_count: 0,
396 title: 'untitled chart',
400 type: netdata.chartTypes.line,
402 update_every: netdata.options.update_every,
407 var c = netdata.charts[id];
409 if(typeof(chart.name) !== 'undefined' && chart.name !== c.name) {
410 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its name');
415 if(typeof(chart.title) !== 'undefined' && chart.title !== c.title) {
416 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its title');
417 c.title = chart.title;
421 if(typeof(chart.units) !== 'undefined' && chart.units !== c.units) {
422 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its units');
423 c.units = chart.units;
427 if(typeof(chart.family) !== 'undefined' && chart.family !== c.family) {
428 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its family');
429 c.family = chart.family;
433 if(typeof(chart.context) !== 'undefined' && chart.context !== c.context) {
434 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its context');
435 c.context = chart.context;
439 if(typeof(chart.type) !== 'undefined' && chart.type !== c.type) {
440 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its type');
445 if(typeof(chart.priority) !== 'undefined' && chart.priority !== c.priority) {
446 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its priority');
447 c.priority = chart.priority;
451 if(typeof(chart.update_every) !== 'undefined' && chart.update_every !== c.update_every) {
452 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' updated its update_every from ' + c.update_every + ' to ' + chart.update_every);
453 c.update_every = chart.update_every;
457 if(typeof(chart.dimensions) !== 'undefined') {
458 for(var x in chart.dimensions) {
459 if(typeof(c.dimensions[x]) === 'undefined') {
460 c._dimensions_count++;
465 id: x, // the unique id of the dimension
466 name: x, // the name of the dimension
467 algorithm: netdata.chartAlgorithms.absolute, // the id of the netdata algorithm
468 multiplier: 1, // the multiplier
469 divisor: 1, // the divisor
470 hidden: false, // is hidden (boolean)
473 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ' created dimension ' + x);
477 var dim = chart.dimensions[x];
478 var d = c.dimensions[x];
480 if(typeof(dim.name) !== 'undefined' && d.name !== dim.name) {
481 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its name');
486 if(typeof(dim.algorithm) !== 'undefined' && d.algorithm !== dim.algorithm) {
487 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its algorithm from ' + d.algorithm + ' to ' + dim.algorithm);
488 d.algorithm = dim.algorithm;
492 if(typeof(dim.multiplier) !== 'undefined' && d.multiplier !== dim.multiplier) {
493 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its multiplier');
494 d.multiplier = dim.multiplier;
498 if(typeof(dim.divisor) !== 'undefined' && d.divisor !== dim.divisor) {
499 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its divisor');
500 d.divisor = dim.divisor;
504 if(typeof(dim.hidden) !== 'undefined' && d.hidden !== dim.hidden) {
505 if(netdata.options.DEBUG === true) netdata.debug('chart ' + id + ', dimension ' + x + ' updated its hidden status');
506 d.hidden = dim.hidden;
510 if(d._updated) c._updated = true;
514 //if(netdata.options.DEBUG === true) netdata.debug(netdata.charts);
515 return netdata.charts[id];
521 runAllServices: function() {
522 if(netdata.options.DEBUG === true) netdata.debug('runAllServices()');
524 var now = new Date().getTime();
525 var len = netdata.services.length;
527 var service = netdata.services[len];
529 if(service.enabled === false || service.running === true) continue;
530 if(now <= service.next_run) continue;
534 now = new Date().getTime();
535 while(service.next_run < now)
536 service.next_run += (service.update_every * 1000);
539 // 1/10th of update_every in pause
540 setTimeout(netdata.runAllServices, netdata.options.update_every * 100);
544 if(netdata.options.DEBUG === true) this.debug('started, services: ' + netdata.stringify(this.services));
546 if(this.services.length === 0) {
547 this.disableNodePlugin();
550 else this.runAllServices();
553 // disable the whole node.js plugin
554 disableNodePlugin: function() {
555 this.send('DISABLE');
559 requestFromParams: function(protocol, hostname, port, path, method) {
568 'Content-Type': 'application/x-www-form-urlencoded',
569 'Connection': 'keep-alive'
571 agent: new http.Agent({
573 keepAliveMsecs: netdata.options.update_every * 1000,
574 maxSockets: 2, // it must be 2 to work
580 requestFromURL: function(a_url) {
581 var u = url.parse(a_url);
582 return netdata.requestFromParams(u.protocol, u.hostname, u.port, u.path, 'GET');
585 configure: function(module, config, callback) {
586 if(netdata.options.DEBUG === true) this.debug(module.name + ': configuring (update_every: ' + this.options.update_every + ')...');
589 module.update_every = this.options.update_every;
591 if(typeof config.update_every !== 'undefined')
592 module.update_every = config.update_every;
594 module.enable_autodetect = (config.enable_autodetect)?true:false;
596 if(typeof(callback) === 'function')
597 module.configure_callback = callback;
599 module.configure_callback = null;
601 var added = module.configure(config);
603 if(netdata.options.DEBUG === true) this.debug(module.name + ': configured, reporting ' + added + ' eligible services.');
605 if(module.configure_callback !== null && added === 0) {
606 if(netdata.options.DEBUG === true) this.debug(module.name + ': configuration finish callback called from configure().');
607 module.configure_callback = null;
615 if(netdata.options.DEBUG === true) netdata.debug('loaded netdata from: ' + __filename);
616 module.exports = netdata;