Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.santaba.agent.groovyapi.expect.Expect;
- import java.security.MessageDigest;
- import java.util.*;
- import java.net.URL;
- import com.vmware.vim25.*;
- import com.vmware.vim25.mo.*;
- import com.santaba.agent.util.esx.EsxVcService;
- // ###############################################
- // ############ Cached Datasource ############
- // ###############################################
- // ## This is used to share string information ##
- // ## between instances. The aim is to remove ##
- // ## the need for many identical collections. ##
- // ###############################################
- // ## You only need to edit between hash areas. ##
- // ###############################################
- // ###############################################
- // ############ Start Config ############
- // ###############################################
- def fileName = "esxVMpowerState";
- def hostname = hostProps.get("system.hostname");
- def user = hostProps.get("esx.user");
- def pass = hostProps.get("esx.pass");
- def wildValue = "##WILDALIAS##";
- // ###############################################
- // ############ End Config ############
- // ###############################################
- // File names must be unique to prevent conflicts so we MD5 the hostname
- MessageDigest digest = MessageDigest.getInstance("MD5");
- digest.update(hostname.bytes);
- def md5 = new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
- // Make referances for files
- new File("tmp").mkdir();
- def dataFile = new File("tmp/${fileName}Data-${md5}.dat");
- def lockFile = new File("tmp/${fileName}Lock-${md5}.dat");
- // Another instance could be updating currently. Check if the lock file exists, and if it does check if its an old one (from a possible crash)
- while(lockFile.exists())
- {
- sleep(50);
- def lockAge = (new Date().getTime() - lockFile.lastModified());
- hadToWait = true;
- if(lockAge > 300000)
- {
- lockFile.delete();
- }
- }
- // Is the data file old (and not being updated)?
- def dataAge = (new Date().getTime() - dataFile.lastModified());
- def data = "";
- if(dataAge > 300000) // If the data is over 1 min old we collect again.
- {
- // Make the lock file so other instances running parallel won't also try and update
- lockFile.write("");
- // ###############################################
- // ############ Start Collecting Data ############
- // ###############################################
- // ## Store what needs to be cached in "data" ##
- // ###############################################
- // build the URL
- def url = String.format("https://%s/sdk", hostname);
- def si = null;
- def svc = null;
- try {
- svc = EsxVcService.connect(url, user, pass, 10 * 1000); // timeout in 10 seconds
- si = svc.getServiceInstance();
- def rootFolder = si.getRootFolder();
- def navigator = new InventoryNavigator(rootFolder);
- //def vm = navigator.searchManagedEntity("VirtualMachine", vmName);
- hostSpecificEntities = navigator.searchManagedEntities("VirtualMachine");
- hostSpecificEntities.each{
- vm ->
- vmName = vm.name
- powerState = vm.runtime.powerState
- switch (powerState) {
- case "poweredOn":
- powerState = 0;
- break;
- case "suspended":
- powerState = 1;
- break;
- case "poweredOff":
- powerState = 2;
- break;
- }
- data += "${vmName}##PowerState=${powerState}\n"
- }
- }
- finally {
- if(svc != null) {
- try {
- svc.dispose();
- }
- catch(Exception e) {
- }
- }
- }
- // ###############################################
- // ############ End Collecting Data ############
- // ###############################################
- dataFile.write(data);
- // Unlock
- lockFile.delete();
- }
- // The actual collection
- data = dataFile.text;
- // ###############################################
- // ############ Start Collection ############
- // ###############################################
- // ## The cache string can be found in "data" ##
- // ###############################################
- data.eachLine
- {
- line->
- if(line.contains(wildValue))
- {
- println(line.split('##')[1]);
- }
- }
- // ###############################################
- // ############ End Collection ############
- // ###############################################
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement