Added bandwidth usage calculation for each of the port.

Auto metering changes to be followed.
parent 7cfa3db1
......@@ -20,7 +20,13 @@ class SimpleMonitor(simple_switch_13.SimpleSwitch13):
[MAIN_DISPATCHER, DEAD_DISPATCHER])
def _state_change_handler(self, ev):
datapath = ev.datapath
bwDBEntry = {'dpid':datapath.id}
bwDBEntry = {'dpid':datapath.id,
'usedbw':0,
'rxBytes':0,
'txBytes':0,
'pktError':0,
'timeStamp':0
}
if ev.state == MAIN_DISPATCHER:
if not datapath.id in self.datapaths:
self.logger.debug('register datapath: %016x', datapath.id)
......@@ -74,9 +80,9 @@ class SimpleMonitor(simple_switch_13.SimpleSwitch13):
'dpid': ev.msg.datapath.id,
'port':stat.port_no,
'usedbw':0,
'rxPkts':stat.rx_packets,
'txPkts':stat.tx_packets,
'rxBytes':stat.rx_bytes,
'txBytes':stat.tx_bytes,
'pktError':stat.tx_dropped,
'timeStamp':stat.duration_nsec
'timeStamp':stat.duration_sec
};
bwDB.updateBWDict(bwDbEntry);
......@@ -19,12 +19,12 @@ def createBWDBConnection():
uniqID INTEGER PRIMARY KEY AUTOINCREMENT,
dpid VARCHAR(255),
port INTEGER,
bandwidth INTEGER,
usedbw INTEGER,
rxPkts BIGINT,
txPkts BIGINT,
pktError BIGINT,
timeStamp DOUBLE PRECISION
bandwidth UNSIGNED INTEGER,
usedbw UNSIGNED INTEGER DEFAULT 0,
rxBytes UNSIGNED BIGINT DEFAULT 0,
txBytes UNSIGNED BIGINT DEFAULT 0,
pktError UNSIGNED BIGINT DEFAULT 0,
timeStamp UNSIGNED BIG INT
);
""");
......@@ -70,13 +70,13 @@ def checkDuplicateEntry(bwDBEntry):
return rc;
def getBWEntry(bwDBEntry):
print "getBWEntry query for flowTable"
print tapID
#print "getBWEntry query for flowTable"
#print tapID
conn = sql.connect(bwdb)
cursor = conn.cursor()
cursor.execute("""select uniqID,bandwidth, usedbw, rxPkts, txPkts,
cursor.execute("""select uniqID,bandwidth, usedbw, rxBytes, txBytes,
pktErro, timeStamp from bwTable where dpid=:dpid and
port=:port """,bwDBEntry);
......@@ -86,8 +86,8 @@ def getBWEntry(bwDBEntry):
rowDict= { 'uniqID' : row[0],
'bandwidth':row[1],
'usedbw':row[2],
'rxPkts':row[3],
'txPkts':row[4],
'rxBytes':row[3],
'txBytes':row[4],
'pktError':row[5],
'timeStamp':row[6]
}
......@@ -120,8 +120,8 @@ def deleteBWDict(bwDBEntry):
#assuming that the entries are always legit to avoid too much
#sanity checks..
def insertBWDBDict(bwDBEntry):
print "insertBWDBDict: print entry for bandwidth table insert query"
print bwDBEntry
#print "insertBWDBDict: print entry for bandwidth table insert query"
#print bwDBEntry
if checkDuplicateEntry(bwDBEntry) is True:
print "insertBWDBDict insertion failed[duplicate error]"
......@@ -133,21 +133,22 @@ def insertBWDBDict(bwDBEntry):
try:
cursor.execute("""
insert into bwTable( dpid, port, bandwidth)
values (:dpid, :port, :bandwidth)
insert into bwTable( dpid, port, bandwidth, usedbw,
rxBytes, txBytes, pktError, timeStamp)
values (:dpid, :port, :bandwidth, :usedbw, :rxBytes,
:txBytes, :pktError, :timeStamp)
""", bwDBEntry);
conn.commit();
rc = cursor.lastrowid
except sql.Error as e:
print "An error occured ",e.args[0];
cursor.close();
conn.close();
return rc;
rc = -1;
cursor.close();
conn.close();
return rc;
def dumpBWTable():
conn = sql.connect(bwdb)
cursor = conn.cursor()
......@@ -159,30 +160,67 @@ def dumpBWTable():
conn.close();
def updateBWDict(bwDBEntry):
print "updateBWDict: print entry for bandwidth table update query"
print bwDBEntry
#print "updateBWDict: print entry for bandwidth table update query"
#print bwDBEntry
conn = sql.connect(bwdb)
cursor = conn.cursor()
rc = None;
rc = True;
try:
cursor.execute("""
update bwTable set usedbw=:usedbw,rxPkts=:rxPkts,
txPkts=:txPkts, pktError=:pktError, timeStamp=:timeStamp
select txBytes, pktError, timeStamp, bandwidth
from bwTable
where dpid=:dpid and port=:port
""",bwDBEntry)
row = cursor.fetchone();
if (row is not None) and (row[2] != 0): #update the entry and recalculate bw used.
#used bw is for txBytes as we focus on tunnel
prevTxBytes = row[0];
pktError = row[1];
prevTimeStamp = row[2];
bandwidth = row[3];
txByte = bwDBEntry.get('txBytes');
timeStamp = bwDBEntry.get('timeStamp');
newBytes = txByte - prevTxBytes;
sec = timeStamp - prevTimeStamp;
try:
rate = newBytes/sec;
except ZeroDivisionError:
print "zero timestamp; set rate to 0"
rate = 0;
utilization = (rate * 100 / bandwidth) #in percentage
#print "calculated bytes, time and rate"
#print newBytes;
#print sec;
#print rate;
if utilization != 0:
print ('switch:', bwDBEntry.get('dpid'),
'port:' , bwDBEntry.get('port'),
'[usage]:',utilization)
bwDBEntry['usedbw']= utilization;
cursor.execute("""
update bwTable set usedbw=:usedbw, rxBytes=:rxBytes,
txBytes=:txBytes, pktError=:pktError, timeStamp=:timeStamp
where dpid=:dpid and port=:port""", bwDBEntry);
conn.commit();
#print "update done"
except sql.Error as e:
print "An error occured ",e.args[0];
cursor.close();
conn.close();
return False;
return True;
rc = False
cursor.close();
conn.close();
return rc;
def main():
bwDbEntry= {
'dpid': '000000001', 'port':3, 'bandwidth':10,
'usedbw':10, 'rxPkts':204034, 'txPkts':432432,
'usedbw':10, 'rxBytes':204034, 'txBytes':432432,
'pktError':5, 'timeStamp':12.4
}
#print bwDbEntry
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment