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.
1 2 |
WARN = 6000 CRIT = 7000 |
1 2 3 |
[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 |
1 2 3 |
[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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#!/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() |