#!/usr/bin/python import sys import time import datetime import mysql.connector params = dict(map(lambda x: x.split(':'), sys.argv[1:])) creds = {'database': params['db'], 'host': params['host'], 'user': params['user'], 'password': params['pw']} warn = float(params['warn']) crit = float(params['crit']) if crit < warn: print "ERROR: You Specify CRITCAL Limit less then WARNING!" sys.exit(0) cnx = mysql.connector.connect(**creds) cursor = cnx.cursor() query = ("SELECT ts, file, position FROM heartbeat WHERE server_id=%s") cursor.execute(query, (params['sid'],)) for (ts, master_log_file, master_log_position) in cursor: ts = ts.split('.')[0] now = time.time() try: data_was_updated = time.mktime(time.strptime(ts, '%Y-%m-%dT%H:%M:%S')) except ValueError: data_was_updated = time.mktime(time.strptime(ts, '%Y-%m-%d %H:%M:%S')) delta = float(now) - float(data_was_updated) cursor.close() cnx.close() def format_output(status): global delta if delta <= 600.0: print 'SLAVE %s: %0.2f seconds behind master (warn - %0.2f, crit - %0.2f), master_log_file = %s, master_log_pos = %s' % (status, delta, warn, crit, master_log_file, master_log_position) else: delta = str(datetime.timedelta(seconds=delta)) delta = delta.split('.')[0] print 'SLAVE %s: %s behind master (warn - %0.2f, crit - %0.2f), master_log_file = %s, master_log_pos = %s' % (status, delta, warn, crit, master_log_file, master_log_position) if master_log_file is None or ts is None: print 'SLAVE UNKNOWN: not full data in heartbeat table' sys.exit(4) elif delta < warn: format_output('OK') sys.exit(0) elif delta < crit and delta >= warn: format_output('WARNING') sys.exit(1) elif delta >= crit: format_output('CRITICAL') sys.exit(2)