summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Tran <jhtran@att.com>2013-04-08 17:08:44 -0700
committerJohn Tran <jhtran@att.com>2013-04-09 00:17:51 +0000
commite640b287ba8982b8083b7c32da35c1cfae5c4817 (patch)
tree098c5c7db5995f3fd8fd3779b4eb01b6f7496f4c
parent3ee8f5780325626260c4c30273863a8be28e3721 (diff)
adding audit usage script
-rwxr-xr-xfiles/default/cinder-volume-usage-audit79
-rw-r--r--recipes/scheduler.rb12
2 files changed, 91 insertions, 0 deletions
diff --git a/files/default/cinder-volume-usage-audit b/files/default/cinder-volume-usage-audit
new file mode 100755
index 0000000..403e901
--- /dev/null
+++ b/files/default/cinder-volume-usage-audit
@@ -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
37import gettext
38import os
39import sys
40import 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...
44POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
45 os.pardir,
46 os.pardir))
47if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'cinder', '__init__.py')):
48 sys.path.insert(0, POSSIBLE_TOPDIR)
49
50gettext.install('cinder', unicode=1)
51from cinder import context
52from cinder import db
53from cinder import flags
54from cinder.openstack.common import log as logging
55from cinder.openstack.common import rpc
56from cinder import utils
57import cinder.volume.utils
58
59
60FLAGS = flags.FLAGS
61
62if __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"
diff --git a/recipes/scheduler.rb b/recipes/scheduler.rb
index 42541ca..09f26c4 100644
--- a/recipes/scheduler.rb
+++ b/recipes/scheduler.rb
@@ -54,6 +54,18 @@ service "cinder-scheduler" do
54 action [ :enable, :start ] 54 action [ :enable, :start ]
55end 55end
56 56
57cookbook_file "/usr/local/bin/cinder-volume-usage-audit" do
58 source "cinder-volume-usage-audit"
59 mode 00755
60 owner "root"
61 group "root"
62end
63
64# cronjob run every minute
65cron "cinder-volume-usage-audit" do
66 command "/usr/local/bin/cinder-volume-usage-audit > /var/log/cinder/audit.log 2>&1"
67end
68
57template "/etc/cinder/cinder.conf" do 69template "/etc/cinder/cinder.conf" do
58 source "cinder.conf.erb" 70 source "cinder.conf.erb"
59 group node["cinder"]["group"] 71 group node["cinder"]["group"]