Пример #1
0
    def _test_config_file(self):
        """
        Configuration file containing information such as dsim, pdu and dataswitch ip's
        return: Dict
        """
        path, _none = os.path.split(__file__)
        path, _none = os.path.split(path)
        try:
            assert os.uname()[1].startswith('dbelab')
        except AssertionError:
            conf_path = 'config/test_conf_site.ini'
        else:
            conf_path = 'config/test_conf_lab.ini'

        config_file = os.path.join(path, conf_path)
        if os.path.isfile(config_file) or os.path.exists(config_file):
            try:
                config = parse_ini_file(config_file)
                return config
            except (IOError, ValueError, TypeError):
                errmsg = (
                    'Failed to read test config file %s, Test will exit' %
                    (config_file))
                LOGGER.exception(errmsg)
                return False
        else:
            LOGGER.error('Test config path: %s does not exist' % config_file)
            return False
Пример #2
0
 def dhost(self, program=False):
     if self._dhost is not None:
         return self._dhost
     else:
         if os.path.exists(self.config_filename):
             LOGGER.info(
                 'Retrieving dsim engine info from config file: %s' %
                 self.config_filename)
             self.corr_config = parse_ini_file(self.config_filename)
             self.dsim_conf = self.corr_config['dsimengine']
         elif self.instrument is not None:
             self.corr2ini_path = '/etc/corr/templates/{}'.format(
                 self.instrument)
             LOGGER.info(
                 'Setting CORR2INI system environment to point to %s' %
                 self.corr2ini_path)
             os.environ['CORR2INI'] = self.corr2ini_path
             self.corr_config = parse_ini_file(self.corr2ini_path)
             self.dsim_conf = self.corr_config['dsimengine']
         else:
             errmsg = (
                 'Could not retrieve dsim information from running config file in /etc/corr, '
                 'Perhaps, restart CBF manually and ensure dsim is running.\n'
             )
             LOGGER.error(errmsg)
             sys.exit(errmsg)
         try:
             dig_host = self.dsim_conf['host']
             self._dhost = None
             self._dhost = FpgaDsimHost(dig_host, config=self.dsim_conf)
         except Exception:
             errmsg = 'Digitiser Simulator failed to retrieve information'
             LOGGER.exception(errmsg)
             sys.exit(errmsg)
         else:
             # Check if D-eng is running else start it.
             if self._dhost.is_running():
                 LOGGER.info('D-Eng is already running.')
                 return self._dhost
Пример #3
0
    def initialise(self, config, name):
        """
        Setup and start sensors
        :param config: the config to use when making the instrument
        :param name: a name for the instrument
        :return:

        """
        try:
            _default_log_dir = '/var/log/corr'
            config_file_dict = parse_ini_file(config)
            log_file_dir = config_file_dict.get('FxCorrelator').get('log_file_dir', _default_log_dir)
            assert os.path.isdir(log_file_dir)
            
            start_time = str(time.time())
            ini_filename = os.path.basename(config)
            log_filename = '{}_{}_sensor_servlet.log'.format(ini_filename, start_time)

            self.instrument = fxcorrelator.FxCorrelator(
                                'dummy fx correlator for sensors', config_source=config,
                                mass_inform_func=self.mass_inform, getLogger=getKatcpLogger,
                                log_filename=log_filename, log_file_dir=log_file_dir)
            self.instrument.initialise(program=False, configure=False, require_epoch=False,
                                    mass_inform_func=self.mass_inform, getLogger=getKatcpLogger,
                                    log_filename=log_filename, log_file_dir=log_file_dir)
            
            #disable manually-issued sensor update informs (aka 'kcs' sensors):
            sensor_manager = sensors.SensorManager(self, self.instrument,kcs_sensors=False,
                                                    mass_inform_func=self.mass_inform,
                                                    log_filename=log_filename,
                                                    log_file_dir=log_file_dir)
            self.instrument.sensor_manager = sensor_manager
            sensors_periodic.setup_sensors(sensor_manager,enable_counters=False)

            # Function created to reassign all non-conforming log-handlers
            loggers_changed = reassign_log_handlers(mass_inform_func=self.mass_inform, 
                                                    log_filename=log_filename, 
                                                    log_file_dir=log_file_dir,
                                                    instrument_name=self.instrument.descriptor)
            
        except Exception as exc:
            stack_trace = traceback.format_exc()
            return self._log_stacktrace(stack_trace, 'Failed to initialise sensor_servlet.')
Пример #4
0
if 'CORR2INI' in os.environ.keys() and args.config == '':
    args.config = os.environ['CORR2INI']
if args.config != '':
    host_list = utils.parse_hosts(args.config, section='fengine')
else:
    host_list = []

try:
    hostname = host_list[int(args.host)]
    logging.info('Got hostname %s from config file.' % hostname)
except ValueError:
    hostname = args.host

# read the config
config = utils.parse_ini_file(args.config)

EXPECTED_FREQS = int(config['fengine']['n_chans'])

required_snaps = ['snap_quant0_ss', 'snap_quant1_ss']

# process the range argument
plotrange = [int(a) for a in args.range.split(',')]
if plotrange[0] == -1:
    plotrange = (0, EXPECTED_FREQS)
if (plotrange[1] != -1) and (plotrange[1] <= plotrange[0]):
    raise RuntimeError('Plot range of %s is incorrect.' % str(plotrange))
plotrange = (int(plotrange[0]), int(plotrange[1]))


def exit_gracefully(_, __):
Пример #5
0
    def __init__(self, config_info, conn_retry=10, console_log_level=logging.ERROR,
                 file_log_level=logging.INFO):
        """
            PowerLogger reads PDU IP addresses from a config file and starts logging
            current and power from each PDU. Values are written to a CSV file. This
            class is threaded and must be started with instance.start()

            params:
                config_info: Dictionary parsed with corr2.utils.parse_ini_file or
                             a config file.
                conn_retry:  Number of connection attempts for initial connection
                             and if contact is lost during logging to PDUs
                console_log_level: Log level for print to std_out
                file_log_level: Log level for logging to file
        """

        # **************************************************************************
        #
        # Add a file handler to log to file and a console handler to print out
        # debug messages. The console handler is added first, to print messages
        # to console change log level for the console handler.
        # logger.handlers[0].setLevel(logging.DEBUG)
        #
        # **************************************************************************
        # create logger
        self.logger = logging.getLogger('power_logger')
        self.logger.setLevel(logging.INFO)
        # create a file handler
        file_handler = logging.FileHandler('power_logger.log')
        file_handler.setLevel(file_log_level)
        # create console handler with a higher log level
        console_handler = logging.StreamHandler()
        console_handler.setLevel(console_log_level)
        # create formatter and add it to the handlers
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_handler.setFormatter(formatter)
        console_handler.setFormatter(formatter)
        # add the handlers to the logger
        self.logger.addHandler(console_handler)
        self.logger.addHandler(file_handler)

        arb = '1234567890123456'

        threading.Thread.__init__(self)
        if isinstance(config_info, dict):
            test_conf = config_info
        elif os.path.exists(config_info):
            try:
                test_conf = parse_ini_file(config_info)
            except (IOError, ValueError, TypeError):
                errmsg = ('Failed to read test config file, Test will exit'
                          '\n\t File:%s Line:%s' % (
                              getframeinfo(currentframe()).filename.split('/')[-1],
                              getframeinfo(currentframe()).lineno))
                self.logger.error(errmsg)
                raise
        else:
            raise IOError
        pdu_names = test_conf['pdu_hosts']['pdus'].split(',')
        pdu_names = [x.replace(' ', '') for x in pdu_names]
        pdu_host_domain = test_conf['pdu_hosts']['pdu_host_domain']
        pdu_hosts = [x + '.' + pdu_host_domain for x in pdu_names]
        self._pdu_hosts = [x.replace(' ', '') for x in pdu_hosts]
        self._pdu_port = test_conf['pdu_hosts']['telnet_port']
        pdu_username = test_conf['pdu_hosts']['username']
        self._pdu_username = decode_passwd(pdu_username, arb)
        pdu_password = test_conf['pdu_hosts']['passwd']
        self._pdu_password = decode_passwd(pdu_password, arb)
        self._stop = threading.Event()
        self._conn_retry = conn_retry
        self.start_timestamp = None
        self.log_file_name = 'pdu_log.csv'
        self.logger.info('PDUs logged: %s' % (pdu_names))
Пример #6
0
if args.log_level != '':
    import logging
    log_level = args.log_level.strip()
    try:
        logging.basicConfig(level=eval('logging. %s' % log_level))
    except AttributeError:
        raise RuntimeError('No such log level: %s' % log_level)

configfile = args.config
if 'CORR2INI' in os.environ.keys() and configfile == '':
    configfile = os.environ['CORR2INI']
if configfile == '':
    raise RuntimeError('No good carrying on without a config file')

configd = utils.parse_ini_file(configfile)
baselines = utils.baselines_from_config(config=configd)
fhosts = utils.hosts_from_config(config=configd, section='fengine')


def baselines_from_source(source):
    rv = {}
    for ctr, baseline in enumerate(baselines):
        if source == baseline[0] or source == baseline[1]:
            rv[ctr] = baseline
    return rv


# host name
if args.host != '':
    host = args.host
Пример #7
0
            '-f', '--frequency', type=float, default=0,
            help='Dsim frequency')
    parser.add_argument(
            '-w', '--cw_scale', type=float, default=0.1,
            help='Constant wave output scale')
    parser.add_argument(
            '-n', '--noise_scale', type=float, default=0.05,
            help='Noise output scale')

    args = parser.parse_args()
    cw_scale = args.cw_scale
    n_scale = args.noise_scale
    freq = args.frequency

    #Connect to DSIM
    corr_conf = utils.parse_ini_file(args.config, ['dsimengine'])
    dsim_conf = corr_conf['dsimengine']
    dig_host = dsim_conf['host']
    dsim_fpg = dsim_conf['bitstream']
    dhost = FpgaDsimHost(dig_host, config=dsim_conf)
    if dig_host.lower().find('skarab') != -1:
        dhost.get_system_information(filename=dsim_fpg)
    else:
        dhost.get_system_information()


    #Clear all noise sources
    sources_names = dhost.noise_sources.names()
    for source in sources_names:
        try:
            noise_source = getattr(dhost.noise_sources, '{}'.format(source))
Пример #8
0
def SpeadRx(config_file, rx_port, dsim_start, capture_start, debug, verbose):
    """
    Receive data from Correlator and play
    """
    if config_file is None:
        sys.exit("Usage: %s --help" % __file__)

    try:
        assert verbose
        _level = 'DEBUG'
        logging.basicConfig(
            level=logging.DEBUG,
            format=
            '%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(pathname)s : '
            '%(lineno)d - %(message)s')
        corr_rx_logger.setLevel(logging.DEBUG)
        spead2_logger.setLevel(logging.DEBUG)
        logging.debug('DEBUG MODE ENABLED')
    except:
        _level = 'INFO'
        logging.basicConfig(
            level=logging.INFO,
            format=
            '%(asctime)s - %(name)s - %(levelname)s - %(module)s - %(pathname)s : '
            '%(lineno)d - %(message)s')
        corr_rx_logger.setLevel(logging.INFO)
        spead2_logger.setLevel(logging.INFO)
    finally:
        logger = logging.getLogger(__name__)
        coloredlogs.install(level=_level, logger=logger)

    if dsim_start and config_file:
        corr_conf = utils.parse_ini_file(config_file, ['dsimengine'])
        dsim_conf = corr_conf['dsimengine']
        dhost = FpgaDsimHost(dsim_conf['host'], config=dsim_conf)
        try:
            assert dhost.is_running()
            dhost.get_system_information()
        except:
            logger.error('DEngine Not Running!')

    try:
        assert capture_start
    except AssertionError:

        @atexit.register
        def Cleanup():
            logger.info('baseline-correlation-products capture stopped!')
            receiver.stop()
            receiver.join()
            logger.info('Receiver stopped.')
            fab.local(
                "kcpcmd -t 60 -s localhost:$(kcpcmd array-list | grep -a array-list | cut -f3 -d ' ' ) capture-stop baseline-correlation-products"
            )
    else:
        logger.info('baseline-correlation-products capture started!')
        fab.local(
            "kcpcmd -t 60 -s localhost:$(kcpcmd array-list | grep -a array-list | cut -f3 -d ' ' ) capture-start baseline-correlation-products"
        )

    try:
        receiver = CorrRx(port=rx_port, queue_size=5)
        _multicast_ips = corr_conf['xengine'].get(
            'multicast_interface_address', '239.100.0.1')
        # import IPython; IPython.embed(header='Python Debugger')
    except Exception as ex:
        template = "An exception of type {0} occured while trying to instantiate receiver. Arguments:\n{1!r}"
        message = template.format(type(ex), ex.args)
        logger.info(message)
    else:
        logger.info('Waiting for receiver to report running')
        receiver.daemon = True
        receiver.start(timeout=10)
        if receiver.running_event.wait(timeout=10):
            logger.info('Receiver ready')
        else:
            msg = 'Receiver not ready'
            logger.info(msg)
            raise RuntimeError(msg)
        try:
            raw_input('Press Enter get clean dump')
            dump = receiver.get_clean_dump()
        except KeyboardInterrupt:
            logger.info('Keyboard interrupt')
        except Exception:
            raise
        else:
            logger.info('Dump received')
        if debug:
            corr_rx_logger.setLevel(logging.FATAL)
            spead2_logger.setLevel(logging.FATAL)
            import IPython
            IPython.embed(header='Python Debugger')
Пример #9
0
    log_level = args.log_level.strip()
    try:
        logging.basicConfig(level=eval('logging.%s' % log_level))
    except AttributeError:
        raise RuntimeError('No such log level: %s' % log_level)

if args.config:
    config_filename = args.config
else:
    try:
        config_filename = os.environ['CORR2INI']
    except KeyError:
        raise RuntimeError(
            'No config file speficied and environment var "CORR2INI" not defined')

corr_conf = utils.parse_ini_file(config_filename, ['dsimengine'])
dsim_conf = corr_conf['dsimengine']
dig_host = dsim_conf['host']

dhost = FpgaDsimHost(dig_host, config=dsim_conf)
print 'Connected to %s.' % dhost.host

if args.start and args.stop:
    raise RuntimeError('Start and stop? You must be crazy!')

something_happened = False
if args.program:
    dhost.initialise()
    something_happened = True
else:
    dhost.get_system_information()