From 4b30649bbdd7db0a6ad012a319da07816d69c96b Mon Sep 17 00:00:00 2001 From: Cyberes Date: Fri, 5 Apr 2024 17:45:24 -0600 Subject: [PATCH] add new check and update other one --- check_mysql_connections.py | 80 ++++++++++++++++++++++++++++++++++++++ check_mysql_slave.py | 8 +++- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 check_mysql_connections.py diff --git a/check_mysql_connections.py b/check_mysql_connections.py new file mode 100644 index 0000000..e330f3d --- /dev/null +++ b/check_mysql_connections.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python +import argparse +import sys +import traceback + +import mysql.connector + +from checker import nagios +from checker.result import quit_check + + +def main(args): + try: + connection = mysql.connector.connect(host=args.host, user=args.username, password=args.password) + cursor = connection.cursor() + cursor.execute("SHOW STATUS LIKE 'Threads_connected'") + result = cursor.fetchone() + active_connections = int(result[1]) + + cursor.execute("SHOW VARIABLES LIKE 'max_connections'") + result = cursor.fetchone() + max_connections = int(result[1]) + + cursor.close() + connection.close() + + except mysql.connector.Error as e: + quit_check(f'unable to connect to MySQL server: {e}', nagios.STATE_CRIT) + return # make pycharm happy + + # =================================================== + # Parse data + + warn_percent = args.warn_percent / 100 + crit_percent = args.crit_percent / 100 + active_connections_percent = active_connections / max_connections + active_connections_percent_str = str(int(active_connections_percent * 100)) + + if active_connections_percent > crit_percent: + exit_code = nagios.STATE_CRIT + elif active_connections_percent > warn_percent: + exit_code = nagios.STATE_WARN + else: + exit_code = nagios.STATE_OK + + perfdata = { + 'percent_active_connections': { + 'value': active_connections_percent_str, + 'min': 0, + 'unit': '%', + 'warn': int(warn_percent * 100), + 'crit': int(crit_percent * 100) + }, + 'active_connections': { + 'value': active_connections, + 'min': 0, + 'max': max_connections, + 'warn': int(active_connections * warn_percent), + 'crit': int(active_connections * crit_percent) + } + } + text_str = f'{active_connections}/{max_connections} ({active_connections_percent_str}%) of available connections used' + quit_check(text_str, exit_code, perfdata) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Check MySQL connections to a server.') + parser.add_argument('--host', required=True, help='The IP of the MySQL server to connect to.') + parser.add_argument('--username', required=True, help='Username.') + parser.add_argument('--password', required=True, help='Password.') + parser.add_argument('--warn-percent', default=50, type=int, help='Connections warning percentage. Default: 50%') + parser.add_argument('--crit-percent', default=75, type=int, help='Connections critical percentage. Default: 75%') + args = parser.parse_args() + + try: + main(args) + except Exception as e: + print(f"UNKNOWN - {e}") + traceback.print_exc() + sys.exit(nagios.STATE_UNKNOWN) diff --git a/check_mysql_slave.py b/check_mysql_slave.py index eb9608b..cd86507 100755 --- a/check_mysql_slave.py +++ b/check_mysql_slave.py @@ -136,4 +136,10 @@ if __name__ == "__main__": parser.add_argument('--warning-deviation', default=10, type=int, help='If the delay deviates more than this percentage from the target delay, return warning.') parser.add_argument('--critical-deviation', default=15, type=int, help='If the delay deviates more than this percentage from the target delay, return critical.') args = parser.parse_args() - main(args) + + try: + main(args) + except Exception as e: + print(f"UNKNOWN - {e}") + traceback.print_exc() + sys.exit(nagios.STATE_UNKNOWN)