简体   繁体   中英

Hi, How do I run the equivalent using pymongo? cfg = rs.conf() db.printSlaveReplicationInfo()

1>How can I run the equivalent using pymongo?

a>

cfg = rs.conf()

b>

db.printSlaveReplicationInfo()

2>Using PyMongo, how can I get the details of other replica sets CLI output in the created clusters . (Note:I have already successfully created cluster.Just I am writing a python script in primary to check the outputs of rs.conf() and db.printSlaveReplicationInfo() in all the replica sets inside cluster and parse the output.)

Any help on this regard is greatly appreciable.

The replica set configuration is stored in the "local" database in a collection called "system.replset" so the equivalent of rs.conf() would be db.system.replset.findOne() when db is local or its equivalent find_one() in Python.

db.printSlaveReplicationInfo() is a big more involved, but you can get all of that information in the local database as well.

It may be easier to get it via admin database command replSetGetStatus which returns a document containing oplog information for each member of the replica set, along with other details. Python MongoDB driver pymongo provides a method to run commands , so you can run it against the admin DB and parse out the output for information about where each member of the replica set is relative to the primary.

I'm basically going to give you a hint rather than directly answer it, because the full answer is to simply code it. But you probably are unaware that you can do this simple thing in the shell:

> db.printSlaveReplicationInfo
function () {
    var startOptimeDate = null;

    function getReplLag(st) {
        assert( startOptimeDate , "how could this be null (getReplLag startOptimeDate)"     );
        print("\tsyncedTo: " + st.toString() );
        var ago = (startOptimeDate-st)/1000;
        var hrs = Math.round(ago/36)/100;
        print("\t" + Math.round(ago) + " secs (" + hrs + " hrs) behind the primary ");
    };

    function getMaster(members) {
        var found;
        members.forEach(function(row) {
            if (row.self) {
                found = row;
                return false;
            }
        });

        if (found) {
            return found;
        }
    };

    function g(x) {
        assert( x , "how could this be null (printSlaveReplicationInfo gx)" )
        print("source: " + x.host);
        if ( x.syncedTo ){
            var st = new Date( DB.tsToSeconds( x.syncedTo ) * 1000 );
            getReplLag(st);
        }
        else {
            print( "\tdoing initial sync" );
        }
    };

    function r(x) {
        assert( x , "how could this be null (printSlaveReplicationInfo rx)" );
        if ( x.state == 1 || x.state == 7 ) {  // ignore primaries (1) and arbiters (7)
            return;
        }

        print("source: " + x.name);
        if ( x.optime ) {
            getReplLag(x.optimeDate);
        }
        else {
            print( "\tno replication info, yet.  State: " + x.stateStr );
        }
    };

    var L = this.getSiblingDB("local");

    if (L.system.replset.count() != 0) {
        var status = this.adminCommand({'replSetGetStatus' : 1});
        startOptimeDate = getMaster(status.members).optimeDate;
        status.members.forEach(r);
    }
    else if( L.sources.count() != 0 ) {
        startOptimeDate = new Date();
        L.sources.find().forEach(g);
    }
    else {
        print("local.sources is empty; is this db a --slave?");
        return;
    }
}

I love REPL's , and much like python's own famous REPL you can just get a dump of what the implemented function does.

Simples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM