How to get hostInfo from all nodes in a replica set from Primary

Hi Team,

We know that db.hostInfo() gives System and OS details of the node we connected to.
Also rs.status or rs.conf() or rs.isMaster() do not provide System or OS info.

If we want to get System and OS details of all the nodes from Primary(without connecting to other nodes), Is it possible ?

Could you please suggest me on how to get System and OS details of all the nodes sitting from Primary.

Thanks in Advance

Hi @venkata_reddy,

The db.hostinfo() shell helper calls the hostInfo administrative command, which must be executed against each replica set member.

However, you can iterate the replica set config and connect to each of the members. Here’s a quick example using the mongo shell:

rs.status().members.forEach(
    function(member) {
        print(`[${member._id}]: ${member.name}`);
        if (member.self) {
            printjson(db.adminCommand('hostInfo').os);
        } else {        
            try {
                var mdb = new Mongo(member.name);
                printjson(mdb.adminCommand('hostInfo').os);
            } catch (error) {
                print("Failed: " + printjson(error));
            }
        }
        print("");
    }
)

Note: the above snippet can be run via any member of the replica set. It doesn’t include authentication (you will need to call db.auth() with appropriate credentials), but should be a useful starting point. I used rs.status().members instead of rs.conf().members, because rs.status() includes the self field (no need to open a new connection to the current member) and some additional fields like health and state that might be interesting for logging/monitoring purposes.

For more robust error handling I recommend implementing this using one of the supported MongoDB drivers.

An alternative to rolling your own monitoring solution would be to use MongoDB Cloud Manager (hosted management platform) or MongoDB Ops Manager (on-premises management platform).

In one of your earlier questions (Health script from Ops manager) it sounded like you were already using Ops Manager.

Regards,
Stennie

2 Likes

Hi @Stennie_X,
Great information and thanks for the update.
I got what I have been looking for.
I modified the following.
i)print([${member._id}]: ${member.host}) as print([${member._id}]: ${member.name});
ii)new Mongo(member.host) as new Mongo(member.name).getDB(“admin”);
and used mdb.auth to get authenticated.

Thank you for the kind help

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.