Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created October 27, 2017 02:23
Show Gist options
  • Save allenyang79/163e4e3be921863a1aa3117697caae71 to your computer and use it in GitHub Desktop.
Save allenyang79/163e4e3be921863a1aa3117697caae71 to your computer and use it in GitHub Desktop.
monitor psql db
import sys
import psycopg2
import psycopg2.extras
conn = psycopg2.connect("host=localhost port=5433 dbname=keycloak user=keycloak password=keycloak")
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute('show pool_nodes')
result = cur.fetchall()
result = sorted(result, key=lambda x:x['hostname'])
#print result
if result[0]['status'] == 'up' and result[0]['role'] == 'primary' and result[0]['status'] == 'up':
sys.exit(0)
#print 'db ha error. primary is not standby'
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment