OpenStack Compute (Nova)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
4.6 KiB

  1. # Copyright 2012 OpenStack Foundation
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import datetime
  16. import webob.exc
  17. from nova.api.openstack import wsgi
  18. from nova.compute import api as compute
  19. from nova.compute import rpcapi as compute_rpcapi
  20. from nova.i18n import _
  21. from nova.policies import instance_usage_audit_log as iual_policies
  22. from nova import utils
  23. class InstanceUsageAuditLogController(wsgi.Controller):
  24. def __init__(self):
  25. super(InstanceUsageAuditLogController, self).__init__()
  26. self.host_api = compute.HostAPI()
  27. @wsgi.expected_errors(())
  28. def index(self, req):
  29. context = req.environ['nova.context']
  30. context.can(iual_policies.BASE_POLICY_NAME % 'list', target={})
  31. task_log = self._get_audit_task_logs(context)
  32. return {'instance_usage_audit_logs': task_log}
  33. @wsgi.expected_errors(400)
  34. def show(self, req, id):
  35. context = req.environ['nova.context']
  36. context.can(iual_policies.BASE_POLICY_NAME % 'show', target={})
  37. try:
  38. if '.' in id:
  39. before_date = datetime.datetime.strptime(str(id),
  40. "%Y-%m-%d %H:%M:%S.%f")
  41. else:
  42. before_date = datetime.datetime.strptime(str(id),
  43. "%Y-%m-%d %H:%M:%S")
  44. except ValueError:
  45. msg = _("Invalid timestamp for date %s") % id
  46. raise webob.exc.HTTPBadRequest(explanation=msg)
  47. task_log = self._get_audit_task_logs(context, before=before_date)
  48. return {'instance_usage_audit_log': task_log}
  49. def _get_audit_task_logs(self, context, before=None):
  50. """Returns a full log for all instance usage audit tasks on all
  51. computes.
  52. :param context: Nova request context.
  53. :param before: By default we look for the audit period most recently
  54. completed before this datetime. Has no effect if both begin and end
  55. are specified.
  56. """
  57. begin, end = utils.last_completed_audit_period(before=before)
  58. task_logs = self.host_api.task_log_get_all(context,
  59. "instance_usage_audit",
  60. begin, end)
  61. # We do this in this way to include disabled compute services,
  62. # which can have instances on them. (mdragon)
  63. filters = {'topic': compute_rpcapi.RPC_TOPIC}
  64. services = self.host_api.service_get_all(context, filters=filters)
  65. hosts = set(serv['host'] for serv in services)
  66. seen_hosts = set()
  67. done_hosts = set()
  68. running_hosts = set()
  69. total_errors = 0
  70. total_items = 0
  71. for tlog in task_logs:
  72. seen_hosts.add(tlog['host'])
  73. if tlog['state'] == "DONE":
  74. done_hosts.add(tlog['host'])
  75. if tlog['state'] == "RUNNING":
  76. running_hosts.add(tlog['host'])
  77. total_errors += tlog['errors']
  78. total_items += tlog['task_items']
  79. log = {tl['host']: dict(state=tl['state'],
  80. instances=tl['task_items'],
  81. errors=tl['errors'],
  82. message=tl['message'])
  83. for tl in task_logs}
  84. missing_hosts = hosts - seen_hosts
  85. overall_status = "%s hosts done. %s errors." % (
  86. 'ALL' if len(done_hosts) == len(hosts)
  87. else "%s of %s" % (len(done_hosts), len(hosts)),
  88. total_errors)
  89. return dict(period_beginning=str(begin),
  90. period_ending=str(end),
  91. num_hosts=len(hosts),
  92. num_hosts_done=len(done_hosts),
  93. num_hosts_running=len(running_hosts),
  94. num_hosts_not_run=len(missing_hosts),
  95. hosts_not_run=list(missing_hosts),
  96. total_instances=total_items,
  97. total_errors=total_errors,
  98. overall_status=overall_status,
  99. log=log)