0
votes

I will preface by saying that I am a novice in both Python and MongoDB.

I am developing a Python script that connects to a Linux Server with an instance of MongoDB and retrieves MongoDB's configuration information. To do this, I have been using Pymongo. For example, here is what I have used to retrieve the "authorization" parameter.

from pymongo import MongoClient
db = MongoClient(dbhost, int(dbport))
db['admin'].authenticate(dbuser,dbpass)

output = db['admin'].command("getCmdLineOpts")

This is equivalent to running the following command in the mongo shell on the server itself.

db.adminCommand("getCmdLineOpts")

The output looks something like this:

{
        "argv" : [
                "/usr/bin/mongod",
                "-f",
                "/etc/mongod.conf"
        ],
        "parsed" : {
                "auditLog" : {
                        "destination" : "file",
                        "filter" : "{ atype: { $in: [ \"createCollection\", \"dropCollection\" ] } }",
                        "format" : "JSON",
                        "path" : "/var/log/mongodb/auditlog.json"
                },
                "config" : "/etc/mongod.conf",
                "net" : {
                        "port" : 27017
                },
                "processManagement" : {
                        "fork" : true,
                        "pidFilePath" : "/var/run/mongodb/mongod.pid",
                        "timeZoneInfo" : "/usr/share/zoneinfo"
                },
                "security" : {
                        "authorization" : "enabled",
                        "clusterAuthMode" : "keyFile",
                        "javascriptEnabled" : false,
                        "keyFile" : "/var/lib/mongo/keyfile"
                },
                "setParameter" : {
                        "enableLocalhostAuthBypass" : "false"
                },
                "storage" : {
                        "dbPath" : "/var/lib/mongo",
                        "journal" : {
                                "enabled" : true
                        }
                },
                "systemLog" : {
                        "destination" : "file",
                        "logAppend" : true,
                        "path" : "/var/log/mongodb/mongod.log",
                        "quiet" : false
                }
        },
        "ok" : 1
}

I have then been able to parse out the parameter I need. For example, to get the audit Log path:

auditLogPath = output['parsed']['auditLog']['path']

which is equivalent to running this in the mongo shell

db.adminCommand("getCmdLineOpts").parsed.auditLog.path

This code works fine, but I am stuck trying to find an equivalent PyMongo command for the Mongo shell native methods. By native methods, this is what I mean https://docs.mongodb.com/manual/reference/method/js-native/.

Specifically, I am trying to read the mongod.service file and report on the parameters in the file. In the mongo shell I can run the following command and receive the contents of the file at that location.

cat("/usr/lib/systemd/system/mongod.service")

I am wondering if there is a way to run these native methods (such as ls or cat), in Pymongo, using some similar structure to

db.command(<commandName>)

I appreciate any help!

1

1 Answers

0
votes

getCmdLineOpts is a server command.

You can issue it as any other command by running {getCmdLineOpts:1}, and it is my understanding that pymongo permits specifying command name only (How to run raw mongodb commands from pymongo) so you could potentially do

db.command("getCmdLineOpts")

Shell native methods only exist in the shell. They do not exist on the server and they are not implemented by pymongo. Python equivalent of cat is something like

open('/path/to/file').read()

and for ls there is How do I list all files of a directory?.