Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #Ensure Python 2/3 compatibility
  2. from __future__ import (absolute_import, division,
  3. print_function, unicode_literals)
  4. from builtins import *
  5. from subprocess import Popen, PIPE, STDOUT
  6. import atexit
  7. import ssl
  8. from pyVim import connect
  9. from pyVmomi import vmodl
  10. from pyVmomi import vim
  11. from influxdb import InfluxDBClient
  12. from datetime import datetime
  13.  
  14. # ESXi host parameters
  15. esxi_host = "REDACTED"
  16. esxi_user = "root"
  17. esxi_password = "REDACTED"
  18. esxi_port = 443
  19.  
  20. # InfluxDB parameters
  21. influxdb_host = "REDACTED"
  22. influxdb_port = 8086
  23. influxdb_database = "R710"
  24. influxdb_user = ""
  25. influxdb_password = ""
  26.  
  27. def write_influxdb(json_body):
  28. client = InfluxDBClient(influxdb_host,influxdb_port,influxdb_user,influxdb_password,influxdb_database)
  29. client.write_points(json_body)
  30.  
  31. def connect_esxi():
  32. try:
  33. _create_unverified_https_context = ssl._create_unverified_context
  34. except AttributeError:
  35. pass
  36. else:
  37. ssl._create_default_https_context = _create_unverified_https_context
  38.  
  39. service_instance = connect.SmartConnect(host=esxi_host,
  40. user=esxi_user,
  41. pwd=esxi_password,
  42. port=esxi_port)
  43.  
  44. # What to do when exiting
  45. atexit.register(connect.Disconnect, service_instance)
  46.  
  47. return service_instance
  48.  
  49. def getsensorinfo():
  50. c = connect_esxi()
  51. host = c.content.searchIndex.FindByDnsName(None,esxi_host,False)
  52. health = host.runtime.healthSystemRuntime.systemHealthInfo.numericSensorInfo
  53.  
  54. return health
  55.  
  56. def gettemp_planar():
  57. temp_array = getsensorinfo()
  58.  
  59. for i in temp_array:
  60. if i.name == 'System Board 1 Planar Temp':
  61. temp = i.currentReading/100
  62. return int(temp)
  63.  
  64. def gettemp_ambient():
  65. temp_array = getsensorinfo()
  66.  
  67. for i in temp_array:
  68. if i.name == 'System Board 1 Ambient Temp':
  69. temp = i.currentReading/100
  70. return int(temp)
  71.  
  72. def gettemp_cpu():
  73. temp_array = getsensorinfo()
  74.  
  75. for i in temp_array:
  76. if i.name == 'Processor 1 Temp':
  77. temp_proc1 = (i.currentReading/100) + 100
  78. if i.name == 'Processor 2 Temp':
  79. temp_proc2 = (i.currentReading/100) + 100
  80.  
  81. return int((temp_proc1 + temp_proc2) / 2)
  82.  
  83. def getfan_speed():
  84. spd_array = getsensorinfo()
  85.  
  86. for i in spd_array:
  87. if i.name == 'System Board 1 FAN 5 RPM':
  88. spd_fan5 = i.currentReading/100
  89. if i.name == 'System Board 1 FAN 4 RPM':
  90. spd_fan4 = i.currentReading/100
  91. if i.name == 'System Board 1 FAN 3 RPM':
  92. spd_fan3 = i.currentReading/100
  93. if i.name == 'System Board 1 FAN 2 RPM':
  94. spd_fan2 = i.currentReading/100
  95. if i.name == 'System Board 1 FAN 1 RPM':
  96. spd_fan1 = i.currentReading/100
  97.  
  98. spd_avg = (spd_fan5 + spd_fan4 + spd_fan3 + spd_fan2 + spd_fan1) / 5
  99.  
  100. return int(spd_avg)
  101.  
  102. def control():
  103. temp_planar = gettemp_planar()
  104. temp_ambient = gettemp_ambient()
  105. temp_cpu = gettemp_cpu()
  106. fan_avg = getfan_speed()
  107.  
  108. current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
  109. db_json = [
  110. {
  111. "measurement": "temperature",
  112. "tags": {
  113. "sensor": "cpu"
  114. },
  115. "time": current_time,
  116. "fields": {
  117. "value": int(temp_cpu)
  118. }
  119. },
  120. {
  121. "measurement": "temperature",
  122. "tags": {
  123. "sensor": "planar"
  124. },
  125. "time": current_time,
  126. "fields": {
  127. "value": int(temp_planar)
  128. }
  129. },
  130. {
  131. "measurement": "temperature",
  132. "tags": {
  133. "sensor": "ambient"
  134. },
  135. "time": current_time,
  136. "fields": {
  137. "value": int(temp_ambient)
  138. }
  139. },
  140. {
  141. "measurement": "speed",
  142. "tags": {
  143. "sensor": "fan"
  144. },
  145. "time": current_time,
  146. "fields": {
  147. "value": int(fan_avg)
  148. }
  149. }
  150. ]
  151. write_influxdb(db_json)
  152. print("Planar Temperature is " + str(temp_planar) + "C")
  153. print("Ambient Temperature is " + str(temp_ambient) + "C")
  154. print("Average Fan Speed is " + str(fan_avg) + "RPM")
  155. print("CPU Temperature is " + str(temp_cpu) + "C")
  156.  
  157. def main():
  158. control()
  159.  
  160. if __name__ == '__main__':
  161. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement