Incorporating old pl_mom into NM.
[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
26 #import socket
27 #import xmlrpclib
28 #import bwlimit
29
30 from sets import Set
31
32 # Utility functions
33 #from pl_mom import *
34
35 # Constants
36 seconds_per_day = 24 * 60 * 60
37 bits_per_byte = 8
38
39 # Defaults
40 debug = False
41 verbose = 0
42 datafile = "/var/lib/misc/bwmon.dat"
43 #nm = None
44
45 # Burst to line rate (or node cap).  Set by NM.
46 default_maxrate = bwlimit.get_bwcap()
47 default_maxi2rate = bwlimit.bwmax
48 default_MinRate = 8
49
50 # What we cap to when slices break the rules.
51 # 500 Kbit or 5.4 GB per day
52 #default_avgrate = 500000
53 # 1.5 Mbit or 16.4 GB per day
54 #default_avgexemptrate = 1500000
55
56 # 5.4 Gbyte per day. 5.4 * 1024 k * 1024M * 1024G 
57 # 5.4 Gbyte per day max allowed transfered per recording period
58 default_ByteMax = 5798205850
59 default_ByteThresh = int(.8 * default_ByteMax) 
60 # 16.4 Gbyte per day max allowed transfered per recording period to I2
61 default_ExemptByteMax = 17609365914 
62 default_ExemptByteThresh = int(.8 * default_ExemptByteMax) 
63
64
65 # Average over 1 day
66 period = 1 * seconds_per_day
67
68 # Message template
69 template = \
70 """
71 The slice %(slice)s has transmitted more than %(bytes)s from
72 %(hostname)s to %(class)s destinations
73 since %(since)s.
74
75 Its maximum %(class)s burst rate will be capped at %(new_maxrate)s/s
76 until %(until)s.
77
78 Please reduce the average %(class)s transmission rate
79 of the slice to %(limit)s per %(period)s.
80
81 """.lstrip()
82
83 footer = \
84 """
85 %(date)s %(hostname)s bwcap %(slice)s
86 """.lstrip()
87
88 class Slice:
89     """
90     Stores the last recorded bandwidth parameters of a slice.
91
92     xid - slice context/VServer ID
93     name - slice name
94     time - beginning of recording period in UNIX seconds
95     bytes - low bandwidth bytes transmitted at the beginning of the recording period
96     i2bytes - high bandwidth bytes transmitted at the beginning of the recording period (for I2 -F)
97     ByteMax - total volume of data allowed
98     ByteThresh - After thresh, cap node to (maxbyte - bytes)/(time left in period)
99     ExemptByteMax - Same as above, but for i2.
100     ExemptByteThresh - i2 ByteThresh
101     maxrate - max_rate slice attribute. 
102     maxexemptrate - max_exempt_rate slice attribute.
103     self.emailed = did we email during this recording period
104
105     """
106
107     def __init__(self, xid, name, maxrate, maxexemptrate, bytes, exemptbytes):
108         self.xid = xid
109         self.name = name
110         self.time = 0
111         self.bytes = 0
112         self.i2bytes = 0
113                 self.MaxRate = default_maxrate
114                 self.MinRate = default_MinRate
115                 self.Mini2Rate = default_MinRate
116                 self.Maxi2Rate = default_maxi2rate
117         self.MaxKByte = default_ByteMax
118         self.ThreshKByte = default_ByteThresh
119         self.Maxi2KByte = default_ExemptByteMax
120         self.Threshi2KByte = default_ExemptByteThresh
121         self.emailed = False
122
123         # Get real values where applicable
124         self.reset(maxrate, maxi2rate, bytes, i2bytes)
125
126     def __repr__(self):
127         return self.name
128
129     def updateSliceAttributes(self, data):
130                 
131                 for sliver in data['slivers']:
132                         for attribute in sliver['attributes']:
133                                 if attribute['name'] == 'net_min_rate':         
134                                         self.MinRate = attribute['value']
135                                 elif attribute['name'] == 'net_max_rate':               
136                                         self.MaxRate = attribute['value']
137                                 elif attribute['name'] == 'net_i2_min_rate':
138                                         self.Mini2Rate = attribute['value']
139                                 elif attribute['name'] == 'net_i2_max_rate':            
140                                         self.Maxi2Rate = attribute['value']
141                                 elif attribute['name'] == 'net_max_kbyte':              
142                                         self.M = attribute['value']
143                                 elif attribute['name'] == 'net_i2_max_kbyte':   
144                                         self.minrate = attribute['value']
145                                 elif attribute['name'] == 'net_thresh_kbyte':   
146                                         self.minrate = attribute['value']
147                                 elif attribute['name'] == 'net_i2_thresh_kbyte':        
148                                         self.minrate = attribute['value']
149
150     def reset(self, maxrate, maxi2rate, bytes, i2bytes):
151         """
152         Begin a new recording period. Remove caps by restoring limits
153         to their default values.
154         """
155         
156         # Query Node Manager for max rate overrides
157         self.updateSliceAttributes()    
158
159         # Reset baseline time
160         self.time = time.time()
161
162         # Reset baseline byte coutns
163         self.bytes = bytes
164         self.i2bytes = exemptbytes
165
166         # Reset email 
167         self.emailed = False
168
169                 # Reset rates.
170         if (self.MaxRate != maxrate) or (self.Maxi2Rate != maxi2rate):
171             print "%s reset to %s/%s" % \
172                   (self.name,
173                    bwlimit.format_tc_rate(self.MaxRate),
174                    bwlimit.format_tc_rate(self.Maxi2Rate))
175             bwlimit.set(xid = self.xid, maxrate = self.MaxRate, maxexemptrate = self.Maxi2Rate)
176
177     def update(self, maxrate, maxi2rate, bytes, ibytes):
178         """
179         Update byte counts and check if byte limits have been
180         exceeded. 
181         """
182     
183         # Query Node Manager for max rate overrides
184         self.updateSliceAttributes()    
185      
186         # Prepare message parameters from the template
187         message = ""
188         params = {'slice': self.name, 'hostname': socket.gethostname(),
189                   'since': time.asctime(time.gmtime(self.time)) + " GMT",
190                   'until': time.asctime(time.gmtime(self.time + period)) + " GMT",
191                   'date': time.asctime(time.gmtime()) + " GMT",
192                   'period': format_period(period)} 
193
194         if bytes >= (self.bytes + self.ByteThresh):
195             new_maxrate = \
196             int(((self.ByteMax - (bytes - self.bytes)) * 8)/(period - int(time.time() - self.time)))
197             if new_maxrate < default_MinRate:
198                 new_maxrate = default_MinRate
199         else:
200             new_maxrate = maxrate
201
202         # Format template parameters for low bandwidth message
203         params['class'] = "low bandwidth"
204         params['bytes'] = format_bytes(bytes - self.bytes)
205         params['maxrate'] = bwlimit.format_tc_rate(maxrate)
206         params['limit'] = format_bytes(self.ByteMax)
207         params['new_maxrate'] = bwlimit.format_tc_rate(new_maxrate)
208
209         if verbose:
210             print "%(slice)s %(class)s " \
211                   "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % \
212                   params
213
214         # Cap low bandwidth burst rate
215         if new_maxrate != maxrate:
216             message += template % params
217             print "%(slice)s %(class)s capped at %(new_maxrate)s/s " % params
218     
219         if exemptbytes >= (self.exemptbytes + self.ExemptByteThresh):
220             new_maxexemptrate = \
221             int(((self.ExemptByteMax - (self.bytes - bytes)) * 8)/(period - int(time.time() - self.time)))
222             if new_maxexemptrate < default_MinRate:
223                 new_maxexemptrate = default_MinRate
224         else:
225             new_maxexemptrate = maxexemptrate
226
227         # Format template parameters for high bandwidth message
228         params['class'] = "high bandwidth"
229         params['bytes'] = format_bytes(exemptbytes - self.exemptbytes)
230         params['maxrate'] = bwlimit.format_tc_rate(maxexemptrate)
231         params['limit'] = format_bytes(self.ExemptByteMax)
232         params['new_maxexemptrate'] = bwlimit.format_tc_rate(new_maxexemptrate)
233
234         if verbose:
235             print "%(slice)s %(class)s " \
236                   "%(bytes)s of %(limit)s (%(new_maxrate)s/s maxrate)" % params
237
238         # Cap high bandwidth burst rate
239         if new_maxexemptrate != maxexemptrate:
240             message += template % params
241             print "%(slice)s %(class)s capped at %(new_maxexemptrate)s/s" % params
242
243         # Apply parameters
244         if new_maxrate != maxrate or new_maxexemptrate != maxexemptrate:
245             bwlimit.set(xid = self.xid, maxrate = new_maxrate, maxexemptrate = new_maxexemptrate)
246
247         # Notify slice
248         if message and self.emailed == False:
249             subject = "pl_mom capped bandwidth of slice %(slice)s on %(hostname)s" % params
250             if debug:
251                 print subject
252                 print message + (footer % params)
253             else:
254                 self.emailed = True
255                 slicemail(self.name, subject, message + (footer % params))
256
257 def main():
258     # Defaults
259     global datafile, period
260     # All slices
261     names = []
262     # Check if we are already running
263     writepid("bwmon")
264
265     try:
266         f = open(datafile, "r+")
267         if verbose:
268             print "Loading %s" % datafile
269         (version, slices) = pickle.load(f)
270         f.close()
271         # Check version of data file
272         if version != "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $":
273             print "Not using old version '%s' data file %s" % (version, datafile)
274             raise Exception
275     except Exception:
276         version = "$Id: bwmon.py,v 1.20 2007/01/10 16:51:04 faiyaza Exp $"
277         slices = {}
278
279     # Get special slice IDs
280     root_xid = bwlimit.get_xid("root")
281     default_xid = bwlimit.get_xid("default")
282
283     live = []
284     # Get actuall running values from tc.
285     for params in bwlimit.get():
286         (xid, share,
287          minrate, maxrate,
288          minexemptrate, maxexemptrate,
289          bytes, i2bytes) = params
290         live.append(xid)
291
292         # Ignore root and default buckets
293         if xid == root_xid or xid == default_xid:
294             continue
295
296         name = bwlimit.get_slice(xid)
297         if name is None:
298             # Orphaned (not associated with a slice) class
299             name = "%d?" % xid
300
301         # Monitor only the specified slices
302         if names and name not in names:
303             continue
304         #slices is populated from the pickle file
305         #xid is populated from bwlimit (read from /etc/passwd) 
306         if slices.has_key(xid):
307             slice = slices[xid]
308             if time.time() >= (slice.time + period) or \
309                bytes < slice.bytes or i2bytes < slice.i2bytes:
310                 # Reset to defaults every 24 hours or if it appears
311                 # that the byte counters have overflowed (or, more
312                 # likely, the node was restarted or the HTB buckets
313                 # were re-initialized).
314                 slice.reset(maxrate, maxexemptrate, bytes, exemptbytes)
315             else:
316                 # Update byte counts
317                 slice.update(maxrate, maxexemptrate, bytes, exemptbytes)
318         else:
319             # New slice, initialize state
320             slice = slices[xid] = Slice(xid, name, maxrate, maxexemptrate, bytes, exemptbytes)
321
322     # Delete dead slices
323     dead = Set(slices.keys()) - Set(live)
324     for xid in dead:
325         del slices[xid]
326
327     if verbose:
328         print "Saving %s" % datafile
329     f = open(datafile, "w")
330     pickle.dump((version, slices), f)
331     f.close()
332
333
334
335 def GetSlivers(data):
336     for sliver in data['slivers']:
337         if sliver.has_key('attributes'):
338             print sliver
339             for attribute in sliver['attributes']:
340                 if attribute['name'] == "KByteThresh": print attribute['value']
341
342 def start(options, config):
343     pass
344
345
346 if __name__ == '__main__':
347     main()