refs #8140 fix cron and scheduler-log script

This commit is contained in:
Juan Ferrer 2025-02-07 14:18:48 +01:00
parent eaef1864af
commit 73d5ac435f
4 changed files with 51 additions and 20 deletions

View File

@ -0,0 +1 @@
*/30 * * * * root /root/scripts/scheduler-log.sh

View File

@ -3,7 +3,9 @@ set -e
logFile="/var/log/mysql/error.log"
dateFile="/tmp/mysql_scheduler_log-lastdate"
logTable="util.eventLog"
logSchema="util"
logTable="eventLog"
pattern='^\d{4}-\d{2}-\d{2}\s\d{1,2}:\d{2}:\d{2}\s\d+\s\[ERROR\] Event Scheduler:'
purgeDays=30
quote() {
@ -17,33 +19,55 @@ if [ "$?" -ne "0" ]; then
exit
fi
if [ -f "$dateFile" ]; then
fromDate=$(cat "$dateFile")
else
fromDate=0
tableExists=$(mysql -Ns -e "SHOW TABLES FROM $logSchema LIKE '$logTable'")
if [ -z "$tableExists" ]; then
mysql <<-EOF
CREATE SCHEMA IF NOT EXISTS $logSchema;
CREATE TABLE $logSchema.$logTable (
id int(11) NOT NULL AUTO_INCREMENT,
date datetime NOT NULL,
event varchar(512) NOT NULL,
error varchar(1024) NOT NULL,
PRIMARY KEY (id),
KEY date (date)
) ENGINE=InnoDB COMMENT='Event scheduler error log';
EOF
fi
lastDate=$(tail -n1 "$logFile" | awk '{print $1" "$2}')
toDate=$(date +%s -d "$lastDate")
if [ -f "$dateFile" ]; then
read -r fromDate < "$dateFile"
else
fromDate=$(date -d "-$purgeDays days" +%s)
fi
awk -v fromDate="$fromDate" -v toDate="$toDate" '{
toDate=$(date +%s)
grep -P "$pattern" "$logFile" | awk -v fromDate="$fromDate" -v toDate="$toDate" '{
split($1, date, "-");
split($2, time, ":");
timestamp = mktime(date[1]" "date[2]" "date[3]" "time[1]" "time[2]" "time[3])
if (timestamp >= fromDate && timestamp < toDate && $4" "$5" "$6 == "[ERROR] Event Scheduler:") {
if (timestamp >= fromDate && timestamp < toDate) {
printf $1" "$2" "$7;
for (i=8; i<=NF; i++) printf FS $i ;
print "";
}
}' "$logFile" | \
\
}' | \
while read line; do
date="$(echo "$line" | cut -d' ' -f1,2)"
event="$(echo "$line" | cut -d' ' -f3)"
error="$(echo "$line" | cut -d' ' -f4-)"
echo "INSERT INTO $logTable (date, event, error)" \
"VALUES ($(quote "$date"), $(quote "$event"), $(quote "$error"))" | mysql
mysql <<-EOF
INSERT INTO $logSchema.$logTable SET
date = $(quote "$date"),
event = $(quote "$event"),
error = $(quote "$error")
EOF
done
echo -n "$toDate" > "$dateFile"
echo "DELETE FROM $logTable WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())" | mysql
echo "$toDate" > "$dateFile"
mysql <<-EOF
DELETE FROM $logSchema.$logTable
WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())
EOF

View File

@ -62,10 +62,18 @@
command: mount -a
when: fstab.changed
- name: Set MariaDB Cron to /etc/cron.d
- name: Configure MariaDB check memory CRON
template:
src: templates/cron_mariadb
dest: /etc/cron.d/vn
src: check-memory.cron
dest: /etc/cron.d/vn-check-memory
owner: root
group: root
mode: u=rw,g=r,o=r
- name: Configure MariaDB scheduler log CRON
copy:
src: scheduler-log.cron
dest: /etc/cron.d/vn-scheduler-log
owner: root
group: root
mode: u=rw,g=r,o=r
@ -197,7 +205,6 @@
path: /var/lib/mysql/
register: mysql_dir
- when: mysql_dir.stat.exists
block:

View File

@ -1,4 +1,3 @@
MAILTO="{{ sysadmin_mail }}"
*/15 * * * * root /root/scripts/check-memory.sh
*/30 * * * * root /root/scripts/scheduler-log.sh