NAGIOS AWS REDSHIFT TABLE COUNT PLUGIN PYTHON

nagiospython-2redshift

I needed a quick plugin to warn me if one of our AWS REDSHIFT instances had a table count above 6000 and alert critical if above 7000. I decided to write a python plugin for nagios to do the chore. You can see the source code and the example of executing it below on the nagios host.

WARN = 6000
CRIT = 7000
[root@devnagios ~]# /usr/bin/python /usr/local/nagios/libexec/check_rs_table_count.py -d awsdevdb \
-H aws-server.redshift.amazonaws.com -u nagios -p password -w 6000 -c 7000
OK!: table count: 694
[root@devnagios ~]# /usr/bin/python /usr/local/nagios/libexec/check_rs_table_count.py -d awsdevdb \
-H aws-server.redshift.amazonaws.com -u nagios -p password -w 100 -c 200
CRIT!: table count: 694
#!/usr/bin/python
""" MODULE IMPORTS """
import psycopg2
import optparse
import sys

""" NAGIOS API RETURN CODES """
nag_ret_dict = {'NagOk': 0,
                'NagWarn': 1,
                'NagCrit': 2,
                'NagUnknown': 3}

""" COMMAND LINE OPTION PARSING """
usage = "USAGE: python %prog -d DATABASE -H REMOTE_HOST -u USER -p DB_PASSWORD -w 6000 -c 7000"
parser = optparse.OptionParser(usage=usage)
parser.add_option('-d', action="store", default="postgres", help="Database selection", type="string")
parser.add_option('-u', action="store", default="postgres", help="DB User", type="string")
parser.add_option('-H', action="store", default="localhost", help="DB Host system", type="string")
parser.add_option('-p', action="store", default="", help="DB Password", type="string")
parser.add_option('-w', action="store", default="-2", help="Warning threshold int", type="int")
parser.add_option('-c', action="store", default="-2", help="critical threshold int", type="int")
options, args = parser.parse_args()

""" ARG COUNT CHECK """
if len(sys.argv[1:]) == 0:
    parser.print_help()
    sys.exit(nag_ret_dict['NagCrit'])

""" TEST DATABASE CONNECTION """
try:
    conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s' port='5439'" \
    % (options.d, options.u, options.H, options.p))
except psycopg2.DatabaseError, e:
    print('CRIT: Error %s' % e)
    sys.exit(nag_ret_dict['NagCrit'])

""" LOCAL SCOPE FUNCTIONS """
def query_rs():
    try:
        cur = conn.cursor()
        cur.execute("""select count(*) from pg_tables;""")
        result = cur.fetchone()
        cast_result = int(result[0])
        warn_thresh = options.w
        crit_thresh = options.c
        if cast_result >= crit_thresh:
            print('CRIT: table count: %d') % result
            sys.exit(nag_ret_dict['NagCrit'])
        elif cast_result >= warn_thresh:
            print ('WARN: table count: %d') % result
            sys.exit(nag_ret_dict['NagWarn'])
        else:
            print('OK!: tables count: %d') % result
    except psycopg2.DatabaseError, e:
        return ('CRIT: Error %s' % e)
        sys.exit(nag_ret_dict['NagCrit'])
    cur.close()
    conn.close()

""" MAIN FUNCTION """
def main():
        query_rs()

if __name__ == "__main__":
    main()