check_mongodb.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:ansible-optools 作者: jonjozwiak 项目源码 文件源码
def mongo_connect(host=None, port=None,ssl=False, user=None,passwd=None,replica=None):
    try:
        # ssl connection for pymongo > 2.1
        if pymongo.version >= "2.1":
            if replica is None:
                con = pymongo.Connection(host, port, read_preference=pymongo.ReadPreference.SECONDARY, ssl=ssl, network_timeout=10)
            else:
                con = pymongo.Connection(host, port, read_preference=pymongo.ReadPreference.SECONDARY, ssl=ssl, replicaSet=replica, network_timeout=10)
        else:
            if replica is None:
                con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
            else:
                con = pymongo.Connection(host, port, slave_okay=True, replicaSet=replica, network_timeout=10)

        if user and passwd:
            db = con["admin"]
            if not db.authenticate(user, passwd): sys.exit("Username/Password incorrect")
    except Exception, e:
        if isinstance(e,pymongo.errors.AutoReconnect) and str(e).find(" is an arbiter") != -1:
            # We got a pymongo AutoReconnect exception that tells us we connected to an Arbiter Server
            # This means: Arbiter is reachable and can answer requests/votes - this is all we need to know from an arbiter
            print "OK - State: 7 (Arbiter)"
            sys.exit(0)
        return exit_with_general_critical(e),None
    return 0,con
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号