useful_scripts/check_mongo.py
Arif Ali 2476319877
Update scripts
* Move the OVN resync stuff to functions.sh, so that it can be used elsewhere
* Add check mongo python script
2024-11-07 12:09:41 +00:00

48 lines
1.1 KiB
Python
Executable File

#!/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'].split(':')[0]}, State: {member['stateStr']}")
if __name__ == '__main__':
sys.exit(main())