2020-11-06 11:00:19 -06:00
|
|
|
#!/usr/bin/env python3
|
2018-07-26 15:39:08 -05:00
|
|
|
|
|
|
|
import time
|
2022-07-10 21:01:10 -05:00
|
|
|
from swsscommon import swsscommon
|
2020-11-06 11:00:19 -06:00
|
|
|
|
2020-11-18 11:33:19 -06:00
|
|
|
# ALPHA defines the size of the window over which we calculate the average value. ALPHA is 2/(N+1) where N is the interval(window size)
|
2020-12-03 17:57:50 -06:00
|
|
|
# In this case we configure the window to be 10s. This way if we have a huge 1s spike in traffic,
|
2020-11-18 11:33:19 -06:00
|
|
|
# the average rate value will show a curve descending from the spike to the usual rate over approximately 10s.
|
|
|
|
DEFAULT_SMOOTH_INTERVAL = '10'
|
|
|
|
DEFAULT_ALPHA = '0.18'
|
|
|
|
|
2020-11-06 11:00:19 -06:00
|
|
|
|
2018-07-26 15:39:08 -05:00
|
|
|
def enable_counter_group(db, name):
|
2021-06-09 05:39:22 -05:00
|
|
|
entry_info = db.get_entry("FLEX_COUNTER_TABLE", name)
|
2018-07-26 15:39:08 -05:00
|
|
|
|
2021-06-09 05:39:22 -05:00
|
|
|
if not entry_info:
|
|
|
|
info = {}
|
|
|
|
info['FLEX_COUNTER_STATUS'] = 'enable'
|
|
|
|
db.mod_entry("FLEX_COUNTER_TABLE", name, info)
|
2021-09-01 23:17:36 -05:00
|
|
|
else:
|
|
|
|
entry_info.update({"FLEX_COUNTER_DELAY_STATUS":"false"})
|
|
|
|
db.mod_entry("FLEX_COUNTER_TABLE", name, entry_info)
|
2020-11-06 11:00:19 -06:00
|
|
|
|
2020-11-18 11:33:19 -06:00
|
|
|
def enable_rates():
|
2020-12-03 17:57:50 -06:00
|
|
|
# set the default interval for rates
|
2022-07-10 21:01:10 -05:00
|
|
|
counters_db = swsscommon.SonicV2Connector()
|
2020-11-18 11:33:19 -06:00
|
|
|
counters_db.connect('COUNTERS_DB')
|
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:PORT', 'PORT_SMOOTH_INTERVAL', DEFAULT_SMOOTH_INTERVAL)
|
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:PORT', 'PORT_ALPHA', DEFAULT_ALPHA)
|
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:RIF', 'RIF_SMOOTH_INTERVAL', DEFAULT_SMOOTH_INTERVAL)
|
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:RIF', 'RIF_ALPHA', DEFAULT_ALPHA)
|
2021-11-24 17:26:52 -06:00
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:TRAP', 'TRAP_SMOOTH_INTERVAL', DEFAULT_SMOOTH_INTERVAL)
|
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:TRAP', 'TRAP_ALPHA', DEFAULT_ALPHA)
|
2021-11-01 12:42:21 -05:00
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:TUNNEL', 'TUNNEL_SMOOTH_INTERVAL', DEFAULT_SMOOTH_INTERVAL)
|
|
|
|
counters_db.set('COUNTERS_DB', 'RATES:TUNNEL', 'TUNNEL_ALPHA', DEFAULT_ALPHA)
|
2020-11-18 11:33:19 -06:00
|
|
|
|
|
|
|
|
2018-07-26 15:39:08 -05:00
|
|
|
def enable_counters():
|
2022-07-10 21:01:10 -05:00
|
|
|
db = swsscommon.ConfigDBConnector()
|
2018-07-26 15:39:08 -05:00
|
|
|
db.connect()
|
2022-03-31 07:23:57 -05:00
|
|
|
default_enabled_counters = ['PORT', 'RIF', 'QUEUE', 'PFCWD', 'PG_WATERMARK', 'PG_DROP',
|
|
|
|
'QUEUE_WATERMARK', 'BUFFER_POOL_WATERMARK', 'PORT_BUFFER_DROP', 'ACL']
|
|
|
|
|
|
|
|
# Enable those default counters
|
|
|
|
for key in default_enabled_counters:
|
|
|
|
enable_counter_group(db, key)
|
|
|
|
|
|
|
|
# Set FLEX_COUNTER_DELAY_STATUS to false for those non-default counters
|
|
|
|
keys = db.get_keys('FLEX_COUNTER_TABLE')
|
|
|
|
for key in keys:
|
|
|
|
if key not in default_enabled_counters:
|
|
|
|
enable_counter_group(db, key)
|
2020-11-18 11:33:19 -06:00
|
|
|
enable_rates()
|
2018-07-26 15:39:08 -05:00
|
|
|
|
2020-11-06 11:00:19 -06:00
|
|
|
|
2018-07-26 15:39:08 -05:00
|
|
|
def get_uptime():
|
|
|
|
with open('/proc/uptime') as fp:
|
|
|
|
return float(fp.read().split(' ')[0])
|
|
|
|
|
2020-11-06 11:00:19 -06:00
|
|
|
|
2018-07-26 15:39:08 -05:00
|
|
|
def main():
|
|
|
|
# If the switch was just started (uptime less than 5 minutes),
|
|
|
|
# wait for 3 minutes and enable counters
|
|
|
|
# otherwise wait for 60 seconds and enable counters
|
|
|
|
uptime = get_uptime()
|
|
|
|
if uptime < 300:
|
|
|
|
time.sleep(180)
|
|
|
|
else:
|
|
|
|
time.sleep(60)
|
|
|
|
enable_counters()
|
|
|
|
|
2020-11-06 11:00:19 -06:00
|
|
|
|
2018-07-26 15:39:08 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|