def main():

    parser = argparse.ArgumentParser(description='Storage Report Generator.')
    
    parser.add_argument('-f', '--config-file', dest='config_file', 
        type=str, required=True, help='Path of the config file.')
    
    parser.add_argument('-D', '--enable-debug', dest='enable_debug', 
        required=False, action='store_true', 
        help='Enables logging of debug messages.')
    
    parser.add_argument('-L', '--enable-local_mode', dest='enable_local', 
        required=False, action='store_true', 
        help='Enables local_mode program execution.')

    args = parser.parse_args()

    if not os.path.isfile(args.config_file):
        raise IOError("The config file does not exist or is not a file: %s" % 
            args.config_file)

    logging_level = logging.ERROR

    if args.enable_debug:
        logging_level = logging.DEBUG

    logging.basicConfig(
        level=logging_level, format='%(asctime)s - %(levelname)s: %(message)s')

    try:

        logging.info('START')

        date_now = datetime.datetime.now()

        check_matplotlib_version()

        local_mode = args.enable_local

        logging.debug("Local mode enabled: %s" % local_mode)

        config = configparser.ConfigParser()
        config.read(args.config_file)

        transfer_mode = config.get('transfer', 'mode')

        chart_dir = config.get('base_chart', 'report_dir')

        file_system = config.get('storage', 'file_system')
        fs_long_name = config.get('storage', 'fs_long_name')
        
        quota_pct_bar_chart = config.get('quota_pct_bar_chart', 'filename')
        usage_quota_bar_chart = config.get('usage_quota_bar_chart', 'filename')
        usage_pie_chart = config.get('usage_pie_chart', 'filename')
        
        num_top_groups = config.getint('usage_pie_chart', 'num_top_groups')

        chart_path_list = \
            create_weekly_reports(local_mode,
                                  chart_dir,
                                  file_system,
                                  fs_long_name,
                                  quota_pct_bar_chart,
                                  usage_quota_bar_chart,
                                  usage_pie_chart,
                                  num_top_groups)

        if transfer_mode == 'on':

            for chart_path in chart_path_list:
                transfer_report('weekly', date_now, chart_path, config)

        logging.info('END')

        return 0
   
    except Exception as e:

        exc_type, exc_obj, exc_tb = sys.exc_info()
        filename = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]

        error_msg = "Caught exception (%s): %s - %s (line: %s)" % (
        exc_type, str(e), filename, exc_tb.tb_lineno)

        logging.error(error_msg)
def main():

    parser = argparse.ArgumentParser(
        description='Lustre Monthly Report Generator')

    parser.add_argument('-f',
                        '--config-file',
                        dest='config_file',
                        type=str,
                        required=True,
                        help='Path of the config file.')

    parser.add_argument('-D',
                        '--enable-debug',
                        dest='enable_debug',
                        required=False,
                        action='store_true',
                        help='Enables logging of debug messages.')

    parser.add_argument('-L',
                        '--enable-local_mode',
                        dest='enable_local',
                        required=False,
                        action='store_true',
                        help='Enables local_mode program execution.')

    args = parser.parse_args()

    if not os.path.isfile(args.config_file):
        raise IOError("The config file does not exist or is not a file: %s" %
                      args.config_file)

    logging_level = logging.ERROR

    if args.enable_debug:
        logging_level = logging.DEBUG

    logging.basicConfig(level=logging_level,
                        format='%(asctime)s - %(levelname)s: %(message)s')

    try:

        logging.info('START')

        date_now = datetime.datetime.now()

        check_matplotlib_version()

        local_mode = args.enable_local

        logging.debug("Local mode enabled: %s" % local_mode)

        config = configparser.ConfigParser(interpolation=None)
        config.read(args.config_file)

        transfer_mode = config.get('transfer', 'mode')

        chart_dir = config.get('base_chart', 'report_dir')
        fs_long_name = config.get('storage', 'fs_long_name')

        date_format = config.get("time_series_chart", "date_format")
        prev_months = config.getint("time_series_chart", "prev_months")

        usage_trend_chart = config.get('usage_trend_chart', 'filename')
        threshold = config.get('usage_trend_chart', 'threshold')

        quota_trend_chart = config.get('quota_trend_chart', 'filename')

        quota_history_table = \
            QuotaHistoryTable(config.get('mysqld', 'host'),
                              config.get('mysqld', 'user'),
                              config.get('mysqld', 'passwd'),
                              config.get('mysqld', 'db'),
                              config.get('report', 'history_table'))

        if prev_months <= 0:
            raise RuntimeError( \
                "Config parameter 'prev_months' must be greater than 0!")

        prev_date = date_now - \
            dateutil.relativedelta.relativedelta(months = prev_months)

        start_date = prev_date.strftime(date_format)
        end_date = date_now.strftime(date_format)

        logging.debug("Time series start date: %s" % start_date)

        chart_path_list = list()

        chart_path = create_usage_trend_chart(local_mode, fs_long_name,
                                              chart_dir, start_date, end_date,
                                              threshold, usage_trend_chart,
                                              quota_history_table)

        logging.debug("Created chart: %s" % chart_path)
        chart_path_list.append(chart_path)

        chart_path = create_quota_trend_chart(local_mode, fs_long_name,
                                              chart_dir, start_date, end_date,
                                              quota_trend_chart,
                                              quota_history_table)

        logging.debug("Created chart: %s" % chart_path)
        chart_path_list.append(chart_path)

        if transfer_mode == 'on':

            for chart_path in chart_path_list:
                transfer_report('monthly', date_now, chart_path, config)

        logging.info('END')

        return 0

    except Exception as e:

        exc_type, exc_obj, exc_tb = sys.exc_info()
        filename = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]

        error_msg = "Caught exception (%s): %s - %s (line: %s)" % \
            (exc_type, str(e), filename, exc_tb.tb_lineno)

        logging.error(error_msg)