Guest User

Untitled

a guest
Jan 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import atexit
  3. import getpass
  4. import time
  5.  
  6. from pyVim import connect
  7. from pyVmomi import vmodl
  8. from pyVmomi import vim
  9.  
  10. import tools.cli as cli
  11.  
  12. vm = {}
  13. counterIDs = {}
  14. cpuHighCounter = {}
  15. cpuLowCounter = {}
  16. memCounter = {}
  17. netCounter = {}
  18. maxCpuUsage = {}
  19. failCounter = {}
  20.  
  21. cpuUsageInMHz = 5
  22. memActive = 65545
  23. netBytesTx = 196619
  24.  
  25. def resetCounter(vmname):
  26. cpuHighCounter[vmname] = 0
  27. cpuLowCounter[vmname] = 0
  28. memCounter[vmname] = 0
  29. netCounter[vmname] = 0
  30.  
  31. def detection(vmname, heartbeat):
  32. global cpuHighCounter
  33. global cpuLowCounter
  34. global memCounter
  35. global netCounter
  36. if heartbeat == "red" and cpuHighCounter[vmname] >= 15 and memCounter >= 15: #5 minutes:
  37. print_error(vmname, "kernel panic")
  38. elif cpuHighCounter[vmname] >= 15 and heartbeat != "red":
  39. print_error(vmname, "kernel loop")
  40. # elif cpuHighCounter[vmname] >= 15 and memCounter >= 15: #5 minutes
  41. # print_error(vmname, "kernel panic")
  42. elif cpuLowCounter[vmname] >= 60: #20 minutes
  43. print_error(vmname, "long time idle vm")
  44.  
  45.  
  46. def init(service_instance):
  47. global vm
  48. global counterIDs
  49. global cpuHighCounter
  50. global cpuLowCounter
  51. global memCounter
  52. global netCounter
  53. global maxCpuUsage
  54. global failCounter
  55.  
  56. content = service_instance.RetrieveContent()
  57. container = content.rootFolder # starting point to look into
  58. perfManager = content.perfManager
  59. viewType = [vim.VirtualMachine] # object types to look for
  60. recursive = True # whether we should look into it recursively
  61.  
  62. containerView = content.viewManager.CreateContainerView(
  63. container, viewType, recursive)
  64. children = containerView.view
  65.  
  66. for virtual_machine in children:
  67. vmname = virtual_machine.summary.config.name
  68. vm[vmname] = virtual_machine
  69. cpuHighCounter[vmname] = 0
  70. cpuLowCounter[vmname] = 0
  71. memCounter[vmname] = 0
  72. netCounter[vmname] = 0
  73. failCounter[vmname] = 0
  74. maxCpuUsage[vmname] = virtual_machine.summary.runtime.maxCpuUsage / virtual_machine.summary.config.numCpu
  75.  
  76. counterIDs = {cpuUsageInMHz, memActive, netBytesTx} #cpu usageinmhz, memory active, network bytesTx
  77.  
  78.  
  79. def refresh(service_instance):
  80. global counterIDs
  81. global cpuHighCounter
  82. global cpuLowCounter
  83. global memCounter
  84. global netCounter
  85. global maxCpuUsage
  86. global failCounter
  87.  
  88. content = service_instance.RetrieveContent()
  89. container = content.rootFolder # starting point to look into
  90. perfManager = content.perfManager
  91. viewType = [vim.VirtualMachine] # object types to look for
  92. recursive = True # whether we should look into it recursively
  93.  
  94. counterInfo = {}
  95. for c in perfManager.perfCounter:
  96. prefix = c.groupInfo.key
  97. fullName = c.groupInfo.key + "." + c.nameInfo.key + "." + c.rollupType
  98. counterInfo[fullName] = c.key
  99.  
  100. containerView = content.viewManager.CreateContainerView(
  101. container, viewType, recursive)
  102. children = containerView.view
  103.  
  104. # Loop through all the VMs
  105. for child in children:
  106. vmname = child.summary.config.name
  107. # Using the IDs form a list of MetricId
  108. # objects for building the Query Spec
  109. metricIDs = [vim.PerformanceManager.MetricId(counterId=c,
  110. instance="*")
  111. for c in counterIDs]
  112. # Build the specification to be used
  113. # for querying the performance manager
  114. spec = vim.PerformanceManager.QuerySpec(maxSample=1,
  115. entity=child,
  116. metricId=metricIDs)
  117. # Query the performance manager
  118. # based on the metrics created above
  119. result = perfManager.QueryStats(querySpec=[spec])
  120.  
  121. # Loop through the results and print the output
  122. for r in result:
  123. vmname = child.summary.config.name
  124. #print("VM name: " + child.summary.config.name)
  125. #print("Heartbeat: "+child.guestHeartbeatStatus)
  126.  
  127. cpuUsage = []
  128.  
  129. for val in result[0].value:
  130. if val.id.counterId == cpuUsageInMHz:
  131. cpuUsage.append(val.value[0])
  132.  
  133. if val.id.counterId == memActive:
  134. if val.value[0] == 0:
  135. memCounter[vmname] += 1
  136.  
  137. if val.id.counterId == netBytesTx:
  138. if val.value[0] == 0:
  139. netCounter[vmname] += 1
  140. #get max cpu usage
  141. cpumax = max(cpuUsage)
  142. cpuUsage.remove(cpumax)
  143. cpumax = max(cpuUsage)
  144. #get min cpu usage
  145. cpumin = min(cpuUsage)
  146.  
  147. #calculate cpu usage
  148. if (float(cpumax) / float(maxCpuUsage[vmname])) >= 0.95: #high cpu usage
  149. cpuHighCounter[vmname] += 1
  150. failCounter[vmname] = 0
  151. #print(vmname + " cpu high "+str(cpuHighCounter[vmname]))
  152. elif (float(cpumin) / float(maxCpuUsage[vmname])) <= 0.03: #low cpu usage
  153. cpuLowCounter[vmname] += 1
  154. failCounter[vmname] = 0
  155. #print(vmname + " cpu low "+str(cpuLowCounter[vmname]))
  156. else:
  157. failCounter[vmname] += 1
  158. if failCounter[vmname] >= 5:
  159. failCounter[vmname] = 0
  160. resetCounter(vmname)
  161. #detection
  162. detection(vmname, child.guestHeartbeatStatus)
  163.  
  164. def print_error(vmname, error_msg):
  165. print(vmname + " \t\t: "+error_msg)
  166.  
  167.  
  168. def main():
  169. args = cli.get_args()
  170. global counterIDs
  171.  
  172. try:
  173. if args.disable_ssl_verification:
  174. service_instance = connect.SmartConnectNoSSL(host=args.host,
  175. user=args.user,
  176. pwd=args.password,
  177. port=int(args.port))
  178. else:
  179. service_instance = connect.SmartConnect(host=args.host,
  180. user=args.user,
  181. pwd=args.password,
  182. port=int(args.port))
  183.  
  184. atexit.register(connect.Disconnect, service_instance)
  185. init(service_instance)
  186. while 1:
  187. print(time.strftime("%Y-%m-%d %H:%M:%S"))
  188. refresh(service_instance)
  189. print("==================================================\n")
  190. time.sleep(20)
  191.  
  192. except vmodl.MethodFault as error:
  193. print("Caught vmodl fault : " + error.msg)
  194. return -1
  195.  
  196. return 0
  197.  
  198. # Start program
  199. if __name__ == "__main__":
  200. main()
Add Comment
Please, Sign In to add comment