def main(): logger = Logger(SYSLOG_IDENTIFIER) logger.set_min_log_priority_info() if len(sys.argv) != 3: raise Exception('Pass service and valid asic-id as arguments') service = sys.argv[1] args_asic_id = sys.argv[2] # Get num asics num_asics = multi_asic.get_num_asics() if num_asics == 0: logger.log_error( 'Detected no asics on this platform for service {}'.format( service)) sys.exit(1) # Connect to STATE_DB and subscribe to chassis-module table notifications state_db = daemon_base.db_connect("CHASSIS_STATE_DB") sel = swsscommon.Select() sst = swsscommon.SubscriberStateTable(state_db, CHASSIS_ASIC_INFO_TABLE) sel.addSelectable(sst) while True: (state, c) = sel.select(SELECT_TIMEOUT_MSECS) if state == swsscommon.Select.TIMEOUT: continue if state != swsscommon.Select.OBJECT: continue (asic_key, asic_op, asic_fvp) = sst.pop() asic_id = re.search(r'\d+$', asic_key) global_asic_id = asic_id.group(0) if asic_op == 'SET': asic_fvs = dict(asic_fvp) asic_name = asic_fvs.get('name') if asic_name is None: logger.log_info('Unable to get asic_name for asic{}'.format( global_asic_id)) continue if asic_name.startswith('FABRIC-CARD') is False: logger.log_info( 'Skipping module with asic_name {} for asic{}'.format( asic_name, global_asic_id)) continue if (global_asic_id == args_asic_id): logger.log_info( 'Detected asic{} is online'.format(global_asic_id)) sys.exit(0) elif asic_op == 'DEL': logger.log_info( 'Detected asic{} is offline'.format(global_asic_id)) sys.exit(1) else: continue
def main(): logger = Logger(SYSLOG_IDENTIFIER) logger.set_min_log_priority_info() if os.getuid() != 0: logger.log_error('Root required to clean up core files') return logger.log_info('Cleaning up core files') core_files = [f for f in os.listdir(CORE_FILE_DIR) if os.path.isfile(os.path.join(CORE_FILE_DIR, f))] core_files_by_process = defaultdict(list) for f in core_files: process = f.split('.')[0] curr_files = core_files_by_process[process] curr_files.append(f) if len(curr_files) > MAX_CORE_FILES: curr_files.sort(reverse = True, key = lambda x: datetime.utcfromtimestamp(int(x.split('.')[1]))) oldest_core = curr_files[MAX_CORE_FILES] logger.log_info('Deleting {}'.format(oldest_core)) try: os.remove(os.path.join(CORE_FILE_DIR, oldest_core)) except: logger.log_error('Unexpected error occured trying to delete {}'.format(oldest_core)) core_files_by_process[process] = curr_files[0:MAX_CORE_FILES] logger.log_info('Finished cleaning up core files')
SYSLOG_IDENTIFIER = 'snmp_yml_to_configdb.py' logger = Logger(SYSLOG_IDENTIFIER) logger.set_min_log_priority_info() snmp_comm_config_db = db.get_table('SNMP_COMMUNITY') snmp_config_db_communities = snmp_comm_config_db.keys() snmp_general_config_db = db.get_table('SNMP') snmp_general_keys = snmp_general_config_db.keys() full_snmp_comm_list = [ 'snmp_rocommunity', 'snmp_rocommunities', 'snmp_rwcommunity', 'snmp_rwcommunities' ] if not os.path.exists('/etc/sonic/snmp.yml'): logger.log_info('/etc/sonic/snmp.yml does not exist') sys.exit(1) with open('/etc/sonic/snmp.yml', 'r') as yaml_file: yaml_snmp_info = yaml.load(yaml_file, Loader=yaml.FullLoader) for comm_type in full_snmp_comm_list: if comm_type in yaml_snmp_info.keys(): if comm_type.startswith('snmp_rocommunities'): for community in yaml_snmp_info[comm_type]: if community not in snmp_config_db_communities: db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RO"}) elif comm_type.startswith('snmp_rocommunity'): community = yaml_snmp_info['snmp_rocommunity'] if community not in snmp_config_db_communities: db.set_entry('SNMP_COMMUNITY', community, {"TYPE": "RO"})