def cpu_usage(): time_list_now = map(int, execute("head -n 1 /proc/stat").split(' ')[2:6]) time_list_was = roll_data('/tmp/func-cpu-host', time_list_now, [0] * 6) deltas = [yi - xi for yi, xi in zip(time_list_now, time_list_was)] try: cpu_pct = 1 - (float(deltas[-1]) / sum(deltas)) except ZeroDivisionError: cpu_pct = 0 return cpu_pct
def cpu_usage(): time_list_now = map(int, execute("vzctl exec %s \"head -n 1 /proc/stat\"" % vm.ID()).split(' ')[2:6]) time_list_was = roll_data('/tmp/openvz-vm-cpu-%s' % vm.ID(), time_list_now, [0] * 6) deltas = [yi - xi for yi, xi in zip(time_list_now, time_list_was)] try: cpu_pct = 1 - (float(deltas[-1]) / sum(deltas)) except ZeroDivisionError: cpu_pct = 0 return cpu_pct
def network_usage(): def get_netstats(): return [int(v) for v in execute("vzctl exec %s \"cat /proc/net/dev|grep venet0 | " "awk -F: '{print \$2}' | awk '{print \$1, \$9}'\"" % vm.ID()).split(' ')] t2, (rx2, tx2) = time.time(), get_netstats() t1, rx1, tx1 = roll_data("/tmp/openvz-vm-network-%s" % vm.ID(), (t2, rx2, tx2), (0, 0, 0)) window = t2 - t1 return ((rx2 - rx1) / window, (tx2 - tx1) / window)
def network_usage(): def get_netstats(): iface = get_config().getstring('general', 'main_iface') return [int(v) for v in execute("grep %s: /proc/net/dev | awk -F: '{print $2}' | " "awk '{print $1, $9}'" % iface).split(' ')] try: t2, (rx2, tx2) = time.time(), get_netstats() t1, rx1, tx1 = roll_data("/tmp/func-network-host", (t2, rx2, tx2), (0, 0, 0)) window = t2 - t1 return ((rx2 - rx1) / window, (tx2 - tx1) / window) except ValueError: return (0, 0) # better this way
def cpu_usage(): time_list_now = map( int, execute("vzctl exec %s \"head -n 1 /proc/stat\"" % vm.ID()).split(' ')[2:6]) time_list_was = roll_data('/tmp/openvz-vm-cpu-%s' % vm.ID(), time_list_now, [0] * 6) deltas = [yi - xi for yi, xi in zip(time_list_now, time_list_was)] try: cpu_pct = 1 - (float(deltas[-1]) / sum(deltas)) except ZeroDivisionError: cpu_pct = 0 return cpu_pct
def cpu_usage(): hn_stats = conn.getCPUStats(True, 0) if hn_stats is None: return 0.0 time_now = (vm.getCPUStats(True, 0)[0]['cpu_time'], sum(hn_stats.itervalues())) time_was = roll_data('/tmp/kvm-vm-cpu-%s' % vm.ID(), time_now, [0] * 6) deltas = [yi - xi for yi, xi in zip(time_now, time_was)] try: cpu_pct = deltas[0] / float(deltas[1]) except ZeroDivisionError: cpu_pct = 0 return cpu_pct
def network_usage(): def get_netstats(): return [ int(v) for v in execute("vzctl exec %s \"cat /proc/net/dev|grep venet0 | " "awk -F: '{print \$2}' | awk '{print \$1, \$9}'\"" % vm.ID()).split(' ') ] t2, (rx2, tx2) = time.time(), get_netstats() t1, rx1, tx1 = roll_data("/tmp/openvz-vm-network-%s" % vm.ID(), (t2, rx2, tx2), (0, 0, 0)) window = t2 - t1 return ((rx2 - rx1) / window, (tx2 - tx1) / window)
def network_usage(): def get_netstats(): iface = get_config().getstring('general', 'main_iface') return [ int(v) for v in execute( "grep %s: /proc/net/dev | awk -F: '{print $2}' | " "awk '{print $1, $9}'" % iface).split(' ') ] try: t2, (rx2, tx2) = time.time(), get_netstats() t1, rx1, tx1 = roll_data("/tmp/func-network-host", (t2, rx2, tx2), (0, 0, 0)) window = t2 - t1 return ((rx2 - rx1) / window, (tx2 - tx1) / window) except ValueError: return (0, 0) # better this way