This example shows the use of JavaScript with the vSphere Automation SDK for REST to view recent statistics for the vCenter database usage in the vCenter Server Appliance.

The example assumes a previously existing session with the vSphere Automation API endpoint. The JavaScript code depends on the Node.js package.

This example requests statistics at 30-minute intervals for a recent 2-hour report period. The example requests the storage used by the inventory component and the storage used by the combination of statistics, events, alarms, and tasks. The example adds the two values to calculate the vCenter Server database usage in each 30-minute roll-up interval, and then reports the maximum size found over the 2-hour report period.

This example depends on the following global variables.

my_http_options

var https = require('https');
var httpPort = 443;
var httpPath = '/rest/appliance/monitoring/query';
var httpMethod = 'GET';

  // Prepare the HTTP request.
  my_http_options = session.my_http_options;
  my_http_options.method = httpMethod;
  var query = '?item.names.1=storage.used.filesystem.vcdb_core_inventory';
  query += '&item.names.2=storage.used.filesystem.vcdb_seat';
  query += '&item.function=MAX&item.interval=MINUTES30';
  var d_now = new Date();
  var ms = d_now.getTime();
  var min_15 = 15*60*1000;
  var endTime = new Date( d_now - min_15 ).toISOString();
  var startTime = new Date( d_now - 9*min_15 ).toISOString();
  query += '&item.start_time=' + startTime;
  query += '&item.end_time=' + endTime;
  my_http_options.path = httpPath + query;

  // Define the callbacks.
  function callback(res) {
    res.on('error', function(err) {console.log('ERROR querying database size: ', err)});
    res.on('data', function(chunk) {data = chunk.toString();});
    res.on('end', function() {
      if (res.statusCode === 200) {
        var results = JSON.parse(data).value;
        var coreSizes = results[0].data;
        var seatSizes = results[1].data;

        // Eliminate empty data points and process remaining data.
        var highest = coreSizes.filter(function(s){return s !== ''}).
                        map(function(n,i){return parseInt(n) + parseInt(seatSizes[i])}).
                        reduce(function(t,n){return Math.max( t,parseInt(n) )});
        console.log('vCenter database inventory + stats, events, alarms, tasks: (max) size = ',
                    highest);
      }
    })
  };
  // Issue the request.
  https.request(my_http_options, callback).end();