| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2016, René Moser <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: nginx_status_info |
| 11 |
short_description: Retrieve information on nginx status |
| 12 |
description: |
| 13 |
- Gathers information from nginx from an URL having C(stub_status) enabled. |
| 14 |
author: "René Moser (@resmo)" |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
- community.general._attributes.info_module |
| 18 |
options: |
| 19 |
url: |
| 20 |
type: str |
| 21 |
description: |
| 22 |
- URL of the nginx status. |
| 23 |
required: true |
| 24 |
timeout: |
| 25 |
type: int |
| 26 |
description: |
| 27 |
- HTTP connection timeout in seconds. |
| 28 |
default: 10 |
| 29 |
|
| 30 |
notes: |
| 31 |
- See U(http://nginx.org/en/docs/http/ngx_http_stub_status_module.html) for more information. |
| 32 |
""" |
| 33 |
|
| 34 |
EXAMPLES = r""" |
| 35 |
# Gather status info from nginx on localhost |
| 36 |
- name: Get current http stats |
| 37 |
community.general.nginx_status_info: |
| 38 |
url: http://localhost/nginx_status |
| 39 |
register: result |
| 40 |
|
| 41 |
# Gather status info from nginx on localhost with a custom timeout of 20 seconds |
| 42 |
- name: Get current http stats |
| 43 |
community.general.nginx_status_info: |
| 44 |
url: http://localhost/nginx_status |
| 45 |
timeout: 20 |
| 46 |
register: result |
| 47 |
""" |
| 48 |
|
| 49 |
RETURN = r""" |
| 50 |
active_connections: |
| 51 |
description: Active connections. |
| 52 |
returned: success |
| 53 |
type: int |
| 54 |
sample: 2340 |
| 55 |
accepts: |
| 56 |
description: The total number of accepted client connections. |
| 57 |
returned: success |
| 58 |
type: int |
| 59 |
sample: 81769947 |
| 60 |
handled: |
| 61 |
description: The total number of handled connections. Generally, the parameter value is the same as accepts unless some |
| 62 |
resource limits have been reached. |
| 63 |
returned: success |
| 64 |
type: int |
| 65 |
sample: 81769947 |
| 66 |
requests: |
| 67 |
description: The total number of client requests. |
| 68 |
returned: success |
| 69 |
type: int |
| 70 |
sample: 144332345 |
| 71 |
reading: |
| 72 |
description: The current number of connections where nginx is reading the request header. |
| 73 |
returned: success |
| 74 |
type: int |
| 75 |
sample: 0 |
| 76 |
writing: |
| 77 |
description: The current number of connections where nginx is writing the response back to the client. |
| 78 |
returned: success |
| 79 |
type: int |
| 80 |
sample: 241 |
| 81 |
waiting: |
| 82 |
description: The current number of idle client connections waiting for a request. |
| 83 |
returned: success |
| 84 |
type: int |
| 85 |
sample: 2092 |
| 86 |
data: |
| 87 |
description: HTTP response as is. |
| 88 |
returned: success |
| 89 |
type: str |
| 90 |
sample: "Active connections: 2340 \nserver accepts handled requests\n 81769947 81769947 144332345 \nReading: 0 Writing: 241 Waiting: 2092 \n" |
| 91 |
""" |
| 92 |
|
| 93 |
import re |
| 94 |
|
| 95 |
from ansible.module_utils.basic import AnsibleModule |
| 96 |
from ansible.module_utils.common.text.converters import to_text |
| 97 |
from ansible.module_utils.urls import fetch_url |
| 98 |
|
| 99 |
|
| 100 |
class NginxStatusInfo: |
| 101 |
def __init__(self): |
| 102 |
self.url = module.params.get("url") |
| 103 |
self.timeout = module.params.get("timeout") |
| 104 |
|
| 105 |
def run(self): |
| 106 |
result = { |
| 107 |
"active_connections": None, |
| 108 |
"accepts": None, |
| 109 |
"handled": None, |
| 110 |
"requests": None, |
| 111 |
"reading": None, |
| 112 |
"writing": None, |
| 113 |
"waiting": None, |
| 114 |
"data": None, |
| 115 |
} |
| 116 |
(response, info) = fetch_url(module=module, url=self.url, force=True, timeout=self.timeout) |
| 117 |
if not response: |
| 118 |
module.fail_json(msg=f"No valid or no response from url {self.url} within {self.timeout} seconds (timeout)") |
| 119 |
|
| 120 |
data = to_text(response.read(), errors="surrogate_or_strict") |
| 121 |
if not data: |
| 122 |
return result |
| 123 |
|
| 124 |
result["data"] = data |
| 125 |
expr = ( |
| 126 |
r"Active connections: ([0-9]+) \nserver accepts handled requests\n ([0-9]+) ([0-9]+) ([0-9]+) \n" |
| 127 |
r"Reading: ([0-9]+) Writing: ([0-9]+) Waiting: ([0-9]+)" |
| 128 |
) |
| 129 |
match = re.match(expr, data, re.S) |
| 130 |
if match: |
| 131 |
result["active_connections"] = int(match.group(1)) |
| 132 |
result["accepts"] = int(match.group(2)) |
| 133 |
result["handled"] = int(match.group(3)) |
| 134 |
result["requests"] = int(match.group(4)) |
| 135 |
result["reading"] = int(match.group(5)) |
| 136 |
result["writing"] = int(match.group(6)) |
| 137 |
result["waiting"] = int(match.group(7)) |
| 138 |
return result |
| 139 |
|
| 140 |
|
| 141 |
def main(): |
| 142 |
global module |
| 143 |
module = AnsibleModule( |
| 144 |
argument_spec=dict( |
| 145 |
url=dict(type="str", required=True), |
| 146 |
timeout=dict(type="int", default=10), |
| 147 |
), |
| 148 |
supports_check_mode=True, |
| 149 |
) |
| 150 |
|
| 151 |
nginx_status_info = NginxStatusInfo().run() |
| 152 |
module.exit_json(changed=False, **nginx_status_info) |
| 153 |
|
| 154 |
|
| 155 |
if __name__ == "__main__": |
| 156 |
main() |