This example shows the use of JavaScript with the vSphere Automation SDK for REST to break down storage consumption by type 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 the most recent statistics for several categories of storage. The example requests both current usage and total storage available for each category, then calculates the percentage used in each category.
This example depends on the following global variables.
■
|
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.function=MAX&item.interval=MINUTES30'; var d_now = new Date(); var ms = d_now.getTime(); var min_30 = 30*60*1000; var startTime = new Date( d_now - min_30 ).toISOString(); var endTime = d_now.toISOString(); query += '&item.start_time=' + startTime; query += '&item.end_time=' + endTime; // Array of monitoring points with output labels: var mon = [ ['VCDB core: ', 'storage.used.filesystem.vcdb_core_inventory', 'storage.totalsize.filesystem.vcdb_core_inventory'], ['VCDB SEAT: ', 'storage.used.filesystem.vcdb_seat', 'storage.totalsize.filesystem.vcdb_seat'], ['Log: ', 'storage.used.filesystem.log', 'storage.totalsize.filesystem.log'] ]; for (var i=0, j=0; i<mon.length; i++) { query += '&item.names.' + (++j).toString() + '=' + mon[i][1]; query += '&item.names.' + (++j).toString() + '=' + mon[i][2]; } my_http_options.path = httpPath + query; // Define the callbacks. function callback(res) { res.on('error', function(err) { console.log('ERROR retrieving storage sizes: ', err)}); res.on('data', function(chunk) {data = chunk.toString();}); res.on('end', function() { if (res.statusCode == 200) { var results = JSON.parse(data).value; console.log(results); for (var i=0, j=0; i<mon.length; i++) { // Discard empty data points: var u='', t; while (u == '') { u = results[j].data.pop(); t = results[j+1].data.pop(); } j += 2; mon[i].push(u, t, (u == 0 ? 0 : Math.floor(10000 * u / t)) / 100); } for (i=0; i<mon.length; i++) { console.log(mon[i][0], mon[i][3], '/', mon[i][4], ' (', mon[i][5], '%)'); } } }) }; // Issue the request. https.request(my_http_options, callback).end();