chelnak
7/22/2015 - 11:01 AM

vRO task to get CDP information from a vsphere cluster and send email

vRO task to get CDP information from a vsphere cluster and send email

//input: userEmail [String]
//input: cluster [VC:ClusterComputeResource

var csv = ""
var hosts = cluster.host

// Setup Mail Client
var message = new EmailMessage();
message.smtpHost = "smtp@org.co.uk";
message.fromName = "vCenter Orchestrator";
message.fromAddress = "noreply@org.co.uk";
message.toAddress = userEmail;

if (hosts != null) {

	for (var i in hosts) {
	
	var	pnicInfo = hosts[i].configManager.networkSystem.queryNetworkHint();
	csv += "<!DOCTYPE html>\n";
	csv += "<html>\n";
	csv += "<head>\n";
	csv += "<title>" + cluster.name + " CDP Report</title>\n";
	csv += "</head>\n";
	csv += "<body>\n";				
	csv += "<table style='border:1px solid black;font-family: Arial;border-collapse: collapse;'>\n";
	csv += "<tr style='background: #005fbf;'>\n";
	csv += "<td colspan='4' style='font-weight: bold;color: #FFF;font-size: 14px;text-align: center;vertical-align: middle;'>" + hosts[i].name + "</td>\n";
	csv += "</tr>\n";
	csv += "<tr style='background: #005fbf;'>\n";
	csv += "\
	<th style='font-weight: bold;font-size: 12px;color: #FFF;border: 1px solid black;padding: 3px;'>VMnic </th>\
	<th style='font-weight: bold;font-size: 12px;color: #FFF;border: 1px solid black;padding: 3px;'>Address </th>\
	<th style='font-weight: bold;font-size: 12px;color: #FFF;border: 1px solid black;padding: 3px;'>DevId </th>\
	<th style='font-weight: bold;font-size: 12px;color: #FFF;border: 1px solid black;padding: 3px;'>PortId </th>\
	\n";
	csv +="</tr>\n";
	
		for (i in pnicInfo) {
		
			if (pnicInfo[i].connectedSwitchPort) {
			csv +="<tr style= 'border:1px solid black;'>\n";
			csv +="\
			<td style='font-size: 12px;border: 1px solid black;padding: 2px;'> " + pnicInfo[i].device + " </td>\
			<td style='font-size: 12px;border: 1px solid black;padding: 2px;'> " + pnicInfo[i].connectedSwitchPort.address + " </td>\
			<td style='font-size: 12px;border: 1px solid black;padding: 2px;'> " + pnicInfo[i].connectedSwitchPort.devId + " </td>\
			<td style='font-size: 12px;border: 1px solid black;padding: 2px;'> " + pnicInfo[i].connectedSwitchPort.portId + " </td>\
			\n";
				
			csv += "</tr>\n";	
				
			}
			else {
				csv += "<tr style='border:1px solid black;background-color: #aad4ff'>\n";
				csv += "\
					<td style='font-size: 12px;border: 1px solid black;padding: 3px;'>" + pnicInfo[i].device + "</td>\
					<td style='font-size: 12px;border: 1px solid black;padding: 3px;'> No CDP Information </td>\
					<td style='font-size: 12px;border: 1px solid black;padding: 3px;'></td>\
					<td style='font-size: 12px;border: 1px solid black;padding: 3px;'></td>\
					\n";
				csv += "</tr>\n";		
					}
			}
	csv += "</table>\n";
	csv += "<br>\n";
	csv += "</body>\n";
	csv += "</html>\n";			
	}
}

	
//Build attachment
var report = new MimeAttachment();
report.name = "CDP-" + cluster.name +".html";
report.content = csv;

//finalize message and send
message.subject = cluster.name + " CDP Report";
message.addMimePart(csv,"text/html; charset=UTF-8");
message.addMimePart(report,"text/html; charset=utf8");
message.sendMessage();