Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ssl connection option and password for node connections #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ optional arguments:
--host HOST Redis host
--port PORT Redis port
--password PASSWORD Redis password
--ssl Enable SSL connection
--cluster Enable cluster mode
--batch_size BATCH_SIZE
Batch size for SCAN command
Expand Down
13 changes: 12 additions & 1 deletion rks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import redis
import rediscluster
from datetime import datetime

from .redis_utils import get_redis_keys, get_redis_cluster_keys


def main():
parser = argparse.ArgumentParser(description='Analyze Redis Instance Key Statistics.')
parser.add_argument('--host', required=True, help='Redis host')
parser.add_argument('--port', required=True, help='Redis port')
parser.add_argument('--password', default=None, help='Redis password')
parser.add_argument('--ssl', action='store_true', help='Enable SSL connection')
parser.add_argument('--cluster', action='store_true', help='Enable cluster mode')
parser.add_argument('--batch_size', type=int, default=1000, help='Batch size for SCAN command')
parser.add_argument('--replica_only', action='store_true', help='Execute only on replica instances')
Expand All @@ -22,6 +25,7 @@ def main():
rc = rediscluster.RedisCluster(startup_nodes=[{"host": args.host, "port": int(args.port)}],
skip_full_coverage_check=True,
password=args.password,
ssl=args.ssl,
socket_timeout=CONNECTION_TIMEOUT)

redis_info = rc.info()
Expand All @@ -32,7 +36,13 @@ def main():

process_start_time = datetime.now()

if get_redis_cluster_keys(rc, args.batch_size, args.replica_only, args.pretty_format) == -1:
if get_redis_cluster_keys(rc,
args.batch_size,
args.replica_only,
args.pretty_format,
args.password,
args.ssl,
CONNECTION_TIMEOUT) == -1:
print(f"Aborted the operation on non-readonly redis at host: {args.host}")
return

Expand All @@ -56,6 +66,7 @@ def main():
try:
r = redis.Redis(host=args.host, port=int(args.port),
password=args.password,
ssl=args.ssl,
socket_timeout=CONNECTION_TIMEOUT)

redis_info = r.info()
Expand Down
10 changes: 8 additions & 2 deletions rks/redis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_redis_keys(r, batch_size, db_num, use_pretty):
return analyze_redis_keys(min_heap, prefix_statistics_map, total_key_size, total_key_count, key_count_by_type, db_num, use_pretty)


def get_redis_cluster_keys(rc, batch_size, replica_only, use_pretty):
def get_redis_cluster_keys(rc, batch_size, replica_only, use_pretty, password, ssl, socket_timeout):
slave_flag = False

min_heap = []
Expand Down Expand Up @@ -96,7 +96,13 @@ def get_redis_cluster_keys(rc, batch_size, replica_only, use_pretty):
node = next((node for node in nodes if node['id'] == master['master']), None)
else:
node = next((node for node in nodes if node['id'] == master['slaves'][0]), None)
r = redis.Redis(host=node['host'], port=node['port'])
r = redis.Redis(
host=node['host'],
port=node['port'],
ssl=ssl,
password=password,
socket_timeout=socket_timeout
)
r.execute_command('READONLY')

script = """
Expand Down