Skip to content

Commit

Permalink
[portstat]: return N/A if no counter info exists (sonic-net#29)
Browse files Browse the repository at this point in the history
Signed-off-by: Sihui Han <sihan@microsoft.com>
  • Loading branch information
sihuihan88 authored and Shuotian Cheng committed Apr 7, 2017
1 parent 52cff2a commit ea8ceba
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions scripts/portstat
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ class Portstat(object):
"""
Get the counters from specific table.
"""
fields = [0,0,0,0,0,0,0,0,0,0]
fields = ["0","0","0","0","0","0","0","0","0","0"]
for counter_name, pos in counter_bucket_dict.iteritems():
full_table_id = COUNTER_TABLE_PREFIX + table_id
fields[pos] += int(self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name))
counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name)
if counter_data is None:
fields[pos] = "N/A"
elif fields[pos] != "N/A":
fields[pos] = str(int(fields[pos]) + int(self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name)))
cntr = NStats._make(fields)
return cntr

Expand Down Expand Up @@ -118,7 +122,7 @@ class Portstat(object):
data.rx_ok, "N/A", "N/A", data.rx_err,
data.rx_drop, data.rx_ovr,
data.tx_ok, "N/A", "N/A", data.tx_err,
data.tx_drop, data.tx_err))
data.tx_drop, data.tx_ovr))

if use_json:
print self.table_as_json(table)
Expand All @@ -134,29 +138,38 @@ class Portstat(object):
"""
Calculate the diff.
"""
new, old = int(newstr), int(oldstr)
return '{:,}'.format(new - old)
if newstr == "N/A" or oldstr == "N/A":
return "N/A"
else:
new, old = int(newstr), int(oldstr)
return '{:,}'.format(new - old)

def ns_rate(newstr, oldstr, delta):
"""
Calculate the rate.
"""
rate = int(ns_diff(newstr, oldstr).replace(',',''))/delta
if rate > 1024*1024*10:
rate = "{:.2f}".format(rate/1024/1024)+' MB'
elif rate > 1024*10:
rate = "{:.2f}".format(rate/1024)+' KB'
if newstr == "N/A" or oldstr == "N/A":
return "N/A"
else:
rate = "{:.2f}".format(rate)+' B'
return rate+'/s'
rate = int(ns_diff(newstr, oldstr).replace(',',''))/delta
if rate > 1024*1024*10:
rate = "{:.2f}".format(rate/1024/1024)+' MB'
elif rate > 1024*10:
rate = "{:.2f}".format(rate/1024)+' KB'
else:
rate = "{:.2f}".format(rate)+' B'
return rate+'/s'

def ns_util(newstr, oldstr, delta):
"""
Calculate the util.
"""
rate = int(ns_diff(newstr, oldstr).replace(',',''))/delta
util = rate/(PORT_RATE*1024*1024*1024/8.0)*100
return "{:.2f}%".format(util)
if newstr == "N/A" or oldstr == "N/A":
return "N/A"
else:
rate = int(ns_diff(newstr, oldstr).replace(',',''))/delta
util = rate/(PORT_RATE*1024*1024*1024/8.0)*100
return "{:.2f}%".format(util)

table = []

Expand Down

0 comments on commit ea8ceba

Please sign in to comment.