* Limits are in KBytes.
[nodemanager.git] / bwmon.py
1 #!/usr/bin/python
2 #
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.
9 #
10 # Two separate limits are enforced, one for destinations exempt from
11 # the node bandwidth cap, and the other for all other destinations.
12 #
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
17 #
18 # $Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $
19 #
20
21 import os
22 import sys
23 import time
24 import pickle
25 import database
26
27 import socket
28 #import xmlrpclib
29 import bwlimit
30 import logger
31
32 from sets import Set
33 try:
34     sys.path.append("/etc/planetlab")
35     from plc_config import *
36 except:
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"
42
43
44 # Utility functions
45 #from pl_mom import *
46
47 # Constants
48 seconds_per_day = 24 * 60 * 60
49 bits_per_byte = 8
50
51 # Defaults
52 debug = False
53 verbose = 0
54 datafile = "/var/lib/misc/bwmon.dat"
55 #nm = None
56
57 # Burst to line rate (or node cap).  Set by NM.
58 default_MaxRate = bwlimit.get_bwcap()
59 default_Maxi2Rate = bwlimit.bwmax
60 # Min rate 8 bits/s 
61 default_MinRate = 0
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
70 default_Share = 1
71
72 # Average over 1 day
73 period = 1 * seconds_per_day
74
75 # Message template
76 template = \
77 """
78 The slice %(slice)s has transmitted more than %(bytes)s from
79 %(hostname)s to %(class)s destinations
80 since %(since)s.
81
82 Its maximum %(class)s burst rate will be capped at %(new_maxrate)s/s
83 until %(until)s.
84
85 Please reduce the average %(class)s transmission rate
86 of the slice to %(limit)s per %(period)s.
87
88 """.lstrip()
89
90 footer = \
91 """
92 %(date)s %(hostname)s bwcap %(slice)s
93 """.lstrip()
94
95 class Slice:
96     """
97     Stores the last recorded bandwidth parameters of a slice.
98
99     xid - slice context/VServer ID
100     name - slice name
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
111
112     """
113
114     def __init__(self, xid, name, maxrate, maxi2rate, bytes, i2bytes, data):
115         self.xid = xid
116         self.name = name
117         self.time = 0
118         self.bytes = 0
119         self.i2bytes = 0
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
128         self.emailed = False
129
130         # Get real values where applicable
131         self.reset(maxrate, maxi2rate, bytes, i2bytes, data)
132
133     def __repr__(self):
134         return self.name
135
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))
181
182
183     def reset(self, runningmaxrate, runningmaxi2rate, usedbytes, usedi2bytes, data):
184         """
185         Begin a new recording period. Remove caps by restoring limits
186         to their default values.
187         """
188         
189         # Query Node Manager for max rate overrides
190         self.updateSliceAttributes(data)    
191
192         # Reset baseline time
193         self.time = time.time()
194
195         # Reset baseline byte coutns
196         self.bytes = usedbytes
197         self.i2bytes = usedi2bytes
198
199         # Reset email 
200         self.emailed = False
201
202         # Reset rates.
203         if (self.MaxRate != runningmaxrate) or (self.Maxi2Rate != runningmaxi2rate):
204             logger.log("bwmon:  %s reset to %s/%s" % \
205                   (self.name,
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,
213                 share = self.Share)
214
215     def update(self, runningmaxrate, runningmaxi2rate, usedbytes, usedi2bytes, data):
216         """
217         Update byte counts and check if byte limits have been
218         exceeded. 
219         """
220     
221         # Query Node Manager for max rate overrides
222         self.updateSliceAttributes(data)    
223      
224         # Prepare message parameters from the template
225         message = ""
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)} 
231
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
239         else:
240             new_maxrate = self.MaxRate 
241
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)
248
249         if verbose:
250             logger.log("bwmon:  %(slice)s %(class)s " \
251                   "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % \
252                   params)
253
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)
258     
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
266         else:
267             new_maxi2rate = self.Maxi2Rate 
268
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)
275
276         if verbose:
277             logger.log("bwmon:  %(slice)s %(class)s " \
278                   "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % params)
279
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)
284
285         # Apply parameters
286         if new_maxrate != runningmaxrate or new_maxi2rate != runningmaxi2rate:
287             bwlimit.set(xid = self.xid, maxrate = new_maxrate, maxexemptrate = new_maxi2rate)
288
289         # Notify slice
290         if message and self.emailed == False:
291             subject = "pl_mom capped bandwidth of slice %(slice)s on %(hostname)s" % params
292             if debug:
293                 logger.log("bwmon:  "+ subject)
294                 logger.log("bwmon:  "+ message + (footer % params))
295             else:
296                 self.emailed = True
297                 slicemail(self.name, subject, message + (footer % params))
298
299 def GetSlivers(data):
300     # Defaults
301     global datafile, \
302         period, \
303         default_MaxRate, \
304         default_Maxi2Rate, \
305         default_MinRate, \
306         default_MaxKByte,\
307         default_ThreshKByte,\
308         default_Maxi2KByte,\
309         default_Threshi2KByte,\
310         default_Share,\
311         verbose
312
313     verbose = True
314     # All slices
315     names = []
316
317     try:
318         f = open(datafile, "r+")
319         if verbose:
320             logger.log("bwmon:  Loading %s" % datafile)
321         (version, slices) = pickle.load(f)
322         f.close()
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))
326             raise Exception
327     except Exception:
328         version = "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $"
329         slices = {}
330
331     # Get special slice IDs
332     root_xid = bwlimit.get_xid("root")
333     default_xid = bwlimit.get_xid("default")
334
335     # {name: xid}
336     live = {}
337     for sliver in data['slivers']:
338         live[sliver['name']] = bwlimit.get_xid(sliver['name'])
339
340     # Get actual running values from tc.
341     for params in bwlimit.get():
342         (xid, share,
343          minrate, maxrate,
344          minexemptrate, maxexemptrate,
345          usedbytes, usedi2bytes) = params
346         
347         # Ignore root and default buckets
348         if xid == root_xid or xid == default_xid:
349             continue
350
351         name = bwlimit.get_slice(xid)
352         if name is None:
353             # Orphaned (not associated with a slice) class
354             name = "%d?" % xid
355             bwlimit.off(xid)
356
357         # Monitor only the specified slices
358         if names and name not in names:
359             continue
360         #slices is populated from the pickle file
361         #xid is populated from bwlimit (read from /etc/passwd) 
362         if slices.has_key(xid):
363             slice = slices[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)
371             else:
372                 # Update byte counts
373                 slice.update(maxrate, maxexemptrate, usedbytes, usedi2bytes, data)
374         else:
375             # New slice, initialize state
376             if verbose:
377                 logger.log("bwmon: New Slice %s" % name)
378             slice = slices[xid] = Slice(xid, name, maxrate, maxexemptrate, usedbytes, usedi2bytes, data)
379
380     # Delete dead slices
381     dead = Set(slices.keys()) - Set(live.values())
382     for xid in dead:
383         del slices[xid]
384         bwlimit.off(xid)
385
386     logger.log("bwmon:  Saving %s" % datafile)
387     f = open(datafile, "w")
388     pickle.dump((version, slices), f)
389     f.close()
390
391
392 #def GetSlivers(data):
393 #   for sliver in data['slivers']:
394 #       if sliver.has_key('attributes'):
395 #          print sliver
396 #           for attribute in sliver['attributes']:
397 #               if attribute['name'] == "KByteThresh": print attribute['value']
398
399 def start(options, config):
400     pass
401
402 def format_bytes(bytes, si = True):
403     """
404     Formats bytes into a string
405     """
406     if si:
407         kilo = 1000.
408     else:
409         # Officially, a kibibyte
410         kilo = 1024.
411
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))
416     elif bytes >= 1000:
417         return "%.1f KB" % (bytes / kilo)
418     else:
419         return "%.0f bytes" % bytes
420
421 def format_period(seconds):
422     """
423     Formats a period in seconds into a string
424     """
425
426     if seconds == (24 * 60 * 60):
427         return "day"
428     elif seconds == (60 * 60):
429         return "hour"
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.)
434     elif seconds > (60):
435         return "%.1f minutes" % (seconds / 60.)
436     else:
437         return "%.0f seconds" % seconds
438
439 def slicemail(slice, subject, body):
440     sendmail = os.popen("/usr/sbin/sendmail -N never -t -f%s" % PLC_MAIL_SUPPORT_ADDRESS, "w")
441
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"]
445     else:
446         to = [PLC_MAIL_SUPPORT_ADDRESS]
447
448     if slice is not None and slice != "root":
449         to.append(PLC_MAIL_SLICE_ADDRESS.replace("SLICE", slice))
450
451     header = {'from': "%s Support <%s>" % (PLC_NAME, PLC_MAIL_SUPPORT_ADDRESS),
452               'to': ", ".join(to),
453               'version': sys.version.split(" ")[0],
454               'subject': subject}
455
456     # Write headers
457     sendmail.write(
458 """
459 Content-type: text/plain
460 From: %(from)s
461 Reply-To: %(from)s
462 To: %(to)s
463 X-Mailer: Python/%(version)s
464 Subject: %(subject)s
465
466 """.lstrip() % header)
467
468     # Write body
469     sendmail.write(body)
470     # Done
471     sendmail.close()
472