#!/usr/bin/env python

## GENERATED FILE - DO NOT EDIT

import urllib2
import sys
import os  
import pymongo


def getClient():
    if 'MONGO_DB_URI' in os.environ:
        return pymongo.MongoClient(os.environ['MONGO_DB_URI'])
    else:
        return pymongo.MongoClient()


def getServerStatus():
    c = getClient()
    return c.admin.command('serverStatus', workingSet=True)

def getDatabasesStats():
    host = "127.0.0.1"
    port = 27017
    c = getClient() 

    dbs = {}
    for k in c.database_names():
        if k != "admin" and k != "local" and k != "":
            db = c[k]
            dbs[k] = {}
            for coll in db.collection_names():
                if '.' not in coll:
                    dbs[k][coll] = db[coll].count()

    return dbs

def doData():
    ss = getDatabasesStats()
    for k,v in ss.iteritems():
        for a,b in v.iteritems():
            print(str(k)+str(a) + ".value " + str(b))


def doConfig():

    print "graph_title MongoDB documents count"
    print "graph_args --base 1000 -l 0 --vertical-label Docs"
    print "graph_category MongoDB"

    ss = getDatabasesStats()
    for k,v in ss.iteritems():
        for a,b in v.iteritems():
            print str(k)+str(a) + ".label " + str(k) + " " + str(a)
            print str(k)+str(a) + ".draw LINE1"

if __name__ == "__main__":          
	
    from os import environ
    if 'HOST' in environ:
        host = environ['HOST']
    if 'PORT' in environ:
        port = environ['PORT']
    if 'USER' in environ:
        user = environ['USER']
    if 'PASSWORD' in environ:
        password = environ['PASSWORD']
    
if len(sys.argv) > 1 and sys.argv[1] == "config":
    doConfig()
else:
    doData()
