3 # Average bandwidth monitoring script. Run periodically via cron(8) to
4 # enforce a soft limit on daily bandwidth usage for each slice. If a
5 # slice is found to have exceeded its daily bandwidth usage when the
6 # script is run, its instantaneous rate will be capped at the desired
7 # average rate. Thus, in the worst case, a slice will only be able to
8 # send a little more than twice its average daily limit.
10 # Two separate limits are enforced, one for destinations exempt from
11 # the node bandwidth cap, and the other for all other destinations.
13 # Mark Huang <mlhuang@cs.princeton.edu>
14 # Andy Bavier <acb@cs.princeton.edu>
15 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
16 # Copyright (C) 2004-2006 The Trustees of Princeton University
18 # $Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $
38 seconds_per_day = 24 * 60 * 60
44 datafile = "/var/lib/misc/bwmon.dat"
47 # Burst to line rate (or node cap). Set by NM.
48 default_maxrate = bwlimit.get_bwcap()
49 default_maxexemptrate = bwlimit.bwmax
51 # What we cap to when slices break the rules.
52 # 500 Kbit or 5.4 GB per day
53 #default_avgrate = 500000
54 # 1.5 Mbit or 16.4 GB per day
55 #default_avgexemptrate = 1500000
57 # 5.4 Gbyte per day. 5.4 * 1024 k * 1024M * 1024G
58 # 5.4 Gbyte per day max allowed transfered per recording period
59 default_ByteMax = 5798205850
60 default_ByteThresh = int(.8 * default_ByteMax)
61 # 16.4 Gbyte per day max allowed transfered per recording period to I2
62 default_ExemptByteMax = 17609365914
63 default_ExemptByteThresh = int(.8 * default_ExemptByteMax)
68 period = 1 * seconds_per_day
73 The slice %(slice)s has transmitted more than %(bytes)s from
74 %(hostname)s to %(class)s destinations
77 Its maximum %(class)s burst rate will be capped at %(new_maxrate)s/s
80 Please reduce the average %(class)s transmission rate
81 of the slice to %(limit)s per %(period)s.
87 %(date)s %(hostname)s bwcap %(slice)s
92 Stores the last recorded bandwidth parameters of a slice.
94 xid - slice context/VServer ID
96 time - beginning of recording period in UNIX seconds
97 bytes - low bandwidth bytes transmitted at the beginning of the recording period
98 exemptbytes - high bandwidth bytes transmitted at the beginning of the recording period (for I2 -F)
99 ByteMax - total volume of data allowed
100 ByteThresh - After thresh, cap node to (maxbyte - bytes)/(time left in period)
101 ExemptByteMax - Same as above, but for i2.
102 ExemptByteThresh - i2 ByteThresh
103 maxrate - max_rate slice attribute.
104 maxexemptrate - max_exempt_rate slice attribute.
105 self.emailed = did we email during this recording period
109 def __init__(self, xid, name, maxrate, maxexemptrate, bytes, exemptbytes):
115 self.ByteMax = default_ByteMax
116 self.ByteThresh = default_ByteThresh
117 self.ExemptByteMax = default_ExemptByteMax
118 self.ExemptByteThresh = default_ExemptByteThresh
119 self.maxrate = default_maxrate
120 self.maxexemptrate = default_maxexemptrate
123 # Get real values where applicable
124 self.reset(maxrate, maxexemptrate, bytes, exemptbytes)
129 def updateSliceAttributes(self):
130 # Query Node Manager for max rate overrides
132 vals = nm.query(self.name,
133 [('nm_net_max_rate', self.maxrate),
134 ('nm_net_max_exempt_rate', self.maxexemptrate),
135 ("nm_net_max_byte", int(self.ByteMax / 1024)),
136 ("nm_net_max_exempt_byte", int(self.ExemptByteMax / 1024)),
137 ("nm_net_max_thresh_byte", int( .8 * self.ByteMax / 1024)),
138 ("nm_net_max_thresh_exempt_byte", int(.8 * self.ExemptByteMax / 1024)),
139 ("nm_net_avg_rate", 0),
140 ("nm_net_avg_exempt_rate", 0)])
149 avgexemptrate) = vals
151 # The shitty bit. Gotta bias the limits so as not to overflow xmlrpc
152 self.ByteMax = ByteMax * 1024
153 self.ByteThresh = ByteThresh * 1024
154 self.ExemptByteMax = ExemptByteMax * 1024
155 self.ExemptByteThresh = ExemptByteThresh * 1024
157 # The hack here is that when i pass 0 to the xmlrpc request to NM,
158 # for rate limits and it comes back non zero, then screw the byte limits.
159 # Mult by the period and recompute the byte limits. The thought is
160 # If/when PLC switches to byte limits, the avgrates wont be used as
161 # slice attributes and will return as 0
163 self.ByteMax = int(avgrate * period / 8)
164 self.ByteThresh = int(self.ByteMax * .8)
166 if (avgexemptrate != 0):
167 self.ExemptByteMax = int(avgexemptrate * period / 8)
168 self.ExemptByteThresh = int(self.ExemptByteMax * .8)
170 except Exception, err:
171 print "Warning: Exception received while querying NM:", err
173 def reset(self, maxrate, maxexemptrate, bytes, exemptbytes):
175 Begin a new recording period. Remove caps by restoring limits
176 to their default values.
179 # Query Node Manager for max rate overrides
180 self.updateSliceAttributes()
182 # Reset baseline time
183 self.time = time.time()
185 # Reset baseline byte coutns
187 self.exemptbytes = exemptbytes
192 if (self.maxrate != maxrate) or (self.maxexemptrate != maxexemptrate):
193 print "%s reset to %s/%s" % \
195 bwlimit.format_tc_rate(self.maxrate),
196 bwlimit.format_tc_rate(self.maxexemptrate))
197 bwlimit.set(xid = self.xid, maxrate = self.maxrate, maxexemptrate = self.maxexemptrate)
199 def update(self, maxrate, maxexemptrate, bytes, exemptbytes):
201 Update byte counts and check if byte limits have been
205 # Query Node Manager for max rate overrides
206 self.updateSliceAttributes()
208 # Prepare message parameters from the template
210 params = {'slice': self.name, 'hostname': socket.gethostname(),
211 'since': time.asctime(time.gmtime(self.time)) + " GMT",
212 'until': time.asctime(time.gmtime(self.time + period)) + " GMT",
213 'date': time.asctime(time.gmtime()) + " GMT",
214 'period': format_period(period)}
216 if bytes >= (self.bytes + self.ByteThresh):
218 int(((self.ByteMax - (bytes - self.bytes)) * 8)/(period - int(time.time() - self.time)))
219 if new_maxrate < default_MinRate:
220 new_maxrate = default_MinRate
222 new_maxrate = maxrate
224 # Format template parameters for low bandwidth message
225 params['class'] = "low bandwidth"
226 params['bytes'] = format_bytes(bytes - self.bytes)
227 params['maxrate'] = bwlimit.format_tc_rate(maxrate)
228 params['limit'] = format_bytes(self.ByteMax)
229 params['new_maxrate'] = bwlimit.format_tc_rate(new_maxrate)
232 print "%(slice)s %(class)s " \
233 "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % \
236 # Cap low bandwidth burst rate
237 if new_maxrate != maxrate:
238 message += template % params
239 print "%(slice)s %(class)s capped at %(new_maxrate)s/s " % params
241 if exemptbytes >= (self.exemptbytes + self.ExemptByteThresh):
242 new_maxexemptrate = \
243 int(((self.ExemptByteMax - (self.bytes - bytes)) * 8)/(period - int(time.time() - self.time)))
244 if new_maxexemptrate < default_MinRate:
245 new_maxexemptrate = default_MinRate
247 new_maxexemptrate = maxexemptrate
249 # Format template parameters for high bandwidth message
250 params['class'] = "high bandwidth"
251 params['bytes'] = format_bytes(exemptbytes - self.exemptbytes)
252 params['maxrate'] = bwlimit.format_tc_rate(maxexemptrate)
253 params['limit'] = format_bytes(self.ExemptByteMax)
254 params['new_maxexemptrate'] = bwlimit.format_tc_rate(new_maxexemptrate)
257 print "%(slice)s %(class)s " \
258 "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % params
260 # Cap high bandwidth burst rate
261 if new_maxexemptrate != maxexemptrate:
262 message += template % params
263 print "%(slice)s %(class)s capped at %(new_maxexemptrate)s/s" % params
266 if new_maxrate != maxrate or new_maxexemptrate != maxexemptrate:
267 bwlimit.set(xid = self.xid, maxrate = new_maxrate, maxexemptrate = new_maxexemptrate)
270 if message and self.emailed == False:
271 subject = "pl_mom capped bandwidth of slice %(slice)s on %(hostname)s" % params
274 print message + (footer % params)
277 slicemail(self.name, subject, message + (footer % params))
283 Usage: %s [OPTIONS]...
286 -d, --debug Enable debugging (default: %s)
287 -v, --verbose Increase verbosity level (default: %d)
288 -f, --file=FILE Data file (default: %s)
289 -s, --slice=SLICE Constrain monitoring to these slices (default: all)
290 -p, --period=SECONDS Interval in seconds over which to enforce average byte limits (default: %s)
291 -h, --help This message
292 """.lstrip() % (sys.argv[0], debug, verbose, datafile, format_period(period))
296 global debug, verbose, datafile, period, nm
301 longopts = ["debug", "verbose", "file=", "slice=", "period=", "help"]
302 (opts, argv) = getopt.getopt(sys.argv[1:], "dvf:s:p:h", longopts)
303 except getopt.GetoptError, err:
304 print "Error: " + err.msg
308 for (opt, optval) in opts:
309 if opt == "-d" or opt == "--debug":
311 elif opt == "-v" or opt == "--verbose":
313 bwlimit.verbose = verbose - 1
314 elif opt == "-f" or opt == "--file":
316 elif opt == "-s" or opt == "--slice":
318 elif opt == "-p" or opt == "--period":
324 # Check if we are already running
328 # Redirect stdout and stderr to syslog
329 syslog.openlog("bwmon")
330 sys.stdout = sys.stderr = Logger()
333 f = open(datafile, "r+")
335 print "Loading %s" % datafile
336 (version, slices) = pickle.load(f)
338 # Check version of data file
339 if version != "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $":
340 print "Not using old version '%s' data file %s" % (version, datafile)
343 version = "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $"
346 # Get special slice IDs
347 root_xid = bwlimit.get_xid("root")
348 default_xid = bwlimit.get_xid("default")
350 #Open connection to Node Manager. Global.
354 # Get actuall running values from tc.
355 for params in bwlimit.get():
358 minexemptrate, maxexemptrate,
359 bytes, exemptbytes) = params
362 # Ignore root and default buckets
363 if xid == root_xid or xid == default_xid:
366 name = bwlimit.get_slice(xid)
368 # Orphaned (not associated with a slice) class
371 # Monitor only the specified slices
372 if names and name not in names:
374 #slices is populated from the pickle file
375 #xid is populated from bwlimit (read from /etc/passwd)
376 if slices.has_key(xid):
378 if time.time() >= (slice.time + period) or \
379 bytes < slice.bytes or exemptbytes < slice.exemptbytes:
380 # Reset to defaults every 24 hours or if it appears
381 # that the byte counters have overflowed (or, more
382 # likely, the node was restarted or the HTB buckets
383 # were re-initialized).
384 slice.reset(maxrate, maxexemptrate, bytes, exemptbytes)
387 slice.update(maxrate, maxexemptrate, bytes, exemptbytes)
389 # New slice, initialize state
390 slice = slices[xid] = Slice(xid, name, maxrate, maxexemptrate, bytes, exemptbytes)
393 dead = Set(slices.keys()) - Set(live)
398 print "Saving %s" % datafile
399 f = open(datafile, "w")
400 pickle.dump((version, slices), f)
405 if __name__ == '__main__':