/
check_repdelay.py
executable file
·106 lines (88 loc) · 3.79 KB
/
check_repdelay.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# written in 2.6.6 on CentOS 6. All other versions untested.
#Header Info
__author__= 'Splix76'
__email__ = 'splixseventysix@gmail.com'
__version__ = '1.0'
__license__ = "GPLv3"
__maintainer__ = "Splix76"
__status__ = "Production"
__credits__ = "Splix76"
"""
This script will check the hot standby replication delay of a postgresql database.
It is more secure to provide the user executing the script with a .pgpass file than to include the password in the script.
"""
#import libraries
from optparse import OptionParser, OptionGroup
import psycopg2
import sys
# parse command arguemnts and return options
def parse_args():
parser = OptionParser()
parser.description = "Check streaming replication delay"
parser.version = __version__
parser.add_option("-H", "--host", dest="hostname", default="127.0.0.1",
help="Name of the host you are checking")
parser.add_option("-O", "--port", dest="port", default="5432",
help="Port you will connect to the database with")
parser.add_option("-U", "--user", dest="username", default="postgres",
help="Username for the database")
parser.add_option("-P", "--password", dest="password",
help="Password the database")
parser.add_option("-D", "--database", dest="database",
help="Datbase you are checking")
parser.add_option("-W", "--warn", dest="warn", default="300",
help="Warning alert delay in seconds")
parser.add_option("-C", "--crit", dest="crit", default="1800",
help="Critical alert delay in seconds")
(options, args) = parser.parse_args()
return options
# check delay using options from parse_args
def check_delay(options):
username = str(options.username)
password = str(options.password)
port = str(options.port)
hostname = str(options.hostname)
database = str(options.database)
#if password is needed, uncomment and set it
conn_string = "host=" + hostname + " dbname=" + database + " user=" + username #+ " password=" + password
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute('SELECT CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0 ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp()) END AS log_delay;')
delay = cursor.fetchall()
delay = delay.pop()
delay = delay[0]
return delay
# return results and graphing data to Nagios
def nagios(delay,options):
#nagios return codes
UNKNOWN = -1
OK = 0
WARNING = 1
CRITICAL = 2
warn = float(options.warn)
crit = float(options.crit)
#pop delay out of list and get float out of tuple for direct comparison to warn/crit float values
if delay > crit:
print "CRITICAL - Rep Delay is:", str(delay), 'Seconds', '| Seconds=' + str(delay) + 's' + str(";") + str(warn) + str(";") + str(crit) + str(";" ) + str("14400")
sys.exit(CRITICAL)
elif delay > warn:
print "WARNING - Rep Delay is:", str(delay), 'Seconds', '| Seconds=' + str(delay) + 's' + str(";") + str(warn) + str(";") + str(crit) + str(";" ) + str("14400")
sys.exit(WARNING)
elif delay < warn and delay < crit:
print "OK - Rep Delay is:", str(delay), 'Seconds', '| Seconds=' + str(delay) + 's' + str(";") + str(warn) + str(";") + str(crit) + str(";" ) + str("14400")
sys.exit(OK)
else:
print "UNKNOWN"
sys.exit(UNKNOWN)
# main function, controls flow of script
def main():
#call parse_arges and return options for script
options = parse_args()
# execute command using options from parse_args
delay = check_delay(options)
#call nagios process
nagios(delay,options)
# call main function
if __name__ == '__main__':
main()