Exemple #1
0
    def test_uses_conf_unit(self, mock_conf):
        mock_conf.volume_usage_audit_period = 'hour'
        begin1, end1 = utils.last_completed_audit_period()
        self.assertEqual(60.0 * 60, (end1 - begin1).total_seconds())

        mock_conf.volume_usage_audit_period = 'day'
        begin2, end2 = utils.last_completed_audit_period()

        self.assertEqual(60.0 * 60 * 24, (end2 - begin2).total_seconds())
    def test_uses_conf_unit(self, mock_conf):
        mock_conf.volume_usage_audit_period = 'hour'
        begin1, end1 = utils.last_completed_audit_period()
        self.assertEqual(60.0 * 60, (end1 - begin1).total_seconds())

        mock_conf.volume_usage_audit_period = 'day'
        begin2, end2 = utils.last_completed_audit_period()

        self.assertEqual(60.0 * 60 * 24, (end2 - begin2).total_seconds())
 def test_hour_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='hour@30')
     self.assertEqual(
         datetime.datetime(minute=30, hour=6, day=5, month=3, year=2012),
         begin)
     self.assertEqual(
         datetime.datetime(minute=30, hour=7, day=5, month=3, year=2012),
         end)
Exemple #4
0
 def test_year_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='year@2')
     self.assertEquals(begin, datetime.datetime(day=1,
                                                month=2,
                                                year=2011))
     self.assertEquals(end, datetime.datetime(day=1,
                                              month=2,
                                              year=2012))
Exemple #5
0
 def test_day(self):
     begin, end = utils.last_completed_audit_period(unit='day')
     self.assertEqual(begin, datetime.datetime(day=4,
                                               month=3,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(day=5,
                                             month=3,
                                             year=2012))
Exemple #6
0
 def test_year_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='year@6')
     self.assertEqual(begin, datetime.datetime(day=1,
                                               month=6,
                                               year=2010))
     self.assertEqual(end, datetime.datetime(day=1,
                                             month=6,
                                             year=2011))
Exemple #7
0
 def test_year_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='year@6')
     self.assertEqual(begin, datetime.datetime(day=1,
                                               month=6,
                                               year=2010))
     self.assertEqual(end, datetime.datetime(day=1,
                                             month=6,
                                             year=2011))
Exemple #8
0
 def test_month_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='month@15')
     self.assertEqual(begin, datetime.datetime(day=15,
                                               month=1,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(day=15,
                                             month=2,
                                             year=2012))
Exemple #9
0
 def test_year_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='year@2')
     self.assertEquals(begin, datetime.datetime(day=1,
                                                month=2,
                                                year=2011))
     self.assertEquals(end, datetime.datetime(day=1,
                                              month=2,
                                              year=2012))
Exemple #10
0
 def test_month(self):
     begin, end = utils.last_completed_audit_period(unit='month')
     self.assertEquals(begin, datetime.datetime(day=1,
                                                month=2,
                                                year=2012))
     self.assertEquals(end, datetime.datetime(day=1,
                                              month=3,
                                              year=2012))
Exemple #11
0
 def test_day(self):
     begin, end = utils.last_completed_audit_period(unit='day')
     self.assertEqual(begin, datetime.datetime(day=4,
                                               month=3,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(day=5,
                                             month=3,
                                             year=2012))
Exemple #12
0
 def test_month_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='month@15')
     self.assertEqual(begin, datetime.datetime(day=15,
                                               month=1,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(day=15,
                                             month=2,
                                             year=2012))
Exemple #13
0
 def test_year(self):
     begin, end = utils.last_completed_audit_period(unit='year')
     self.assertEqual(begin, datetime.datetime(day=1,
                                               month=1,
                                               year=2011))
     self.assertEqual(end, datetime.datetime(day=1,
                                             month=1,
                                             year=2012))
Exemple #14
0
 def test_month(self):
     begin, end = utils.last_completed_audit_period(unit='month')
     self.assertEquals(begin, datetime.datetime(day=1,
                                                month=2,
                                                year=2012))
     self.assertEquals(end, datetime.datetime(day=1,
                                              month=3,
                                              year=2012))
Exemple #15
0
 def test_year(self):
     begin, end = utils.last_completed_audit_period(unit='year')
     self.assertEqual(begin, datetime.datetime(day=1,
                                               month=1,
                                               year=2011))
     self.assertEqual(end, datetime.datetime(day=1,
                                             month=1,
                                             year=2012))
Exemple #16
0
 def test_day_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='day@6')
     self.assertEquals(begin, datetime.datetime(hour=6,
                                                day=4,
                                                month=3,
                                                year=2012))
     self.assertEquals(end, datetime.datetime(hour=6,
                                              day=5,
                                              month=3,
                                              year=2012))
Exemple #17
0
 def test_day_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='day@10')
     self.assertEqual(begin, datetime.datetime(hour=10,
                                               day=3,
                                               month=3,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(hour=10,
                                             day=4,
                                             month=3,
                                             year=2012))
Exemple #18
0
 def test_day_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='day@6')
     self.assertEquals(begin, datetime.datetime(hour=6,
                                                day=4,
                                                month=3,
                                                year=2012))
     self.assertEquals(end, datetime.datetime(hour=6,
                                              day=5,
                                              month=3,
                                              year=2012))
Exemple #19
0
 def test_day_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='day@10')
     self.assertEqual(begin, datetime.datetime(hour=10,
                                               day=3,
                                               month=3,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(hour=10,
                                             day=4,
                                             month=3,
                                             year=2012))
Exemple #20
0
 def test_hour_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='hour@10')
     self.assertEquals(
         begin,
         datetime.datetime(minute=10, hour=7, day=5, month=3, year=2012))
     self.assertEquals(
         end, datetime.datetime(minute=10,
                                hour=8,
                                day=5,
                                month=3,
                                year=2012))
Exemple #21
0
 def test_hour(self):
     begin, end = utils.last_completed_audit_period(unit='hour')
     self.assertEqual(begin,
                      datetime.datetime(hour=7,
                                        day=5,
                                        month=3,
                                        year=2012))
     self.assertEqual(end, datetime.datetime(hour=8,
                                             day=5,
                                             month=3,
                                             year=2012))
Exemple #22
0
 def test_hour(self):
     begin, end = utils.last_completed_audit_period(unit='hour')
     self.assertEqual(begin,
                      datetime.datetime(hour=7,
                                        day=5,
                                        month=3,
                                        year=2012))
     self.assertEqual(end, datetime.datetime(hour=8,
                                             day=5,
                                             month=3,
                                             year=2012))
Exemple #23
0
 def test_hour_with_offset_after_current(self):
     begin, end = utils.last_completed_audit_period(unit='hour@30')
     self.assertEqual(begin, datetime.datetime(minute=30,
                                               hour=6,
                                               day=5,
                                               month=3,
                                               year=2012))
     self.assertEqual(end, datetime.datetime(minute=30,
                                             hour=7,
                                             day=5,
                                             month=3,
                                             year=2012))
def main():
    objects.register_all()
    admin_context = context.get_admin_context()
    CONF(sys.argv[1:], project='cinder',
         version=version.version_string())
    logging.setup(CONF, "cinder")
    LOG = logging.getLogger("cinder")
    rpc.init(CONF)

    begin, end = utils.last_completed_audit_period()
    begin, end = _time_error(LOG, begin, end)

    LOG.info(_LI("Starting volume usage audit"))
    msg = _LI("Creating usages for %(begin_period)s until %(end_period)s")
    LOG.info(msg, {"begin_period": str(begin), "end_period": str(end)})

    extra_info = {
        'audit_period_beginning': str(begin),
        'audit_period_ending': str(end),
    }

    volumes = objects.VolumeList.get_all_active_by_window(admin_context,
                                                          begin,
                                                          end)

    LOG.info(_LI("Found %d volumes"), len(volumes))
    for volume_ref in volumes:
        _obj_ref_action(_vol_notify_usage, LOG, volume_ref, extra_info,
                        admin_context, begin, end,
                        cinder.volume.utils.notify_about_volume_usage,
                        "volume_id", "volume")

    snapshots = objects.SnapshotList.get_all_active_by_window(admin_context,
                                                              begin, end)
    LOG.info(_LI("Found %d snapshots"), len(snapshots))
    for snapshot_ref in snapshots:
        _obj_ref_action(_snap_notify_usage, LOG, snapshot_ref, extra_info,
                        admin_context, begin,
                        end, cinder.volume.utils.notify_about_snapshot_usage,
                        "snapshot_id", "snapshot")

    backups = objects.BackupList.get_all_active_by_window(admin_context,
                                                          begin, end)

    LOG.info(_LI("Found %d backups"), len(backups))
    for backup_ref in backups:
        _obj_ref_action(_backup_notify_usage, LOG, backup_ref, extra_info,
                        admin_context, begin,
                        end, cinder.volume.utils.notify_about_backup_usage,
                        "backup_id", "backup")
    LOG.info(_LI("Volume usage audit completed"))
def main():
    objects.register_all()
    admin_context = context.get_admin_context()
    CONF(sys.argv[1:], project='cinder',
         version=version.version_string())
    logging.setup(CONF, "cinder")
    LOG = logging.getLogger("cinder")
    rpc.init(CONF)

    begin, end = utils.last_completed_audit_period()
    begin, end = _time_error(LOG, begin, end)

    LOG.info("Starting volume usage audit")
    LOG.info("Creating usages for %(begin_period)s until %(end_period)s",
             {"begin_period": begin, "end_period": end})

    extra_info = {
        'audit_period_beginning': str(begin),
        'audit_period_ending': str(end),
    }

    volumes = objects.VolumeList.get_all_active_by_window(admin_context,
                                                          begin,
                                                          end)

    LOG.info("Found %d volumes", len(volumes))
    for volume_ref in volumes:
        _obj_ref_action(_vol_notify_usage, LOG, volume_ref, extra_info,
                        admin_context, begin, end,
                        cinder.volume.utils.notify_about_volume_usage,
                        "volume_id", "volume")

    snapshots = objects.SnapshotList.get_all_active_by_window(admin_context,
                                                              begin, end)
    LOG.info("Found %d snapshots", len(snapshots))
    for snapshot_ref in snapshots:
        _obj_ref_action(_snap_notify_usage, LOG, snapshot_ref, extra_info,
                        admin_context, begin,
                        end, cinder.volume.utils.notify_about_snapshot_usage,
                        "snapshot_id", "snapshot")

    backups = objects.BackupList.get_all_active_by_window(admin_context,
                                                          begin, end)

    LOG.info("Found %d backups", len(backups))
    for backup_ref in backups:
        _obj_ref_action(_backup_notify_usage, LOG, backup_ref, extra_info,
                        admin_context, begin,
                        end, cinder.volume.utils.notify_about_backup_usage,
                        "backup_id", "backup")
    LOG.info("Volume usage audit completed")
Exemple #26
0
 def test_hour_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='hour@10')
     self.assertEqual(datetime.datetime(minute=10,
                                        hour=7,
                                        day=5,
                                        month=3,
                                        year=2012),
                      begin)
     self.assertEqual(datetime.datetime(minute=10,
                                        hour=8,
                                        day=5,
                                        month=3,
                                        year=2012),
                      end)
Exemple #27
0
def notify_usage_exists(context, volume_ref, current_period=False):
    """Generates 'exists' notification for a volume for usage auditing
       purposes.

       Generates usage for last completed period, unless 'current_period'
       is True.
    """
    begin, end = utils.last_completed_audit_period()
    if current_period:
        audit_start = end
        audit_end = timeutils.utcnow()
    else:
        audit_start = begin
        audit_end = end

    extra_usage_info = dict(audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end))

    notify_about_volume_usage(context, volume_ref, "exists", extra_usage_info=extra_usage_info)
Exemple #28
0
def notify_usage_exists(context, volume_ref, current_period=False):
    """ Generates 'exists' notification for a volume for usage auditing
        purposes.

        Generates usage for last completed period, unless 'current_period'
        is True."""
    begin, end = utils.last_completed_audit_period()
    if current_period:
        audit_start = end
        audit_end = timeutils.utcnow()
    else:
        audit_start = begin
        audit_end = end

    extra_usage_info = dict(audit_period_beginning=str(audit_start),
                            audit_period_ending=str(audit_end))

    notify_about_volume_usage(
            context, volume_ref, 'exists', extra_usage_info=extra_usage_info)
 def test_month_jan_day_not_first(self, mock_utcnow):
     begin, end = utils.last_completed_audit_period(unit='month')
     self.assertEqual(datetime.datetime(day=1, month=12, year=2011), begin)
     self.assertEqual(datetime.datetime(day=1, month=1, year=2012), end)
Exemple #30
0
 def test_month_jan_day_not_first(self, mock_utcnow):
     begin, end = utils.last_completed_audit_period(unit='month')
     self.assertEqual(datetime.datetime(day=1, month=12, year=2011), begin)
     self.assertEqual(datetime.datetime(day=1, month=1, year=2012), end)
Exemple #31
0
def main():
    admin_context = context.get_admin_context()
    CONF(sys.argv[1:], project='cinder',
         version=version.version_string())
    logging.setup("cinder")
    LOG = logging.getLogger("cinder")
    rpc.init(CONF)
    begin, end = utils.last_completed_audit_period()
    if CONF.start_time:
        begin = datetime.strptime(CONF.start_time, "%Y-%m-%d %H:%M:%S")
    if CONF.end_time:
        end = datetime.strptime(CONF.end_time, "%Y-%m-%d %H:%M:%S")
    if not end > begin:
        msg = _("The end time (%(end)s) must be after the start "
                "time (%(start)s).") % {'start': begin,
                                        'end': end}
        print(msg)
        LOG.error(msg)
        sys.exit(-1)
    print(_("Starting volume usage audit"))
    msg = _("Creating usages for %(begin_period)s until %(end_period)s")
    print(msg % {"begin_period": str(begin), "end_period": str(end)})

    extra_info = {
        'audit_period_beginning': str(begin),
        'audit_period_ending': str(end),
    }

    volumes = db.volume_get_active_by_window(admin_context,
                                             begin,
                                             end)
    print(_("Found %d volumes") % len(volumes))
    for volume_ref in volumes:
        try:
            LOG.debug("Send exists notification for <volume_id: "
                      "%(volume_id)s> <project_id %(project_id)s> "
                      "<%(extra_info)s>" %
                      {'volume_id': volume_ref.id,
                       'project_id': volume_ref.project_id,
                       'extra_info': extra_info})
            cinder.volume.utils.notify_about_volume_usage(
                admin_context,
                volume_ref,
                'exists', extra_usage_info=extra_info)
        except Exception as e:
            LOG.error(_LE("Failed to send exists notification"
                          " for volume %s.") %
                      volume_ref.id)
            print(traceback.format_exc(e))

        if (CONF.send_actions and
                volume_ref.created_at > begin and
                volume_ref.created_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(volume_ref.created_at),
                    'audit_period_ending': str(volume_ref.created_at),
                }
                LOG.debug("Send create notification for "
                          "<volume_id: %(volume_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>" %
                          {'volume_id': volume_ref.id,
                           'project_id': volume_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'create.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'create.end', extra_usage_info=local_extra_info)
            except Exception as e:
                LOG.error(_LE("Failed to send create notification for "
                              "volume %s.") % volume_ref.id)
                print(traceback.format_exc(e))

        if (CONF.send_actions and volume_ref.deleted_at and
                volume_ref.deleted_at > begin and
                volume_ref.deleted_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(volume_ref.deleted_at),
                    'audit_period_ending': str(volume_ref.deleted_at),
                }
                LOG.debug("Send delete notification for "
                          "<volume_id: %(volume_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>" %
                          {'volume_id': volume_ref.id,
                           'project_id': volume_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'delete.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'delete.end', extra_usage_info=local_extra_info)
            except Exception as e:
                LOG.error(_LE("Failed to send delete notification for volume "
                              "%s.") % volume_ref.id)
                print(traceback.format_exc(e))

    snapshots = db.snapshot_get_active_by_window(admin_context,
                                                 begin,
                                                 end)
    print(_("Found %d snapshots") % len(snapshots))
    for snapshot_ref in snapshots:
        try:
            LOG.debug("Send notification for <snapshot_id: %(snapshot_id)s> "
                      "<project_id %(project_id)s> <%(extra_info)s>" %
                      {'snapshot_id': snapshot_ref.id,
                       'project_id': snapshot_ref.project_id,
                       'extra_info': extra_info})
            cinder.volume.utils.notify_about_snapshot_usage(admin_context,
                                                            snapshot_ref,
                                                            'exists',
                                                            extra_info)
        except Exception as e:
            LOG.error(_LE("Failed to send exists notification "
                          "for snapshot %s.")
                      % snapshot_ref.id)
            print(traceback.format_exc(e))

        if (CONF.send_actions and
                snapshot_ref.created_at > begin and
                snapshot_ref.created_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(snapshot_ref.created_at),
                    'audit_period_ending': str(snapshot_ref.created_at),
                }
                LOG.debug("Send create notification for "
                          "<snapshot_id: %(snapshot_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>" %
                          {'snapshot_id': snapshot_ref.id,
                           'project_id': snapshot_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'create.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'create.end', extra_usage_info=local_extra_info)
            except Exception as e:
                LOG.error(_LE("Failed to send create notification for snapshot"
                              "%s.") % snapshot_ref.id)
                print(traceback.format_exc(e))

        if (CONF.send_actions and snapshot_ref.deleted_at and
                snapshot_ref.deleted_at > begin and
                snapshot_ref.deleted_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(snapshot_ref.deleted_at),
                    'audit_period_ending': str(snapshot_ref.deleted_at),
                }
                LOG.debug("Send delete notification for "
                          "<snapshot_id: %(snapshot_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>" %
                          {'snapshot_id': snapshot_ref.id,
                           'project_id': snapshot_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'delete.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'delete.end', extra_usage_info=local_extra_info)
            except Exception as e:
                LOG.error(_LE("Failed to send delete notification for snapshot"
                              "%s.") % snapshot_ref.id)
                print(traceback.format_exc(e))

    print(_("Volume usage audit completed"))
def main():
    objects.register_all()
    admin_context = context.get_admin_context()
    CONF(sys.argv[1:], project='cinder',
         version=version.version_string())
    logging.setup(CONF, "cinder")
    LOG = logging.getLogger("cinder")
    rpc.init(CONF)
    begin, end = utils.last_completed_audit_period()
    if CONF.start_time:
        begin = datetime.datetime.strptime(CONF.start_time,
                                           "%Y-%m-%d %H:%M:%S")
    if CONF.end_time:
        end = datetime.datetime.strptime(CONF.end_time,
                                         "%Y-%m-%d %H:%M:%S")
    if not end > begin:
        msg = _("The end time (%(end)s) must be after the start "
                "time (%(start)s).") % {'start': begin,
                                        'end': end}
        LOG.error(msg)
        sys.exit(-1)
    LOG.debug("Starting volume usage audit")
    msg = _("Creating usages for %(begin_period)s until %(end_period)s")
    LOG.debug(msg, {"begin_period": str(begin), "end_period": str(end)})

    extra_info = {
        'audit_period_beginning': str(begin),
        'audit_period_ending': str(end),
    }

    volumes = db.volume_get_active_by_window(admin_context,
                                             begin,
                                             end)
    LOG.debug("Found %d volumes"), len(volumes)
    for volume_ref in volumes:
        try:
            LOG.debug("Send exists notification for <volume_id: "
                      "%(volume_id)s> <project_id %(project_id)s> "
                      "<%(extra_info)s>",
                      {'volume_id': volume_ref.id,
                       'project_id': volume_ref.project_id,
                       'extra_info': extra_info})
            cinder.volume.utils.notify_about_volume_usage(
                admin_context,
                volume_ref,
                'exists', extra_usage_info=extra_info)
        except Exception as exc_msg:
            LOG.exception(_LE("Exists volume notification failed: %s"),
                          exc_msg, resource=volume_ref)

        if (CONF.send_actions and
                volume_ref.created_at > begin and
                volume_ref.created_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(volume_ref.created_at),
                    'audit_period_ending': str(volume_ref.created_at),
                }
                LOG.debug("Send create notification for "
                          "<volume_id: %(volume_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>",
                          {'volume_id': volume_ref.id,
                           'project_id': volume_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'create.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'create.end', extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Create volume notification failed: %s"),
                              exc_msg, resource=volume_ref)

        if (CONF.send_actions and volume_ref.deleted_at and
                volume_ref.deleted_at > begin and
                volume_ref.deleted_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(volume_ref.deleted_at),
                    'audit_period_ending': str(volume_ref.deleted_at),
                }
                LOG.debug("Send delete notification for "
                          "<volume_id: %(volume_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>",
                          {'volume_id': volume_ref.id,
                           'project_id': volume_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'delete.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'delete.end', extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Delete volume notification failed: %s"),
                              exc_msg, resource=volume_ref)

    snapshots = objects.SnapshotList.get_active_by_window(admin_context,
                                                          begin, end)
    LOG.debug("Found %d snapshots"), len(snapshots)
    for snapshot_ref in snapshots:
        try:
            LOG.debug("Send notification for <snapshot_id: %(snapshot_id)s> "
                      "<project_id %(project_id)s> <%(extra_info)s>",
                      {'snapshot_id': snapshot_ref.id,
                       'project_id': snapshot_ref.project_id,
                       'extra_info': extra_info})
            cinder.volume.utils.notify_about_snapshot_usage(admin_context,
                                                            snapshot_ref,
                                                            'exists',
                                                            extra_info)
        except Exception as exc_msg:
            LOG.exception(_LE("Exists snapshot notification failed: %s"),
                          exc_msg, resource=snapshot_ref)

        if (CONF.send_actions and
                snapshot_ref.created_at > begin and
                snapshot_ref.created_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(snapshot_ref.created_at),
                    'audit_period_ending': str(snapshot_ref.created_at),
                }
                LOG.debug("Send create notification for "
                          "<snapshot_id: %(snapshot_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>",
                          {'snapshot_id': snapshot_ref.id,
                           'project_id': snapshot_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'create.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'create.end', extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Create snapshot notification failed: %s"),
                              exc_msg, resource=snapshot_ref)

        if (CONF.send_actions and snapshot_ref.deleted_at and
                snapshot_ref.deleted_at > begin and
                snapshot_ref.deleted_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(snapshot_ref.deleted_at),
                    'audit_period_ending': str(snapshot_ref.deleted_at),
                }
                LOG.debug("Send delete notification for "
                          "<snapshot_id: %(snapshot_id)s> "
                          "<project_id %(project_id)s> <%(extra_info)s>",
                          {'snapshot_id': snapshot_ref.id,
                           'project_id': snapshot_ref.project_id,
                           'extra_info': local_extra_info})
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'delete.start', extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'delete.end', extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Delete snapshot notification failed: %s"),
                              exc_msg, resource=snapshot_ref)

    LOG.debug("Volume usage audit completed")
Exemple #33
0
def main():
    objects.register_all()
    admin_context = context.get_admin_context()
    CONF(sys.argv[1:], project='cinder', version=version.version_string())
    logging.setup(CONF, "cinder")
    LOG = logging.getLogger("cinder")
    rpc.init(CONF)
    begin, end = utils.last_completed_audit_period()
    if CONF.start_time:
        begin = datetime.datetime.strptime(CONF.start_time,
                                           "%Y-%m-%d %H:%M:%S")
    if CONF.end_time:
        end = datetime.datetime.strptime(CONF.end_time, "%Y-%m-%d %H:%M:%S")
    if not end > begin:
        msg = _("The end time (%(end)s) must be after the start "
                "time (%(start)s).") % {
                    'start': begin,
                    'end': end
                }
        LOG.error(msg)
        sys.exit(-1)
    LOG.debug("Starting volume usage audit")
    msg = _("Creating usages for %(begin_period)s until %(end_period)s")
    LOG.debug(msg, {"begin_period": str(begin), "end_period": str(end)})

    extra_info = {
        'audit_period_beginning': str(begin),
        'audit_period_ending': str(end),
    }

    volumes = db.volume_get_active_by_window(admin_context, begin, end)
    LOG.debug("Found %d volumes", len(volumes))
    for volume_ref in volumes:
        try:
            LOG.debug(
                "Send exists notification for <volume_id: "
                "%(volume_id)s> <project_id %(project_id)s> "
                "<%(extra_info)s>", {
                    'volume_id': volume_ref.id,
                    'project_id': volume_ref.project_id,
                    'extra_info': extra_info
                })
            cinder.volume.utils.notify_about_volume_usage(
                admin_context,
                volume_ref,
                'exists',
                extra_usage_info=extra_info)
        except Exception as exc_msg:
            LOG.exception(_LE("Exists volume notification failed: %s"),
                          exc_msg,
                          resource=volume_ref)

        if (CONF.send_actions and volume_ref.created_at > begin
                and volume_ref.created_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(volume_ref.created_at),
                    'audit_period_ending': str(volume_ref.created_at),
                }
                LOG.debug(
                    "Send create notification for "
                    "<volume_id: %(volume_id)s> "
                    "<project_id %(project_id)s> <%(extra_info)s>", {
                        'volume_id': volume_ref.id,
                        'project_id': volume_ref.project_id,
                        'extra_info': local_extra_info
                    })
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'create.start',
                    extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'create.end',
                    extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Create volume notification failed: %s"),
                              exc_msg,
                              resource=volume_ref)

        if (CONF.send_actions and volume_ref.deleted_at
                and volume_ref.deleted_at > begin
                and volume_ref.deleted_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(volume_ref.deleted_at),
                    'audit_period_ending': str(volume_ref.deleted_at),
                }
                LOG.debug(
                    "Send delete notification for "
                    "<volume_id: %(volume_id)s> "
                    "<project_id %(project_id)s> <%(extra_info)s>", {
                        'volume_id': volume_ref.id,
                        'project_id': volume_ref.project_id,
                        'extra_info': local_extra_info
                    })
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'delete.start',
                    extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_volume_usage(
                    admin_context,
                    volume_ref,
                    'delete.end',
                    extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Delete volume notification failed: %s"),
                              exc_msg,
                              resource=volume_ref)

    snapshots = objects.SnapshotList.get_active_by_window(
        admin_context, begin, end)
    LOG.debug("Found %d snapshots", len(snapshots))
    for snapshot_ref in snapshots:
        try:
            LOG.debug(
                "Send notification for <snapshot_id: %(snapshot_id)s> "
                "<project_id %(project_id)s> <%(extra_info)s>", {
                    'snapshot_id': snapshot_ref.id,
                    'project_id': snapshot_ref.project_id,
                    'extra_info': extra_info
                })
            cinder.volume.utils.notify_about_snapshot_usage(
                admin_context, snapshot_ref, 'exists', extra_info)
        except Exception as exc_msg:
            LOG.exception(_LE("Exists snapshot notification failed: %s"),
                          exc_msg,
                          resource=snapshot_ref)

        if (CONF.send_actions and snapshot_ref.created_at > begin
                and snapshot_ref.created_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(snapshot_ref.created_at),
                    'audit_period_ending': str(snapshot_ref.created_at),
                }
                LOG.debug(
                    "Send create notification for "
                    "<snapshot_id: %(snapshot_id)s> "
                    "<project_id %(project_id)s> <%(extra_info)s>", {
                        'snapshot_id': snapshot_ref.id,
                        'project_id': snapshot_ref.project_id,
                        'extra_info': local_extra_info
                    })
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'create.start',
                    extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'create.end',
                    extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Create snapshot notification failed: %s"),
                              exc_msg,
                              resource=snapshot_ref)

        if (CONF.send_actions and snapshot_ref.deleted_at
                and snapshot_ref.deleted_at > begin
                and snapshot_ref.deleted_at < end):
            try:
                local_extra_info = {
                    'audit_period_beginning': str(snapshot_ref.deleted_at),
                    'audit_period_ending': str(snapshot_ref.deleted_at),
                }
                LOG.debug(
                    "Send delete notification for "
                    "<snapshot_id: %(snapshot_id)s> "
                    "<project_id %(project_id)s> <%(extra_info)s>", {
                        'snapshot_id': snapshot_ref.id,
                        'project_id': snapshot_ref.project_id,
                        'extra_info': local_extra_info
                    })
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'delete.start',
                    extra_usage_info=local_extra_info)
                cinder.volume.utils.notify_about_snapshot_usage(
                    admin_context,
                    snapshot_ref,
                    'delete.end',
                    extra_usage_info=local_extra_info)
            except Exception as exc_msg:
                LOG.exception(_LE("Delete snapshot notification failed: %s"),
                              exc_msg,
                              resource=snapshot_ref)

    LOG.debug("Volume usage audit completed")
 def test_month_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='month@2')
     self.assertEqual(datetime.datetime(day=2, month=2, year=2012), begin)
     self.assertEqual(datetime.datetime(day=2, month=3, year=2012), end)
Exemple #35
0
 def test_month_with_offset_before_current(self):
     begin, end = utils.last_completed_audit_period(unit='month@2')
     self.assertEqual(datetime.datetime(day=2, month=2, year=2012), begin)
     self.assertEqual(datetime.datetime(day=2, month=3, year=2012), end)