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 $
34 sys.path.append("/etc/planetlab")
35 from plc_config import *
37 logger.log("bwmon: Warning: Configuration file /etc/planetlab/plc_config.py not found")
38 PLC_NAME = "PlanetLab"
39 PLC_SLICE_PREFIX = "pl"
40 PLC_MAIL_SUPPORT_ADDRESS = "support@planet-lab.org"
41 PLC_MAIL_SLICE_ADDRESS = "SLICE@slices.planet-lab.org"
48 seconds_per_day = 24 * 60 * 60
54 datafile = "/var/lib/misc/bwmon.dat"
57 # Burst to line rate (or node cap). Set by NM.
58 default_MaxRate = bwlimit.get_bwcap()
59 default_Maxi2Rate = bwlimit.bwmax
62 # 5.4 Gbyte per day. 5.4 * 1024 k * 1024M * 1024G
63 # 5.4 Gbyte per day max allowed transfered per recording period
64 default_MaxKByte = 5662310
65 default_ThreshKByte = int(.8 * default_MaxKByte)
66 # 16.4 Gbyte per day max allowed transfered per recording period to I2
67 default_Maxi2KByte = 17196646
68 default_Threshi2KByte = int(.8 * default_Maxi2KByte)
69 # Default share quanta
73 period = 1 * seconds_per_day
78 The slice %(slice)s has transmitted more than %(bytes)s from
79 %(hostname)s to %(class)s destinations
82 Its maximum %(class)s burst rate will be capped at %(new_maxrate)s/s
85 Please reduce the average %(class)s transmission rate
86 of the slice to %(limit)s per %(period)s.
92 %(date)s %(hostname)s bwcap %(slice)s
97 Stores the last recorded bandwidth parameters of a slice.
99 xid - slice context/VServer ID
101 time - beginning of recording period in UNIX seconds
102 bytes - low bandwidth bytes transmitted at the beginning of the recording period
103 i2bytes - high bandwidth bytes transmitted at the beginning of the recording period (for I2 -F)
104 ByteMax - total volume of data allowed
105 ByteThresh - After thresh, cap node to (maxbyte - bytes)/(time left in period)
106 ExemptByteMax - Same as above, but for i2.
107 ExemptByteThresh - i2 ByteThresh
108 maxrate - max_rate slice attribute.
109 maxexemptrate - max_exempt_rate slice attribute.
110 self.emailed = did we email during this recording period
114 def __init__(self, xid, name, maxrate, maxi2rate, bytes, i2bytes, data):
120 self.MaxRate = default_MaxRate
121 self.MinRate = default_MinRate
122 self.Maxi2Rate = default_Maxi2Rate
123 self.MaxKByte = default_MaxKByte
124 self.ThreshKByte = default_ThreshKByte
125 self.Maxi2KByte = default_Maxi2KByte
126 self.Threshi2KByte = default_Threshi2KByte
127 self.Share = default_Share
130 # Get real values where applicable
131 self.reset(maxrate, maxi2rate, bytes, i2bytes, data)
136 @database.synchronized
137 def updateSliceAttributes(self, data):
138 for sliver in data['slivers']:
139 if sliver['name'] == self.name:
140 for attribute in sliver['attributes']:
141 if attribute['name'] == 'net_min_rate':
142 self.MinRate = attribute['value']
143 logger.log("bwmon: Updating %s. Min Rate - %s" \
144 %(self.name, self.MinRate))
145 elif attribute['name'] == 'net_max_rate':
146 self.MaxRate = attribute['value']
147 logger.log("bwmon: Updating %s. Max Rate - %s" \
148 %(self.name, self.MaxRate))
149 elif attribute['name'] == 'net_i2_min_rate':
150 self.Mini2Rate = attribute['value']
151 logger.log("bwmon: Updating %s. Min i2 Rate - %s" \
152 %(self.name, self.Mini2Rate))
153 elif attribute['name'] == 'net_i2_max_rate':
154 self.Maxi2Rate = attribute['value']
155 logger.log("bwmon: Updating %s. Max i2 Rate - %s" \
156 %(self.name, self.Maxi2Rate))
157 elif attribute['name'] == 'net_max_kbyte':
158 self.MaxKByte = attribute['value']
159 logger.log("bwmon: Updating %s. Max KByte lim - %s" \
160 %(self.name, self.MaxKByte))
161 elif attribute['name'] == 'net_i2_max_kbyte':
162 self.Maxi2KByte = attribute['value']
163 logger.log("bwmon: Updating %s. Max i2 KByte - %s" \
164 %(self.name, self.Maxi2KByte))
165 elif attribute['name'] == 'net_thresh_kbyte':
166 self.ThreshKByte = attribute['value']
167 logger.log("bwmon: Updating %s. Thresh KByte - %s" \
168 %(self.name, self.ThreshKByte))
169 elif attribute['name'] == 'net_i2_thresh_kbyte':
170 self.Threshi2KByte = attribute['value']
171 logger.log("bwmon: Updating %s. i2 Thresh KByte - %s" \
172 %(self.name, self.Threshi2KByte))
173 elif attribute['name'] == 'net_share':
174 self.Share = attribute['value']
175 logger.log("bwmon: Updating %s. Net Share - %s" \
176 %(self.name, self.Share))
177 elif attribute['name'] == 'net_i2_share':
178 self.Sharei2 = attribute['value']
179 logger.log("bwmon: Updating %s. Net i2 Share - %s" \
180 %(self.name, self.i2Share))
183 def reset(self, runningmaxrate, runningmaxi2rate, usedbytes, usedi2bytes, data):
185 Begin a new recording period. Remove caps by restoring limits
186 to their default values.
189 # Query Node Manager for max rate overrides
190 self.updateSliceAttributes(data)
192 # Reset baseline time
193 self.time = time.time()
195 # Reset baseline byte coutns
196 self.bytes = usedbytes
197 self.i2bytes = usedi2bytes
203 if (self.MaxRate != runningmaxrate) or (self.Maxi2Rate != runningmaxi2rate):
204 logger.log("bwmon: %s reset to %s/%s" % \
206 bwlimit.format_tc_rate(self.MaxRate),
207 bwlimit.format_tc_rate(self.Maxi2Rate)))
208 bwlimit.set(xid = self.xid,
209 minrate = self.MinRate,
210 maxrate = self.MaxRate,
211 maxexemptrate = self.Maxi2Rate,
212 minexemptrate = self.Mini2Rate,
215 def update(self, runningmaxrate, runningmaxi2rate, usedbytes, usedi2bytes, data):
217 Update byte counts and check if byte limits have been
221 # Query Node Manager for max rate overrides
222 self.updateSliceAttributes(data)
224 # Prepare message parameters from the template
226 params = {'slice': self.name, 'hostname': socket.gethostname(),
227 'since': time.asctime(time.gmtime(self.time)) + " GMT",
228 'until': time.asctime(time.gmtime(self.time + period)) + " GMT",
229 'date': time.asctime(time.gmtime()) + " GMT",
230 'period': format_period(period)}
232 if usedbytes >= (self.bytes + (self.ThreshKByte * 1024)):
233 maxbyte = self.MaxKByte * 1024
234 bytesused = bytes - self.bytes
235 timeused = int(time.time() - self.time)
236 new_maxrate = int(((maxbyte - bytesused) * 8)/(period - timeused))
237 if new_maxrate < self.MinRate:
238 new_maxrate = self.MinRate
240 new_maxrate = self.MaxRate
242 # Format template parameters for low bandwidth message
243 params['class'] = "low bandwidth"
244 params['bytes'] = format_bytes(usedbytes - self.bytes)
245 params['maxrate'] = bwlimit.format_tc_rate(runningmaxrate)
246 params['limit'] = format_bytes(self.MaxKByte * 1024)
247 params['new_maxrate'] = bwlimit.format_tc_rate(new_maxrate)
250 logger.log("bwmon: %(slice)s %(class)s " \
251 "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % \
254 # Cap low bandwidth burst rate
255 if new_maxrate != runningmaxrate:
256 message += template % params
257 logger.log("bwmon: %(slice)s %(class)s capped at %(new_maxrate)s/s " % params)
259 if usedi2bytes >= (self.i2bytes + (self.Threshi2KByte * 1024)):
260 maxi2byte = self.Maxi2KByte * 1024
261 i2bytesused = i2bytes - self.i2bytes
262 timeused = int(time.time() - self.time)
263 new_maxi2rate = int(((maxi2byte - i2bytesused) * 8)/(period - timeused))
264 if new_maxi2rate < self.Mini2Rate:
265 new_maxi2rate = self.Mini2Rate
267 new_maxi2rate = self.Maxi2Rate
269 # Format template parameters for high bandwidth message
270 params['class'] = "high bandwidth"
271 params['bytes'] = format_bytes(usedi2bytes - self.i2bytes)
272 params['maxrate'] = bwlimit.format_tc_rate(runningmaxi2rate)
273 params['limit'] = format_bytes(self.Maxi2KByte * 1024)
274 params['new_maxexemptrate'] = bwlimit.format_tc_rate(new_maxi2rate)
277 logger.log("bwmon: %(slice)s %(class)s " \
278 "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % params)
280 # Cap high bandwidth burst rate
281 if new_maxi2rate != runningmaxi2rate:
282 message += template % params
283 logger.log("bwmon: %(slice)s %(class)s capped at %(new_maxexemptrate)s/s" % params)
286 if new_maxrate != runningmaxrate or new_maxi2rate != runningmaxi2rate:
287 bwlimit.set(xid = self.xid, maxrate = new_maxrate, maxexemptrate = new_maxi2rate)
290 if message and self.emailed == False:
291 subject = "pl_mom capped bandwidth of slice %(slice)s on %(hostname)s" % params
293 logger.log("bwmon: "+ subject)
294 logger.log("bwmon: "+ message + (footer % params))
297 slicemail(self.name, subject, message + (footer % params))
299 def GetSlivers(data):
307 default_ThreshKByte,\
309 default_Threshi2KByte,\
318 f = open(datafile, "r+")
320 logger.log("bwmon: Loading %s" % datafile)
321 (version, slices) = pickle.load(f)
323 # Check version of data file
324 if version != "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $":
325 logger.log("bwmon: Not using old version '%s' data file %s" % (version, datafile))
328 version = "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $"
331 # Get special slice IDs
332 root_xid = bwlimit.get_xid("root")
333 default_xid = bwlimit.get_xid("default")
337 for sliver in data['slivers']:
338 live[sliver['name']] = bwlimit.get_xid(sliver['name'])
340 # Get actual running values from tc.
341 for params in bwlimit.get():
344 minexemptrate, maxexemptrate,
345 usedbytes, usedi2bytes) = params
347 # Ignore root and default buckets
348 if xid == root_xid or xid == default_xid:
351 name = bwlimit.get_slice(xid)
353 # Orphaned (not associated with a slice) class
357 # Monitor only the specified slices
358 if names and name not in names:
360 #slices is populated from the pickle file
361 #xid is populated from bwlimit (read from /etc/passwd)
362 if slices.has_key(xid):
364 if time.time() >= (slice.time + period) or \
365 usedbytes < slice.bytes or usedi2bytes < slice.i2bytes:
366 # Reset to defaults every 24 hours or if it appears
367 # that the byte counters have overflowed (or, more
368 # likely, the node was restarted or the HTB buckets
369 # were re-initialized).
370 slice.reset(maxrate, maxexemptrate, usedbytes, usedi2bytes, data)
373 slice.update(maxrate, maxexemptrate, usedbytes, usedi2bytes, data)
375 # New slice, initialize state
377 logger.log("bwmon: New Slice %s" % name)
378 slice = slices[xid] = Slice(xid, name, maxrate, maxexemptrate, usedbytes, usedi2bytes, data)
381 dead = Set(slices.keys()) - Set(live.values())
386 logger.log("bwmon: Saving %s" % datafile)
387 f = open(datafile, "w")
388 pickle.dump((version, slices), f)
392 #def GetSlivers(data):
393 # for sliver in data['slivers']:
394 # if sliver.has_key('attributes'):
396 # for attribute in sliver['attributes']:
397 # if attribute['name'] == "KByteThresh": print attribute['value']
399 def start(options, config):
402 def format_bytes(bytes, si = True):
404 Formats bytes into a string
409 # Officially, a kibibyte
412 if bytes >= (kilo * kilo * kilo):
413 return "%.1f GB" % (bytes / (kilo * kilo * kilo))
414 elif bytes >= 1000000:
415 return "%.1f MB" % (bytes / (kilo * kilo))
417 return "%.1f KB" % (bytes / kilo)
419 return "%.0f bytes" % bytes
421 def format_period(seconds):
423 Formats a period in seconds into a string
426 if seconds == (24 * 60 * 60):
428 elif seconds == (60 * 60):
430 elif seconds > (24 * 60 * 60):
431 return "%.1f days" % (seconds / 24. / 60. / 60.)
432 elif seconds > (60 * 60):
433 return "%.1f hours" % (seconds / 60. / 60.)
435 return "%.1f minutes" % (seconds / 60.)
437 return "%.0f seconds" % seconds
439 def slicemail(slice, subject, body):
440 sendmail = os.popen("/usr/sbin/sendmail -N never -t -f%s" % PLC_MAIL_SUPPORT_ADDRESS, "w")
442 # PLC has a separate list for pl_mom messages
443 if PLC_MAIL_SUPPORT_ADDRESS == "support@planet-lab.org":
444 to = ["pl-mom@planet-lab.org"]
446 to = [PLC_MAIL_SUPPORT_ADDRESS]
448 if slice is not None and slice != "root":
449 to.append(PLC_MAIL_SLICE_ADDRESS.replace("SLICE", slice))
451 header = {'from': "%s Support <%s>" % (PLC_NAME, PLC_MAIL_SUPPORT_ADDRESS),
453 'version': sys.version.split(" ")[0],
459 Content-type: text/plain
463 X-Mailer: Python/%(version)s
466 """.lstrip() % header)