lib/zabbix.js
function flush(targetBuilder, sender, flushInterval, timestamp, metrics) {
  debug(`starting flush for timestamp ${timestamp}`);

  const flushStart = tsNow();
  const handle = (processor, stat, value) => {
    try {
      const { host, key } = targetBuilder(stat);
      processor(host, key, value).forEach((item) => {
        sender.addItem(item.host, item.key, item.value);
        debug(`${item.host} -> ${item.key} -> ${item.value}`);
      });
    } catch (err) {
      stats.last_exception = tsNow();
      error(err);
    }
  };

  const counterProcessor = itemsForCounter.bind(undefined, flushInterval);
  Object.keys(metrics.counters).forEach((stat) => {
    handle(counterProcessor, stat, metrics.counters[stat]);
  });

  const timerProcessor = itemsForTimer.bind(undefined, metrics.pctThreshold);
  Object.keys(metrics.timers).forEach((stat) => {
    handle(timerProcessor, stat, metrics.timers[stat]);
  });

  Object.keys(metrics.gauges).forEach((stat) => {
    handle(itemsForGauge, stat, metrics.gauges[stat]);
  });

  stats.flush_length = sender.items.length;
  debug(`flushing ${stats.flush_length} items to zabbix`);

  // Send the items to Zabbix
  sender.send((err, res) => {
    if (err) {
      stats.last_exception = tsNow();
      error(err);
      // eslint-disable-next-line no-param-reassign
      sender.items = [];
    } else {
      stats.last_flush = timestamp;
      stats.flush_time = flushStart - stats.last_flush;
      debug(`flush completed in ${stats.flush_time} seconds`);
    }
    if (res.info) {
      info(res.info);
    }
  });
}
function flush(targetBuilder, sender, flushInterval, timestamp, metrics) {
  debug(`starting flush for timestamp ${timestamp}`);

  const flushStart = tsNow();
  const handle = (processor, stat, value) => {
    try {
      const { host, key } = targetBuilder(stat);
      processor(host, key, value).forEach((item) => {
        sender.addItem(item.host, item.key, item.value);
        debug(`${item.host} -> ${item.key} -> ${item.value}`);
      });
    } catch (err) {
      stats.last_exception = tsNow();
      error(err);
    }
  };

  const counterProcessor = itemsForCounter.bind(undefined, flushInterval);
  Object.keys(metrics.counters).forEach((stat) => {
    handle(counterProcessor, stat, metrics.counters[stat]);
  });

  const timerProcessor = itemsForTimer.bind(undefined, metrics.pctThreshold);
  Object.keys(metrics.timers).forEach((stat) => {
    handle(timerProcessor, stat, metrics.timers[stat]);
  });

  Object.keys(metrics.gauges).forEach((stat) => {
    handle(itemsForGauge, stat, metrics.gauges[stat]);
  });

  stats.flush_length = sender.items.length;
  debug(`flushing ${stats.flush_length} items to zabbix`);

  // Send the items to Zabbix
  sender.send((err, res) => {
    if (err) {
      stats.last_exception = tsNow();
      error(err);
      // eslint-disable-next-line no-param-reassign
      sender.items = [];
    } else {
      stats.last_flush = timestamp;
      stats.flush_time = flushStart - stats.last_flush;
      debug(`flush completed in ${stats.flush_time} seconds`);
    }
    if (res.info) {
      info(res.info);
    }
  });
}
function flush ( targetBuilder , sender , flushInterval , timestamp , metrics ) { debug ( ` ${ timestamp } ` ) ; const flushStart = tsNow ( ) ; const handle = ( processor , stat , value ) => { try { const { host , key } = targetBuilder ( stat ) ; processor ( host , key , value ) . forEach ( ( item ) => { sender . addItem ( item . host , item . key , item . value ) ; debug ( ` ${ item . host } ${ item . key } ${ item . value } ` ) ; } ) ; } catch ( err ) { stats . last_exception = tsNow ( ) ; error ( err ) ; } } ; const counterProcessor = itemsForCounter . bind ( undefined , flushInterval ) ; Object . keys ( metrics . counters ) . forEach ( ( stat ) => { handle ( counterProcessor , stat , metrics . counters [ stat ] ) ; } ) ; const timerProcessor = itemsForTimer . bind ( undefined , metrics . pctThreshold ) ; Object . keys ( metrics . timers ) . forEach ( ( stat ) => { handle ( timerProcessor , stat , metrics . timers [ stat ] ) ; } ) ; Object . keys ( metrics . gauges ) . forEach ( ( stat ) => { handle ( itemsForGauge , stat , metrics . gauges [ stat ] ) ; } ) ; stats . flush_length = sender . items . length ; debug ( ` ${ stats . flush_length } ` ) ; // Send the items to Zabbix sender . send ( ( err , res ) => { if ( err ) { stats . last_exception = tsNow ( ) ; error ( err ) ; // eslint-disable-next-line no-param-reassign sender . items = [ ] ; } else { stats . last_flush = timestamp ; stats . flush_time = flushStart - stats . last_flush ; debug ( ` ${ stats . flush_time } ` ) ; } if ( res . info ) { info ( res . info ) ; } } ) ; }
Flush metrics data to Zabbix.
@param {function} targetBuilder Returns a {host,key} object based on the stat provided.
@param {ZabbixSender} sender Instance of ZabbixSender for sending stats to Zabbix.
@param {number} flushInterval How long stats were collected, for calculating average.
@param {number} timestamp Time of flush as unix timestamp.
@param {Object} metrics Metrics provided by StatsD.
@returns {undefined}
Flush metrics data to Zabbix .