# Path: mongoctl/utils.py
# def resolve_path(path):
#     # handle file uris
#     path = path.replace("file://", "")
# 
#     # expand vars
#     path =  os.path.expandvars(custom_expanduser(path))
#     # Turn relative paths to absolute
#     try:
#         path = os.path.abspath(path)
#     except OSError, e:
#         # handle the case where cwd does not exist
#         if "No such file or directory" in str(e):
#             pass
#         else:
#             raise
# 
#     return path
# 
# def system_memory_size_gbs():
#     """
#     credit https://stackoverflow.com/questions/22102999/get-total-physical-memory-in-python
#     :return: total system memory size in gbs
#     """
# 
#     mem_bytes = psutil.virtual_memory().total
#     return mem_bytes/(1024.**3)
# 
# Path: mongoctl/mongoctl_logging.py
# def log_verbose(msg):
#     get_logger().log(VERBOSE, msg)
# 
# def log_debug(msg):
#     get_logger().debug(msg)
# 
# def log_exception(exception):
#     log_debug("EXCEPTION: %s" % exception)
#     log_debug(traceback.format_exc())
# 
# def log_warning(msg):
#     get_logger().warning(msg)
# 
# def log_info(msg):
#     get_logger().info(msg)
# 
# Path: mongoctl/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause
# 
# Path: mongoctl/mongodb_version.py
# def make_version_info(version_number, edition=None):
#     if version_number is None:
#         return None
# 
#     version_number = version_number.strip()
#     version_number = version_number.replace("-pre-" , "-pre")
#     version_info = MongoDBVersionInfo(version_number, edition=edition)
# 
#     # validate version string
#     if not is_valid_version_info(version_info):
#         raise MongoctlException("Invalid version '%s." % version_info)
#     else:
#         return version_info


import server
from mongoctl.utils import resolve_path, system_memory_size_gbs
from mongoctl.mongoctl_logging import log_verbose, log_debug, log_exception, \
    log_warning, log_info
from bson.son import SON
from mongoctl.errors import MongoctlException
from replicaset_cluster import ReplicaSetCluster, get_member_repl_lag
from sharded_cluster import ShardedCluster
from mongoctl.mongodb_version import make_version_info
                db = "/[dbname]"
            else:
                db = ""
        else:
            db = "/" + db

        creds = "[dbuser]:[dbpass]@" if self.is_auth() else ""
        return "mongodb://%s%s%s" % (creds, self.get_address_display(), db)

    ###########################################################################
    def get_rs_status(self):
        try:
            rs_status_cmd = SON([('replSetGetStatus', 1)])
            rs_status =  self.db_command(rs_status_cmd, 'admin')
            return rs_status
        except (Exception,RuntimeError), e:
            log_debug("Cannot get rs status from server '%s'. cause: %s" %
                        (self.id, e))
            log_exception(e)
            return None

    ###########################################################################
    def get_member_rs_status(self):
        rs_status =  self.get_rs_status()
        if rs_status:
            try:
                for member in rs_status['members']:
                    if 'self' in member and member['self']:
                        return member
            except (Exception,RuntimeError), e:

# Path: mongoctl/utils.py
# def resolve_path(path):
#     # handle file uris
#     path = path.replace("file://", "")
# 
#     # expand vars
#     path =  os.path.expandvars(custom_expanduser(path))
#     # Turn relative paths to absolute
#     try:
#         path = os.path.abspath(path)
#     except OSError, e:
#         # handle the case where cwd does not exist
#         if "No such file or directory" in str(e):
#             pass
#         else:
#             raise
# 
#     return path
# 
# def system_memory_size_gbs():
#     """
#     credit https://stackoverflow.com/questions/22102999/get-total-physical-memory-in-python
#     :return: total system memory size in gbs
#     """
# 
#     mem_bytes = psutil.virtual_memory().total
#     return mem_bytes/(1024.**3)
# 
# Path: mongoctl/mongoctl_logging.py
# def log_verbose(msg):
#     get_logger().log(VERBOSE, msg)
# 
# def log_debug(msg):
#     get_logger().debug(msg)
# 
# def log_exception(exception):
#     log_debug("EXCEPTION: %s" % exception)
#     log_debug(traceback.format_exc())
# 
# def log_warning(msg):
#     get_logger().warning(msg)
# 
# def log_info(msg):
#     get_logger().info(msg)
# 
# Path: mongoctl/errors.py
# class MongoctlException(Exception):
#     def __init__(self, message, cause=None):
#         super(MongoctlException, self).__init__(message)
#         self._cause = cause
# 
# Path: mongoctl/mongodb_version.py
# def make_version_info(version_number, edition=None):
#     if version_number is None:
#         return None
# 
#     version_number = version_number.strip()
#     version_number = version_number.replace("-pre-" , "-pre")
#     version_info = MongoDBVersionInfo(version_number, edition=edition)
# 
#     # validate version string
#     if not is_valid_version_info(version_info):
#         raise MongoctlException("Invalid version '%s." % version_info)
#     else:
#         return version_info


# Path: mongoctl/objects/mongod.py
import server
from mongoctl.utils import resolve_path, system_memory_size_gbs
from mongoctl.mongoctl_logging import log_verbose, log_debug, log_exception, \
    log_warning, log_info
from bson.son import SON
from mongoctl.errors import MongoctlException
from replicaset_cluster import ReplicaSetCluster, get_member_repl_lag
from sharded_cluster import ShardedCluster
from mongoctl.mongodb_version import make_version_info

                db = "/[dbname]"
            else:
                db = ""
        else:
            db = "/" + db

        creds = "[dbuser]:[dbpass]@" if self.is_auth() else ""
        return "mongodb://%s%s%s" % (creds, self.get_address_display(), db)

    ###########################################################################
    def get_rs_status(self):
        try:
            rs_status_cmd = SON([('replSetGetStatus', 1)])
            rs_status =  self.db_command(rs_status_cmd, 'admin')
            return rs_status
        except (Exception,RuntimeError), e:
            log_debug("Cannot get rs status from server '%s'. cause: %s" %
                        (self.id, e))
            log_exception(e)
            return None

    ###########################################################################
    def get_member_rs_status(self):
        rs_status =  self.get_rs_status()
        if rs_status:
            try:
                for member in rs_status['members']:
                    if 'self' in member and member['self']:
                        return member
            except (Exception,RuntimeError), e:

                log_debug("Cannot get member rs status from server '%s'."