diff --git a/roles/db/files/scheduler-log.cron b/roles/db/files/scheduler-log.cron new file mode 100644 index 0000000..c11daec --- /dev/null +++ b/roles/db/files/scheduler-log.cron @@ -0,0 +1 @@ +*/30 * * * * root /root/scripts/scheduler-log.sh diff --git a/roles/db/files/scripts/scheduler-log.sh b/roles/db/files/scripts/scheduler-log.sh index 9cb0b37..17ac94f 100755 --- a/roles/db/files/scripts/scheduler-log.sh +++ b/roles/db/files/scripts/scheduler-log.sh @@ -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 diff --git a/roles/db/tasks/mariadb.yml b/roles/db/tasks/mariadb.yml index f7cce5d..9182a91 100644 --- a/roles/db/tasks/mariadb.yml +++ b/roles/db/tasks/mariadb.yml @@ -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: diff --git a/roles/db/templates/cron_mariadb b/roles/db/templates/check-memory.cron similarity index 60% rename from roles/db/templates/cron_mariadb rename to roles/db/templates/check-memory.cron index bc281bd..a6cfa3e 100644 --- a/roles/db/templates/cron_mariadb +++ b/roles/db/templates/check-memory.cron @@ -1,4 +1,3 @@ MAILTO="{{ sysadmin_mail }}" */15 * * * * root /root/scripts/check-memory.sh -*/30 * * * * root /root/scripts/scheduler-log.sh