Skip to content

Instantly share code, notes, and snippets.

@arif-ali
Created February 8, 2024 13:14
Show Gist options
  • Save arif-ali/aa7910f5eb03302a0076f93f5ea0ddb5 to your computer and use it in GitHub Desktop.
Save arif-ali/aa7910f5eb03302a0076f93f5ea0ddb5 to your computer and use it in GitHub Desktop.
Check the current replication status of mongodb
#!/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())
@arif-ali
Copy link
Author

arif-ali commented Feb 8, 2024

  1. Login to one of the juju controllers

    juju ssh -m controller 0
    
  2. Then install python3-pymongo

    sudo apt -y install python3-pymongo
    
  3. Copy over or create the file

  4. The run it

    sudo ./check_mongo.py
    

The output should be similar to

Host: 10.0.1.226:37017, State: SECONDARY
Host: 10.0.1.227:37017, State: PRIMARY
Host: 10.0.1.104:37017, State: SECONDARY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment