File: //lib/python2.7/site-packages/lap/pgsql_replication_master.py
import psycopg2
# get the parameters to connect
def __run__(params):
conn = None
try:
conn = psycopg2.connect(host=params.get('host'),
port=int(params.get('port')),
database=params.get('dbname'),
user=params.get('user'),
password=params.get('passwd'))
cursor = conn.cursor()
# get the number of slaves connected
cursor.execute("SELECT COUNT(*) AS slave_count FROM pg_stat_replication;")
number_slaves = cursor.fetchone()[0]
if number_slaves == 0:
return[2, 'CRITICAL: There is no slave connected']
elif number_slaves > 0:
return[0, 'OK: Number of slaves connected: %s' % number_slaves]
else:
return[2, 'CRITICAL: Impossible to determine the number of slaves']
except Exception, e:
return [2, 'CRITICAL: {0}'.format(str(e))]
print "erro"
finally:
try:
if conn:
conn.close()
except:
# finally clause, don't bother about errors here
pass