Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. import com.santaba.agent.groovyapi.expect.Expect;
  2. import java.security.MessageDigest;
  3. import java.util.*;
  4. import java.net.URL;
  5. import com.vmware.vim25.*;
  6. import com.vmware.vim25.mo.*;
  7. import com.santaba.agent.util.esx.EsxVcService;
  8. // ###############################################
  9. // ############ Cached Datasource ############
  10. // ###############################################
  11. // ## This is used to share string information ##
  12. // ## between instances. The aim is to remove ##
  13. // ## the need for many identical collections. ##
  14. // ###############################################
  15. // ## You only need to edit between hash areas. ##
  16. // ###############################################
  17. // ###############################################
  18. // ############ Start Config ############
  19. // ###############################################
  20. def fileName = "esxVMpowerState";
  21. def hostname = hostProps.get("system.hostname");
  22. def user = hostProps.get("esx.user");
  23. def pass = hostProps.get("esx.pass");
  24. def wildValue = "##WILDALIAS##";
  25. // ###############################################
  26. // ############ End Config ############
  27. // ###############################################
  28. // File names must be unique to prevent conflicts so we MD5 the hostname
  29. MessageDigest digest = MessageDigest.getInstance("MD5");
  30. digest.update(hostname.bytes);
  31. def md5 = new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
  32. // Make referances for files
  33. new File("tmp").mkdir();
  34. def dataFile = new File("tmp/${fileName}Data-${md5}.dat");
  35. def lockFile = new File("tmp/${fileName}Lock-${md5}.dat");
  36. // 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)
  37. while(lockFile.exists())
  38. {
  39. sleep(50);
  40. def lockAge = (new Date().getTime() - lockFile.lastModified());
  41. hadToWait = true;
  42. if(lockAge > 300000)
  43. {
  44. lockFile.delete();
  45. }
  46. }
  47. // Is the data file old (and not being updated)?
  48. def dataAge = (new Date().getTime() - dataFile.lastModified());
  49. def data = "";
  50. if(dataAge > 300000) // If the data is over 1 min old we collect again.
  51. {
  52. // Make the lock file so other instances running parallel won't also try and update
  53. lockFile.write("");
  54. // ###############################################
  55. // ############ Start Collecting Data ############
  56. // ###############################################
  57. // ## Store what needs to be cached in "data" ##
  58. // ###############################################
  59. // build the URL
  60. def url = String.format("https://%s/sdk", hostname);
  61.  
  62. def si = null;
  63. def svc = null;
  64.  
  65. try {
  66.  
  67. svc = EsxVcService.connect(url, user, pass, 10 * 1000); // timeout in 10 seconds
  68. si = svc.getServiceInstance();
  69.  
  70. def rootFolder = si.getRootFolder();
  71. def navigator = new InventoryNavigator(rootFolder);
  72.  
  73. //def vm = navigator.searchManagedEntity("VirtualMachine", vmName);
  74. hostSpecificEntities = navigator.searchManagedEntities("VirtualMachine");
  75.  
  76. hostSpecificEntities.each{
  77.  
  78. vm ->
  79.  
  80. vmName = vm.name
  81. powerState = vm.runtime.powerState
  82.  
  83. switch (powerState) {
  84. case "poweredOn":
  85. powerState = 0;
  86. break;
  87. case "suspended":
  88. powerState = 1;
  89. break;
  90. case "poweredOff":
  91. powerState = 2;
  92. break;
  93. }
  94. data += "${vmName}##PowerState=${powerState}\n"
  95. }
  96. }
  97. finally {
  98.  
  99. if(svc != null) {
  100. try {
  101. svc.dispose();
  102. }
  103. catch(Exception e) {
  104. }
  105. }
  106. }
  107. // ###############################################
  108. // ############ End Collecting Data ############
  109. // ###############################################
  110. dataFile.write(data);
  111. // Unlock
  112. lockFile.delete();
  113. }
  114. // The actual collection
  115. data = dataFile.text;
  116. // ###############################################
  117. // ############ Start Collection ############
  118. // ###############################################
  119. // ## The cache string can be found in "data" ##
  120. // ###############################################
  121. data.eachLine
  122. {
  123. line->
  124. if(line.contains(wildValue))
  125. {
  126. println(line.split('##')[1]);
  127. }
  128. }
  129. // ###############################################
  130. // ############ End Collection ############
  131. // ###############################################
  132. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement