mirror of
https://github.com/Security-Onion-Solutions/securityonion.git
synced 2025-12-06 09:12:45 +01:00
[feat] Add salt module to check if mysql is accepting db connections
This commit is contained in:
35
salt/_modules/mysql.py
Normal file
35
salt/_modules/mysql.py
Normal file
@@ -0,0 +1,35 @@
|
||||
#!py
|
||||
|
||||
from MySQLdb import _mysql
|
||||
import logging
|
||||
import time
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def status(retry):
|
||||
mainint = __salt__['pillar.get']('sensor:mainint', __salt__['pillar.get']('manager:mainint'))
|
||||
mainip = __salt__['grains.get']('ip_interfaces').get(mainint)[0]
|
||||
|
||||
mysql_up = False
|
||||
for i in range(0, retry):
|
||||
log.debug(f'Connection attempt {i+1}')
|
||||
try:
|
||||
_mysql.connect(
|
||||
host=mainip,
|
||||
user="root",
|
||||
passwd=__salt__['pillar.get']('secrets:mysql')
|
||||
)
|
||||
mysql_up = True
|
||||
break
|
||||
except _mysql.OperationalError as e:
|
||||
log.debug(e)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
if not mysql_up:
|
||||
log.error(f'Could not connect to MySQL server on {mainip} after {retry} attempts.')
|
||||
|
||||
return mysql_up
|
||||
Reference in New Issue
Block a user