~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to bin/nova-instance-usage-audit

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 instances existing
20
 
   during the audit period.
21
 
 
22
 
   Together with the notifications generated by compute on instance
23
 
   create/delete/resize, over that time period, this allows an external
24
 
   system consuming usage notification feeds to calculate instance 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 datetime
38
 
import gettext
39
 
import os
40
 
import sys
41
 
import time
42
 
import traceback
43
 
 
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]),
47
 
                                   os.pardir,
48
 
                                   os.pardir))
49
 
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
50
 
    sys.path.insert(0, POSSIBLE_TOPDIR)
51
 
 
52
 
gettext.install('nova', unicode=1)
53
 
import nova.compute.utils
54
 
from nova import context
55
 
from nova import db
56
 
from nova import exception
57
 
from nova import flags
58
 
from nova.openstack.common import log as logging
59
 
from nova.openstack.common import rpc
60
 
from nova import utils
61
 
 
62
 
 
63
 
FLAGS = flags.FLAGS
64
 
 
65
 
if __name__ == '__main__':
66
 
    admin_context = context.get_admin_context()
67
 
    flags.parse_args(sys.argv)
68
 
    logging.setup("nova")
69
 
    begin, end = utils.last_completed_audit_period()
70
 
    print "Starting instance usage audit"
71
 
    print "Creating usages for %s until %s" % (str(begin), str(end))
72
 
    instances = db.instance_get_active_by_window_joined(admin_context,
73
 
                                                        begin,
74
 
                                                        end)
75
 
    print "Found %d instances" % len(instances)
76
 
    for instance_ref in instances:
77
 
        try:
78
 
            nova.compute.utils.notify_usage_exists(
79
 
                    admin_context, instance_ref,
80
 
                    ignore_missing_network_data=False)
81
 
        except Exception, e:
82
 
            print traceback.format_exc(e)
83
 
    print "Instance usage audit completed"