Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Ensure Python 2/3 compatibility
- from __future__ import (absolute_import, division,
- print_function, unicode_literals)
- from builtins import *
- from subprocess import Popen, PIPE, STDOUT
- import atexit
- import ssl
- from pyVim import connect
- from pyVmomi import vmodl
- from pyVmomi import vim
- from influxdb import InfluxDBClient
- from datetime import datetime
- # ESXi host parameters
- esxi_host = "REDACTED"
- esxi_user = "root"
- esxi_password = "REDACTED"
- esxi_port = 443
- # InfluxDB parameters
- influxdb_host = "REDACTED"
- influxdb_port = 8086
- influxdb_database = "R710"
- influxdb_user = ""
- influxdb_password = ""
- def write_influxdb(json_body):
- client = InfluxDBClient(influxdb_host,influxdb_port,influxdb_user,influxdb_password,influxdb_database)
- client.write_points(json_body)
- def connect_esxi():
- try:
- _create_unverified_https_context = ssl._create_unverified_context
- except AttributeError:
- pass
- else:
- ssl._create_default_https_context = _create_unverified_https_context
- service_instance = connect.SmartConnect(host=esxi_host,
- user=esxi_user,
- pwd=esxi_password,
- port=esxi_port)
- # What to do when exiting
- atexit.register(connect.Disconnect, service_instance)
- return service_instance
- def getsensorinfo():
- c = connect_esxi()
- host = c.content.searchIndex.FindByDnsName(None,esxi_host,False)
- health = host.runtime.healthSystemRuntime.systemHealthInfo.numericSensorInfo
- return health
- def gettemp_planar():
- temp_array = getsensorinfo()
- for i in temp_array:
- if i.name == 'System Board 1 Planar Temp':
- temp = i.currentReading/100
- return int(temp)
- def gettemp_ambient():
- temp_array = getsensorinfo()
- for i in temp_array:
- if i.name == 'System Board 1 Ambient Temp':
- temp = i.currentReading/100
- return int(temp)
- def gettemp_cpu():
- temp_array = getsensorinfo()
- for i in temp_array:
- if i.name == 'Processor 1 Temp':
- temp_proc1 = (i.currentReading/100) + 100
- if i.name == 'Processor 2 Temp':
- temp_proc2 = (i.currentReading/100) + 100
- return int((temp_proc1 + temp_proc2) / 2)
- def getfan_speed():
- spd_array = getsensorinfo()
- for i in spd_array:
- if i.name == 'System Board 1 FAN 5 RPM':
- spd_fan5 = i.currentReading/100
- if i.name == 'System Board 1 FAN 4 RPM':
- spd_fan4 = i.currentReading/100
- if i.name == 'System Board 1 FAN 3 RPM':
- spd_fan3 = i.currentReading/100
- if i.name == 'System Board 1 FAN 2 RPM':
- spd_fan2 = i.currentReading/100
- if i.name == 'System Board 1 FAN 1 RPM':
- spd_fan1 = i.currentReading/100
- spd_avg = (spd_fan5 + spd_fan4 + spd_fan3 + spd_fan2 + spd_fan1) / 5
- return int(spd_avg)
- def control():
- temp_planar = gettemp_planar()
- temp_ambient = gettemp_ambient()
- temp_cpu = gettemp_cpu()
- fan_avg = getfan_speed()
- current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
- db_json = [
- {
- "measurement": "temperature",
- "tags": {
- "sensor": "cpu"
- },
- "time": current_time,
- "fields": {
- "value": int(temp_cpu)
- }
- },
- {
- "measurement": "temperature",
- "tags": {
- "sensor": "planar"
- },
- "time": current_time,
- "fields": {
- "value": int(temp_planar)
- }
- },
- {
- "measurement": "temperature",
- "tags": {
- "sensor": "ambient"
- },
- "time": current_time,
- "fields": {
- "value": int(temp_ambient)
- }
- },
- {
- "measurement": "speed",
- "tags": {
- "sensor": "fan"
- },
- "time": current_time,
- "fields": {
- "value": int(fan_avg)
- }
- }
- ]
- write_influxdb(db_json)
- print("Planar Temperature is " + str(temp_planar) + "C")
- print("Ambient Temperature is " + str(temp_ambient) + "C")
- print("Average Fan Speed is " + str(fan_avg) + "RPM")
- print("CPU Temperature is " + str(temp_cpu) + "C")
- def main():
- control()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement