#!/usr/bin/python # # xen-stat.py - Displays stats about Xen domains (HVM and PVM) # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version # 2 of the License, or (at your option) any later version. # # Diego Woitasen, # # Tested with Xen 3.1.x # # The output displays: # # -CPU time # -Memory assigned # -Network: Bytes transmitted and received. # -Disks: Sectors read and written # # Limitations: # # -Needs this patch to get HVM networking stats: # http://xenbits.xensource.com/xen-unstable.hg?raw-diff/3ab6635f783d/tools/python/xen/xend/image.py # -For 'file:' virtual block devices, it shows information about the # the real device where the file resides. Works for me :) # import sys import os sys.path.append('/usr/lib/python') from xen.xm.XenAPI import * def net_stats(dom_name): dev_file = open('/proc/net/dev') lines = dev_file.readlines() if domains_type[dom_name] == 'hvm': dev_prefix = 'tap%d.' % (domains_id[dom_name]) else: dev_prefix = 'vif%d.' % (domains_id[dom_name]) net_tx = 0 net_rx = 0 for line in lines: if line.startswith(dev_prefix): line = line[line.find(':') + 1:] fields = line.split() net_rx += int(fields[0]) net_tx += int(fields[8]) return net_tx, net_rx def disk_stats(dom_name): info = domains_info[dom_name] disks = [] for i in info: if i[0] == 'device' and i[1][0] == 'vbd': disk = i[1][1][1] disk = disk.split(':') disks.append(disk) vbd_wr = 0 vbd_rr = 0 for disk in disks: if disk[0] == 'file': minor = os.minor(os.stat(disk[1]).st_dev) elif disk[0] == 'phy': devname = os.readlink(disk[1]) minor = os.minor(os.stat(devname).st_rdev) else: continue statsfile = '/sys/block/dm-%s/stat' % (str(minor)) stats = open(statsfile).readline().split() vbd_rr += int(stats[2]) vbd_wr += int(stats[6]) return vbd_wr, vbd_rr def info_get(dom_name, param): for i in domains_info[dom_name]: if i[0] == param: return i[1] def cpu_stat(dom_name): return info_get(dom_name, 'cpu_time') def mem_stat(dom_name): return info_get(dom_name, 'memory') #xend connection xmlsession = Session('http://localhost:8006') xend = xmlsession.xend domains = xend.domains_with_state(True, 'all', True) #domain type indexed by domain name domains_type = {} domains_info = {} domains_id = {} for dom in domains: type = 'pvm' for info in dom: if info[0] == 'image': for i in info[1]: if i == 'hvm': type = 'hvm' if info[0] == 'domid': id = info[1] if info[0] == 'name': name = info[1] if id == 0: continue domains_type[name] = type; domains_info[name] = dom domains_id[name] = id #print domains_type #print domains_info #print domains_id for dom_name, dom_info in domains_info.items(): cpu = cpu_stat(dom_name) mem = mem_stat(dom_name) net_tx, net_rx = net_stats(dom_name) vbd_wr, vbd_rr = disk_stats(dom_name) print 'NAME=', dom_name print 'CPU=', cpu print 'MEM=', mem print 'NET_TX=', net_tx print 'NET_RX=', net_rx print 'VBD_WR=', vbd_wr print 'VBD_RR=', vbd_rr print