OpenStack中怎么扩展自定义功能
OpenStack中怎么扩展自定义功能,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
Text代码
[pipeline:openstackapi11] pipeline = faultwrap authtoken keystonecontext ratelimit audit extensions osapiapp11 [filter:audit] paste.filter_factory = nova.api.openstack.audit:AuditMiddleware.factory
然后我们写一个Middleware:
Python代码
import time
from nova import log as logging
from nova import wsgi as base_wsgi
from nova.api.openstack import wsgi
LOG = logging.getLogger('nova.api.audit')
class AuditMiddleware(base_wsgi.Middleware):
"""store POST/PUT/DELETE api request for audit."""
def __init__(self, application, audit_methods='POST,PUT,DELETE'):
base_wsgi.Middleware.__init__(self, application)
self._audit_methods = audit_methods.split(",")
def process_request(self, req):
self._need_audit = req.method in self._audit_methods
if self._need_audit:
self._request = req
self._requested_at = time.time()
def process_response(self, response):
if self._need_audit and response.status_int >= 200 and response.status_int < 300:
self._store_log(response)
return response
def _store_log(self, response):
req = self._request
LOG.info("tenant: %s, user: %s, %s: %s, at: %s",
req.headers.get('X-Tenant', 'admin'),
req.headers.get('X-User', 'admin'),
req.method,
req.path_info,
self._requested_at)
重启一下nova-api进程,然后在dashboard上做一些操作,我们就能在日志文件里面看到如下的信息:
Text代码
tenant: 1, user: admin, POST: /1/os-security-group-rules, at: 1326352441.16 tenant: 1, user: admin, DELETE: /1/servers/32, at: 1326353021.58
这里默认记录所有的非GET请求,如果不想将PUT请求记录(PUT对应更新),在配置文件里面更改一下:
Text代码
[filter:audit] audit_methods=POST,DELETE
关于OpenStack中怎么扩展自定义功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。