{"id":500,"date":"2016-11-22T16:25:56","date_gmt":"2016-11-22T21:25:56","guid":{"rendered":"http:\/\/jasonralph.org\/?p=500"},"modified":"2016-11-25T09:57:53","modified_gmt":"2016-11-25T14:57:53","slug":"nagios-aws-redshift-table-count-plugin-python","status":"publish","type":"post","link":"https:\/\/jasonralph.org\/?p=500","title":{"rendered":"NAGIOS AWS REDSHIFT TABLE COUNT PLUGIN PYTHON"},"content":{"rendered":"<p>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.  <\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\nWARN = 6000\r\nCRIT = 7000\r\n<\/pre>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n[root@devnagios ~]# \/usr\/bin\/python \/usr\/local\/nagios\/libexec\/check_rs_table_count.py -d awsdevdb \\\r\n-H aws-server.redshift.amazonaws.com -u nagios -p password -w 6000 -c 7000\r\nOK!: table count: 694\r\n<\/pre>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n[root@devnagios ~]# \/usr\/bin\/python \/usr\/local\/nagios\/libexec\/check_rs_table_count.py -d awsdevdb \\\r\n-H aws-server.redshift.amazonaws.com -u nagios -p password -w 100 -c 200\r\nCRIT!: table count: 694\r\n<\/pre>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n#!\/usr\/bin\/python\r\n\"\"\" MODULE IMPORTS \"\"\"\r\nimport psycopg2\r\nimport optparse\r\nimport sys\r\n\r\n\"\"\" NAGIOS API RETURN CODES \"\"\"\r\nnag_ret_dict = {'NagOk': 0,\r\n                'NagWarn': 1,\r\n                'NagCrit': 2,\r\n                'NagUnknown': 3}\r\n\r\n\"\"\" COMMAND LINE OPTION PARSING \"\"\"\r\nusage = \"USAGE: python %prog -d DATABASE -H REMOTE_HOST -u USER -p DB_PASSWORD -w 6000 -c 7000\"\r\nparser = optparse.OptionParser(usage=usage)\r\nparser.add_option('-d', action=\"store\", default=\"postgres\", help=\"Database selection\", type=\"string\")\r\nparser.add_option('-u', action=\"store\", default=\"postgres\", help=\"DB User\", type=\"string\")\r\nparser.add_option('-H', action=\"store\", default=\"localhost\", help=\"DB Host system\", type=\"string\")\r\nparser.add_option('-p', action=\"store\", default=\"\", help=\"DB Password\", type=\"string\")\r\nparser.add_option('-w', action=\"store\", default=\"-2\", help=\"Warning threshold int\", type=\"int\")\r\nparser.add_option('-c', action=\"store\", default=\"-2\", help=\"critical threshold int\", type=\"int\")\r\noptions, args = parser.parse_args()\r\n\r\n\"\"\" ARG COUNT CHECK \"\"\"\r\nif len(sys.argv[1:]) == 0:\r\n    parser.print_help()\r\n    sys.exit(nag_ret_dict['NagCrit'])\r\n\r\n\"\"\" TEST DATABASE CONNECTION \"\"\"\r\ntry:\r\n    conn = psycopg2.connect(\"dbname='%s' user='%s' host='%s' password='%s' port='5439'\" \\\r\n    % (options.d, options.u, options.H, options.p))\r\nexcept psycopg2.DatabaseError, e:\r\n    print('CRIT: Error %s' % e)\r\n    sys.exit(nag_ret_dict['NagCrit'])\r\n\r\n\"\"\" LOCAL SCOPE FUNCTIONS \"\"\"\r\ndef query_rs():\r\n    try:\r\n        cur = conn.cursor()\r\n        cur.execute(\"\"\"select count(*) from pg_tables;\"\"\")\r\n        result = cur.fetchone()\r\n        cast_result = int(result[0])\r\n        warn_thresh = options.w\r\n        crit_thresh = options.c\r\n        if cast_result >= crit_thresh:\r\n            print('CRIT: table count: %d') % result\r\n            sys.exit(nag_ret_dict['NagCrit'])\r\n        elif cast_result >= warn_thresh:\r\n            print ('WARN: table count: %d') % result\r\n            sys.exit(nag_ret_dict['NagWarn'])\r\n        else:\r\n            print('OK!: tables count: %d') % result\r\n    except psycopg2.DatabaseError, e:\r\n        return ('CRIT: Error %s' % e)\r\n        sys.exit(nag_ret_dict['NagCrit'])\r\n    cur.close()\r\n    conn.close()\r\n\r\n\"\"\" MAIN FUNCTION \"\"\"\r\ndef main():\r\n        query_rs()\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,24,50],"class_list":["post-500","post","type-post","status-publish","format-standard","hentry","category-python","tag-nagios","tag-python-2","tag-redshift"],"_links":{"self":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=500"}],"version-history":[{"count":10,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/500\/revisions"}],"predecessor-version":[{"id":518,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/500\/revisions\/518"}],"wp:attachment":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}