#!/bin/bash # addtimestamp.sh # (C) 2012 Daniel Marschall, ViaThinkSoft # # This script watches a logfile and adds timestamps in front of each line # This is e.g. necessary for MySQL logs in combination with fail2ban, because MySQL does not log timestamps correctly in the general log ... # # Version: 2012-02-02 02:44 if [ $# -lt 2 ] then echo "Syntax: $0 []"; exit 2; fi ERRORLOG="$1"; shift; OUTLOG="$1"; shift; FILTERREGEX="$1"; shift; ( tail -n 1 -F "$ERRORLOG" | while read inputline do OUTPUT=0; if [ "$FILTERREGEX" = "" ] then OUTPUT=1; else echo "$inputline" | grep -e "$FILTERREGEX" 2>&1 > /dev/null; if [ $? -eq 0 ] then OUTPUT=1; else OUTPUT=0; fi fi if [ $OUTPUT -eq 1 ] then TIMESTAMP=$( date "+%Y-%m-%d %H:%M:%S %z" ); echo "[$TIMESTAMP] $inputline"; fi done ) 2>"/dev/null" >> "$OUTLOG"; exit 0;