Healthcheck middleware 插件

class oslo_middleware.healthcheck.Healthcheck(application: WSGIApplication | None, conf: dict[str, ty.Any] | cfg.ConfigOpts | None = None)

用于监控的 Healthcheck 应用。

它将响应 200,正文为“OK”。或者,如果其中一个后端报告应用程序问题,则响应 503,正文为原因。

原因如下:

  • 负载均衡器可以“ping”此 URL 以确定服务可用性。

  • 提供一个类似于 apache 中“mod_status”的端点,该端点可以提供有关服务器活动的信息(或不提供信息,具体取决于配置)。

  • (等等)

注意

此中间件指示 API 可访问,但它并不指示 API 是否一定能正常运行,或者任何其他 API 请求是否真的有效。

示例请求/响应(详细模式)

$ curl -i -X HEAD "http://0.0.0.0:8775/healthcheck"
HTTP/1.1 204 No Content
Content-Type: text/plain; charset=UTF-8
Content-Length: 0
Date: Fri, 11 Sep 2015 18:55:08 GMT

$ curl -i -X GET "http://0.0.0.0:8775/healthcheck"
HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 2
Date: Fri, 11 Sep 2015 18:55:43 GMT

OK

$ curl -X GET -i -H "Accept: application/json" "http://0.0.0.0:8775/healthcheck"
HTTP/1.0 200 OK
Date: Wed, 24 Aug 2016 06:09:58 GMT
Content-Type: application/json
Content-Length: 63

{
    "detailed": false,
    "reasons": [
        "OK"
    ]
}

$ curl -X GET -i -H "Accept: text/html" "http://0.0.0.0:8775/healthcheck"
HTTP/1.0 200 OK
Date: Wed, 24 Aug 2016 06:10:42 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 239

<HTML>
<HEAD><TITLE>Healthcheck Status</TITLE></HEAD>
<BODY>

<H2>Result of 1 checks:</H2>
<TABLE bgcolor="#ffffff" border="1">
<TBODY>
<TR>

<TH>
Reason
</TH>
</TR>
<TR>
    <TD>OK</TD>
</TR>
</TBODY>
</TABLE>
<HR></HR>

</BODY>

示例请求/响应(详细模式)

$ curl -X GET -i -H "Accept: application/json" "http://0.0.0.0:8775/healthcheck"
HTTP/1.0 200 OK
Date: Wed, 24 Aug 2016 06:11:59 GMT
Content-Type: application/json
Content-Length: 3480

{
    "detailed": true,
    "gc": {
        "counts": [
            293,
            10,
            5
        ],
        "threshold": [
            700,
            10,
            10
        ]
    },
    "greenthreads": [
       ...
    ],
    "now": "2016-08-24 06:11:59.419267",
    "platform": "Linux-4.2.0-27-generic-x86_64-with-Ubuntu-14.04-trusty",
    "python_version": "2.7.6 (default, Jun 22 2015, 17:58:13) \n[GCC 4.8.2]",
    "reasons": [
        {
            "class": "HealthcheckResult",
            "details": "Path '/tmp/dead' was not found",
            "reason": "OK"
        }
    ],
    "threads": [
        ...
    ]
}

$ curl -X GET -i -H "Accept: text/html" "http://0.0.0.0:8775/healthcheck"
HTTP/1.0 200 OK
Date: Wed, 24 Aug 2016 06:36:07 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 6838

<HTML>
<HEAD><TITLE>Healthcheck Status</TITLE></HEAD>
<BODY>
<H1>Server status</H1>
<B>Server hostname:</B><PRE>...</PRE>
<B>Current time:</B><PRE>2016-08-24 06:36:07.302559</PRE>
<B>Python version:</B><PRE>2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2]</PRE>
<B>Platform:</B><PRE>Linux-4.2.0-27-generic-x86_64-with-Ubuntu-14.04-trusty</PRE>
<HR></HR>
<H2>Garbage collector:</H2>
<B>Counts:</B><PRE>(77, 1, 6)</PRE>
<B>Thresholds:</B><PRE>(700, 10, 10)</PRE>

<HR></HR>
<H2>Result of 1 checks:</H2>
<TABLE bgcolor="#ffffff" border="1">
<TBODY>
<TR>
<TH>
Kind
</TH>
<TH>
Reason
</TH>
<TH>
Details
</TH>

</TR>
<TR>
<TD>HealthcheckResult</TD>
    <TD>OK</TD>
<TD>Path &#39;/tmp/dead&#39; was not found</TD>
</TR>
</TBODY>
</TABLE>
<HR></HR>
<H2>1 greenthread(s) active:</H2>
<TABLE bgcolor="#ffffff" border="1">
<TBODY>
<TR>
    <TD><PRE>  File &#34;oslo_middleware/healthcheck/__main__.py&#34;, line 94, in &lt;module&gt;
    main()
  File &#34;oslo_middleware/healthcheck/__main__.py&#34;, line 90, in main
    server.serve_forever()
  ...
</PRE></TD>
</TR>
</TBODY>
</TABLE>
<HR></HR>
<H2>1 thread(s) active:</H2>
<TABLE bgcolor="#ffffff" border="1">
<TBODY>
<TR>
    <TD><PRE>  File &#34;oslo_middleware/healthcheck/__main__.py&#34;, line 94, in &lt;module&gt;
    main()
  File &#34;oslo_middleware/healthcheck/__main__.py&#34;, line 90, in main
    server.serve_forever()
  ....
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>

粘贴配置示例

[app:healthcheck]
use = egg:oslo.middleware:healthcheck
backends = disable_by_file
disable_by_file_path = /var/run/nova/healthcheck_disable

[pipeline:public_api]
pipeline = healthcheck sizelimit [...] public_service

如果希望拥有具有不同 healthcheck 配置的管道,可以定义多个 filter 部分,例如:

[composite:public_api]
use = egg:Paste#urlmap
/ = public_api_pipeline
/healthcheck = healthcheck_public

[composite:admin_api]
use = egg:Paste#urlmap
/ = admin_api_pipeline
/healthcheck = healthcheck_admin

[pipeline:public_api_pipeline]
pipeline = sizelimit [...] public_service

[pipeline:admin_api_pipeline]
pipeline = sizelimit [...] admin_service

[app:healthcheck_public]
use = egg:oslo.middleware:healthcheck
backends = disable_by_file
disable_by_file_path = /var/run/nova/healthcheck_public_disable

[filter:healthcheck_admin]
use = egg:oslo.middleware:healthcheck
backends = disable_by_file
disable_by_file_path = /var/run/nova/healthcheck_admin_disable
classmethod app_factory(global_conf: dict[str, Any] | None, **local_conf: Any) Self

paste.deploy 的工厂方法。

参数:
  • global_conf – 所有中间件的选项字典(通常是 paste 部署配置文件中的 [DEFAULT] 部分)

  • local_conf – 专门用于此中间件的选项(通常是 paste 部署配置文件中间件部分中定义的选项)

classmethod factory(global_conf: dict[str, ty.Any] | None, **local_conf: ty.Any) ty.Callable[[WSGIApplication], base.ConfigurableMiddleware]

paste.deploy 的工厂方法。

参数:
  • global_conf – 所有中间件的选项字典(通常是 paste 部署配置文件中的 [DEFAULT] 部分)

  • local_conf – 专门用于此中间件的选项(通常是 paste 部署配置文件中间件部分中定义的选项)

class oslo_middleware.healthcheck.Reason
class oslo_middleware.healthcheck.disable_by_file.DisableByFileHealthcheck(oslo_conf: cfg.ConfigOpts, conf: dict[str, ty.Any])

DisableByFile healthcheck 中间件插件

此插件检查文件的存在性,以报告服务是否可用。

中间件配置示例

[filter:healthcheck]
paste.filter_factory = oslo_middleware:Healthcheck.factory
path = /healthcheck
backends = disable_by_file
disable_by_file_path = /var/run/nova/healthcheck_disable
# set to True to enable detailed output, False is the default
detailed = False
healthcheck(server_port: int) HealthcheckResult

由 healthcheck 中间件调用的方法

返回值:HealthcheckResult 对象

class oslo_middleware.healthcheck.disable_by_file.DisableByFilesPortsHealthcheck(oslo_conf: cfg.ConfigOpts, conf: dict[str, ty.Any])

DisableByFilesPorts healthcheck 中间件插件

此插件检查为在特定端口上运行的应用程序提供的文件的存在性,以报告服务是否可用。

中间件配置示例

[filter:healthcheck]
paste.filter_factory = oslo_middleware:Healthcheck.factory
path = /healthcheck
backends = disable_by_files_ports
disable_by_file_paths = 5000:/var/run/keystone/healthcheck_disable,             35357:/var/run/keystone/admin_healthcheck_disable
# set to True to enable detailed output, False is the default
detailed = False
healthcheck(server_port: int) HealthcheckResult

由 healthcheck 中间件调用的方法

返回值:HealthcheckResult 对象

class oslo_middleware.healthcheck.enable_by_files.EnableByFilesHealthcheck(oslo_conf: cfg.ConfigOpts, conf: dict[str, Any])

EnableByFilesHealthcheck healthcheck 中间件插件

此插件检查在指定位置是否存在文件。

中间件配置示例

[app:healthcheck]
paste.app_factory = oslo_middleware:Healthcheck.app_factory
path = /healthcheck
backends = enable_by_files
enable_by_file_paths = /var/lib/glance/images/.marker,
    /var/lib/glance/os_glance_staging_store/.marker
# set to True to enable detailed output, False is the default
detailed = False
healthcheck(server_port: int) HealthcheckResult

由 healthcheck 中间件调用的方法

返回值:HealthcheckResult 对象

可用插件

disable_by_file

DisableByFile healthcheck 中间件插件

此插件检查文件的存在性,以报告服务是否可用。

中间件配置示例

[filter:healthcheck]
paste.filter_factory = oslo_middleware:Healthcheck.factory
path = /healthcheck
backends = disable_by_file
disable_by_file_path = /var/run/nova/healthcheck_disable
# set to True to enable detailed output, False is the default
detailed = False

disable_by_files_ports

DisableByFilesPorts healthcheck 中间件插件

此插件检查为在特定端口上运行的应用程序提供的文件的存在性,以报告服务是否可用。

中间件配置示例

[filter:healthcheck]
paste.filter_factory = oslo_middleware:Healthcheck.factory
path = /healthcheck
backends = disable_by_files_ports
disable_by_file_paths = 5000:/var/run/keystone/healthcheck_disable,             35357:/var/run/keystone/admin_healthcheck_disable
# set to True to enable detailed output, False is the default
detailed = False

enable_by_files

EnableByFilesHealthcheck healthcheck 中间件插件

此插件检查在指定位置是否存在文件。

中间件配置示例

[app:healthcheck]
paste.app_factory = oslo_middleware:Healthcheck.app_factory
path = /healthcheck
backends = enable_by_files
enable_by_file_paths = /var/lib/glance/images/.marker,
    /var/lib/glance/os_glance_staging_store/.marker
# set to True to enable detailed output, False is the default
detailed = False