Merge pull request 'refs #8140 fix cron and scheduler-log script' (!67) from 8140-fixSchedulerLog into main

Reviewed-on: #67
Reviewed-by: Xavi Lleó <xavi@verdnatura.es>
This commit is contained in:
Xavi Lleó 2025-02-07 13:38:35 +00:00
commit 9ee1a95f52
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" logFile="/var/log/mysql/error.log"
dateFile="/tmp/mysql_scheduler_log-lastdate" 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 purgeDays=30
quote() { quote() {
@ -17,33 +19,55 @@ if [ "$?" -ne "0" ]; then
exit exit
fi fi
if [ -f "$dateFile" ]; then tableExists=$(mysql -Ns -e "SHOW TABLES FROM $logSchema LIKE '$logTable'")
fromDate=$(cat "$dateFile")
else if [ -z "$tableExists" ]; then
fromDate=0 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 fi
lastDate=$(tail -n1 "$logFile" | awk '{print $1" "$2}') if [ -f "$dateFile" ]; then
toDate=$(date +%s -d "$lastDate") 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($1, date, "-");
split($2, time, ":"); split($2, time, ":");
timestamp = mktime(date[1]" "date[2]" "date[3]" "time[1]" "time[2]" "time[3]) 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; printf $1" "$2" "$7;
for (i=8; i<=NF; i++) printf FS $i ; for (i=8; i<=NF; i++) printf FS $i ;
print ""; print "";
} }
}' "$logFile" | \ }' | \
\
while read line; do while read line; do
date="$(echo "$line" | cut -d' ' -f1,2)" date="$(echo "$line" | cut -d' ' -f1,2)"
event="$(echo "$line" | cut -d' ' -f3)" event="$(echo "$line" | cut -d' ' -f3)"
error="$(echo "$line" | cut -d' ' -f4-)" 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 done
echo -n "$toDate" > "$dateFile" echo "$toDate" > "$dateFile"
echo "DELETE FROM $logTable WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())" | mysql mysql <<-EOF
DELETE FROM $logSchema.$logTable
WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())
EOF

View File

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

View File

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