Example #1
0
def pseudo_main():
    """Basically the main(). Did it this way so it can easily be used as a standalone module or called from another.

    :return: Exit code. See exist codes in brcddb.brcddb_common
    :rtype: int
    """
    global _generic_table_add, _key_conv_tbl, _control_tables

    # Get and validate the user inputs.
    bf, cf, rf, s_flag, log, nl = parse_args()
    if s_flag:
        brcdapi_log.set_suppress_all()
    if not nl:
        brcdapi_log.open_log(log)
    rf = brcdapi_file.full_file_name(rf, '.xlsx')
    bf = brcdapi_file.full_file_name(bf, '.json')
    cf = brcdapi_file.full_file_name(cf, '.json')
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ml.append(':START: Compare Report:')
    ml.append('    Base file:    ' + bf)
    ml.append('    Compare file: ' + cf)
    ml.append('    Report file:  ' + rf)
    brcdapi_log.log(ml, True)

    # Read the projects to compare and build the cross references
    ml = list()
    b_proj_obj = brcddb_project.read_from(bf)
    c_proj_obj = brcddb_project.read_from(cf)
    if b_proj_obj is None:
        ml.append('Missing or invalid base project.')
    if c_proj_obj is None:
        ml.append('Missing or invalid compare project.')
    if len(ml) > 0:
        brcdapi_log.log(ml, True)
        return brcddb_common.EXIT_STATUS_ERROR
    brcddb_project.build_xref(b_proj_obj)
    brcddb_project.build_xref(c_proj_obj)

    # Build out the key conversion tables. Used in _format_disp()
    for k, v in brcddb_rt.Port.port_display_tbl.items():
        if k[0] != '_':  # Make sure it's not a custom key
            _key_conv_tbl.update({'_port_objs/s/p/' + k: v.get('d')})
    for k, v in brcddb_rt.Login.login_display_tbl.items():
        if k[0] != '_':  # Make sure it's not a custom key
            _key_conv_tbl.update({k: v.get('d')})
    for table_obj in _generic_table_add:
        for k, v in table_obj.items():
            if k[0] != '_':  # Make sure it's not a custom key
                if isinstance(v, dict):
                    for k1, v1 in v.items():
                        _key_conv_tbl.update({k + '/' + k1: v1})
                else:
                    _key_conv_tbl.update({k: v})

    # Compare the two projects
    brcdapi_log.log('Please wait. The comparison may take several seconds', True)
    c, compare_obj = brcddb_compare.compare(b_proj_obj, c_proj_obj, None, _control_tables)
    _new_report(c, b_proj_obj, c_proj_obj, _project_scrub(compare_obj), rf)
    return brcddb_common.EXIT_STATUS_OK
Example #2
0
def pseudo_main():
    """Basically the main(). Did it this way so it can easily be used as a standalone module or called from another.

    :return: Exit code. See exist codes in brcddb.brcddb_common
    :rtype: int
    """
    ip, user_id, pw, outf, sec, s_flag, vd, c_file, fid, log, nl = parse_args()
    if vd:
        brcdapi_rest.verbose_debug = True
    if s_flag:
        brcdapi_log.set_suppress_all()
    if not nl:
        brcdapi_log.open_log(log)
    if sec is None:
        sec = 'none'
    fid_l = None if fid is None else fid.split(',')
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ml.append('IP:          ' + brcdapi_util.mask_ip_addr(ip, True))
    ml.append('ID:          ' + user_id)
    ml.append('security:    ' + sec)
    ml.append('Output file: ' + outf)
    ml.append('KPI file:    ' + str(c_file))
    ml.append('FID List:    ' + str(fid))
    brcdapi_log.log(ml, True)
    outf = brcdapi_file.full_file_name(outf, '.json')

    # Create project
    proj_obj = brcddb_project.new(
        "Captured_data",
        datetime.datetime.now().strftime('%d %b %Y %H:%M:%S'))
    proj_obj.s_python_version(sys.version)
    proj_obj.s_description("This is a test")

    # Login
    session = api_int.login(user_id, pw, ip, sec, proj_obj)
    if brcdapi_auth.is_error(session):
        return brcddb_common.EXIT_STATUS_API_ERROR

    # Collect the data
    try:
        api_int.get_batch(session, proj_obj, _kpi_list(session, c_file), fid_l)
    except BaseException as e:
        brcdapi_log.exception(
            'Programming error encountered. Exception is: ' + str(e), True)

    # Logout
    obj = brcdapi_rest.logout(session)
    if brcdapi_auth.is_error(obj):
        brcdapi_log.log(brcdapi_auth.formatted_error_msg(obj), True)

    # Dump the database to a file
    if _WRITE:
        brcdapi_log.log('Saving project to: ' + outf, True)
        plain_copy = dict()
        brcddb_copy.brcddb_to_plain_copy(proj_obj, plain_copy)
        brcdapi_file.write_dump(plain_copy, outf)
        brcdapi_log.log('Save complete', True)

    return proj_obj.r_exit_code()
def combine_main():
    """Basically the main(). Did it this way so it can easily be used as a standalone module or called from another.

    :return: Exit code
    :rtype: int
    """
    global _DEBUG

    # Get and validate user input
    inf, outf, s_flag, log, nl = parse_args()
    if s_flag:
        brcdapi_log.set_suppress_all()
    if not nl:
        brcdapi_log.open_log(log)
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ml.append('Directory, -i:   ' + inf)
    ml.append('Output file, -o: ' + outf)
    brcdapi_log.log(ml, True)

    # Create project
    proj_obj = brcddb_project.new(
        inf,
        datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))
    proj_obj.s_python_version(sys.version)
    proj_obj.s_description('Captured data from ' + inf)

    # Get a list of files - Filter out directories is just to protect the user. It shouldn't be necessary.
    outf = brcdapi_file.full_file_name(outf, '.json')
    files = brcdapi_file.read_directory(inf)
    if outf in files:
        brcdapi_log.log(
            'Combined output file, ' + outf + ', already exists in: ' + inf +
            '. Processing halted', True)
        proj_obj.s_error_flag()
    else:
        x = len('.json')
        for file in [
                f for f in files
                if len(f) > x and f.lower()[len(f) - x:] == '.json'
        ]:
            brcdapi_log.log('Processing file: ' + file, True)
            obj = brcdapi_file.read_dump(inf + '/' + file)
            brcddb_copy.plain_copy_to_brcddb(obj, proj_obj)

        # Now save the combined file
        plain_copy = dict()
        brcddb_copy.brcddb_to_plain_copy(proj_obj, plain_copy)
        try:
            brcdapi_file.write_dump(plain_copy, inf + '/' + outf)
        except FileNotFoundError:
            brcdapi_log.log(['', 'Folder not found: ' + inf, ''], True)

    return brcddb_common.EXIT_STATUS_OK
Example #4
0
def _get_input():
    """Retrieves the command line input, reads the input Workbook, and validates the input

    :return ec: Error code from brcddb.brcddb_common
    :rtype ec: int
    :return sl: List of switches to poll as read from the input Workbook
    :rtype sl: list
    :return pl: List of project files to combine
    :rtype pl: list
    :return cfg_name: Name of zone configuration file to create
    :rtype cfg_name: str, None
    :return a_cfg: Activation flag. If True, activate the zone configuration specified by cfg_name
    :rtype a_cfg: bool
    :return t_flag: Test flag. If True, test only. Do not make any changes
    :rtype t_flag: bool
    :return scan_flag: Scan flag. If True, scan files and switches for fabric information
    :rtype scan_flag: bool
    :return addl_parms: Additional parameters (logging and debug flags) to pass to multi_capture.py
    :rtype addl_parms: list
    """
    global _DEBUG, __version__

    # Initialize the return variables
    ec = brcddb_common.EXIT_STATUS_OK
    addl_parms = list()

    # Get and validate the user input
    ip, user_id, pw, sec, fid, cfile, wwn, a_flag, scan_flag, d_flag, s_flag, log, nl = parse_args(
    )
    if s_flag:
        addl_parms.append('-sup')
        brcdapi_log.set_suppress_all()
    if nl:
        addl_parms.append('-nl')
    else:
        brcdapi_log.open_log(log)
    if log is not None:
        addl_parms.extend(['-log', log])
    if d_flag:
        addl_parms.append('-d')
        brcdapi_rest.verbose_debug = True
    buf = ''
    if isinstance(fid, int):
        buf = ' INVALID. Must be an integer in the range of 1-128' if fid < 0 or fid > 128 else ''
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ml.append('zone_merge.py version: ' + __version__)
    ml.append('IP address:            ' + brcdapi_util.mask_ip_addr(ip))
    ml.append('ID:                    ' + str(user_id))
    ml.append('HTTPS:                 ' + str(sec))
    ml.append('FID:                   ' + str(fid) + buf)
    ml.append('Input file:            ' + cfile)
    ml.append('WWN:                   ' + str(wwn))
    ml.append('Activate zone cfg:     ' + str(a_flag))
    ml.append('Scan:                  ' + str(scan_flag))
    brcdapi_log.log(ml, True)
    if len(buf) > 0:
        return brcddb_common.EXIT_STATUS_INPUT_ERROR
    if sec is None:
        sec = 'none'
    ml = list()
    if not scan_flag:
        if ip is None:
            ml.append('  IP address (-ip)')
        if user_id is None:
            ml.append('  User ID (-id)')
        if pw is None:
            ml.append('  Password (-pw)')
        if fid is None:
            ml.append('  FID (-fid)')
        if len(buf) > 0:
            ml.append('  FID ' + buf)
        if wwn is None:
            ml.append('  Fabric WWN (-wwn)')
        if len(ml) > 0:
            ml.insert(0, 'Missing the following required parameters:')
            ml.append('Use the -h option for additional help.')
            brcdapi_log.log(ml, True)
            ec = brcddb_common.EXIT_STATUS_INPUT_ERROR

    return ec, ip, user_id, pw, sec, scan_flag, fid, cfile, wwn, a_flag, scan_flag, addl_parms
Example #5
0
def _get_input():
    """Parses the module load command line

    :return ip_addr: IP address
    :rtype ip_addr: str
    :return id: User ID
    :rtype id: str
    :return pw: Password
    :rtype pw: str
    :return http_sec: Type of HTTP security
    :rtype http_sec: str
    :return suppress_flag: True - suppress all print to STD_OUT
    :rtype suppress_flag: bool
    :return fid: Fabric ID in chassis specified by -ip where the zoning information is to copied to.
    :rtype fid: int
    :return suppress_flag: True - suppress all print to STD_OUT
    :rtype suppress_flag: bool
    :return report_name: Name of Excel report.
    :rtype report_name: str
    """
    global _DEBUG, _DEBUG_sup, _DEBUG_r, _DEBUG_i, _DEBUG_gp, _DEBUG_gs, _DEBUG_gt, _DEBUG_log, _DEBUG_nl

    if _DEBUG:
        args_sup, args_r, args_i, args_gp, args_gs, args_gt, args_log, args_nl =\
            _DEBUG_sup, _DEBUG_r, _DEBUG_i, _DEBUG_gp, _DEBUG_gs, _DEBUG_gt, _DEBUG_log, _DEBUG_nl

    else:
        buf = 'Create Excel Workbook from statistics gathered with stats_c.py. WARNING: Graphing parameters are not ' \
              'yet implemented. This module will only create the Excel Workbook with tables. You will then need to ' \
              'create your own graphs manually.'
        parser = argparse.ArgumentParser(description=buf)
        buf = 'Suppress all library generated output to STD_IO except the exit code. Useful with batch processing'
        parser.add_argument('-sup',
                            help=buf,
                            action='store_true',
                            required=False)
        parser.add_argument(
            '-r',
            help='Required. Report name. ".xlsx" is automatically appended.',
            required=True)
        buf = 'Required. Name of data input file. This must be the output file, -o, from stats_c.py. ".json" is '\
              'automatically appended'
        parser.add_argument('-i', help=buf, required=True)
        buf = 'Optional. Creates a worksheet with a graph of one or more statistical counters for a port. Useful for ' \
              'analyzing performance for a specific port. Parameters that follow are the port number followed by any '\
              'of the statistical parameters in brocade-interface/fibrechannel-statistics. Only the final leaf should '\
              'be used. All parameters must be separated by a comma. Separate multiple graphs with a semi-colon. '\
              'Graphs are plotted against the sample time. For example, to graph the Tx and Rx frames for port 3/14: '\
              '"-gp 3/14,in-frames,out-frames"'
        parser.add_argument('-gp', help=buf, required=False)
        buf = 'Optional. Similar to the -gp option. Creates a worksheet with a graph for a specific statistical ' \
              'counter for one or more ports. Parameters that follow are the statistical counter followed by the '\
              'ports. To automatically pick the ports with the highest peak counts in any of the individual samples, '\
              'enter top-x instead of specific ports. To automatically pick the ports with highest accumulated count '\
              'over all samples, enter avg-x. For example, to graph the 5 ports with the highest accumulated BB '\
              'credit zero counters: -gs bb-credit-zero,avg-5.'
        parser.add_argument('-gs', help=buf, required=False)
        buf = 'Optional. Specifies the graph type. Valid chart types are: ' + ', '.join(
            report_graph.chart_types) + '. '
        buf += 'Default: line.'
        parser.add_argument('-gt', help=buf, required=False)
        buf = '(Optional) Directory where log file is to be created. Default is to use the current directory. The log '\
              'file name will always be "Log_xxxx" where xxxx is a time and date stamp.'
        parser.add_argument(
            '-log',
            help=buf,
            required=False,
        )
        buf = '(Optional) No parameters. When set, a log file is not created. The default is to create a log file.'
        parser.add_argument('-nl',
                            help=buf,
                            action='store_true',
                            required=False)
        args = parser.parse_args()
        args_sup, args_r, args_i, args_gp, args_gs, args_gt, args_log, args_nl =\
            args.sup, args.r, args.i, args.gp, args.gs, args.gt, args.log, args.nl

    # Set up log file and debug
    if args_sup:
        brcdapi_log.set_suppress_all()
    if not args_nl:
        brcdapi_log.open_log(args_log)

    return brcddb_file.full_file_name(args_r, '.xlsx'),\
        brcddb_file.full_file_name(args_i, '.json'), \
        args_gp, \
        args_gs, \
        args_gt
Example #6
0
def psuedo_main():
    """Basically the main(). Did it this way so it can easily be used as a standalone module or called from another.

    :return: Exit code. See exist codes in brcddb.brcddb_common
    :rtype: int
    """
    global _DEBUG

    addl_parms_all, addl_parms_capture, addl_parms_report = list(), list(
    ), list()

    # Get and parse the input data
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ml.append(os.path.basename(__file__) + ' version: ' + __version__)
    in_file, folder, sfp, iocp, report_flag, kpi_file, s_flag, vd, log, nl = parse_args(
    )
    if kpi_file is not None:
        addl_parms_capture.extend(['-c', kpi_file])
    if vd:
        brcdapi_rest.verbose_debug = True
        addl_parms_capture.append('-d')
    if s_flag:
        brcdapi_log.set_suppress_all()
        addl_parms_all.append('-sup')
    if nl:
        addl_parms_all.append('-nl')
    else:
        brcdapi_log.open_log(log)
        if log is not None:
            addl_parms_all.extend(['-log', log])
    if iocp is not None:
        addl_parms_report.extend(['-iocp', iocp])
    ml.append('Input file:    ' + in_file)
    date_str = '_' + datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
    if folder is None:
        folder = '_capture' + date_str
        ml.append('Output Folder: (Automatic) ' + folder)
    else:
        ml.append('Output Folder: ' + folder)
    ml.append('SFP:           ' + str(sfp))
    ml.append('IOCP:          ' + str(iocp))
    ml.append('Report:        ' + str(report_flag))
    ml.append('KPI File:      ' + str(kpi_file))
    ml.append('Suppress:      ' + str(s_flag))
    ml.append('Verbose Debug: ' + str(vd))
    brcdapi_log.log(ml, True)

    # Read the file with login credentials and perform some basic validation
    ml, switch_parms = list(), list()
    if sfp is not None:
        addl_parms_report.extend(['-sfp', sfp])
    file = brcdapi_file.full_file_name(in_file, '.xlsx')
    try:
        for d in excel_util.parse_parameters(sheet_name='parameters',
                                             hdr_row=0,
                                             wb_name=file)['content']:
            buf = brcdapi_file.full_file_name(
                d['name'].split('/').pop().split('\\').pop(),
                '.json')  # Just file name
            switch_parms.append([
                '-id', d['user_id'], '-pw', d['pw'], '-ip', d['ip_addr'], '-s',
                'none' if d['security'] is None else d['security'], '-f',
                folder + '/' + buf
            ])
    except FileNotFoundError:
        ml.extend(['', file + ' not found.'])

    # Create the folder
    try:
        os.mkdir(folder)
    except FileExistsError:
        ml.extend('Folder ' + folder + ' already exists.')
    except FileNotFoundError:
        ml.extend(['', folder + ' contains a path that does not exist.'])
    if len(ml) > 0:
        brcdapi_log.log(ml, True)
        return brcddb_common.EXIT_STATUS_INPUT_ERROR

    # Kick off all the data captures
    pid_l = list()
    for temp_l in switch_parms:
        params = ['python.exe', 'capture.py'
                  ] + temp_l + addl_parms_capture + addl_parms_all
        if _DEBUG:
            brcdapi_log.log(' '.join(params), True)
        pid_l.append(subprocess.Popen(params))

    # Below waits for all processes to complete before generating the report.
    pid_done = [p.wait() for p in pid_l]
    for i in range(0, len(pid_done)):
        brcdapi_log.log(
            'Completed switch capture at index ' + str(i) +
            '. Ending status: ' + str(pid_done[i]), True)

    # Combine the captured data
    brcdapi_log.log('Combining captured data. This may take several seconds',
                    True)
    params = ['python.exe', 'combine.py', '-i', folder, '-o', 'combined.json'
              ] + addl_parms_all
    if _DEBUG:
        brcdapi_log.log('DEBUG: ' + ' '.join(params), True)
    ec = subprocess.Popen(params).wait()
    brcdapi_log.log('Combine completed with status: ' + str(ec), True)

    # Generate the report
    if report_flag and ec == brcddb_common.EXIT_STATUS_OK:
        brcdapi_log.log('Data collection complete. Generating report.', True)
        buf = folder + '/report_' + date_str + '.xlsx'
        params = [
            'python.exe', 'report.py', '-i', folder + '/combined.json', '-o',
            buf
        ]
        params.extend(addl_parms_report + addl_parms_all)
        if _DEBUG:
            brcdapi_log.log('DEBUG: ' + ' '.join(params), True)
        ec = subprocess.Popen(params).wait()

    return ec
Example #7
0
def pseudo_main():
    """Basically the main().

    :return: Exit code
    :rtype: int
    """
    global _DEBUG, _sfp_monitoring_system

    # Get and validate the command line input
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ip, user_id, pw, sec, fid, new_policy, file, mp, enable_flag, s_flag, vd, log, nl = parse_args(
    )
    if vd:
        brcdapi_rest.verbose_debug = True
    if not nl:
        brcdapi_log.open_log(log)
    if s_flag:
        brcdapi_log.set_suppress_all()
    fid = int(fid)
    if sec is None:
        sec = 'none'
    ml.extend([
        'FID:                 ' + str(fid),
        'New policy:          ' + new_policy,
        'SFP rules file:      ' + str(file),
        'Policy to clone:     ' + 'Active' if mp is None else mp,
        'Activate new policy: ' + 'Yes' if enable_flag else 'No',
        'The \'User Warning: Data Validation ...\' can be ignored.',
    ])
    brcdapi_log.log(ml, True)

    # Login
    session = api_int.login(user_id, pw, ip, sec)
    if brcdapi_auth.is_error(
            session):  # api_int.login() logs detailed error message
        return brcddb_common.EXIT_STATUS_API_ERROR

    # try/except used during development to ensure logout due to programming errors.
    ec = brcddb_common.EXIT_STATUS_ERROR  # Return code normally gets overwritten
    try:
        if file is None:
            sfp_rules = list()
        else:
            file = brcdapi_file.full_file_name(file, '.xlsx')
            brcdapi_log.log(
                'Adding rules can take up to a minute. Please be patient.',
                True)
            _sfp_monitoring_system = [
                'CURRENT', 'RXP', 'SFP_TEMP', 'TXP', 'VOLTAGE'
            ]
            sfp_rules = report_utils.parse_sfp_file_for_rules(
                file, _get_groups(session, fid))
        if sfp_rules is None:
            brcdapi_log.log('Error opening or reading ' + file, True)
        else:
            ec = _modify_maps(session, fid, sfp_rules, new_policy, mp,
                              enable_flag)
    except BaseException as e:
        brcdapi_log.log(
            'Programming error encountered. Exception is: ' + str(e), True)

    obj = brcdapi_rest.logout(session)
    if brcdapi_auth.is_error(obj):
        brcdapi_log.log(
            'Logout failed. API response:\n' +
            brcdapi_auth.formatted_error_msg(obj), True)
        ec = brcddb_common.EXIT_STATUS_API_ERROR

    return ec
Example #8
0
def pseudo_main():
    """Basically the main().

    :return: Exit code
    :rtype: int
    """
    global _DEBUG

    # Get and validate command line input.
    ec = brcddb_common.EXIT_STATUS_OK
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ip, user_id, pw, sec, file, force, s_flag, echo, vd, log, nl = parse_args()
    if vd:
        brcdapi_rest.verbose_debug = True
    if s_flag:
        brcdapi_log.set_suppress_all()
    if not nl:
        brcdapi_log.open_log(log)
    if sec is None:
        sec = 'none'
    file = brcdapi_file.full_file_name(file, '.xlsx')
    if ip is not None:
        if user_id is None:
            ml.append('Missing user ID, -id')
            ec = brcddb_common.EXIT_STATUS_INPUT_ERROR
        if pw is None:
            ml.append('Missing password, -pw')
            ec = brcddb_common.EXIT_STATUS_INPUT_ERROR
    ml.append('File:       ' + file)
    ml.append(
        'IP address: ' +
        brcdapi_util.mask_ip_addr(ip) if isinstance(ip, str) else str(ip))
    ml.append('ID:         ' + str(user_id))
    ml.append('sec:        ' + sec)
    if len(ml) > 0:
        brcdapi_log.log(ml, True)
    if ec != brcddb_common.EXIT_STATUS_OK:
        return ec
    echo = False if echo is None else echo

    # Read in the Workbook, generate the portaddress --bind commands, and configure the switch(es)
    switch_d_list = [
        switch_d for switch_d in report_utils.parse_switch_file(file).values()
    ]
    session = proj_obj = None

    try:
        for switch_d in switch_d_list:
            switch_d.update(err_msgs=list())

            # Create the bind commands
            if switch_d['bind']:
                _bind_commands(switch_d)
                cli_l = switch_d['bind_commands'].copy()
                i = 0
                while i < len(cli_l):
                    cli_l.insert(i, '')
                    i += 16
                cli_l.insert(
                    0, '\n# Bind commands for FID ' + str(switch_d['fid']))
                cli_l.append('\n# End bind commands for FID ' +
                             str(switch_d['fid']))
                brcdapi_log.log(cli_l, True)

            # Create the logical switch
            if ip is not None and switch_d['switch_flag']:

                if session is None:  # Login
                    session = api_int.login(user_id, pw, ip, sec, proj_obj)
                    if fos_auth.is_error(session):
                        return brcddb_common.EXIT_STATUS_API_ERROR

                if proj_obj is None:  # Create a project object
                    proj_obj = brcddb_project.new(
                        'Create_LS',
                        datetime.datetime.now().strftime('%d %b %Y %H:%M:%S'))
                    proj_obj.s_python_version(sys.version)
                    proj_obj.s_description('Creating logical switches from ' +
                                           os.path.basename(__file__))

                api_int.get_batch(session, proj_obj, _basic_capture_kpi_l,
                                  None)
                if proj_obj.r_is_any_error():
                    switch_d['err_msgs'].append(
                        'Error reading logical switch information from chassis'
                    )
                    brcdapi_log.log(
                        switch_d['err_msgs'][len(switch_d['err_msgs']) - 1],
                        True)
                else:
                    ec = _configure_switch(user_id, pw, session, proj_obj,
                                           switch_d, force, echo)

    except BaseException as e:
        switch_d['err_msgs'].append(
            'Programming error encountered. Exception: ' + str(e))
        brcdapi_log.log(switch_d['err_msgs'][len(switch_d['err_msgs']) - 1],
                        True)
        ec = brcddb_common.EXIT_STATUS_ERROR

    # Logout and create and print a summary report
    if session is not None:
        obj = brcdapi_rest.logout(session)
        if fos_auth.is_error(obj):
            brcdapi_log.log(fos_auth.formatted_error_msg(obj), True)
            ec = brcddb_common.EXIT_STATUS_API_ERROR
    if ip is not None:
        _print_summary(switch_d_list)

    return ec
def pseudo_main():
    """Basically the main(). Did it this way so it can easily be used as a standalone module or called from another.

    :return: Exit code. See exist codes in brcddb.brcddb_common
    :rtype: int
    """
    # Get and validate the input parameters. Add all input arguments to the log
    ip, user_id, pw, sec, cli_file, fid, t_flag, f_flag, b_flag, s_flag, vd, log, nl = parse_args(
    )
    if not nl:
        brcdapi_log.open_log(log)
    if vd:
        brcdapi_rest.verbose_debug = True
    content = {
        'fid': fid,
        'ip-addr': ip,
        'id': user_id,
        'pw': pw,
        'sec': sec,
        'force': False if f_flag is None else f_flag,
        'test': False if t_flag is None else t_flag,
        'bulk': False if b_flag is None else b_flag,
    }

    if s_flag:
        brcdapi_log.set_suppress_all()
    ml = list()
    if _DEBUG:
        ml.append('WARNING!!! Debug is enabled')
    ml.append('IP:          ' + brcdapi_util.mask_ip_addr(ip, True))
    ml.append('ID:          ' + user_id)
    ml.append('CLI file:    ' + cli_file)
    ml.append('FID:         ' + str(fid))
    ml.append('Test flag:   ' + str(t_flag))
    ml.append('Force flag:  ' + str(f_flag))
    ml.append('Bulk flag:   ' + str(b_flag))
    brcdapi_log.log(ml, True)

    # Read in the CLI file, condition the input strings and send it
    ml = list()
    try:
        file_contents = brcdapi_file.read_file(cli_file)
    except FileNotFoundError:
        ml.extend([
            '', 'File ' + cli_file +
            ' not found. Did you remember the file extension?'
        ])
    except PermissionError:
        ml.extend(['', 'You do not have permission to read ' + cli_file])
    if len(ml) > 0:
        brcdapi_log.log(ml, True)
        return brcddb_common.EXIT_STATUS_INPUT_ERROR

    content.update(changes=brcddb_util.parse_cli(file_contents))
    if t_flag:
        content.update(test=True)
    response = brcddb_zone.send_zoning(content)

    # General information
    ec = brcddb_common.EXIT_STATUS_OK
    total_changes = total_failures = total_io = i = 0
    for obj in response:
        if isinstance(
                obj, dict
        ):  # obj is None for blank or commented our lines in the input
            if obj.get('changed'):
                total_changes += 1
            if obj.get('fail'):
                total_failures += 1
                brcdapi_log.log(_format_fault(obj, i, file_contents), True)
                ec = brcddb_common.EXIT_STATUS_ERROR
            if obj.get('io'):
                total_io += 1
        i += 1
    ml = [
        '', 'Summary:', 'Total Changes  : ' + str(total_changes),
        'Total Failures : ' + str(total_failures),
        'Total I/O      : ' + str(total_io)
    ]
    brcdapi_log.log(ml, True)

    return ec
def _get_input():
    """Parses the module load command line

    :return ip_addr: IP address
    :rtype ip_addr: str
    :return id: User ID
    :rtype id: str
    :return pw: Password
    :rtype pw: str
    :return http_sec: Type of HTTP security
    :rtype http_sec: str
    :return fid: Fabric ID in chassis specified by -ip where the zoning information is to copied to.
    :rtype fid: int
    :return suppress_flag: True - suppress all print to STD_OUT
    :rtype suppress_flag: bool
    :return suppress_flag: True - suppress all print to STD_OUT
    :rtype suppress_flag: bool
    :return poll_time: Polling interval in seconds
    :return poll_time: int, None
    :return max_samples: Maximum number of samples to collect
    :rtype max_samples: int, None
    :return file:  Name of output file where raw data is to be stored
    :rtype file: str
    """
    global _DEBUG, _DEBUG_ip, _DEBUG_id, _DEBUG_pw, _DEBUG_sec, _DEBUG_fid, _DEBUG_sup
    global _DEBUG_p, _DEBUG_m, _DEBUG_o, _DEBUG_d, _DEBUG_log, _DEBUG_nl

    if _DEBUG:
        args_ip, args_id, args_pw, args_sec, args_fid, args_sup, args_p, args_m, args_o, args_d, args_log, args_nl =\
            _DEBUG_ip, _DEBUG_id, _DEBUG_pw, _DEBUG_sec, _DEBUG_fid, _DEBUG_sup, _DEBUG_p, _DEBUG_m, _DEBUG_o, \
            _DEBUG_d, _DEBUG_log, _DEBUG_nl
    else:
        buf = 'Collect port statistics at a specified poll interval. Use Control-C to stop data collection and write ' \
              'report'
        parser = argparse.ArgumentParser(description=buf)
        parser.add_argument('-ip', help='Required. IP address', required=True)
        parser.add_argument('-id', help='Required. User ID', required=True)
        parser.add_argument('-pw', help='Required. Password', required=True)
        buf = '(Optional) \'CA\' or \'self\' for HTTPS mode. Default is HTTP'
        parser.add_argument(
            '-s',
            help=buf,
            required=False,
        )
        buf = 'Required. Name of output file where raw data is to be stored. ".json" extension is automatically '\
              'appended.'
        parser.add_argument('-o', help=buf, required=True)
        buf = '(Optional) Virtual Fabric ID (1 - 128) of switch to read statistics from. Default is 128'
        parser.add_argument('-fid', help=buf, type=int, required=False)
        buf = '(Optional) No arguments. Suppress all library generated output to STD_IO except the exit code. Useful '\
              'with batch processing'
        parser.add_argument('-sup',
                            help=buf,
                            action='store_true',
                            required=False)
        buf = '(Optional) Polling interval in seconds. Default is '\
              + str(_DEFAULT_POLL_INTERVAL) + '. The minimum is ' + str(_MIN_POLL) + ' seconds.'
        parser.add_argument('-p', help=buf, type=float, required=False)
        buf = '(Optional) Samples are collected until this maximum is reached or a Control-C keyboard interrupt is '\
              'received. Default: ' + str(_DEFAULT_MAX_SAMPLE)
        parser.add_argument('-m', help=buf, type=int, required=False)
        buf = '(Optional) No arguments. Enable debug logging'
        parser.add_argument('-d',
                            help=buf,
                            action='store_true',
                            required=False)
        buf = '(Optional) Directory where log file is to be created. Default is to use the current directory. The log '\
              'file name will always be "Log_xxxx" where xxxx is a time and date stamp.'
        parser.add_argument(
            '-log',
            help=buf,
            required=False,
        )
        buf = '(Optional) No parameters. When set, a log file is not created. The default is to create a log file.'
        parser.add_argument('-nl',
                            help=buf,
                            action='store_true',
                            required=False)
        args = parser.parse_args()
        args_ip, args_id, args_pw, args_sec, args_fid, args_sup, args_p, args_m, args_o, args_d, args_log, args_nl =\
            args.ip, args.id, args.pw, args.sec, args.fid, args.sup, args.p, args.m, args.o, args.d, args.log, args.nl

    # Condition input
    sec = 'none' if args_sec is None else args_sec

    # Set up the log file & debug mode
    if not args.nl:
        brcdapi_log.open_log(args.log)
    if args.d:
        brcdapi_rest.verbose_debug = True
    if args.sup:
        brcdapi_log.set_suppress_all()

    return args_ip, args_id, args_pw, sec, args_fid, args_p, args_m, brcdapi_file.full_file_name(
        args_o, '.json')
def _get_input():
    """Retrieves the command line input, reads the input Workbook, and validates the input

    :return ec: Error code from brcddb.brcddb_common
    :rtype ec: int
    :return sl: List of switches to poll as read from the input Workbook
    :rtype sl: list
    :return pl: List of project files to combine
    :rtype pl: list
    :return cfg_name: Name of zone configuration file to create
    :rtype cfg_name: str, None
    :return a_cfg: Activation flag. If True, activate the zone configuration specified by cfg_name
    :rtype a_cfg: bool
    :return t_flag: Test flag. If True, test only. Do not make any changes
    :rtype t_flag: bool
    :return scan_flag: Scan flag. If True, scan files and switches for fabric information
    :rtype scan_flag: bool
    :return cli_flag: If True, generate CLI
    :rtype cli_flag: bool
    :return addl_parms: Additional parameters (logging and debug flags) to pass to multi_capture.py
    :rtype addl_parms: list
    """
    global _DEBUG, __version__

    # Initialize the return variables
    ec = brcddb_common.EXIT_STATUS_OK
    sl = list()
    pl = list()
    addl_parms = list()

    # Get the user input
    c_file, cfg_name, a_flag, t_flag, scan_flag, cli_flag, d_flag, s_flag, log, nl = parse_args(
    )
    if s_flag:
        addl_parms.append('-sup')
        brcdapi_log.set_suppress_all()
    if nl:
        addl_parms.append('-nl')
    else:
        brcdapi_log.open_log(log)
    if log is not None:
        addl_parms.extend(['-log', log])
    if d_flag:
        addl_parms.append('-d')
        brcdapi_rest.verbose_debug = True
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ml.append('zone_merge.py version: ' + __version__)
    ml.append('Login credential file: ' + str(c_file))
    ml.append('Common zone cfg name:  ' + str(cfg_name))
    ml.append('Activate zone cfg:     ' + str(a_flag))
    ml.append('Scan flag, -scan:      ' + str(scan_flag))
    ml.append('CLI flag, -cli:        ' + str(cli_flag))
    ml.append('Test:                  ' + str(t_flag))
    brcdapi_log.log(ml, True)
    c_file = brcdapi_file.full_file_name(c_file, '.xlsx')

    # Parse the input file
    ml = list()
    switch_l = excel_util.parse_parameters(sheet_name='parameters',
                                           hdr_row=0,
                                           wb_name=c_file)['content']
    if a_flag and not isinstance(cfg_name, str):
        ml.append(
            'Configuration activate flag, -a, specified without a valid zone configuration name, -cfg'
        )
    if len(ml) == 0:
        for i in range(0, len(switch_l)):
            sub_d = switch_l[i]
            buf = sub_d.get('project_file')
            if buf is None:
                sl.append(_condition_input(sub_d))
            else:
                pl.append(sub_d)
                if not scan_flag and not gen_util.is_wwn(sub_d.get('fab_wwn'),
                                                         full_check=True):
                    ml.append('fab_wwn is not a valid WWN in row ' +
                              str(i + 1))
    if len(ml) > 0:
        brcdapi_log.log(ml, True)
        ec = brcddb_common.EXIT_STATUS_INPUT_ERROR

    return ec, sl, pl, cfg_name, a_flag, t_flag, scan_flag, cli_flag, addl_parms
def pseudo_main():
    """Basically the main(). Did it this way so it can easily be used as a standalone module or called from another.

    :return: Exit code. See exist codes in brcddb.brcddb_common
    :rtype: int
    """
    ec = brcddb_common.EXIT_STATUS_OK

    # Get the user input
    ml = ['WARNING!!! Debug is enabled'] if _DEBUG else list()
    ip, user_id, pw, sec, s_flag, fid, vd, log, nl = parse_args()
    if not nl:
        brcdapi_log.open_log(log)
    if vd:
        brcdapi_rest.verbose_debug = True
    if s_flag:
        brcdapi_log.set_suppress_all()
    if sec is None:
        sec = 'none'
    fid_list = None if fid is None else [int(i) for i in fid.split(',')]
    ml.extend([
        'IP address: ' + ip, 'User ID:    ' + user_id, 'Security:   ' + sec,
        'Surpress:   ' + str(s_flag),
        'FID:        ' + 'Automatic' if fid is None else fid
    ])
    brcdapi_log.log(ml, True)

    # Create the project
    proj_obj = brcddb_project.new(
        'Captured_data',
        datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S'))
    proj_obj.s_python_version(sys.version)
    proj_obj.s_description('Unused ports to disable')

    # Login
    session = api_int.login(user_id, pw, ip, sec, proj_obj)
    if fos_auth.is_error(session):
        brcdapi_log.log(fos_auth.formatted_error_msg(session), True)
        return brcddb_common.EXIT_STATUS_ERROR

    # Capture data - stats are cleared on a per port basis so this is needed to determine what the ports are.
    try:  # I always put all code after login in a try/except so that if I have a code bug, I still logout
        brcdapi_log.log('Capturing data', True)
        api_int.get_batch(session, proj_obj, chassis_uris, list(),
                          fid_list)  # Captured data is put in proj_obj
        chassis_obj = proj_obj.r_chassis_obj(session.get('chassis_wwn'))

        # Clear stats on each switch
        for switch_obj in chassis_obj.r_switch_objects():
            fid = brcddb_switch.switch_fid(switch_obj)
            if fid_list is None or fid in fid_list:
                temp_ec = clear_stats(session, switch_obj)
                ec = temp_ec if ec != brcddb_common.EXIT_STATUS_OK else ec
    except:  # Bare because I don't care what happened. I just want to logout.
        brcdapi_log.exception('Programming error encountered', True)
        ec = brcddb_common.EXIT_STATUS_ERROR

    # Logout
    obj = brcdapi_rest.logout(session)
    if fos_auth.is_error(obj):
        brcdapi_log.log(fos_auth.formatted_error_msg(obj), True)
        return brcddb_common.EXIT_STATUS_ERROR

    return ec
Example #13
0
def _get_input():
    """Parses the module load command line

    :return i: Input file name
    :rtype i: str
    :return o: Output file name
    :rtype o: str
    :return sfp: Name of SFP rules file
    :rtype sfp: str, None
    :return iocp: Name of folder containing IOCP files
    :rtype iocp: str, None
    :return c: Custom report parameters passed to _custom_report(). Typically not used.
    :rtype c: str, None
    """
    global _DEBUG, _DEBUG_i, _DEBUG_o, _DEBUG_sfp, _DEBUG_sup, _DEBUG_iocp, _DEBUG_log, _DEBUG_nl, _DEBUG_c

    if _DEBUG:
        args_i, args_o, args_sfp, args_sup, args_iocp, args_log, args_nl, args_c =\
            _DEBUG_i, _DEBUG_o, _DEBUG_sfp, _DEBUG_sup, _DEBUG_iocp, _DEBUG_log, _DEBUG_nl, _DEBUG_c
    else:
        parser = argparse.ArgumentParser(
            description='Create a general report in Excel.')
        buf = 'Required. Name of input file generated by capture.py, combine.py, or multi_capture.py. Extension '\
              '".json" is automatically added if no extension present.'
        parser.add_argument('-i', help=buf, required=True)
        parser.add_argument(
            '-o',
            help=
            'Required. Name of report file. ".xlsx" automatically appended.',
            required=True)
        buf = 'Optional. Name of the Excel Workbook with SFP thresholds. This is the same file used as input to '\
              'applications.maps_config. This is useful for checking SFPs against the new recommended MAPS rules '\
              'before implementing them or filling in missing rules.'
        parser.add_argument('-sfp', help=buf, required=False)
        buf = 'Optional. For FICON environments only. Name of folder with IOCP files. All files in this folder must '\
              'be IOCP files (build I/O configuration statements from HCD) and must begin with the CEC serial number'\
              ' followed by \'_\'. Leading 0s are not required. Example, for a CEC with serial number '\
              '12345: 12345_M90_iocp.txt'
        parser.add_argument('-iocp', help=buf, required=False)
        buf = 'Optional. Suppress all output to STD_IO except the exit code and argument parsing errors. Useful with '\
              'batch processing where only the exit status code is desired. Messages are still printed to the log file'\
              '. No operands.'
        parser.add_argument('-sup',
                            help=buf,
                            action='store_true',
                            required=False)
        buf = '(Optional) Directory where log file is to be created. Default is to use the current directory. The ' \
              'log file name will always be "Log_xxxx" where xxxx is a time and date stamp.'
        parser.add_argument(
            '-log',
            help=buf,
            required=False,
        )
        buf = '(Optional) No parameters. When set, a log file is not created. The default is to create a log file.'
        parser.add_argument('-nl',
                            help=buf,
                            action='store_true',
                            required=False)
        parser.add_argument(
            '-c',
            help='(Optional) Custom parameter to be used with _custom_report',
            required=False)
        args = parser.parse_args()
        args_i, args_o, args_sfp, args_sup, args_iocp, args_log, args_nl, args_c =\
            args.i, args.o, args.sfp, args.sup, args.iocp, args.log, args.nl, args.c

    # Set up log file and debug
    if args_sup:
        brcdapi_log.set_suppress_all()
    if not args_nl:
        brcdapi_log.open_log(args_log)

    return brcapi_file.full_file_name(args_i, '.json'), \
        brcapi_file.full_file_name(args_o, '.xlsx'), \
        brcapi_file.full_file_name(args_sfp, '.xlsx'), \
        args_iocp, \
        args_c