nova/bin/instance-usage-audit

78 lines
2.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2011 Openstack, LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Cron script to generate usage notifications for instances neither created
2011-06-28 09:08:35 +00:00
nor destroyed in a given time period.
Together with the notifications generated by compute on instance
2011-06-28 09:08:35 +00:00
create/delete/resize, over that ime period, this allows an external
system consuming usage notification feeds to calculate instance usage
for each tenant.
Time periods are specified as 'hour', 'month', 'day' or 'year'
hour = previous hour. If run at 9:07am, will generate usage for 8-9am.
month = previous month. If the script is run April 1, it will generate
usages for March 1 thry March 31.
day = previous day. if run on July 4th, it generates usages for July 3rd.
year = previous year. If run on Jan 1, it generates usages for
2011-06-28 09:08:35 +00:00
Jan 1 thru Dec 31 of the previous year.
"""
import datetime
import gettext
import os
import sys
import time
# If ../nova/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python...
POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir,
os.pardir))
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
sys.path.insert(0, POSSIBLE_TOPDIR)
gettext.install('nova', unicode=1)
from nova import context
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova import utils
from nova.compute.utils import notify_usage_exists
FLAGS = flags.FLAGS
if __name__ == '__main__':
admin_context = context.get_admin_context()
utils.default_flagfile()
flags.FLAGS(sys.argv)
logging.setup()
begin, end = utils.current_audit_period()
print "Creating usages for %s until %s" % (str(begin), str(end))
instances = db.instance_get_active_by_window_joined(admin_context,
begin,
end)
print "%s instances" % len(instances)
for instance_ref in instances:
notify_usage_exists(instance_ref)