add new check and update other one

This commit is contained in:
Cyberes 2024-04-05 17:45:24 -06:00
parent b66ba4fa37
commit 4b30649bbd
2 changed files with 87 additions and 1 deletions

View File

@ -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)

View File

@ -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)