Add source and example config

master
Emiel Kollof 2019-08-13 23:10:56 +02:00
parent 91cd6a4ff5
commit d20abbfba9
2 changed files with 55 additions and 0 deletions

5
.check_mongo Normal file
View File

@ -0,0 +1,5 @@
[mongodb]
hostname = localhost
username = admin
password = changethis

50
check_mongo.py Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python
import os
import sys
import pymongo
import configparser
from pprint import pprint
def readconfig():
home = os.getenv("HOME")
cfile = home + os.sep + ".check_mongo"
config = configparser.ConfigParser()
if os.path.isfile(cfile):
config.read(cfile)
else:
print("Config file {} not found".format(cfile))
sys.exit(2)
hostname = config.get('mongodb', 'hostname', fallback='localhost')
username = config.get('mongodb', 'username', fallback='admin')
password = config.get('mongodb', 'password', fallback=False)
if not password:
print("Password not present in config")
sys.exit(2)
ret = dict()
ret['username'] = username
ret['hostname'] = hostname
ret['password'] = password
return ret
def main():
config = readconfig()
connectstring = "mongodb://{}:{}@{}".format(config['username'],
config['password'],
config['hostname'])
mdb = pymongo.MongoClient(connectstring)
if __name__ == '__main__':
sys.exit(main())