1#!/usr/local/bin/python3.8 2# -*- coding: utf-8 -*- 3 4# Copyright: (c) 2020, Pavlo Bashynskyi (@levonet) <levonet@gmail.com> 5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 6 7from __future__ import absolute_import, division, print_function 8__metaclass__ = type 9 10 11DOCUMENTATION = r''' 12--- 13module: redis_info 14short_description: Gather information about Redis servers 15version_added: '0.2.0' 16description: 17- Gathers information and statistics about Redis servers. 18options: 19 login_host: 20 description: 21 - The host running the database. 22 type: str 23 default: localhost 24 login_port: 25 description: 26 - The port to connect to. 27 type: int 28 default: 6379 29 login_password: 30 description: 31 - The password used to authenticate with, when authentication is enabled for the Redis server. 32 type: str 33notes: 34- Requires the redis-py Python package on the remote host. You can 35 install it with pip (C(pip install redis)) or with a package manager. 36 U(https://github.com/andymccurdy/redis-py) 37seealso: 38- module: community.general.redis 39requirements: [ redis ] 40author: "Pavlo Bashynskyi (@levonet)" 41''' 42 43EXAMPLES = r''' 44- name: Get server information 45 community.general.redis_info: 46 register: result 47 48- name: Print server information 49 ansible.builtin.debug: 50 var: result.info 51''' 52 53RETURN = r''' 54info: 55 description: The default set of server information sections U(https://redis.io/commands/info). 56 returned: success 57 type: dict 58 sample: { 59 "active_defrag_hits": 0, 60 "active_defrag_key_hits": 0, 61 "active_defrag_key_misses": 0, 62 "active_defrag_misses": 0, 63 "active_defrag_running": 0, 64 "allocator_active": 932409344, 65 "allocator_allocated": 932062792, 66 "allocator_frag_bytes": 346552, 67 "allocator_frag_ratio": 1.0, 68 "allocator_resident": 947253248, 69 "allocator_rss_bytes": 14843904, 70 "allocator_rss_ratio": 1.02, 71 "aof_current_rewrite_time_sec": -1, 72 "aof_enabled": 0, 73 "aof_last_bgrewrite_status": "ok", 74 "aof_last_cow_size": 0, 75 "aof_last_rewrite_time_sec": -1, 76 "aof_last_write_status": "ok", 77 "aof_rewrite_in_progress": 0, 78 "aof_rewrite_scheduled": 0, 79 "arch_bits": 64, 80 "atomicvar_api": "atomic-builtin", 81 "blocked_clients": 0, 82 "client_recent_max_input_buffer": 4, 83 "client_recent_max_output_buffer": 0, 84 "cluster_enabled": 0, 85 "config_file": "", 86 "configured_hz": 10, 87 "connected_clients": 4, 88 "connected_slaves": 0, 89 "db0": { 90 "avg_ttl": 1945628530, 91 "expires": 16, 92 "keys": 3341411 93 }, 94 "evicted_keys": 0, 95 "executable": "/data/redis-server", 96 "expired_keys": 9, 97 "expired_stale_perc": 1.72, 98 "expired_time_cap_reached_count": 0, 99 "gcc_version": "9.2.0", 100 "hz": 10, 101 "instantaneous_input_kbps": 0.0, 102 "instantaneous_ops_per_sec": 0, 103 "instantaneous_output_kbps": 0.0, 104 "keyspace_hits": 0, 105 "keyspace_misses": 0, 106 "latest_fork_usec": 0, 107 "lazyfree_pending_objects": 0, 108 "loading": 0, 109 "lru_clock": 11603632, 110 "master_repl_offset": 118831417, 111 "master_replid": "0d904704e424e38c3cd896783e9f9d28d4836e5e", 112 "master_replid2": "0000000000000000000000000000000000000000", 113 "maxmemory": 0, 114 "maxmemory_human": "0B", 115 "maxmemory_policy": "noeviction", 116 "mem_allocator": "jemalloc-5.1.0", 117 "mem_aof_buffer": 0, 118 "mem_clients_normal": 49694, 119 "mem_clients_slaves": 0, 120 "mem_fragmentation_bytes": 12355480, 121 "mem_fragmentation_ratio": 1.01, 122 "mem_not_counted_for_evict": 0, 123 "mem_replication_backlog": 1048576, 124 "migrate_cached_sockets": 0, 125 "multiplexing_api": "epoll", 126 "number_of_cached_scripts": 0, 127 "os": "Linux 3.10.0-862.14.4.el7.x86_64 x86_64", 128 "process_id": 1, 129 "pubsub_channels": 0, 130 "pubsub_patterns": 0, 131 "rdb_bgsave_in_progress": 0, 132 "rdb_changes_since_last_save": 671, 133 "rdb_current_bgsave_time_sec": -1, 134 "rdb_last_bgsave_status": "ok", 135 "rdb_last_bgsave_time_sec": -1, 136 "rdb_last_cow_size": 0, 137 "rdb_last_save_time": 1588702236, 138 "redis_build_id": "a31260535f820267", 139 "redis_git_dirty": 0, 140 "redis_git_sha1": 0, 141 "redis_mode": "standalone", 142 "redis_version": "999.999.999", 143 "rejected_connections": 0, 144 "repl_backlog_active": 1, 145 "repl_backlog_first_byte_offset": 118707937, 146 "repl_backlog_histlen": 123481, 147 "repl_backlog_size": 1048576, 148 "role": "master", 149 "rss_overhead_bytes": -3051520, 150 "rss_overhead_ratio": 1.0, 151 "run_id": "8d252f66c3ef89bd60a060cf8dc5cfe3d511c5e4", 152 "second_repl_offset": 118830003, 153 "slave_expires_tracked_keys": 0, 154 "sync_full": 0, 155 "sync_partial_err": 0, 156 "sync_partial_ok": 0, 157 "tcp_port": 6379, 158 "total_commands_processed": 885, 159 "total_connections_received": 10, 160 "total_net_input_bytes": 802709255, 161 "total_net_output_bytes": 31754, 162 "total_system_memory": 135029538816, 163 "total_system_memory_human": "125.76G", 164 "uptime_in_days": 53, 165 "uptime_in_seconds": 4631778, 166 "used_cpu_sys": 4.668282, 167 "used_cpu_sys_children": 0.002191, 168 "used_cpu_user": 4.21088, 169 "used_cpu_user_children": 0.0, 170 "used_memory": 931908760, 171 "used_memory_dataset": 910774306, 172 "used_memory_dataset_perc": "97.82%", 173 "used_memory_human": "888.74M", 174 "used_memory_lua": 37888, 175 "used_memory_lua_human": "37.00K", 176 "used_memory_overhead": 21134454, 177 "used_memory_peak": 932015216, 178 "used_memory_peak_human": "888.84M", 179 "used_memory_peak_perc": "99.99%", 180 "used_memory_rss": 944201728, 181 "used_memory_rss_human": "900.46M", 182 "used_memory_scripts": 0, 183 "used_memory_scripts_human": "0B", 184 "used_memory_startup": 791264 185 } 186''' 187 188import traceback 189 190REDIS_IMP_ERR = None 191try: 192 from redis import StrictRedis 193 HAS_REDIS_PACKAGE = True 194except ImportError: 195 REDIS_IMP_ERR = traceback.format_exc() 196 HAS_REDIS_PACKAGE = False 197 198from ansible.module_utils.basic import AnsibleModule, missing_required_lib 199from ansible.module_utils.common.text.converters import to_native 200 201 202def redis_client(**client_params): 203 return StrictRedis(**client_params) 204 205 206# Module execution. 207def main(): 208 module = AnsibleModule( 209 argument_spec=dict( 210 login_host=dict(type='str', default='localhost'), 211 login_port=dict(type='int', default=6379), 212 login_password=dict(type='str', no_log=True), 213 ), 214 supports_check_mode=True, 215 ) 216 217 if not HAS_REDIS_PACKAGE: 218 module.fail_json(msg=missing_required_lib('redis'), exception=REDIS_IMP_ERR) 219 220 login_host = module.params['login_host'] 221 login_port = module.params['login_port'] 222 login_password = module.params['login_password'] 223 224 # Connect and check 225 client = redis_client(host=login_host, port=login_port, password=login_password) 226 try: 227 client.ping() 228 except Exception as e: 229 module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) 230 231 info = client.info() 232 module.exit_json(changed=False, info=info) 233 234 235if __name__ == '__main__': 236 main() 237