Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # oled-esxi.py - Polls ESXi host for health / utilisation and displays it on a SSD1322 256x64 OLED display
- # Designed for use with https://forums.aida64.com/topic/4479-sale-er-oledm032-ssd1322-32-oled-usb-interface/
- # Written by SnarlingFox 20190824
- # Import GPIO and FT232H modules.
- from luma.core.interface.serial import spi
- from luma.core.render import canvas
- from luma.oled.device import ssd1322
- from PIL import ImageFont
- import Adafruit_GPIO as GPIO
- import Adafruit_GPIO.FT232H as FT232H
- from os.path import getmtime
- from pyVim.connect import SmartConnect
- from decimal import Decimal
- import os, time, sys, ssl
- WATCHED_FILES_MTIMES = [(__file__, getmtime(__file__))]
- # Connect to OLED FTDI device
- class FTDI_WRAPPER_SPI( FT232H.SPI ):
- # This class wrapper implements the missing attributes required by the luma core interface
- def writebytes(self, data):
- self.write(data)
- def open(self, port, device):
- # Do nothing
- a=1
- FT232H.use_FT232H() # Temporarily disable the built-in FTDI serial driver on Mac & Linux platforms.
- ft232h = FT232H.FT232H() # Create an FT232H object that grabs the first available FT232H device found.
- # Pins initialization
- CS = 3 #D3 Brown
- RESET = 13 #C5 Purple
- DC = 14 #C6 White
- serial = FTDI_WRAPPER_SPI(ft232h, cs=CS, max_speed_hz=8000000)
- ft232h_gpio = ft232h
- SPI = spi(spi=serial, gpio=ft232h_gpio, gpio_DC=DC, gpio_RST=RESET)
- device = ssd1322(SPI, rotate=0)
- device.persist = False
- # Set up some graphical helpers
- def draw_gauge_percentage (canvas, x0, y0, x1, y1, percentage, legend = ""):
- x1 = x0 + x1
- y1 = y0 + y1
- gap_degrees = 60
- line_width = 5
- line_height = 10
- bezel_thickness = 2
- if percentage < 0: percentage = 0
- if percentage > 100: percentage = 100
- start_position = -270 + (gap_degrees / 2)
- end_position = 90 - (gap_degrees / 2)
- percentage_start_position = start_position + ((end_position - start_position) * (0.01 * percentage)) - (line_width / 2)
- percentage_end_position = start_position + ((end_position - start_position) * (0.01 * percentage)) + (line_width / 2)
- canvas.pieslice(
- [x0, y0, x1, y1],
- start_position,
- end_position,
- fill="#fff"
- )
- canvas.ellipse(
- [x0 + bezel_thickness, y0 + bezel_thickness, x1 - bezel_thickness, y1 - bezel_thickness],
- fill="#000", width=0
- )
- canvas.pieslice(
- [x0 + bezel_thickness, y0 + bezel_thickness, x1 - bezel_thickness, y1 - bezel_thickness],
- percentage_start_position,
- percentage_end_position,
- fill="#999"
- )
- canvas.ellipse(
- [x0 + line_height, y0 + line_height, x1 - line_height, y1 - line_height],
- fill="black", width=0
- )
- textsize = canvas.textsize(legend)
- legend_offset_x = x0 + ((x1-x0-textsize[0])/2)
- legend_offset_y = y0 + ((y1-y0-textsize[1])/2) + 1
- canvas.text([legend_offset_x, legend_offset_y], legend)
- def draw_bargraph_horizontal_percentage (canvas, x0, y0, x1, y1, percentage, legend = ""):
- x1 = x0 + x1
- y1 = y0 + y1
- if percentage < 0: percentage = 0
- if percentage > 100: percentage = 100
- canvas.rectangle([x0, y0, x1, y1], outline=None, fill="#333")
- canvas.rectangle([x0, y0, x0 + ((x1 - x0) * (0.01 * percentage)), y1], outline=None, fill="#777")
- canvas.rectangle([x0, y0, x1, y1], outline="#fff", fill=None)
- textsize = canvas.textsize(legend)
- legend_offset_x = x0 + ((x1-x0-textsize[0])/2)
- legend_offset_y = y0 + ((y1-y0-textsize[1])/2)
- canvas.text([legend_offset_x, legend_offset_y], legend)
- def draw_bargraph_vertical_percentage (canvas, x0, y0, x1, y1, percentage, legend = ""):
- x1 = x0 + x1
- y1 = y0 + y1
- if percentage < 0: percentage = 0
- if percentage > 100: percentage = 100
- canvas.rectangle([x0, y0, x1, y1], outline=None, fill="#333")
- canvas.rectangle([x0, y1 - ((y1 - y0) * (0.01 * percentage)), x1, y1], outline=None, fill="#777")
- canvas.rectangle([x0, y0, x1, y1], outline="#fff", fill=None)
- textsize = canvas.textsize(legend)
- legend_offset_x = x0 + ((x1-x0-textsize[0])/2)
- legend_offset_y = y0 + ((y1-y0-textsize[1])/2)
- canvas.text([legend_offset_x, legend_offset_y], legend)
- def draw_label (canvas, x0, y0, x1, y1, legend = ""):
- x1 = x0 + x1
- y1 = y0 + y1
- textsize = canvas.textsize(legend)
- legend_offset_x = x0 + ((x1-x0-textsize[0])/2)
- legend_offset_y = y0 + ((y1-y0-textsize[1])/2)
- canvas.text([legend_offset_x, legend_offset_y], legend)
- # Connect to ESXi
- s = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
- s.verify_mode = ssl.CERT_NONE
- c = SmartConnect(host="HIDDEN", user="HIDDEN", pwd='HIDDEN', sslContext=s)
- print "Connection to FTDI OLED and ESXi successful, sending output to OLED.."
- # Main loop starts here
- while True:
- # Poll ESXi usage summary
- summary = c.content.rootFolder.childEntity[0].hostFolder.childEntity[0].summary
- cpuInfo = c.content.rootFolder.childEntity[0].hostFolder.childEntity[0].host[0].hardware.cpuInfo
- cpuCapacity = (cpuInfo.hz * cpuInfo.numCpuCores) / 1000 / 1000
- memCapacity = summary.effectiveMemory / 1024 / 1024
- currentStats = c.content.rootFolder.childEntity[0].hostFolder.childEntity[0].host[0].summary.quickStats
- cpuUtilPercent = int((100 / float(cpuCapacity)) * float(currentStats.overallCpuUsage))
- memUtilPercent = int((100 / float(memCapacity)) * float(currentStats.overallMemoryUsage))
- # Poll ESXi hardware sensors
- sensors = dict()
- for sensor in c.content.rootFolder.childEntity[0].hostFolder.childEntity[0].host[0].runtime.healthSystemRuntime.systemHealthInfo.numericSensorInfo:
- if sensor.sensorType != 'temperature' and sensor.sensorType != 'fan': continue
- sensors[sensor.name] = Decimal(int(sensor.currentReading)).shift(sensor.unitModifier)
- # print sensor.name, ':', sensors[sensor.name], sensor.baseUnits
- with canvas(device) as draw:
- # Blank screen
- draw.rectangle(device.bounding_box, outline="black", fill="black")
- # Draw CPU and mem
- draw.rectangle([0,0,76,63], outline="#fff", fill="black")
- draw.rectangle([0,0,76,15], outline="#fff", fill="#555")
- draw_label(draw, 0, 0, 76, 15, "Host Usage")
- draw_gauge_percentage(draw, 4, 28, 32, 32, cpuUtilPercent, str(cpuUtilPercent) + '%')
- draw_label(draw, 4, 16, 32, 12, "CPU")
- draw_gauge_percentage(draw, 40, 28, 32, 32, memUtilPercent, str(memUtilPercent) + '%')
- draw_label(draw, 40, 16, 32, 12, "MEM")
- # Draw Temps
- draw.rectangle([80,0,175,63], outline="#fff", fill="black")
- draw.rectangle([80,0,175,15], outline="#fff", fill="#555")
- draw_label(draw, 80, 0, 95, 15, "Temperatures")
- i = 0 ; i_step = 11 ; j = 0
- for sensor in sensors:
- if 'cpu' in sensor.lower():
- draw_bargraph_horizontal_percentage(draw, 83, 18 + i, 89, i_step - 2, int((100 / float(85)) * float(sensors[sensor])), "CPU" + str(j) + " " + str(sensors[sensor]) + "'C")
- i += i_step; j += 1
- draw_bargraph_horizontal_percentage(draw, 83, 18 + i, 89, i_step - 2, int((100 / float(85)) * float(sensors['System Board 2 Peripheral Temp'])), "Mobo " + str(sensors['System Board 2 Peripheral Temp']) + "'C")
- i += i_step; j += 1
- draw_bargraph_horizontal_percentage(draw, 83, 18 + i, 89, i_step - 2, int((100 / float(85)) * float(sensors['System Board 1 System Temp'])), "Case " + str(sensors['System Board 1 System Temp']) + "'C")
- # Draw Fans
- draw.rectangle([179,0,255,63], outline="#fff", fill="black")
- draw.rectangle([179,0,255,15], outline="#fff", fill="#555")
- draw_label(draw, 179, 0, 76, 15, "Case Fans")
- i = 0 ; i_step = 9 ; j = 0
- for sensor in sensors:
- if 'fan' in sensor.lower():
- draw_bargraph_vertical_percentage(draw, 182 + i, 28, i_step - 2, 32, int((100 / float(2000)) * float(sensors[sensor])))
- draw_label(draw, 182 + i, 17, i_step + 1, 12, str(j))
- i += i_step; j += 1
- time.sleep(3)
- # Check if we were modified and exit so we can be reloaded
- for f, mtime in WATCHED_FILES_MTIMES:
- if getmtime(f) != mtime:
- sys.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement