diff --git a/mininet/monitor.py b/mininet/monitor.py index d36c2b0..66bb7ef 100644 --- a/mininet/monitor.py +++ b/mininet/monitor.py @@ -2,6 +2,8 @@ from time import sleep, time from subprocess import Popen, PIPE from multiprocessing import Process import re +import os +from mininet.log import info, error, debug, output class Monitor(object): @@ -9,18 +11,34 @@ class Monitor(object): self.monitors = [] self.output_dir = output_dir + # Create output directory if it doesn't exist already + debug('Monitoring output dir: %s' % self.output_dir) + if not os.path.isdir(self.output_dir): + os.makedirs(self.output_dir) + + # Add general process monitors + # Bandwidth monitor + self.monitors.append(Process(target=self.monitor_devs_ng, + args=('%s/bwm.txt' % self.output_dir, 1.0))) + + # CPU monitor + self.monitors.append(Process(target=self.monitor_cpu, + args=('%s/cpu.txt' % self.output_dir, ))) + def start(self): '''Start all the system monitors''' + # Start the monitors for m in self.monitors: m.start() def stop(self): '''Terminate all the system monitors''' + # Stop the monitors for m in self.monitors: m.terminate() self.monitors = [] - def monitor_qlen(self, iface, interval_sec = 0.01, fname='%s/qlen.txt' % self.output_dir): + def monitor_qlen(self, iface, interval_sec = 0.01, fname='%s/qlen.txt' % '.'): pat_queued = re.compile(r'backlog\s[^\s]+\s([\d]+)p') cmd = "tc -s qdisc show dev %s" % (iface) ret = [] @@ -37,7 +55,7 @@ class Monitor(object): sleep(interval_sec) return - def monitor_count(self, ipt_args="--src 10.0.0.0/8", interval_sec=0.01, fname='%s/bytes_sent.txt' % self.output_dir, chain="OUTPUT"): + def monitor_count(self, ipt_args="--src 10.0.0.0/8", interval_sec=0.01, fname='%s/bytes_sent.txt' % '.', chain="OUTPUT"): cmd = "iptables -I %(chain)s 1 %(filter)s -j RETURN" % { "filter": ipt_args, "chain": chain, @@ -59,7 +77,7 @@ class Monitor(object): sleep(interval_sec) return - def monitor_devs(self, dev_pattern='^sw', fname="%s/bytes_sent.txt" % self.output_dir, interval_sec=0.01): + def monitor_devs(self, dev_pattern='^sw', fname="%s/bytes_sent.txt" % '.', interval_sec=0.01): """Aggregates (sums) all txed bytes and rate (in Mbps) from devices whose name matches @dev_pattern and writes to @fname""" pat = re.compile(dev_pattern) @@ -81,16 +99,16 @@ class Monitor(object): sleep(interval_sec) return - def monitor_devs_ng(self, fname="%s/txrate.txt" % self.output_dir, interval_sec=0.01): + def monitor_devs_ng(self, fname="%s/txrate.txt" % '.', interval_sec=0.01): """Uses bwm-ng tool to collect iface tx rate stats. Very reliable.""" cmd = "sleep 1; bwm-ng -t %s -o csv -u bits -T rate -C ',' > %s" % (interval_sec * 1000, fname) Popen(cmd, shell=True).wait() - def monitor_cpu(self, fname="%s/cpu.txt" % self.output_dir, container=None): + def monitor_cpu(self, fname="%s/cpu.txt" % '.'): cmd = "(top -b -p 1 -d 1 | grep --line-buffered \"^Cpu\") > %s" % fname Popen(cmd, shell=True).wait() - def monitor_cpuacct(self, hosts, fname="%s/cpuacct.txt" % self.output_dir, interval_sec=1.0): + def monitor_cpuacct(self, hosts, fname="%s/cpuacct.txt" % '.', interval_sec=1.0): outfile = open(fname, 'a') hnames = ' '.join([h.name for h in hosts]) cpuacct_cmd = 'cgget -g cpuacct %s' % hnames diff --git a/mininet/net.py b/mininet/net.py index d7a673d..71839c1 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -91,6 +91,7 @@ import re import select import signal from time import sleep +from datetime import datetime from mininet.cli import CLI from mininet.log import info, error, debug, output @@ -99,6 +100,7 @@ from mininet.link import Link, Intf from mininet.util import quietRun, fixLimits, numCores from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd from mininet.term import cleanUpScreens, makeTerms +from mininet.monitor import Monitor class Mininet( object ): "Network emulation with hosts spawned in network namespaces." @@ -145,6 +147,9 @@ class Mininet( object ): self.nextCore = 0 # next core for pinning hosts to CPUs self.listenPort = listenPort + dt = datetime.now() + self.monitoring = Monitor(output_dir='/tmp/%s-%s' % (str(dt.date()), str(dt.time()))) + self.hosts = [] self.switches = [] self.controllers = [] @@ -339,6 +344,9 @@ class Mininet( object ): info( switch.name + ' ') switch.start( self.controllers ) info( '\n' ) + info( '*** Starting system monitor\n' ) + self.monitoring.start() + info( 'Logging monitoring info in: %s\n' % self.monitoring.output_dir ) def stop( self ): "Stop the controller(s), switches and hosts" @@ -359,6 +367,9 @@ class Mininet( object ): for controller in self.controllers: info( controller.name + ' ' ) controller.stop() + info( '\n' ) + info( '*** Stopping system monitor\n' ) + self.monitoring.stop() info( '\n*** Done\n' ) def run( self, test, *args, **kwargs ):