/** * Global variables */ var gangliaTableId = 'nodesDatatable'; var gangliaNodesList; /** * Load Ganglia monitoring tool * * @return Nothing */ function loadGangliaMon() { // Get Ganglia tab var gangliaTab = $('#gangliamon'); // Check whether Ganglia RPMs are installed on the xCAT MN $.ajax({ url : 'lib/systemcmd.php', dataType : 'json', data : { cmd : 'rpm -q rrdtool ganglia-gmetad ganglia-gmond ganglia-web' }, success : checkGangliaRPMs }); // Create groups and nodes DIV var groups = $('
'); var nodes = $(''); gangliaTab.append(groups); gangliaTab.append(nodes); // Create info bar var info = createInfoBar('Select a group to view its nodes'); nodes.append(info); // Get groups $.ajax( { url : 'lib/cmd.php', dataType : 'json', data : { cmd : 'extnoderange', tgt : '/.*', args : 'subgroups', msg : '' }, success : loadGroups4Ganglia }); return; } /** * Check whether Ganglia RPMs are installed * * @param data * Data returned from HTTP request * @return Nothing */ function checkGangliaRPMs(data) { var gangliaTab = $('#gangliamon'); // Get the list of Ganglia RPMs installed var status = data.rsp.split(/\n/); var gangliaRPMs = [ "rrdtool", "ganglia-gmetad", "ganglia-gmond", "ganglia-web" ]; var warningMsg = 'Before continuing, please install the following packages: '; var missingRPMs = false; for ( var i in status) { if (status[i].indexOf("not installed") > -1) { warningMsg += gangliaRPMs[i] + ' '; missingRPMs = true; } } // Append Ganglia PDF if (missingRPMs) { warningMsg += ". Refer to xCAT2-Monitoring.pdf for more information."; var warningBar = createWarnBar(warningMsg); warningBar.css('margin-bottom', '10px'); warningBar.prependTo(gangliaTab); } else { // Check if ganglia is running on the xCAT MN $.ajax( { url : 'lib/cmd.php', dataType : 'json', data : { cmd : 'monls', tgt : '', args : 'gangliamon', msg : '' }, /** * Append warning message * * @param data * Data returned from HTTP request * @return Nothing */ success : function(data) { if (data.rsp[0].indexOf("not-monitored") > -1) { // Create link to start Ganglia var startLnk = $('Click here'); startLnk.css( { 'color' : 'blue', 'text-decoration' : 'none' }); startLnk.click(function() { // Turn on Ganglia for all nodes monitorNode('', 'on'); }); // Create warning bar var warningBar = $(''); var msg = $(''); msg.append(''); msg.append('Please start Ganglia Monitoring on xCAT. '); msg.append(startLnk); msg.append(' to start Ganglia Monitoring.'); warningBar.append(msg); warningBar.css('margin-bottom', '10px'); // If there are any warning messages, append this warning after it var curWarnings = $('#gangliamon').find('.ui-state-error'); var gangliaTab = $('#gangliamon'); if (curWarnings.length) { curWarnings.after(warningBar); } else { warningBar.prependTo(gangliaTab); } } } }); } return; } /** * Load groups * * @param data * Data returned from HTTP request * @return */ function loadGroups4Ganglia(data) { // Remove loader $('#groups').find('img').remove(); var groups = data.rsp; setGroupsCookies(data); // Create a list of groups var ul = $('
',
		'power', 
		'ganglia');
	// Create a datatable
	var gangliaTable = new DataTable(gangliaTableId);
	gangliaTable.init(sorted);
	// Go through each node
	for ( var node in attrs) {
		// Create a row
		var row = new Array();
		// Create a check box, node link, and get node status
		var checkBx = '';
		var nodeLink = $('' + node + '').bind('click', loadNode);
		
		// If there is no status attribute for the node, do not try to access hash table
		// Else the code will break
		var status = '';
		if (attrs[node]['status']) {
			status = attrs[node]['status'].replace('sshd', 'ping');
		}
		// Push in checkbox, node, status, and power
		row.push(checkBx, nodeLink, status, '', '');
		// Go through each header
		for ( var i = 5; i < sorted.length; i++) {
			// Add the node attributes to the row
			var key = sorted[i];
			
			// Do not put comments and status in
			if (key != 'usercomment' && key != 'status' && key.indexOf('statustime') < 0) {
    			var val = attrs[node][key];
    			if (val) {
    				row.push(val);
    			} else {
    				row.push('');
    			}
			}
		}
		// Add the row to the table
		gangliaTable.add(row);
	}
	
	// Clear the tab before inserting the table
	$('#nodesTab').children().remove();
	// Create action bar
	var actionBar = $('');
	/**
	 * The following actions are available to perform against a given node:
	 * power and monitor
	 */
	/*
	 * Power
	 */
	var powerLnk = $('Power');
	// Power on
	var powerOnLnk = $('Power on');
	powerOnLnk.click(function() {
		var tgtNodes = getNodesChecked(gangliaTableId);
		if (tgtNodes) {
			powerNode(tgtNodes, 'on');
		}
	});
	// Power off
	var powerOffLnk = $('Power off');
	powerOffLnk.click(function() {
		var tgtNodes = getNodesChecked(gangliaTableId);
		if (tgtNodes) {
			powerNode(tgtNodes, 'off');
		}
	});
	// Power actions
	var powerActions = [ powerOnLnk, powerOffLnk ];
	var powerActionMenu = createMenu(powerActions);
	/*
	 * Monitor
	 */
	var monitorLnk = $('Monitor');
	// Turn monitoring on
	var monitorOnLnk = $('Monitor on');
	monitorOnLnk.click(function() {
		var tgtNodes = getNodesChecked(gangliaTableId);
		if (tgtNodes) {
			monitorNode(tgtNodes, 'on');
		}
	});
	// Turn monitoring off
	var monitorOffLnk = $('Monitor off');
	monitorOffLnk.click(function() {
		var tgtNodes = getNodesChecked(gangliaTableId);
		if (tgtNodes) {
			monitorNode(tgtNodes, 'off');
		}
	});
	
	// Install Ganglia
	var installLnk = $('Install');
	installLnk.click(function() {
		var tgtNodes = getNodesChecked(gangliaTableId);
		if (tgtNodes) {
			installGanglia(tgtNodes);
		}
	});
	// Power actions
	var monitorActions = [ monitorOnLnk, monitorOffLnk ];
	var monitorActionMenu = createMenu(monitorActions);
	// Create an action menu
	var actionsDIV = $('');
	var actions = [ [ powerLnk, powerActionMenu ], [ monitorLnk, monitorActionMenu ], installLnk ];
	var actionMenu = createMenu(actions);
	actionMenu.superfish();
	actionsDIV.append(actionMenu);
	actionBar.append(actionsDIV);
	$('#nodesTab').append(actionBar);
	// Insert table
	$('#nodesTab').append(gangliaTable.object());
	// Turn table into a datatable
	var gangliaDataTable = $('#' + gangliaTableId).dataTable({
		'iDisplayLength': 50
	});
	
	// Filter table when enter key is pressed
	$('#' + gangliaTableId + '_filter input').unbind();
	$('#' + gangliaTableId + '_filter input').bind('keyup', function(e){
		if (e.keyCode == 13) {
			var table = $('#' + gangliaTableId).dataTable();
			table.fnFilter($(this).val());
			
			// If there are nodes found, get the node attributes
			if (!$('#' + gangliaTableId + ' .dataTables_empty').length) {
				getNodeAttrs4Ganglia(group);
			}
		}
	});
	
	// Load node definitions when next or previous buttons are clicked
	$('#' + gangliaTableId + '_next, #' + gangliaTableId + '_previous').click(function() {
		getNodeAttrs4Ganglia(group);
	});
	// Do not sort ping, power, and comment column
	var cols = $('#' + gangliaTableId + ' thead tr th').click(function() {		
		getNodeAttrs4Ganglia(group);
	});
	var pingCol = $('#' + gangliaTableId + ' thead tr th').eq(2);
	var powerCol = $('#' + gangliaTableId + ' thead tr th').eq(3);
	var gangliaCol = $('#' + gangliaTableId + ' thead tr th').eq(4);
	pingCol.unbind('click');
	powerCol.unbind('click');
	gangliaCol.unbind('click');
	// Create enough space for loader to be displayed
	var style = {'min-width': '60px', 'text-align': 'center'};
	$('#' + gangliaTableId + ' tbody tr td:nth-child(3)').css(style);
	$('#' + gangliaTableId + ' tbody tr td:nth-child(4)').css(style);
	$('#' + gangliaTableId + ' tbody tr td:nth-child(5)').css(style);
	// Instead refresh the ping status and power status
	pingCol.find('span a').bind('click', function(event) {
		refreshNodeStatus(group, gangliaTableId);
	});
	powerCol.find('span a').bind('click', function(event) {
		refreshPowerStatus(group, gangliaTableId);
	});
	gangliaCol.find('span a').bind('click', function(event) {
		refreshGangliaStatus(group);
	});
	
	// Create tooltip for status 
	var tooltipConf = {
		position: "center right",
		offset: [-2, 10],
		effect: "fade",	
		opacity: 0.8,
		relative: true,
		predelay: 800
	};
	
	var pingTip = createStatusToolTip();
	pingCol.find('span').append(pingTip);
	pingCol.find('span a').tooltip(tooltipConf);
	
	// Create tooltip for power 
	var powerTip = createPowerToolTip();
	powerCol.find('span').append(powerTip);
	powerCol.find('span a').tooltip(tooltipConf);
	
	// Create tooltip for ganglia 
	var gangliaTip = createGangliaToolTip();
	gangliaCol.find('span').append(gangliaTip);
	gangliaCol.find('span a').tooltip(tooltipConf);
	/**
	 * Get node and ganglia status
	 */
	
	// If request to get node status is made
	if (getNodeStatus) {
    	// Get the node status
    	$.ajax( {
    		url : 'lib/cmd.php',
    		dataType : 'json',
    		data : {
    			cmd : 'nodestat',
    			tgt : group,
    			args : '',
    			msg : ''
    		},
    
    		success : loadNodeStatus
    	});
	} else {
		// Hide status loader
		var statCol = $('#' + gangliaTableId + ' thead tr th').eq(2);
		statCol.find('img').hide();
	}
	// Get the status of Ganglia
	var nodes = getNodesShown(gangliaTableId);
	$.ajax( {
		url : 'lib/cmd.php',
		dataType : 'json',
		data : {
			cmd : 'webrun',
			tgt : '',
			args : 'gangliastatus;' + nodes,
			msg : ''
		},
		success : loadGangliaStatus
	});
	
	/**
	 * Additional ajax requests need to be made for zVM
	 */
	
	// Get index of hcp column
	var i = $.inArray('hcp', sorted);
	var archCol = $.inArray('arch', sorted);
	if (i) {
		// Get hardware control point
		var rows = gangliaTable.object().find('tbody tr');
		var hcps = new Object();
		var rowsNum = rows.size();
		for (var j = 0; j < rowsNum; j++) {
			var val = rows.eq(j).find('td').eq(i).html();
			var archval = rows.eq(j).find('td').eq(archCol).html();
			if (-1 == archval.indexOf('390')){
				continue;
			}
			hcps[val] = 1;
		}
		var args;
		for (var h in hcps) {
			// Get node without domain name
			args = h.split('.');
			
			// If there are no disk pools or network names cookie for this hcp
			if (!$.cookie(args[0] + 'diskpools') || !$.cookie(args[0] + 'networks')) {
    			// Check if SMAPI is online
    			$.ajax( {
    				url : 'lib/cmd.php',
    				dataType : 'json',
    				data : {
    					cmd : 'lsvm',
    					tgt : args[0],
    					args : '',
    					msg : 'group=' + group + ';hcp=' + args[0]
    				},
    
    				// Load hardware control point specific info
    				// Get disk pools and network names
    				success : loadHcpInfo
    			});		
			}
		} // End of for
	} // End of if
}
/**
 * Load the status of Ganglia for a given group
 * 
 * @param data
 *            Data returned from HTTP request
 * @return Nothing
 */
function loadGangliaStatus(data) {
	// Get datatable
	var datatable = $('#' + gangliaTableId).dataTable();
	var ganglia = data.rsp;
	var rowNum, node, status, args;
	for ( var i in ganglia) {
		// ganglia[0] = nodeName and ganglia[1] = state
		node = jQuery.trim(ganglia[i][0]);
		status = jQuery.trim(ganglia[i][1]);
		// Get the row containing the node
		rowNum = findRow(node, '#' + gangliaTableId, 1);
		// Update the power status column
		datatable.fnUpdate(status, rowNum, 4);
	}
	// Hide Ganglia loader
	var gangliaCol = $('#' + gangliaTableId + ' thead tr th').eq(4);
	gangliaCol.find('img').hide();
}
/**
 * Refresh the status of Ganglia for each node
 * 
 * @param group
 *            Group name
 * @return Nothing
 */
function refreshGangliaStatus(group) {
	// Show ganglia loader
	var gangliaCol = $('#' + gangliaTableId + ' thead tr th').eq(4);
	gangliaCol.find('img').show();
	
	// Get power status for nodes shown
	var nodes = getNodesShown(gangliaTableId);
	// Get the status of Ganglia
	$.ajax( {
		url : 'lib/cmd.php',
		dataType : 'json',
		data : {
			cmd : 'webrun',
			tgt : '',
			args : 'gangliastatus;' + nodes,
			msg : ''
		},
		success : loadGangliaStatus
	});
}
/**
 * Turn on monitoring for a given node
 * 
 * @param node
 *            Node to monitor on or off
 * @param monitor
 *            Monitor state, on or off
 * @return Nothing
 */
function monitorNode(node, monitor) {
	if (monitor == 'on') {
		// Append loader to warning bar
		var gangliaLoader = createLoader('');
		var warningBar = $('#gangliamon').find('.ui-state-error p');
		if (warningBar.length) {
			warningBar.append(gangliaLoader);
		}
		if (node) {
			// Check if ganglia RPMs are installed
			$.ajax( {
				url : 'lib/cmd.php',
				dataType : 'json',
				data : {
					cmd : 'webrun',
					tgt : '',
					args : 'gangliacheck;' + node,
					msg : node	// Node range will be passed along in data.msg
				},
				/**
				 * Start ganglia on a given node range
				 * 
				 * @param data
				 *            Data returned from HTTP request
				 * @return Nothing
				 */
				success : function(data) {
					// Get response
					var out = data.rsp[0].split(/\n/);
					// Go through each line
					var warn = false;
					var warningMsg = '';
					for (var i in out) {
						// If an RPM is not installed
						if (out[i].indexOf('not installed') > -1) {
							warn = true;
							
							if (warningMsg) {
								warningMsg += ' Updating table 