Add source and example config
parent
91cd6a4ff5
commit
d20abbfba9
|
@ -0,0 +1,5 @@
|
|||
[mongodb]
|
||||
hostname = localhost
|
||||
username = admin
|
||||
password = changethis
|
||||
|
|
@ -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())
|
Loading…
Reference in New Issue