1837121 Members
2550 Online
110112 Solutions
New Discussion

Re: Scripting

 
blal
Frequent Advisor

Scripting

Hi,

I need to extract all entries logged for the last 15 Minutes from syslog starting from current time .How can I do it in a shell script.

Thanks in advance
Regards
Live and let live.
4 REPLIES 4
Rodney Hills
Honored Contributor

Re: Scripting

Usually a sysadmin type would do a

tail -20 /var/adm/syslog/syslog

to see the last entries.

If you are planning on feeding this into a program, then that me stickier...

HTH

-- Rod Hills
There be dragons...
blal
Frequent Advisor

Re: Scripting

Hi Rodney,

My requirement is to get entries which are logged in last 15 Minutes.

Regards


Live and let live.
Sundar_7
Honored Contributor

Re: Scripting

Here is something that will do the job.

Note: The script requires perl binary in the system.

typeset -i CUR=0
CUR=$(/usr/bin/perl -e '{print (time())}')

(( FIFTEEN_MIN_BEFORE = CUR - (15*60) ))

DATE_TO_GREP=$(echo "0D${FIFTEEN_MIN_BEFORE}=Y" | adb | awk '{print $2,$3,$4}' | awk -F: '{print $1":"$2}')
grep "^${DATE_TO_GREP}" /var/adm/syslog/syslog.log

Learn What to do ,How to do and more importantly When to do ?
Muthukumar_5
Honored Contributor

Re: Scripting

We can do this with shell script as,
--------------------------------
#!/usr/bin/sh
set -x

# Execute with minute < ./script 15>
IN=${1:-15}
SYSLOG=/var/adm/syslog/syslog.log

if [[ ! -f $SYSLOG ]]
then
echo "No $SYSLOG file there"
exit 1
fi

Day=$(date +'%b %d')
hour=$(date +"%H")
min=$(date +"%M")

now=$(echo $hour:$(echo $min | cut -c 1))
if [[ $min -lt $IN ]]
then
let hh=$hour-1
let mm=60+$min-$IN
before=$(echo $hh:$mm)
else
let mm=$min-$IN
let hh=$hour
before=$(echo $hh:$(echo $mm | cut -c 1))
fi

grep "$Day" $SYSLOG | grep -E "${before}[0-9]*|${now}[0-9]*"
-----------------------------

I have tried to collect the informations on syslog between time frame now and before 15 minutes there ...
Easy to suggest when don't know about the problem!