Skip to content

Instantly share code, notes, and snippets.

@pyro2927
Last active August 29, 2015 14:06
Show Gist options
  • Save pyro2927/3813fb860bffe808f1f4 to your computer and use it in GitHub Desktop.
Save pyro2927/3813fb860bffe808f1f4 to your computer and use it in GitHub Desktop.
Middle-layer to present ESXi vCenter monitoring stats in Bjango's iStat2
## converting ESXi stats to iStat stats
## passed via XML on a TCP connection over port 5109
## example XML at:
## https://github.com/threez/istat/blob/master/spec/frames/measurement_request_spec.rb#L14
## Main things to go after:
#
# CPU usage, id, user, system, nice
# LOAD
# MEMORY usage, wired, active, inactive, free, total, swap used, swap_total, page_ins, page_outs
import socket
import time
import pdb
from pysphere import *
server = VIServer()
server.connect('192.168.1.1', 'root', 'root')
hosts = server.get_hosts()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 5109))
s.listen(1)
conn, addr = s.accept()
while 1:
# TODO: receive auth
data = conn.recv(1024)
if data:
print "received data:", data
#TODO: formulate better registration response
# n = uptime
# c = update
# ath = 0, meaning user doesn't need to auth
conn.send('<?xml version="1.0" encoding="UTF-8"?><isr pl="2" ath="0" ss="6" c="28406490" n="28406489"></isr>')
# would do an 'else' here, but app tends to heartbeat
xml = '<?xml version="1.0" encoding="UTF-8"?><isr ready="1">'
disks = '<DISKS>'
pm = server.get_performance_manager()
# Get network interfaces
for host_mor, host_name in hosts.items():
prop = VIProperty(server, host_mor)
# get CPU usage / CPU cores
# overallCpuUsage returned in MHz
# overallMemoryUsage returned in MB
# cpu_usage = prop.summary.quickStats.overallCpuUsage / prop.hardware.cpuInfo.numCpuCores / 100
usages = pm.get_entity_statistic(host_mor, ['mem.totalCapacity'])
memory_total = int(usages[0].value) # also returned in MB
memory_used = prop.summary.quickStats.overallMemoryUsage
memory_free = memory_total - memory_used
mem_string = '<MEM w="0" a="%s" i="0" f="%s" t="%s" su="0" st="0" pi="0" po="0"></MEM>' % (str(memory_used), str(memory_free), str(memory_total))
print mem_string
xml += mem_string
usages = pm.get_entity_statistic(host_mor, ['cpu.usage'])
cpu_string = '<CPU>'
cpu_used = prop.summary.quickStats.overallCpuUsage
for cpu_usage in usages:
cpu_used = int(cpu_usage.value)/prop.hardware.cpuInfo.numCpuCores
cpu_string += '<c id="%s" u="%s" s="0" n="0"></c></CPU>' % (cpu_usage.time.strftime('%s'), cpu_used)
cpu_string += '</CPU>'
print cpu_string
xml += cpu_string
for ds_mor, ds_name in server.get_datastores().items():
props = VIProperty(server, ds_mor)
# calculate disk usage
disk_total = props.summary.capacity/1000000.0 # need to convert bytes to megabytes
disk_free = props.summary.freeSpace/1000000.0
disk_used = disk_total - disk_free
disk_percent = disk_used/disk_total * 100.0
disks += '<d n="%s" uuid="%s" f="%s" p="%s" u="%s"></d>' % (ds_name, props.summary.url, str(disk_total), str(disk_percent), str(disk_used))
# TODO: map out XML child objects
disks += '</DISKS>'
xml += disks
xml += '</isr>\n'
conn.send(xml)
time.sleep(1)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment