22from subprocess import Popen , PIPE
33from multiprocessing import Process
44import re
5+ import os
6+ from mininet .log import info , error , debug , output
57
68class 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
0 commit comments