| 1 |
#!/usr/bin/python |
| 2 |
# Copyright Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: statsd |
| 10 |
short_description: Send metrics to StatsD |
| 11 |
version_added: 2.1.0 |
| 12 |
description: |
| 13 |
- The C(statsd) module sends metrics to StatsD. |
| 14 |
- For more information, see U(https://statsd-metrics.readthedocs.io/en/latest/). |
| 15 |
- Supported metric types are V(counter) and V(gauge). Currently unupported metric types are V(timer), V(set), and V(gaugedelta). |
| 16 |
author: "Mark Mercado (@mamercad)" |
| 17 |
requirements: |
| 18 |
- statsd |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: none |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
state: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- State of the check, only V(present) makes sense. |
| 31 |
choices: ["present"] |
| 32 |
default: present |
| 33 |
host: |
| 34 |
type: str |
| 35 |
default: localhost |
| 36 |
description: |
| 37 |
- StatsD host (hostname or IP) to send metrics to. |
| 38 |
port: |
| 39 |
type: int |
| 40 |
default: 8125 |
| 41 |
description: |
| 42 |
- The port on O(host) which StatsD is listening on. |
| 43 |
protocol: |
| 44 |
type: str |
| 45 |
default: udp |
| 46 |
choices: ["udp", "tcp"] |
| 47 |
description: |
| 48 |
- The transport protocol to send metrics over. |
| 49 |
timeout: |
| 50 |
type: float |
| 51 |
default: 1.0 |
| 52 |
description: |
| 53 |
- Sender timeout, only applicable if O(protocol) is V(tcp). |
| 54 |
metric: |
| 55 |
type: str |
| 56 |
required: true |
| 57 |
description: |
| 58 |
- The name of the metric. |
| 59 |
metric_type: |
| 60 |
type: str |
| 61 |
required: true |
| 62 |
choices: ["counter", "gauge"] |
| 63 |
description: |
| 64 |
- The type of metric. |
| 65 |
metric_prefix: |
| 66 |
type: str |
| 67 |
description: |
| 68 |
- The prefix to add to the metric. |
| 69 |
default: '' |
| 70 |
value: |
| 71 |
type: int |
| 72 |
required: true |
| 73 |
description: |
| 74 |
- The value of the metric. |
| 75 |
delta: |
| 76 |
type: bool |
| 77 |
default: false |
| 78 |
description: |
| 79 |
- If the metric is of type V(gauge), change the value by O(delta). |
| 80 |
""" |
| 81 |
|
| 82 |
EXAMPLES = r""" |
| 83 |
- name: Increment the metric my_counter by 1 |
| 84 |
community.general.statsd: |
| 85 |
host: localhost |
| 86 |
port: 9125 |
| 87 |
protocol: tcp |
| 88 |
metric: my_counter |
| 89 |
metric_type: counter |
| 90 |
value: 1 |
| 91 |
|
| 92 |
- name: Set the gauge my_gauge to 7 |
| 93 |
community.general.statsd: |
| 94 |
host: localhost |
| 95 |
port: 9125 |
| 96 |
protocol: tcp |
| 97 |
metric: my_gauge |
| 98 |
metric_type: gauge |
| 99 |
value: 7 |
| 100 |
""" |
| 101 |
|
| 102 |
|
| 103 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 104 |
|
| 105 |
try: |
| 106 |
from statsd import StatsClient, TCPStatsClient |
| 107 |
|
| 108 |
HAS_STATSD = True |
| 109 |
except ImportError: |
| 110 |
HAS_STATSD = False |
| 111 |
|
| 112 |
|
| 113 |
def udp_statsd_client(**client_params): |
| 114 |
return StatsClient(**client_params) |
| 115 |
|
| 116 |
|
| 117 |
def tcp_statsd_client(**client_params): |
| 118 |
return TCPStatsClient(**client_params) |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
module = AnsibleModule( |
| 123 |
argument_spec=dict( |
| 124 |
state=dict(type="str", default="present", choices=["present"]), |
| 125 |
host=dict(type="str", default="localhost"), |
| 126 |
port=dict(type="int", default=8125), |
| 127 |
protocol=dict(type="str", default="udp", choices=["udp", "tcp"]), |
| 128 |
timeout=dict(type="float", default=1.0), |
| 129 |
metric=dict(type="str", required=True), |
| 130 |
metric_type=dict(type="str", required=True, choices=["counter", "gauge"]), |
| 131 |
metric_prefix=dict(type="str", default=""), |
| 132 |
value=dict(type="int", required=True), |
| 133 |
delta=dict(type="bool", default=False), |
| 134 |
), |
| 135 |
supports_check_mode=False, |
| 136 |
) |
| 137 |
|
| 138 |
if not HAS_STATSD: |
| 139 |
module.fail_json(msg=missing_required_lib("statsd")) |
| 140 |
|
| 141 |
host = module.params.get("host") |
| 142 |
port = module.params.get("port") |
| 143 |
protocol = module.params.get("protocol") |
| 144 |
timeout = module.params.get("timeout") |
| 145 |
metric = module.params.get("metric") |
| 146 |
metric_type = module.params.get("metric_type") |
| 147 |
metric_prefix = module.params.get("metric_prefix") |
| 148 |
value = module.params.get("value") |
| 149 |
delta = module.params.get("delta") |
| 150 |
|
| 151 |
if protocol == "udp": |
| 152 |
client = udp_statsd_client(host=host, port=port, prefix=metric_prefix, maxudpsize=512, ipv6=False) |
| 153 |
elif protocol == "tcp": |
| 154 |
client = tcp_statsd_client(host=host, port=port, timeout=timeout, prefix=metric_prefix, ipv6=False) |
| 155 |
|
| 156 |
metric_name = f"{metric_prefix}/{metric}" if metric_prefix else metric |
| 157 |
metric_display_value = f"{value} (delta={delta})" if metric_type == "gauge" else value |
| 158 |
|
| 159 |
try: |
| 160 |
if metric_type == "counter": |
| 161 |
client.incr(metric, value) |
| 162 |
elif metric_type == "gauge": |
| 163 |
client.gauge(metric, value, delta=delta) |
| 164 |
|
| 165 |
except Exception as exc: |
| 166 |
module.fail_json(msg=f"Failed sending to StatsD {exc}") |
| 167 |
|
| 168 |
finally: |
| 169 |
if protocol == "tcp": |
| 170 |
client.close() |
| 171 |
|
| 172 |
module.exit_json(msg=f"Sent {metric_type} {metric_name} -> {metric_display_value!s} to StatsD", changed=True) |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
main() |