lib/zabbix.js
function itemsForTimer(percentiles, host, key, data) {
  const values = data.sort((a, b) => (a - b));
  const count = values.length;
  const min = values[0];
  const max = values[count - 1];

  let mean = min;
  let maxAtThreshold = max;

  const items = [
    {
      host,
      key: `${key}[lower]`,
      value: min || 0,
    },
    {
      host,
      key: `${key}[upper]`,
      value: max || 0,
    },
    {
      host,
      key: `${key}[count]`,
      value: count,
    },
  ];

  percentiles.forEach((percentile) => {
    const strPercentile = percentile.toString().replace('.', '_');

    if (count > 1) {
      const thresholdIndex = Math.round(((100 - percentile) / 100) * count);
      const numInThreshold = count - thresholdIndex;
      const percentValues = values.slice(0, numInThreshold);
      maxAtThreshold = percentValues[numInThreshold - 1];

      // Average the remaining timings
      let sum = 0;
      for (let i = 0; i < numInThreshold; i += 1) {
        sum += percentValues[i];
      }

      mean = sum / numInThreshold;
    }

    items.push({
      host,
      key: `${key}[mean][${strPercentile}]`,
      value: mean || 0,
    });
    items.push({
      host,
      key: `${key}[upper][${strPercentile}]`,
      value: maxAtThreshold || 0,
    });
  });

  return items;
}
function itemsForTimer(percentiles, host, key, data) {
  const values = data.sort((a, b) => (a - b));
  const count = values.length;
  const min = values[0];
  const max = values[count - 1];

  let mean = min;
  let maxAtThreshold = max;

  const items = [
    {
      host,
      key: `${key}[lower]`,
      value: min || 0,
    },
    {
      host,
      key: `${key}[upper]`,
      value: max || 0,
    },
    {
      host,
      key: `${key}[count]`,
      value: count,
    },
  ];

  percentiles.forEach((percentile) => {
    const strPercentile = percentile.toString().replace('.', '_');

    if (count > 1) {
      const thresholdIndex = Math.round(((100 - percentile) / 100) * count);
      const numInThreshold = count - thresholdIndex;
      const percentValues = values.slice(0, numInThreshold);
      maxAtThreshold = percentValues[numInThreshold - 1];

      // Average the remaining timings
      let sum = 0;
      for (let i = 0; i < numInThreshold; i += 1) {
        sum += percentValues[i];
      }

      mean = sum / numInThreshold;
    }

    items.push({
      host,
      key: `${key}[mean][${strPercentile}]`,
      value: mean || 0,
    });
    items.push({
      host,
      key: `${key}[upper][${strPercentile}]`,
      value: maxAtThreshold || 0,
    });
  });

  return items;
}
function itemsForTimer ( percentiles , host , key , data ) { const values = data . sort ( ( a , b ) => ( a - b ) ) ; const count = values . length ; const min = values [ 0 ] ; const max = values [ count - 1 ] ; let mean = min ; let maxAtThreshold = max ; const items = [ { host , key : ` ${ key } ` , value : min || 0 , } , { host , key : ` ${ key } ` , value : max || 0 , } , { host , key : ` ${ key } ` , value : count , } , ] ; percentiles . forEach ( ( percentile ) => { const strPercentile = percentile . toString ( ) . replace ( '.' , '_' ) ; if ( count > 1 ) { const thresholdIndex = Math . round ( ( ( 100 - percentile ) / 100 ) * count ) ; const numInThreshold = count - thresholdIndex ; const percentValues = values . slice ( 0 , numInThreshold ) ; maxAtThreshold = percentValues [ numInThreshold - 1 ] ; // Average the remaining timings let sum = 0 ; for ( let i = 0 ; i < numInThreshold ; i += 1 ) { sum += percentValues [ i ] ; } mean = sum / numInThreshold ; } items . push ( { host , key : ` ${ key } ${ strPercentile } ` , value : mean || 0 , } ) ; items . push ( { host , key : ` ${ key } ${ strPercentile } ` , value : maxAtThreshold || 0 , } ) ; } ) ; return items ; }
Generate items for a timer.
@param {array} percentiles Array of numbers, percentiles to calculate mean and max for.
@param {string} host Hostname in Zabbix.
@param {string} key Item key in Zabbix.
@param {number} data All timing values collected during interval.
@returns {array} Array of {host, key, value} objects.
Generate items for a timer .