SnapShot report of Linux/Windows server using vRealize Orchestrator

This is my first vRealize Orchestrator script to pull the information of snapshots of virtual machine x hours old.










Once you are done with attibutes and input/output parameters, time to write script.

----------------------------------Code to pull snapshot report-------------------------------
ar now = new Date();

var now_minus_hours = new Date();
now_minus_hours.setHours(now.getHours() - max_hours);
var i, logtext, contentMessage ;
System.log("Listing snapshots, older than: "+ now_minus_hours);
var vms = System.getModule("com.vmware.library.vc.vm.os").getAllLinuxVMs() ;
content = "VM Name,Power State,Snapshot Name \n";
var outStr = "";
contentMessage = "Hi Team, <br> <br> PFA snapshot status of Linux servers. <br> <br> Thanks<br> Manoj";
for (i in vms)
{
                var vm = vms[i];
                if(vm.snapshot)
                {
                                var allSnaps = new Array();
                                var allParents;
                                var snapshot = vm.snapshot;
                                var Power = vm.runtime.powerState.value;
                                var rootSnapshotList = snapshot.rootSnapshotList;
                                for each (var rsnap in rootSnapshotList)
                                {
                                getSnapshotsOfVM(rsnap);
                                }
                for each (var snap in allSnaps)
                                {
                                if(snap.createTime < now_minus_hours)
                                                {
                                                                //System.log("VM Name: " + snap.vm.name + " Snapshot Name: " + snap.name + " Snapshot creation time: " + snap.createTime.toLocaleString());
                                                                logtext = snap.vm.name + "," + Power + "," + snap.name + "\n";
                                                                content = content +  logtext ;
                                                }
                                }

     }
}

function getSnapshotsOfVM(snapshotTree)
{
    allSnaps.push(snapshotTree);
    var childTrees = snapshotTree.childSnapshotList;
    if(childTrees != null) {
        for(var index in childTrees) {
            if(childTrees[index] != null) {
                getSnapshotsOfVM(childTrees[index]);
            }
        }
    }
}

var message = new EmailMessage();
// Override default settings if and only if input parameter is set
if ( smtpHost != null && smtpHost.length > 0 ){
    message.smtpHost = smtpHost;
}
if ( smtpPort != null && smtpPort > 0 ){
    message.smtpPort = smtpPort;
}
if ( fromName != null && fromName.length > 0){
    message.fromName = fromName;
}
if ( fromAddress != null && fromAddress.length > 0){
    message.fromAddress = fromAddress;
}
message.toAddress = toAddress;
message.subject = subject;
message.ccAddress = ccAddress;
message.addMimePart(contentMessage,"text/html; charset=UTF-8");
//Create Attachment
var fileAttachment = new MimeAttachment();
fileAttachment.name = "SnapShot_Linux.csv";
fileAttachment.content = content;
message.addMimePart(fileAttachment,"application/json; charset=UTF-8");
System.log( "sending mail to host: " + message.bccAddress + ":" + message.smtpPort + " with user:" + message.username + ", from:" + message.fromAddress + ", to:" + message.toAddress );
message.sendMessage();

-------------------------------End Code to pull snapshot report-----------------------------

Please replace below line if you want to pull snapshot report of Windows servers.

var vms = System.getModule("com.vmware.library.vc.vm.os").getAllWindowsVMs() ;

Blow is written since long time, you are welcome to comment here.


No comments:

Post a Comment