2
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Copyright (c) 2011 OpenStack, LLC.
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
11
# http://www.apache.org/licenses/LICENSE-2.0
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
19
"""Cron script to generate usage notifications for volumes existing during
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
27
Time periods are specified as 'hour', 'month', 'day' or 'year'
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.
44
# If ../nova/__init__.py exists, add ../ to Python search path, so that
45
# it will override what happens to be installed in /usr/(local/)lib/python...
46
POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
49
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
50
sys.path.insert(0, POSSIBLE_TOPDIR)
52
gettext.install('nova', unicode=1)
53
from nova import context
55
from nova import exception
56
from nova import flags
57
from nova.openstack.common import log as logging
58
from nova.openstack.common import rpc
59
from nova import utils
60
from nova.volume import utils as volume_utils
64
if __name__ == '__main__':
65
admin_context = context.get_admin_context()
66
utils.default_cfgfile()
69
begin, end = utils.last_completed_audit_period()
70
print "Starting volume usage audit"
71
print "Creating usages for %s until %s" % (str(begin), str(end))
72
volumes = db.volume_get_active_by_window(admin_context,
75
print "Found %d volumes" % len(volumes)
76
for volume_ref in volumes:
78
volume_utils.notify_usage_exists(
79
admin_context, volume_ref)
81
print traceback.format_exc(e)
82
print "Volume usage audit completed"