Created
February 8, 2024 13:14
-
-
Save arif-ali/aa7910f5eb03302a0076f93f5ea0ddb5 to your computer and use it in GitHub Desktop.
Check the current replication status of mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pymongo import MongoClient | |
import yaml | |
import os | |
import sys | |
host = "localhost" | |
port = "37017" | |
def _import_yaml(): | |
global password, username | |
agent_dir = "/var/lib/juju/agents" | |
entries = os.listdir(agent_dir) | |
for file_name in entries: | |
if "machine" in file_name: | |
machine = file_name | |
break | |
with open(f'{agent_dir}/{machine}/agent.conf', 'r') as stream: | |
agent = yaml.safe_load(stream) | |
password = agent['statepassword'] | |
username = agent['tag'] | |
def main(): | |
_import_yaml() | |
client = MongoClient(f"{host}:{port}", | |
username=username, | |
password=password, | |
authSource='admin', | |
tls=True, | |
tlsAllowInvalidCertificates=True, | |
) | |
rep_status = client.admin.command('replSetGetStatus') | |
for member in rep_status['members']: | |
print(f"Host: {member['name']}, State: {member['stateStr']}") | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Login to one of the juju controllers
Then install
python3-pymongo
Copy over or create the file
The run it
The output should be similar to