Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues672 portstat support clear or stat one port #676

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 26 additions & 8 deletions scripts/portstat
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Portstat(object):
self.db.connect(self.db.COUNTERS_DB)
self.db.connect(self.db.APPL_DB)

def get_cnstat(self):
def get_cnstat(self, clearport):
"""
Get the counters info from database.
"""
Expand All @@ -97,6 +97,9 @@ class Portstat(object):
cnstat_dict['time'] = datetime.datetime.now()
if counter_port_name_map is None:
return cnstat_dict
if None != clearport and clearport in natsorted(counter_port_name_map):
cnstat_dict[clearport] = get_counters(counter_port_name_map[clearport])
return cnstat_dict
for port in natsorted(counter_port_name_map):
cnstat_dict[port] = get_counters(counter_port_name_map[port])
return cnstat_dict
Expand Down Expand Up @@ -132,7 +135,7 @@ class Portstat(object):
else:
return STATUS_NA

def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json=False, print_all=False):
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json=False, print_all=False, stat_intf=None):
"""
Print the difference between two cnstat results.
"""
Expand All @@ -152,6 +155,8 @@ class Portstat(object):
else:
old_cntr = NStats._make([0] * BUCKET_NUM)
port_speed = self.get_port_speed(key)
if stat_intf and stat_intf != key:
continue
if print_all:
table.append((key, self.get_port_state(key),
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
Expand Down Expand Up @@ -206,6 +211,8 @@ Examples:
portstat -r
portstat -a
portstat -p 20
portstat -c -P Ethernet12
portstat -i Ethernet12
""")

parser.add_argument('-c', '--clear', action='store_true', help='Copy & clear stats')
Expand All @@ -216,6 +223,8 @@ Examples:
parser.add_argument('-a', '--all', action='store_true', help='Display all the stats counters')
parser.add_argument('-t', '--tag', type=str, help='Save stats with name TAG', default=None)
parser.add_argument('-p', '--period', type=int, help='Display stats over a specified period (in seconds).', default=0)
parser.add_argument('-i', '--interface', type=str, help='Display port the stats counters.', default=None)
parser.add_argument('-P', '--port-clear', type=str, help='Clear stat of port.', default=None)
args = parser.parse_args()

save_fresh_stats = args.clear
Expand All @@ -227,6 +236,8 @@ Examples:
uid = str(os.getuid())
wait_time_in_seconds = args.period
print_all = args.all
stat_intf = args.interface
clear_port = args.port_clear

if tag_name is not None:
cnstat_file = uid + "-" + tag_name
Expand Down Expand Up @@ -260,7 +271,7 @@ Examples:
sys.exit(0)

portstat = Portstat()
cnstat_dict = portstat.get_cnstat()
cnstat_dict = portstat.get_cnstat(clear_port)

# Now decide what information to display
if raw_stats:
Expand All @@ -278,7 +289,14 @@ Examples:

if save_fresh_stats:
try:
pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'w'))
if clear_port == None or os.path.isfile(cnstat_fqn_file) == False:
pickle.dump(cnstat_dict, open(cnstat_fqn_file, 'w'))
else:
cnstat_dict_o = OrderedDict()
with open(cnstat_fqn_file, 'r') as f:
cnstat_dict_o = pickle.loads(f.read())
cnstat_dict_o[clear_port] = cnstat_dict[clear_port]
pickle.dump(cnstat_dict_o, open(cnstat_fqn_file, 'w'))
except IOError as e:
sys.exit(e.errno)
else:
Expand All @@ -291,21 +309,21 @@ Examples:
try:
cnstat_cached_dict = pickle.load(open(cnstat_fqn_file, 'r'))
print "Last cached time was " + str(cnstat_cached_dict.get('time'))
portstat.cnstat_diff_print(cnstat_dict, cnstat_cached_dict, use_json, print_all)
portstat.cnstat_diff_print(cnstat_dict, cnstat_cached_dict, use_json, print_all, stat_intf)
except IOError as e:
print e.errno, e
else:
if tag_name:
print "\nFile '%s' does not exist" % cnstat_fqn_file
print "Did you run 'portstat -c -t %s' to record the counters via tag %s?\n" % (tag_name, tag_name)
else:
portstat.cnstat_diff_print(cnstat_dict, {}, use_json, print_all)
portstat.cnstat_diff_print(cnstat_dict, {}, use_json, print_all, stat_intf)
else:
#wait for the specified time and then gather the new stats and output the difference.
time.sleep(wait_time_in_seconds)
print "The rates are calculated within %s seconds period" % wait_time_in_seconds
cnstat_new_dict = portstat.get_cnstat()
portstat.cnstat_diff_print(cnstat_new_dict, cnstat_dict, use_json, print_all)
cnstat_new_dict = portstat.get_cnstat(None)
portstat.cnstat_diff_print(cnstat_new_dict, cnstat_dict, use_json, print_all, stat_intf)

if __name__ == "__main__":
main()