Last active
August 29, 2015 14:21
-
-
Save drldcsta/a770abd83cbdf432a00f to your computer and use it in GitHub Desktop.
log parsing script. Only interesting in that it's a good example of using lock files to make sure there is never more than one copy of a script running on cron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
###################################################### | |
# Program: logGen.sh | |
# Date Created: 22 Aug 2012 | |
# Description: parses the manager log in real time into daily error files | |
# Date Updated: 27 Nov 2013 | |
# |_moved all data parsing logic to awk instead of shell | |
# |_Need to analyze memory/CPU usage and consider changing | |
# |_restart frequency in cron | |
# Developer: Darrell DeCosta (Senior Support Engineer) | |
###################################################### | |
#Prefix for pid file | |
pidPrefix="logGen" | |
#output direcory | |
outDir="/opt/company.TV/logs/allerrors/" | |
#Simple function to see if running on primary | |
checkPrime () | |
{ | |
if /sbin/ifconfig eth0:0|/bin/grep -wq inet;then isPrime=1;else isPrime=0;fi | |
} | |
#function to kill previous instances of this script | |
killScript () | |
{ | |
/usr/bin/find /var/run -name "${pidPrefix}.*.pid" |while read pidFile;do | |
if [[ "${pidFile}" != "/var/run/${pidPrefix}.${$}.pid" ]];then | |
/bin/kill -- -$(/bin/cat ${pidFile}) | |
/bin/rm ${pidFile} | |
fi | |
done | |
} | |
#Check to see if primary | |
#If so, kill any previous instance and start log parsing | |
#If not, just kill leftover running processes | |
checkPrime | |
if [[ "${isPrime}" -eq 1 ]];then | |
echo "$$" > /var/run/${pidPrefix}.$$.pid | |
killScript | |
tail -F -n0 /opt/company.TV/logs/manager_proxy.log|awk -v dir=$outDir '{OFS="|"} | |
{ | |
if ($3 == "E") | |
{ | |
file="allerrors."$1".log" | |
gsub("/",".",file) | |
if ($9 ~ /@/) | |
print $1,$2,$8,$9,substr($0, index($0,$10)) >> dir file | |
else {if ($8 !~ /@/) | |
print $1,$2,$7,"NULL",substr($0, index($0,$8)) >> dir file | |
} | |
fflush(stdout); | |
} | |
}' | |
else | |
killScript | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment