#!/usr/bin/python

import pg_monitor
import argparse
import sys

def getDescText () :
        return  "Options to be passed as checks  \
                backends  -->> Checks for the number of clients/connections currently connected to the specified database\
                connections  -->> Tests the databases if they currently accept connections or not  \
                wals  -->> keeps tab on the number/size of wal files present in the pg_xlog directory  \
                vacuum -->> Inspects the tables and produces a report of those that has not been vauumed for a given period of time  \
                autovacuum -->> Same as vacuum except that this is triggered by the database's internal processes  \
                analyze  -->> reports tables that have not been analyzed after a given period of time  \
                autoanalyze  -->> Same as analyze but triggered by the database's internal processes. \
                table_bloat  -->> Inspect all tables and reports the bloat percentage which is usually a function of vacuum and/or autovacuuming not running regularly  \
                index_bloat -->> Same as table bloat, except for indexes. This could be remedied by reindexing  \
                blocking  -->> This checks for locks transactional/relational that are blocking other processes from running.  \
                nonblocking -->> This checks for locks that are running more than the specified type. This normally do not block, but could become a blocking lock \
                if it runs longer than necessary. In some cases, it is the long running locks that become the blocking types  \
                table_size -->> Monitors table sizes  \
                index_size -->> Monitors index sizes  \
                database_size  -->> Monitors database sizes  \
                checkpoints -->> Monitors checkpoint frequency  \
                duplicate_indexes -->> Monitors indexes and report duplicates  \
                replica_lag -->> Monitors all replicas connected to a given master "



def runMonitor() :
        parse = argparse.ArgumentParser(prog='pg_monitor' ,description = 'Checks for a PostgreSQL server')
        parse.add_argument ('--port' , type=str , default=5432 , nargs='?', help='Port of the database to be accessed. Default:  %(default)s')
        parse.add_argument ( '--user' , type=str , default='postgres', help='Username allowed to access the database. Default:  %(default)s')
        parse.add_argument ('--password', type=str , default='' , help='Password of the supplied user. Default:  %(default)s' )
        parse.add_argument ( '--dbname', type=str , default='postgres',nargs='?', help='The name(s) of database(s) to be accessed. Default:  %(default)s'  )
        parse.add_argument ( '--host', type=str , default='localhost', nargs='?' ,help='Host of the database. Default:  %(default)s' )
        parse.add_argument ( '--check', type=str, required=True, choices=['backends','connections','wals','vacuum','autovacuum','analyze','autoanalyze',\
                             'table_bloat','index_bloat','blocking','nonblocking','table_size','index_size','database_size','checkpoints',\
                              'duplicate_indexes','replica_lag'], help=getDescText() )
        parse.add_argument ('--find' , type=str , nargs='?',default='', help = 'Finding partterns in locks')
	parse.add_argument ('--exclude_db' , type=str , nargs='?',default=None , help = 'Exclude database not to be queried')
	parse.add_argument ('--exclude_pattern' , type=str , nargs='?',default='', help = 'Exclude patterns not to be searched for')
        parse.add_argument ( '--warning', type=str, default =None, help='Values to be considered as warning signs ' )
        parse.add_argument ( '--critical', type=str , default=None ,help='Values for the critical checks')
	parse.add_argument ( '--output_format', type=str , default='check_mk', choices=['check_mk','cmk','nagios','ngs'] ,help='Values for the critical checks')
        # assign all arguments to an array
        args = parse.parse_args()
        # Get the individual arguments
        param = {}
	host = str( args.host ).split(',')
	port = str( args.port ).split(',')

        param['host'] = host
        param['port'] = port
        param['user'] = args.user
        param['password'] = args.password
        param['dbname'] = str(args.dbname).split(',')
        param['check'] = args.check
        param['find'] = str( args.find ).split(',')
	param['warning'] = args.warning
	param['critical'] = args.critical
	param['exclude_db'] = str ( args.exclude_db ).split(',')
	param['exclude_pattern'] = str ( args.exclude_pattern ).split(',')
	param['output_format'] = args.output_format

	pg_monitor.getArgs(param)


if __name__ == '__main__' :
        runMonitor()

