Skip to content

Commit

Permalink
Merge pull request #1154 from proditis/issue-1152
Browse files Browse the repository at this point in the history
Add support for database healthchecks fixes #1152
  • Loading branch information
proditis authored Apr 19, 2024
2 parents f105c5a + 024ce65 commit d01c606
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ansible/runonce/db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
kern.maxfiles: 312180
rcctl:
- { name: check_quotas, state: "disable" }
- { name: cron, state: "disable" }
- { name: cron, state: "enable" }
- { name: resolved, state: "disable" }
#- { name: dhcpleased, state: "disable" }
- { name: ntpd, state: "enable" }
Expand Down Expand Up @@ -128,6 +128,12 @@
content: "{{ myname }}\n"
dest: /etc/myname

- name: Copy events checker
template:
src: "{{playbook_dir}}/../../contrib/mysql-events-checker.sh"
mode: 0555
dest: /usr/local/sbin/mysql-events-checker

- name: Create fresh /etc/hosts
copy:
content: "127.0.0.1 localhost\n{{db_ip}} {{ myname.split('.')[0] | lower }} {{ myname }}\n"
Expand Down Expand Up @@ -387,5 +393,15 @@
failed_when: result.rc not in [0,2]
register: result

- name: Install cron entries
cron:
name: "{{item.name}}"
user: "root"
minute: "{{item.minute | default(omit)}}"
special_time: "{{item.special_time|default(omit)}}"
job: "{{item.job}}"
with_items:
- { name: "events checker", minute: "*/1", job: "/usr/local/sbin/mysql-events-checker" }

- name: display post install message
debug: msg="Reboot the system for the changes to take effect"
76 changes: 76 additions & 0 deletions contrib/mysql-events-checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/sh
#
# Check a given database for events disabled
# more than a given number of seconds (300 default).
#

## default values modify as you please
DATABASE="echoCTF"
INTERVAL=300
VERBOSE=0
ENABLE=1


usage () {
echo -e "\t\tMySQL events checker"
echo -e "\t\t--------------------\n"
echo "usage: $0 [-ehv] ] [-d database] [-i interval]"
echo " -h this help"
echo " -v verbose output (default: disabled)"
echo " -e dont enable events that are disabled (default: enable)"
echo " -d database check events for the given database (using: ${DATABASE})"
echo " -i interval check for events disabled longer than interval in seconds (using: ${INTERVAL})"
echo
}


OPTSTRING=":vehd:i:"

while getopts ${OPTSTRING} opt; do
case ${opt} in
d)
DATABASE="${OPTARG}"
;;
i)
INTERVAL="${OPTARG}"
;;
v)
VERBOSE=1
;;
e)
ENABLE=0
;;
h)
usage
exit 0
;;
:)
echo "Option -${OPTARG} requires an argument."
echo
usage
exit 1
;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
exit 1
;;
esac
done

if [ $VERBOSE -ne 0 ]; then
echo "Using database: ${DATABASE}"
fi

for _ev in $(mysql -NBe "SELECT CONCAT('${DATABASE}.',EVENT_NAME) from INFORMATION_SCHEMA.EVENTS WHERE EVENT_SCHEMA='${DATABASE}' AND STATUS='DISABLED' AND (UNIX_TIMESTAMP(LAST_ALTERED)+${INTERVAL})<UNIX_TIMESTAMP(now()) AND @@EVENT_SCHEDULER='ON'");do
if [ $VERBOSE -ne 0 ]; then
echo "EVENT ${_ev} IS DISABLED LONGER THAN ${INTERVAL} seconds";
fi
if [ $ENABLE -ne 0 ]; then
if [ $VERBOSE -ne 0 ]; then
echo "ENABLING ${_ev}"
fi
mysql -e "ALTER EVENT ${_ev} ENABLE"
fi
done;

0 comments on commit d01c606

Please sign in to comment.