{"id":262,"date":"2023-09-13T17:14:51","date_gmt":"2023-09-13T17:14:51","guid":{"rendered":"http:\/\/77interactive.com\/?p=262"},"modified":"2023-09-25T22:07:26","modified_gmt":"2023-09-25T22:07:26","slug":"read-mongodb-configuration-file","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=262","title":{"rendered":"Read MongoDB Configuration File"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Find and read the file using Python<\/h2>\n\n\n\n<p>The following python script reads mongod.conf file and returns some settings. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\r\nimport sys\r\nimport yaml\r\nimport json\r\n\r\ndef read_mongodb_conf(file_path):\r\n    # Define the settings we're interested in reading from mongod.conf\r\n    # If a setting is not set or commented out, then \"Not Set\" is displayed\r\n    settings = &#91;\r\n        \"net.bindIp\", \"net.tls.mode\",\"replication.replSetName\", \"replication.oplogSizeMB\", \"net.port\",\r\n        \"security.authorization\", \"security.keyFile\", \"storage.dbPath\", \"systemLog.path\", \"systemLog.logAppend\",\r\n        \"storage.engine\", \"storage.journal.enabled\", \"storage.journal.commitIntervalMs\",\r\n        \"operationProfiling.mode\", \"operationProfiling.slowOpThresholdMs\", \"operationProfiling.slowOpSampleRate\",\r\n        \"storage.wiredTiger.engineConfig.cacheSizeGB\", \"storage.wiredTiger.engineConfig.directoryForIndexes\"\r\n    ]\r\n\r\n    # Initialize a dictionary with \"Not Set\" as default for all settings and empty comments\r\n    # These will be updated with the actual values, if they exist\r\n    values = {setting: \"Not Set\" for setting in settings}\r\n\r\n    try:\r\n        # Open the file and load the YAML data\r\n        with open(file_path, 'r') as file:\r\n            config = yaml.safe_load(file)\r\n            \r\n            # Loop through the settings in the file\r\n            for setting in settings:\r\n                keys = setting.split('.')\r\n                value = config\r\n                \r\n                # Navigate through the nested keys\r\n                for key in keys:\r\n                    if key in value:\r\n                        value = value&#91;key]\r\n                    else:\r\n                        value = None\r\n                        break\r\n                # If the value is not None, then update the dictionary\r\n                # with the actual value.\r\n                if value is not None:\r\n                    values&#91;setting] = value\r\n    # Handle exceptions\r\n    except FileNotFoundError:\r\n        print(f\"Error: The file '{file_path}' was not found.\")\r\n        return\r\n    except yaml.YAMLError:\r\n        print(f\"Error: Invalid YAML format in '{file_path}'.\")\r\n        return\r\n\r\n    return values\r\n\r\nif __name__ == \"__main__\":\r\n    # Set the default path to the MongoDB configuration file\r\n    # and the output file.\r\n    default_path = \"\/etc\/mongod.conf\"\r\n    output_file = \"\/tmp\/mongoverification.json\"\r\n    \r\n    # If the user passes -h or --help, then display the help message\r\n    if len(sys.argv) == 2 and sys.argv&#91;1] in &#91;\"-h\", \"--help\"]:\r\n        print(\"Usage: python script_name.py &#91;path_to_mongod.conf]\")\r\n        print(f\"Default path if not specified: {default_path}\")\r\n        print(\"Example: python script_name.py \/path\/to\/your\/mongod.conf\")\r\n    else:\r\n        file_path = sys.argv&#91;1] if len(sys.argv) > 1 else default_path\r\n        values = read_mongodb_conf(file_path)\r\n\r\n        if values:\r\n            # Output to the console\r\n            print(\"{:&lt;30} | {}\".format(\"Setting\", \"Current Value\"))\r\n            print(\"-\" * 65)\r\n            for setting, value in values.items():\r\n                print(\"{:&lt;30} | {}\".format(setting, value))\r\n\r\n            # Append the results to the file in JSON format\r\n            with open(output_file, 'a') as json_file:\r\n                json.dump(values, json_file, indent=4)\r\n                json_file.write('\\n')  # Separate entries by a newline\r\n\r\n            print(f\"\\nResults appended to: {output_file}\")\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Find and read the file using Python The following python script reads mongod.conf file and returns some settings.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,2],"tags":[42,5,3],"class_list":["post-262","post","type-post","status-publish","format-standard","hentry","category-mongodb","category-python","tag-linux","tag-mongodb","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Read MongoDB Configuration File - 77 Interactive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/77interactive.com\/?p=262\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Read MongoDB Configuration File - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"Find and read the file using Python The following python script reads mongod.conf file and returns some settings.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=262\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-13T17:14:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-25T22:07:26+00:00\" \/>\n<meta name=\"author\" content=\"Rudy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=262#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=262\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Read MongoDB Configuration File\",\"datePublished\":\"2023-09-13T17:14:51+00:00\",\"dateModified\":\"2023-09-25T22:07:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=262\"},\"wordCount\":23,\"keywords\":[\"Linux\",\"mongodb\",\"python\"],\"articleSection\":[\"mongodb\",\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=262\",\"url\":\"http:\/\/77interactive.com\/?p=262\",\"name\":\"Read MongoDB Configuration File - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"datePublished\":\"2023-09-13T17:14:51+00:00\",\"dateModified\":\"2023-09-25T22:07:26+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=262#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=262\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=262#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Read MongoDB Configuration File\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/77interactive.com\/#website\",\"url\":\"http:\/\/77interactive.com\/\",\"name\":\"77 Interactive\",\"description\":\"Rudy&#039;s Code snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/77interactive.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\",\"name\":\"Rudy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"caption\":\"Rudy\"},\"url\":\"http:\/\/77interactive.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Read MongoDB Configuration File - 77 Interactive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/77interactive.com\/?p=262","og_locale":"en_US","og_type":"article","og_title":"Read MongoDB Configuration File - 77 Interactive","og_description":"Find and read the file using Python The following python script reads mongod.conf file and returns some settings.","og_url":"http:\/\/77interactive.com\/?p=262","og_site_name":"77 Interactive","article_published_time":"2023-09-13T17:14:51+00:00","article_modified_time":"2023-09-25T22:07:26+00:00","author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=262#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=262"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Read MongoDB Configuration File","datePublished":"2023-09-13T17:14:51+00:00","dateModified":"2023-09-25T22:07:26+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=262"},"wordCount":23,"keywords":["Linux","mongodb","python"],"articleSection":["mongodb","Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=262","url":"http:\/\/77interactive.com\/?p=262","name":"Read MongoDB Configuration File - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"datePublished":"2023-09-13T17:14:51+00:00","dateModified":"2023-09-25T22:07:26+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=262#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=262"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=262#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Read MongoDB Configuration File"}]},{"@type":"WebSite","@id":"http:\/\/77interactive.com\/#website","url":"http:\/\/77interactive.com\/","name":"77 Interactive","description":"Rudy&#039;s Code snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/77interactive.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9","name":"Rudy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","caption":"Rudy"},"url":"http:\/\/77interactive.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/262\/revisions"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}