| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2020, Pavlo Bashynskyi (@levonet) <[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: redis_info |
| 11 |
short_description: Gather information about Redis servers |
| 12 |
version_added: '0.2.0' |
| 13 |
description: |
| 14 |
- Gathers information and statistics about Redis servers. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._redis |
| 17 |
- community.general._attributes |
| 18 |
- community.general._attributes.info_module |
| 19 |
options: |
| 20 |
login_user: |
| 21 |
version_added: 7.5.0 |
| 22 |
validate_certs: |
| 23 |
version_added: 7.5.0 |
| 24 |
tls: |
| 25 |
default: false |
| 26 |
version_added: 7.5.0 |
| 27 |
ca_certs: |
| 28 |
version_added: 7.5.0 |
| 29 |
cluster: |
| 30 |
default: false |
| 31 |
description: Get informations about cluster status as RV(cluster). |
| 32 |
type: bool |
| 33 |
version_added: 9.1.0 |
| 34 |
seealso: |
| 35 |
- module: community.general.redis |
| 36 |
author: "Pavlo Bashynskyi (@levonet)" |
| 37 |
""" |
| 38 |
|
| 39 |
EXAMPLES = r""" |
| 40 |
- name: Get server information |
| 41 |
community.general.redis_info: |
| 42 |
register: result |
| 43 |
|
| 44 |
- name: Print server information |
| 45 |
ansible.builtin.debug: |
| 46 |
var: result.info |
| 47 |
|
| 48 |
- name: Get server cluster information |
| 49 |
community.general.redis_info: |
| 50 |
cluster: true |
| 51 |
register: result |
| 52 |
|
| 53 |
- name: Print server cluster information |
| 54 |
ansible.builtin.debug: |
| 55 |
var: result.cluster_info |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = r""" |
| 59 |
info: |
| 60 |
description: The default set of server information sections U(https://redis.io/commands/info). |
| 61 |
returned: success |
| 62 |
type: dict |
| 63 |
sample: |
| 64 |
{ |
| 65 |
"active_defrag_hits": 0, |
| 66 |
"active_defrag_key_hits": 0, |
| 67 |
"active_defrag_key_misses": 0, |
| 68 |
"active_defrag_misses": 0, |
| 69 |
"active_defrag_running": 0, |
| 70 |
"allocator_active": 932409344, |
| 71 |
"allocator_allocated": 932062792, |
| 72 |
"allocator_frag_bytes": 346552, |
| 73 |
"allocator_frag_ratio": 1.0, |
| 74 |
"allocator_resident": 947253248, |
| 75 |
"allocator_rss_bytes": 14843904, |
| 76 |
"allocator_rss_ratio": 1.02, |
| 77 |
"aof_current_rewrite_time_sec": -1, |
| 78 |
"aof_enabled": 0, |
| 79 |
"aof_last_bgrewrite_status": "ok", |
| 80 |
"aof_last_cow_size": 0, |
| 81 |
"aof_last_rewrite_time_sec": -1, |
| 82 |
"aof_last_write_status": "ok", |
| 83 |
"aof_rewrite_in_progress": 0, |
| 84 |
"aof_rewrite_scheduled": 0, |
| 85 |
"arch_bits": 64, |
| 86 |
"atomicvar_api": "atomic-builtin", |
| 87 |
"blocked_clients": 0, |
| 88 |
"client_recent_max_input_buffer": 4, |
| 89 |
"client_recent_max_output_buffer": 0, |
| 90 |
"cluster_enabled": 0, |
| 91 |
"config_file": "", |
| 92 |
"configured_hz": 10, |
| 93 |
"connected_clients": 4, |
| 94 |
"connected_slaves": 0, |
| 95 |
"db0": { |
| 96 |
"avg_ttl": 1945628530, |
| 97 |
"expires": 16, |
| 98 |
"keys": 3341411 |
| 99 |
}, |
| 100 |
"evicted_keys": 0, |
| 101 |
"executable": "/data/redis-server", |
| 102 |
"expired_keys": 9, |
| 103 |
"expired_stale_perc": 1.72, |
| 104 |
"expired_time_cap_reached_count": 0, |
| 105 |
"gcc_version": "9.2.0", |
| 106 |
"hz": 10, |
| 107 |
"instantaneous_input_kbps": 0.0, |
| 108 |
"instantaneous_ops_per_sec": 0, |
| 109 |
"instantaneous_output_kbps": 0.0, |
| 110 |
"keyspace_hits": 0, |
| 111 |
"keyspace_misses": 0, |
| 112 |
"latest_fork_usec": 0, |
| 113 |
"lazyfree_pending_objects": 0, |
| 114 |
"loading": 0, |
| 115 |
"lru_clock": 11603632, |
| 116 |
"master_repl_offset": 118831417, |
| 117 |
"master_replid": "0d904704e424e38c3cd896783e9f9d28d4836e5e", |
| 118 |
"master_replid2": "0000000000000000000000000000000000000000", |
| 119 |
"maxmemory": 0, |
| 120 |
"maxmemory_human": "0B", |
| 121 |
"maxmemory_policy": "noeviction", |
| 122 |
"mem_allocator": "jemalloc-5.1.0", |
| 123 |
"mem_aof_buffer": 0, |
| 124 |
"mem_clients_normal": 49694, |
| 125 |
"mem_clients_slaves": 0, |
| 126 |
"mem_fragmentation_bytes": 12355480, |
| 127 |
"mem_fragmentation_ratio": 1.01, |
| 128 |
"mem_not_counted_for_evict": 0, |
| 129 |
"mem_replication_backlog": 1048576, |
| 130 |
"migrate_cached_sockets": 0, |
| 131 |
"multiplexing_api": "epoll", |
| 132 |
"number_of_cached_scripts": 0, |
| 133 |
"os": "Linux 3.10.0-862.14.4.el7.x86_64 x86_64", |
| 134 |
"process_id": 1, |
| 135 |
"pubsub_channels": 0, |
| 136 |
"pubsub_patterns": 0, |
| 137 |
"rdb_bgsave_in_progress": 0, |
| 138 |
"rdb_changes_since_last_save": 671, |
| 139 |
"rdb_current_bgsave_time_sec": -1, |
| 140 |
"rdb_last_bgsave_status": "ok", |
| 141 |
"rdb_last_bgsave_time_sec": -1, |
| 142 |
"rdb_last_cow_size": 0, |
| 143 |
"rdb_last_save_time": 1588702236, |
| 144 |
"redis_build_id": "a31260535f820267", |
| 145 |
"redis_git_dirty": 0, |
| 146 |
"redis_git_sha1": 0, |
| 147 |
"redis_mode": "standalone", |
| 148 |
"redis_version": "999.999.999", |
| 149 |
"rejected_connections": 0, |
| 150 |
"repl_backlog_active": 1, |
| 151 |
"repl_backlog_first_byte_offset": 118707937, |
| 152 |
"repl_backlog_histlen": 123481, |
| 153 |
"repl_backlog_size": 1048576, |
| 154 |
"role": "master", |
| 155 |
"rss_overhead_bytes": -3051520, |
| 156 |
"rss_overhead_ratio": 1.0, |
| 157 |
"run_id": "8d252f66c3ef89bd60a060cf8dc5cfe3d511c5e4", |
| 158 |
"second_repl_offset": 118830003, |
| 159 |
"slave_expires_tracked_keys": 0, |
| 160 |
"sync_full": 0, |
| 161 |
"sync_partial_err": 0, |
| 162 |
"sync_partial_ok": 0, |
| 163 |
"tcp_port": 6379, |
| 164 |
"total_commands_processed": 885, |
| 165 |
"total_connections_received": 10, |
| 166 |
"total_net_input_bytes": 802709255, |
| 167 |
"total_net_output_bytes": 31754, |
| 168 |
"total_system_memory": 135029538816, |
| 169 |
"total_system_memory_human": "125.76G", |
| 170 |
"uptime_in_days": 53, |
| 171 |
"uptime_in_seconds": 4631778, |
| 172 |
"used_cpu_sys": 4.668282, |
| 173 |
"used_cpu_sys_children": 0.002191, |
| 174 |
"used_cpu_user": 4.21088, |
| 175 |
"used_cpu_user_children": 0.0, |
| 176 |
"used_memory": 931908760, |
| 177 |
"used_memory_dataset": 910774306, |
| 178 |
"used_memory_dataset_perc": "97.82%", |
| 179 |
"used_memory_human": "888.74M", |
| 180 |
"used_memory_lua": 37888, |
| 181 |
"used_memory_lua_human": "37.00K", |
| 182 |
"used_memory_overhead": 21134454, |
| 183 |
"used_memory_peak": 932015216, |
| 184 |
"used_memory_peak_human": "888.84M", |
| 185 |
"used_memory_peak_perc": "99.99%", |
| 186 |
"used_memory_rss": 944201728, |
| 187 |
"used_memory_rss_human": "900.46M", |
| 188 |
"used_memory_scripts": 0, |
| 189 |
"used_memory_scripts_human": "0B", |
| 190 |
"used_memory_startup": 791264 |
| 191 |
} |
| 192 |
cluster: |
| 193 |
description: The default set of cluster information sections U(https://redis.io/commands/cluster-info). |
| 194 |
returned: success if O(cluster=true) |
| 195 |
version_added: 9.1.0 |
| 196 |
type: dict |
| 197 |
sample: |
| 198 |
{ |
| 199 |
"cluster_state": "ok", |
| 200 |
"cluster_slots_assigned": 16384, |
| 201 |
"cluster_slots_ok": 16384, |
| 202 |
"cluster_slots_pfail": 0, |
| 203 |
"cluster_slots_fail": 0, |
| 204 |
"cluster_known_nodes": 6, |
| 205 |
"cluster_size": 3, |
| 206 |
"cluster_current_epoch": 6, |
| 207 |
"cluster_my_epoch": 2, |
| 208 |
"cluster_stats_messages_sent": 1483972, |
| 209 |
"cluster_stats_messages_received": 1483968, |
| 210 |
"total_cluster_links_buffer_limit_exceeded": 0 |
| 211 |
} |
| 212 |
""" |
| 213 |
|
| 214 |
import traceback |
| 215 |
|
| 216 |
REDIS_IMP_ERR = None |
| 217 |
try: |
| 218 |
from redis import StrictRedis |
| 219 |
|
| 220 |
HAS_REDIS_PACKAGE = True |
| 221 |
except ImportError: |
| 222 |
REDIS_IMP_ERR = traceback.format_exc() |
| 223 |
HAS_REDIS_PACKAGE = False |
| 224 |
|
| 225 |
from ansible.module_utils.basic import AnsibleModule |
| 226 |
|
| 227 |
from ansible_collections.community.general.plugins.module_utils._redis import ( |
| 228 |
fail_imports, |
| 229 |
redis_auth_argument_spec, |
| 230 |
redis_auth_params, |
| 231 |
) |
| 232 |
|
| 233 |
|
| 234 |
def redis_client(**client_params): |
| 235 |
return StrictRedis(**client_params) |
| 236 |
|
| 237 |
|
| 238 |
# Module execution. |
| 239 |
def main(): |
| 240 |
module_args = dict( |
| 241 |
cluster=dict(type="bool", default=False), |
| 242 |
) |
| 243 |
module_args.update(redis_auth_argument_spec(tls_default=False)) |
| 244 |
module = AnsibleModule( |
| 245 |
argument_spec=module_args, |
| 246 |
supports_check_mode=True, |
| 247 |
) |
| 248 |
|
| 249 |
fail_imports(module, module.params["tls"]) |
| 250 |
|
| 251 |
redis_params = redis_auth_params(module) |
| 252 |
cluster = module.params["cluster"] |
| 253 |
|
| 254 |
# Connect and check |
| 255 |
client = redis_client(**redis_params) |
| 256 |
try: |
| 257 |
client.ping() |
| 258 |
except Exception as e: |
| 259 |
module.fail_json(msg=f"unable to connect to database: {e}", exception=traceback.format_exc()) |
| 260 |
|
| 261 |
info = client.info() |
| 262 |
|
| 263 |
result = dict(changed=False, info=info) |
| 264 |
|
| 265 |
if cluster: |
| 266 |
result["cluster_info"] = client.execute_command("CLUSTER INFO") |
| 267 |
|
| 268 |
module.exit_json(**result) |
| 269 |
|
| 270 |
|
| 271 |
if __name__ == "__main__": |
| 272 |
main() |