var util = require('util');
var Duplex = require('stream').Duplex;

var LoadAverage = module.exports = function LoadAverage(options) {
  this.warn = options.warn;
  this.critical = options.critical;

  Duplex.call(this, { objectMode: true });
};
util.inherits(LoadAverage, Duplex);

LoadAverage.prototype._write = function(chunk, _, cb) {
  var split = chunk.name.split('.');
  if (split[0] !== 'load-average') return cb();

  var period = parseInt(split[1], 10);
  if (!this.warn[period] || !this.critical[period]) return cb();

  var nprocs = chunk.meta.nprocs;
  var value = chunk.value.toFixed(2) + '/' + nprocs + ' CPUs';

  var message = period + ' minute load average (' + value + ') on host ' + chunk.host;

  if (chunk.value > this.critical[period] * nprocs)
  {
    this.push(
    {
        name: 'load-average',
        host: chunk.host,
        message: message,
        status: 'critical',
        value: value
    });
  }
  else if (chunk.value > this.warn[period] * nprocs)
  {
    this.push(
    {
        name: 'load-average',
        host: chunk.host,
        message: message,
        status: 'warning',
        value: value
    });
  }

  cb();
};

LoadAverage.prototype._read = function(){};

var util = require('util');
var Duplex = require('stream').Duplex;

var LoadAverage = module.exports = function LoadAverage(options) {
  this.warn = options.warn;
  this.critical = options.critical;

  Duplex.call(this, { objectMode: true });
};
util.inherits(LoadAverage, Duplex);

LoadAverage.prototype._write = function(chunk, _, cb) {
  var split = chunk.name.split('.');
  if (split[0] !== 'load-average') return cb();

  var period = parseInt(split[1], 10);
  if (!this.warn[period] || !this.critical[period]) return cb();

  var nprocs = chunk.meta.nprocs;
  var value = chunk.value.toFixed(2) + '/' + nprocs + ' CPUs';

  var message = period + ' minute load average (' + value + ') on host ' + chunk.host;

  if (chunk.value > this.critical[period] * nprocs)
  {
    this.push(
    {
        name: chunk.name,
        host: chunk.host,
        message: message,
        status: 'critical',
        value: value
    });
  }
  else if (chunk.value > this.warn[period] * nprocs)
  {
    this.push(
    {
        name: chunk.name,
        host: chunk.host,
        message: message,
        status: 'warning',
        value: value
    });
  }

  cb();
};

LoadAverage.prototype._read = function(){};

Use event name for incident name
Use event name for incident name
