Browse Source

adding audit usage script

John Tran 6 years ago
parent
commit
e640b287ba
2 changed files with 91 additions and 0 deletions
  1. 79
    0
      files/default/cinder-volume-usage-audit
  2. 12
    0
      recipes/scheduler.rb

+ 79
- 0
files/default/cinder-volume-usage-audit View File

@@ -0,0 +1,79 @@
1
+#!/usr/bin/env python
2
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
+
4
+# Copyright (c) 2011 Openstack, LLC.
5
+# All Rights Reserved.
6
+#
7
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
8
+#    not use this file except in compliance with the License. You may obtain
9
+#    a copy of the License at
10
+#
11
+#         http://www.apache.org/licenses/LICENSE-2.0
12
+#
13
+#    Unless required by applicable law or agreed to in writing, software
14
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
+#    License for the specific language governing permissions and limitations
17
+#    under the License.
18
+
19
+"""Cron script to generate usage notifications for volumes existing during
20
+   the audit period.
21
+
22
+   Together with the notifications generated by volumes
23
+   create/delete/resize, over that time period, this allows an external
24
+   system consuming usage notification feeds to calculate volume usage
25
+   for each tenant.
26
+
27
+   Time periods are specified as 'hour', 'month', 'day' or 'year'
28
+
29
+   hour = previous hour. If run at 9:07am, will generate usage for 8-9am.
30
+   month = previous month. If the script is run April 1, it will generate
31
+           usages for March 1 through March 31.
32
+   day = previous day. if run on July 4th, it generates usages for July 3rd.
33
+   year = previous year. If run on Jan 1, it generates usages for
34
+        Jan 1 through Dec 31 of the previous year.
35
+"""
36
+
37
+import gettext
38
+import os
39
+import sys
40
+import traceback
41
+
42
+# If ../cinder/__init__.py exists, add ../ to Python search path, so that
43
+# it will override what happens to be installed in /usr/(local/)lib/python...
44
+POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
45
+                                   os.pardir,
46
+                                   os.pardir))
47
+if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'cinder', '__init__.py')):
48
+    sys.path.insert(0, POSSIBLE_TOPDIR)
49
+
50
+gettext.install('cinder', unicode=1)
51
+from cinder import context
52
+from cinder import db
53
+from cinder import flags
54
+from cinder.openstack.common import log as logging
55
+from cinder.openstack.common import rpc
56
+from cinder import utils
57
+import cinder.volume.utils
58
+
59
+
60
+FLAGS = flags.FLAGS
61
+
62
+if __name__ == '__main__':
63
+    admin_context = context.get_admin_context()
64
+    flags.parse_args(sys.argv)
65
+    logging.setup("cinder")
66
+    begin, end = utils.last_completed_audit_period('day')
67
+    print "Starting volume usage audit"
68
+    print "Creating usages for %s until %s" % (str(begin), str(end))
69
+    volumes = db.volume_get_active_by_window(admin_context,
70
+                                             begin,
71
+                                             end)
72
+    print "Found %d volumes" % len(volumes)
73
+    for volume_ref in volumes:
74
+        try:
75
+            cinder.volume.utils.notify_usage_exists(
76
+                    admin_context, volume_ref)
77
+        except Exception, e:
78
+            print traceback.format_exc(e)
79
+    print "Volume usage audit completed"

+ 12
- 0
recipes/scheduler.rb View File

@@ -54,6 +54,18 @@ service "cinder-scheduler" do
54 54
   action [ :enable, :start ]
55 55
 end
56 56
 
57
+cookbook_file "/usr/local/bin/cinder-volume-usage-audit" do
58
+  source "cinder-volume-usage-audit"
59
+  mode  00755
60
+  owner "root"
61
+  group "root"
62
+end
63
+
64
+# cronjob run every minute
65
+cron "cinder-volume-usage-audit" do
66
+  command "/usr/local/bin/cinder-volume-usage-audit > /var/log/cinder/audit.log 2>&1"
67
+end
68
+
57 69
 template "/etc/cinder/cinder.conf" do
58 70
   source "cinder.conf.erb"
59 71
   group  node["cinder"]["group"]

Loading…
Cancel
Save