50 lines
1.2 KiB
Bash
50 lines
1.2 KiB
Bash
|
#!/bin/bash
|
||
|
set -e
|
||
|
|
||
|
logFile="/var/log/mysql/error.log"
|
||
|
dateFile="/tmp/mysql_scheduler_log-lastdate"
|
||
|
logTable="util.eventLog"
|
||
|
purgeDays=30
|
||
|
|
||
|
quote() {
|
||
|
local str=${1//\'/\'\'/}
|
||
|
local str=${str//\\/\\\\}
|
||
|
echo "'$str'"
|
||
|
}
|
||
|
|
||
|
mysql -e "SELECT TRUE" > /dev/null 2>&1
|
||
|
if [ "$?" -ne "0" ]; then
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
if [ -f "$dateFile" ]; then
|
||
|
fromDate=$(cat "$dateFile")
|
||
|
else
|
||
|
fromDate=0
|
||
|
fi
|
||
|
|
||
|
lastDate=$(tail -n1 "$logFile" | awk '{print $1" "$2}')
|
||
|
toDate=$(date +%s -d "$lastDate")
|
||
|
|
||
|
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:") {
|
||
|
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
|
||
|
done
|
||
|
|
||
|
echo -n "$toDate" > "$dateFile"
|
||
|
echo "DELETE FROM $logTable WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())" | mysql
|