81 lines
2.8 KiB
Python
Executable File
81 lines
2.8 KiB
Python
Executable File
#!/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(max_connections * warn_percent),
|
|
'crit': int(max_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 to connect with.')
|
|
parser.add_argument('--password', required=True, help='Password to connect with.')
|
|
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)
|