Browse Source

Allow user to specify audit period.

The script 'cinder-volume-usage-audit' is generally run by cron to
generate volume and snapshot exists audit notifications that can be
consumed by ceilometer or other auditing systems. In this mode it sends
notifications for volumes that existed in the 'last completed audit
period' as calculated by cinder.utils.last_completed_audit_period()

However, it may sometimes be necessary to be able to send notifications
for audit periods other than than the last completed audit period. For
example if the host running the audit script was down for a period.

This patch provides three optional command line parameters to the script
cinder-volume-usage-audit. If no parameters are provided then the
operation of the script is unchanged. If parameters are provided then
they have the following effects:

--start_time specifies a date and time that is used in place of the
start of the last completed audit period.

--end_time specifies a date and time that is used in place of the end of
the last completed audit period.

--send_actions As well as sending exits notifications the script will
send any create or delete notifications that would have been sent during
the specified audit period.

Closes-Bug: #1259147

Change-Id: I0cfe963ea59d811ba4bac0bd3de31382a088b8c8
tags/2014.1.b2
Ollie Leahy 5 years ago
parent
commit
d09b7c78b3
1 changed files with 157 additions and 4 deletions
  1. 157
    4
      bin/cinder-volume-usage-audit

+ 157
- 4
bin/cinder-volume-usage-audit View File

@@ -34,6 +34,7 @@
34 34
 
35 35
 from __future__ import print_function
36 36
 
37
+from datetime import datetime
37 38
 import os
38 39
 import sys
39 40
 import traceback
@@ -51,8 +52,6 @@ if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'cinder', '__init__.py')):
51 52
 from cinder.openstack.common import gettextutils
52 53
 gettextutils.install('cinder')
53 54
 
54
-# Need to register global_opts
55
-from cinder.common import config  # noqa
56 55
 from cinder import context
57 56
 from cinder import db
58 57
 from cinder.openstack.common import log as logging
@@ -62,6 +61,23 @@ import cinder.volume.utils
62 61
 
63 62
 
64 63
 CONF = cfg.CONF
64
+script_opts = [
65
+    cfg.StrOpt('start_time',
66
+               default=None,
67
+               help="If this option is specified then the start time "
68
+                    "specified is used instead of the start time of the "
69
+                    "last completed audit period."),
70
+    cfg.StrOpt('end_time',
71
+               default=None,
72
+               help="If this option is specified then the end time "
73
+                    "specified is used instead of the end time of the "
74
+                    "last completed audit period."),
75
+    cfg.BoolOpt('send_actions',
76
+                default=False,
77
+                help="Send the volume and snapshot create and delete "
78
+                     "notifications generated in the specified period."),
79
+]
80
+CONF.register_cli_opts(script_opts)
65 81
 
66 82
 
67 83
 if __name__ == '__main__':
@@ -69,7 +85,19 @@ if __name__ == '__main__':
69 85
     CONF(sys.argv[1:], project='cinder',
70 86
          version=version.version_string())
71 87
     logging.setup("cinder")
88
+    LOG = logging.getLogger("cinder")
72 89
     begin, end = utils.last_completed_audit_period()
90
+    if CONF.start_time:
91
+        begin = datetime.strptime(CONF.start_time, "%Y-%m-%d %H:%M:%S")
92
+    if CONF.end_time:
93
+        end = datetime.strptime(CONF.end_time, "%Y-%m-%d %H:%M:%S")
94
+    if not end > begin:
95
+        msg = _("The end time (%(end)s) must be after the start "
96
+                "time (%(start)s).") % {'start': begin,
97
+                                        'end': end}
98
+        print(msg)
99
+        LOG.error(msg)
100
+        sys.exit(-1)
73 101
     print(_("Starting volume usage audit"))
74 102
     msg = _("Creating usages for %(begin_period)s until %(end_period)s")
75 103
     print(msg % {"begin_period": str(begin), "end_period": str(end)})
@@ -85,22 +113,147 @@ if __name__ == '__main__':
85 113
     print(_("Found %d volumes") % len(volumes))
86 114
     for volume_ref in volumes:
87 115
         try:
88
-            cinder.volume.utils.notify_usage_exists(admin_context,
89
-                                                    volume_ref)
116
+            LOG.debug(_("Send exists notification for <volume_id: "
117
+                        "%(volume_id)s> <project_id %(project_id)s> "
118
+                        "<%(extra_info)s>") %
119
+                      {'volume_id': volume_ref.id,
120
+                       'project_id': volume_ref.project_id,
121
+                       'extra_info': extra_info})
122
+            cinder.volume.utils.notify_about_volume_usage(
123
+                admin_context,
124
+                volume_ref,
125
+                'exists', extra_usage_info=extra_info)
90 126
         except Exception as e:
127
+            LOG.error(_("Failed to send exists notification for volume %s.") %
128
+                      volume_ref.id)
91 129
             print(traceback.format_exc(e))
92 130
 
131
+        if (CONF.send_actions and
132
+                volume_ref.created_at > begin and
133
+                volume_ref.created_at < end):
134
+            try:
135
+                local_extra_info = {
136
+                    'audit_period_beginning': str(volume_ref.created_at),
137
+                    'audit_period_ending': str(volume_ref.created_at),
138
+                }
139
+                LOG.debug(_("Send create notification for "
140
+                          "<volume_id: %(volume_id)s> "
141
+                          "<project_id %(project_id)s> <%(extra_info)s>") %
142
+                          {'volume_id': volume_ref.id,
143
+                           'project_id': volume_ref.project_id,
144
+                           'extra_info': local_extra_info})
145
+                cinder.volume.utils.notify_about_volume_usage(
146
+                    admin_context,
147
+                    volume_ref,
148
+                    'create.start', extra_usage_info=local_extra_info)
149
+                cinder.volume.utils.notify_about_volume_usage(
150
+                    admin_context,
151
+                    volume_ref,
152
+                    'create.end', extra_usage_info=local_extra_info)
153
+            except Exception as e:
154
+                LOG.error(_("Failed to send create notification for "
155
+                            "volume %s.") % volume_ref.id)
156
+                print(traceback.format_exc(e))
157
+
158
+        if (CONF.send_actions and volume_ref.deleted_at and
159
+                volume_ref.deleted_at > begin and
160
+                volume_ref.deleted_at < end):
161
+            try:
162
+                local_extra_info = {
163
+                    'audit_period_beginning': str(volume_ref.deleted_at),
164
+                    'audit_period_ending': str(volume_ref.deleted_at),
165
+                }
166
+                LOG.debug(_("Send delete notification for "
167
+                          "<volume_id: %(volume_id)s> "
168
+                          "<project_id %(project_id)s> <%(extra_info)s>") %
169
+                          {'volume_id': volume_ref.id,
170
+                           'project_id': volume_ref.project_id,
171
+                           'extra_info': local_extra_info})
172
+                cinder.volume.utils.notify_about_volume_usage(
173
+                    admin_context,
174
+                    volume_ref,
175
+                    'delete.start', extra_usage_info=local_extra_info)
176
+                cinder.volume.utils.notify_about_volume_usage(
177
+                    admin_context,
178
+                    volume_ref,
179
+                    'delete.end', extra_usage_info=local_extra_info)
180
+            except Exception as e:
181
+                LOG.error(_("Failed to send delete notification for volume "
182
+                            "%s.") % volume_ref.id)
183
+                print(traceback.format_exc(e))
184
+
93 185
     snapshots = db.snapshot_get_active_by_window(admin_context,
94 186
                                                  begin,
95 187
                                                  end)
96 188
     print(_("Found %d snapshots") % len(snapshots))
97 189
     for snapshot_ref in snapshots:
98 190
         try:
191
+            LOG.debug(_("Send notification for <snapshot_id: %(snapshot_id)s> "
192
+                      "<project_id %(project_id)s> <%(extra_info)s>") %
193
+                      {'snapshot_id': snapshot_ref.id,
194
+                       'project_id': snapshot_ref.project_id,
195
+                       'extra_info': extra_info})
99 196
             cinder.volume.utils.notify_about_snapshot_usage(admin_context,
100 197
                                                             snapshot_ref,
101 198
                                                             'exists',
102 199
                                                             extra_info)
103 200
         except Exception as e:
201
+            LOG.error(_("Failed to send exists notification for snapshot %s.")
202
+                      % snapshot_ref.id)
104 203
             print(traceback.format_exc(e))
105 204
 
205
+        if (CONF.send_actions and
206
+                snapshot_ref.created_at > begin and
207
+                snapshot_ref.created_at < end):
208
+            try:
209
+                local_extra_info = {
210
+                    'audit_period_beginning': str(snapshot_ref.created_at),
211
+                    'audit_period_ending': str(snapshot_ref.created_at),
212
+                }
213
+                LOG.debug(_("Send create notification for "
214
+                          "<snapshot_id: %(snapshot_id)s> "
215
+                          "<project_id %(project_id)s> <%(extra_info)s>") %
216
+                          {'snapshot_id': snapshot_ref.id,
217
+                           'project_id': snapshot_ref.project_id,
218
+                           'extra_info': local_extra_info})
219
+                cinder.volume.utils.notify_about_snapshot_usage(
220
+                    admin_context,
221
+                    snapshot_ref,
222
+                    'create.start', extra_usage_info=local_extra_info)
223
+                cinder.volume.utils.notify_about_snapshot_usage(
224
+                    admin_context,
225
+                    snapshot_ref,
226
+                    'create.end', extra_usage_info=local_extra_info)
227
+            except Exception as e:
228
+                LOG.error(_("Failed to send create notification for snapshot "
229
+                            "%s.") % snapshot_ref.id)
230
+                print(traceback.format_exc(e))
231
+
232
+        if (CONF.send_actions and snapshot_ref.deleted_at and
233
+                snapshot_ref.deleted_at > begin and
234
+                snapshot_ref.deleted_at < end):
235
+            try:
236
+                local_extra_info = {
237
+                    'audit_period_beginning': str(snapshot_ref.deleted_at),
238
+                    'audit_period_ending': str(snapshot_ref.deleted_at),
239
+                }
240
+                LOG.debug(_("Send delete notification for "
241
+                          "<snapshot_id: %(snapshot_id)s> "
242
+                          "<project_id %(project_id)s> <%(extra_info)s>") %
243
+                          {'snapshot_id': snapshot_ref.id,
244
+                           'project_id': snapshot_ref.project_id,
245
+                           'extra_info': local_extra_info})
246
+                cinder.volume.utils.notify_about_snapshot_usage(
247
+                    admin_context,
248
+                    snapshot_ref,
249
+                    'delete.start', extra_usage_info=local_extra_info)
250
+                cinder.volume.utils.notify_about_snapshot_usage(
251
+                    admin_context,
252
+                    snapshot_ref,
253
+                    'delete.end', extra_usage_info=local_extra_info)
254
+            except Exception as e:
255
+                LOG.error(_("Failed to send delete notification for snapshot "
256
+                            "%s.") % snapshot_ref.id)
257
+                print(traceback.format_exc(e))
258
+
106 259
     print(_("Volume usage audit completed"))

Loading…
Cancel
Save