Skip to content

Commit 035d4d2

Browse files
author
Nikhil Handigol
committed
integrating monitoring code into net -- tested
1 parent 550ee24 commit 035d4d2

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

mininet/monitor.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,43 @@
22
from subprocess import Popen, PIPE
33
from multiprocessing import Process
44
import re
5+
import os
6+
from mininet.log import info, error, debug, output
57

68
class Monitor(object):
79

810
def __init__(self, output_dir='/tmp'):
911
self.monitors = []
1012
self.output_dir = output_dir
1113

14+
# Create output directory if it doesn't exist already
15+
debug('Monitoring output dir: %s' % self.output_dir)
16+
if not os.path.isdir(self.output_dir):
17+
os.makedirs(self.output_dir)
18+
19+
# Add general process monitors
20+
# Bandwidth monitor
21+
self.monitors.append(Process(target=self.monitor_devs_ng,
22+
args=('%s/bwm.txt' % self.output_dir, 1.0)))
23+
24+
# CPU monitor
25+
self.monitors.append(Process(target=self.monitor_cpu,
26+
args=('%s/cpu.txt' % self.output_dir, )))
27+
1228
def start(self):
1329
'''Start all the system monitors'''
30+
# Start the monitors
1431
for m in self.monitors:
1532
m.start()
1633

1734
def stop(self):
1835
'''Terminate all the system monitors'''
36+
# Stop the monitors
1937
for m in self.monitors:
2038
m.terminate()
2139
self.monitors = []
2240

23-
def monitor_qlen(self, iface, interval_sec = 0.01, fname='%s/qlen.txt' % self.output_dir):
41+
def monitor_qlen(self, iface, interval_sec = 0.01, fname='%s/qlen.txt' % '.'):
2442
pat_queued = re.compile(r'backlog\s[^\s]+\s([\d]+)p')
2543
cmd = "tc -s qdisc show dev %s" % (iface)
2644
ret = []
@@ -37,7 +55,7 @@ def monitor_qlen(self, iface, interval_sec = 0.01, fname='%s/qlen.txt' % self.ou
3755
sleep(interval_sec)
3856
return
3957

40-
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"):
58+
def monitor_count(self, ipt_args="--src 10.0.0.0/8", interval_sec=0.01, fname='%s/bytes_sent.txt' % '.', chain="OUTPUT"):
4159
cmd = "iptables -I %(chain)s 1 %(filter)s -j RETURN" % {
4260
"filter": ipt_args,
4361
"chain": chain,
@@ -59,7 +77,7 @@ def monitor_count(self, ipt_args="--src 10.0.0.0/8", interval_sec=0.01, fname='%
5977
sleep(interval_sec)
6078
return
6179

62-
def monitor_devs(self, dev_pattern='^sw', fname="%s/bytes_sent.txt" % self.output_dir, interval_sec=0.01):
80+
def monitor_devs(self, dev_pattern='^sw', fname="%s/bytes_sent.txt" % '.', interval_sec=0.01):
6381
"""Aggregates (sums) all txed bytes and rate (in Mbps) from devices whose name
6482
matches @dev_pattern and writes to @fname"""
6583
pat = re.compile(dev_pattern)
@@ -81,16 +99,16 @@ def monitor_devs(self, dev_pattern='^sw', fname="%s/bytes_sent.txt" % self.outpu
8199
sleep(interval_sec)
82100
return
83101

84-
def monitor_devs_ng(self, fname="%s/txrate.txt" % self.output_dir, interval_sec=0.01):
102+
def monitor_devs_ng(self, fname="%s/txrate.txt" % '.', interval_sec=0.01):
85103
"""Uses bwm-ng tool to collect iface tx rate stats. Very reliable."""
86104
cmd = "sleep 1; bwm-ng -t %s -o csv -u bits -T rate -C ',' > %s" % (interval_sec * 1000, fname)
87105
Popen(cmd, shell=True).wait()
88106

89-
def monitor_cpu(self, fname="%s/cpu.txt" % self.output_dir, container=None):
107+
def monitor_cpu(self, fname="%s/cpu.txt" % '.'):
90108
cmd = "(top -b -p 1 -d 1 | grep --line-buffered \"^Cpu\") > %s" % fname
91109
Popen(cmd, shell=True).wait()
92110

93-
def monitor_cpuacct(self, hosts, fname="%s/cpuacct.txt" % self.output_dir, interval_sec=1.0):
111+
def monitor_cpuacct(self, hosts, fname="%s/cpuacct.txt" % '.', interval_sec=1.0):
94112
outfile = open(fname, 'a')
95113
hnames = ' '.join([h.name for h in hosts])
96114
cpuacct_cmd = 'cgget -g cpuacct %s' % hnames

mininet/net.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import select
9292
import signal
9393
from time import sleep
94+
from datetime import datetime
9495

9596
from mininet.cli import CLI
9697
from mininet.log import info, error, debug, output
@@ -99,6 +100,7 @@
99100
from mininet.util import quietRun, fixLimits, numCores
100101
from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd
101102
from mininet.term import cleanUpScreens, makeTerms
103+
from mininet.monitor import Monitor
102104

103105
class Mininet( object ):
104106
"Network emulation with hosts spawned in network namespaces."
@@ -145,6 +147,9 @@ def __init__( self, topo=None, switch=OVSKernelSwitch, host=Host,
145147
self.nextCore = 0 # next core for pinning hosts to CPUs
146148
self.listenPort = listenPort
147149

150+
dt = datetime.now()
151+
self.monitoring = Monitor(output_dir='/tmp/%s-%s' % (str(dt.date()), str(dt.time())))
152+
148153
self.hosts = []
149154
self.switches = []
150155
self.controllers = []
@@ -339,6 +344,9 @@ def start( self ):
339344
info( switch.name + ' ')
340345
switch.start( self.controllers )
341346
info( '\n' )
347+
info( '*** Starting system monitor\n' )
348+
self.monitoring.start()
349+
info( 'Logging monitoring info in: %s\n' % self.monitoring.output_dir )
342350

343351
def stop( self ):
344352
"Stop the controller(s), switches and hosts"
@@ -359,6 +367,9 @@ def stop( self ):
359367
for controller in self.controllers:
360368
info( controller.name + ' ' )
361369
controller.stop()
370+
info( '\n' )
371+
info( '*** Stopping system monitor\n' )
372+
self.monitoring.stop()
362373
info( '\n*** Done\n' )
363374

364375
def run( self, test, *args, **kwargs ):

0 commit comments

Comments
 (0)