Пример #1
0
    def test_write_summary_csv(self):
        fake_csvfile = StringIO.StringIO()

        writer = output.CsvUtilizationWriter(self.test_data, fake_csvfile)
        writer.write_summary_csv()

        self.assertMultiLineEqual(fake_csvfile.getvalue(),
                                  self.expected_summary_csv)
Пример #2
0
    def test_write_accountonly_csv(self):
        fake_csvfile = StringIO.StringIO()

        fields = ['account']

        writer = output.CsvUtilizationWriter(self.test_data, fake_csvfile,
                                             fields)
        writer.write_accountonly_csv()
        self.assertMultiLineEqual(fake_csvfile.getvalue(),
                                  self.expected_accountonly_csv)
Пример #3
0
    def test_write_summary_csv_filtered(self):
        fake_csvfile = StringIO.StringIO()

        writer = output.CsvUtilizationWriter(
            self.test_data,
            fake_csvfile,
            output_fields=['account', 'bytes_used'])
        writer.write_summary_csv()

        self.assertMultiLineEqual(fake_csvfile.getvalue(),
                                  self.expected_summary_csv_filtered)
Пример #4
0
    def test_get_fields(self):
        writer = output.CsvUtilizationWriter(self.test_data, "fakey",
                                             "fake_fields")
        fields = writer.get_fields(self.test_data)

        expected_fields = [
            'account', 'start', 'end', 'container_count', 'object_count',
            'bytes_used', 'pct_complete', 'policy'
        ]

        self.assertItemsEqual(fields, expected_fields)
Пример #5
0
    def test_write_raw_csv(self):
        fake_csvfile = StringIO.StringIO()

        # try to force an order
        fields = [
            'account', 'start', 'end', 'container_count', 'object_count',
            'bytes_used', 'pct_complete', 'policy'
        ]

        writer = output.CsvUtilizationWriter(self.test_data, fake_csvfile,
                                             fields)
        writer.write_raw_csv()

        self.assertMultiLineEqual(fake_csvfile.getvalue(), self.expected_csv)
Пример #6
0
def main(args=None):
    if not args:
        args = sys.argv[1:]

    config = parse_args(args)

    if config.verbose:
        if config.verbose > 1:
            # no lower logging level, but lets handle it anyway and just set it to DEBUG
            logger = setup_logging(os.path.basename(sys.argv[0]), logging.DEBUG)
        else:
            logger = setup_logging(os.path.basename(sys.argv[0]), logging.DEBUG)
    else:
        logger = setup_logging(os.path.basename(sys.argv[0]), logging.INFO)

    if config.quiet:
        logging.disable(logging.CRITICAL)

    logger.info("Starting SS-Utilization o-matic...")
    logger.debug("Got configuration:")
    for item in config.__dict__:
        logger.debug("%s: %s " % (item, config.__dict__[item]))

    capture_fields = None
    if config.output_fields:
        capture_fields = map_fields(config.output_fields)

    try:
        ssapiclient = api.SwiftStackAPIClient(controller=config.controller_host,
                                              apiuser=config.ssapi_user,
                                              apikey=config.ssapi_key,
                                              controller_cert=config.controller_cert_path)
        # TODO: all this logic needs to be in some unit-testable function!!
        util_output = {}
        for p in config.storage_policy:
            util_accts = ssapiclient.get_accounts(cluster=config.cluster_id,
                                                  start_time=config.start_datetime,
                                                  end_time=config.end_datetime,
                                                  policy=p)
            logger.info("Got %d accounts in utilization period for policy %s" %
                        (len(util_accts), p))

            if len(util_accts) > 0:
                if config.account_output is False:
                    for account in util_accts:
                        if account not in util_output:
                            util_output[account] = {}
                        records = ssapiclient.get_acct_util(cluster=config.cluster_id,
                                                            account=account,
                                                            start_time=config.start_datetime,
                                                            end_time=config.end_datetime,
                                                            policy=p)
                        util_output[account][p] = records
                        logger.info("Got %d records for account %s in policy %s" % (len(records),
                                                                                    account,
                                                                                    p))
            else:
                logger.warn("No accounts found in utilization period for policy %s" % p)

        if util_output:
            if config.output_file:
                with open(config.output_file, 'wb') as f:
                    if config.account_output:
                        capture_fields = ['account']
                        writer = output.CsvUtilizationWriter(util_accts, f, output_fields=capture_fields)
                        writer.write_accountonly_csv()
                    else:
                        writer = output.CsvUtilizationWriter(util_output, f, output_fields=capture_fields)
                        if config.raw_output:
                            writer.write_raw_csv()
                        else:
                            writer.write_summary_csv()
                    logger.info("Wrote %s" % config.output_file)
            else:
                fake_csvfile = StringIO.StringIO()
                if config.account_output:
                    capture_fields = ['account']
                    writer = output.CsvUtilizationWriter(util_accts, fake_csvfile,
                                                         output_fields=capture_fields)
                    writer.write_accountonly_csv()
                else:
                    writer = output.CsvUtilizationWriter(util_output, fake_csvfile,
                                                         output_fields=capture_fields)
                    if config.raw_output:
                        writer.write_raw_csv()
                    else:
                        writer.write_summary_csv()
                print fake_csvfile.getvalue()
                fake_csvfile.close()
        else:
            logger.error("No utilization data found in specified time range!")

    except:
        raise

    logger.info("Utilization Run Complete")
Пример #7
0
    def test_summarize(self):
        writer = output.CsvUtilizationWriter(self.test_data, "fakey")

        writer.summarize()

        self.assertEqual(writer.summary, self.expected_summary)