01-18-2005
Parse for errors shell script
All,
I have a shell script which parses the /var/adm/messages file for errors every 15 minutes as a cron job. The script runs at 01, 16, 31, and 46 minutes every hour. The problem is if the error is encountered any time during the beginning of hour I can get paged three times. I would like to find a way to take the current time and subtract the previous time interval (for example 15 minutes) to read only the messages from this time. This way I would only get paged once. Any suggestions to improve the shell script are appreciated.
Thanks,
Mike
Here is the shell script:
#!/bin/sh
#
# Script to read the /var/adm/messages file for errors.
#
# Environment variables
#
LOGFILE=/sysadmin/varmessages.log
OLDLOGFILE=/sysadmin/varmessages.old.log
MESSAGES=/var/adm/messages
MESSAGEALERT=/sysadmin/varmessagealart.txt
CURMONTH=`date +%m`
CURDAY=`date +%d`
CURYEAR=`date +%Y`
# Convert numeric month to character for today's month.
case $CURMONTH in
1|01) CURMONTH=Jan;;
2|02) CURMONTH=Feb;;
3|03) CURMONTH=Mar;;
4|04) CURMONTH=Apr;;
5|05) CURMONTH=May;;
6|06) CURMONTH=Jun;;
7|07) CURMONTH=Jul;;
8|08) CURMONTH=Aug;;
9|09) CURMONTH=Sep;;
10) CURMONTH=Oct;;
11) CURMONTH=Nov;;
12) CURMONTH=Dec;;
esac
# Remove leading zeros from today's day.
case $CURDAY in
01) CURDAY=1;;
02) CURDAY=2;;
03) CURDAY=3;;
04) CURDAY=4;;
05) CURDAY=5;;
06) CURDAY=6;;
07) CURDAY=7;;
08) CURDAY=8;;
09) CURDAY=9;;
esac
# Get current hour for today.
#
THOUR=`date +%T | awk -F: '{ print $1 }'`
# If the hour is less than 10, add a leading zero to hour.
HOUR=$THOUR
case $i in
0) HOUR=00;;
1) HOUR=01;;
2) HOUR=02;;
3) HOUR=03;;
4) HOUR=04;;
5) HOUR=05;;
6) HOUR=06;;
7) HOUR=07;;
8) HOUR=08;;
9) HOUR=09;;
esac
# Remove log file if the file exists
if [ -f $LOGFILE ] ; then
rm $LOGFILE
fi
# Remove old log file if the file exists
if [ -f $OLDLOGFILE ] ; then
rm $OLDLOGFILE
fi
# Loop through lines in the MESSAGEALERT text file and grep for
# line in the MESSAGES text file. If the line is found at least
# once and it has not been reported, then place in log file.
while LINE="`line`"
do
ALERT=`grep -c "$LINE" $MESSAGES`
# grep for alerts in the current hour
if [ $CURDAY -lt 10 ] ; then
cat $MESSAGES | grep "$CURMONTH $CURDAY $HOUR" | grep "$LINE" >> $LOGFILE
else
cat $MESSAGES | grep "$CURMONTH $CURDAY $HOUR" | grep "$LINE" >> $LOGFILE
fi
done < $MESSAGEALERT
# Loop through lines in the MESSAGEALERT text file and grep for
# line in the MESSAGES text file. If the line is found at least
# once and it has not been reported, then place in log file.
while LINE="`line`"
do
ALERT=`grep -c "$LINE" $LOGFILE`
# grep for alerts in the current hour and send page from /etc/aliases alias
if [ $ALERT -ge 1 ] ; then
echo "$LINE" >> $OLDLOGFILE
echo "$LINE" | mailx -s "`uname -n` system messages alert" pager
fi
done < $MESSAGEALERT
exit 0
The /sysadmin/varmessagealart.txt has the following text to parse for:
Media Error
Memory Error
Hardware Error
SCSI bus reset
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi
I am using shell script and i need to parse the data between parenthesis.How do i do it using shell script.
Ex: section(name).
I want to extract name from the above string using shell script. (4 Replies)
Discussion started by: julie_s
4 Replies
2. UNIX for Advanced & Expert Users
hi all,
i have a html file something similar to this.
<tr class="evenrow">
<td class="data">added</td><td class="data">xyz@abc.com</td>
<td class="data">filename.sql</td><td class="modifications-data">08/25/2009 07:58:40</td><td class="data">Added TK prof script</td>
</tr>
<tr... (1 Reply)
Discussion started by: sais
1 Replies
3. Shell Programming and Scripting
hey watsup guys
i am new in the shell script world. so i need help fom you guys, i have written these two codes and they both give the same errors( expr : syntax error).
Code 1 :
#! /bin/sh
# count1 appends an increment to a file 200 times
# note that a file called numbers must be... (5 Replies)
Discussion started by: surubi_abada
5 Replies
4. Shell Programming and Scripting
Hello, I was hoping for a second pair of eyes or a little bit of help figuring out what my error is in a script. I did some searching in the forums and didn't find anything so please forgive me if it a similar problem has been discussed before.
My script accepts normal user arguments; however,... (2 Replies)
Discussion started by: Jackinthemox
2 Replies
5. Shell Programming and Scripting
Raw Results:
results|192.168.2|192.168.2.1|general/udp|10287|Security Note|For your information, here is the traceroute from 192.168.2.24 to 192.168.2.1 : \n192.168.2.24\n192.168.2.1\n\n
results|192.168.2|192.168.2.1|ssh (22/tcp)|22964|Security Note|An SSH server is running on this port.\n... (2 Replies)
Discussion started by: jroberson
2 Replies
6. Shell Programming and Scripting
Hi,
I have a bash script which calls a few "make". I would like to know whether the makefile failed with any errors. How do I do that in the script?
Thanks,
S (2 Replies)
Discussion started by: suryaemlinux
2 Replies
7. Emergency UNIX and Linux Support
I want to delete archivelog files that has been archived and applied from primary database
to standby database. This piece of script is working in Linux server. However, I copy it to
Unix server with tiny modification. It won't work and generate the error message. I have checked
code carefullt... (8 Replies)
Discussion started by: duke0001
8 Replies
8. Shell Programming and Scripting
So I have a script that does a greater than or less than with a variable. My problem is that when I checked the script from the command line I was getting this error..
I am not really familiar with shell scripting. I am trying to get this script to work with conky so that it will do the... (8 Replies)
Discussion started by: krisdeniseriley
8 Replies
9. Shell Programming and Scripting
I have a shell script which select total count from a table and use its value in a if condition like below
connect_string="username/password@tnsname"
tot=`sqlplus -s $connect_string << EOF
set echo off
set feedback off
set head off
select count(*) from test_table;
EOF
`
if
then
echo... (2 Replies)
Discussion started by: vel4ever
2 Replies
10. Shell Programming and Scripting
Hello All,
i am facing difficulty in validating the values,
kindly help me in resolving the issue.
Thanks a lot in advance.
-Chandra
Script:Test.sh
#! /bin/sh
# ***************************************************************************
# Function to display help
function usage()... (1 Reply)
Discussion started by: duddukuri
1 Replies
AT(1) General Commands Manual AT(1)
NAME
at - execute commands at a later time
SYNOPSIS
at [ -c ] [ -s ] [ -m ] time [ day ] [ file ]
DESCRIPTION
At spools away a copy of the named file to be used as input to sh(1) or csh(1). If the -c flag (for (csh(1))) or the -s flag (for (sh(1)))
is specified, then that shell will be used to execute the job; if no shell is specified, the current environment shell is used. If no file
name is specified, at prompts for commands from standard input until a ^D is typed.
If the -m flag is specified, mail will be sent to the user after the job has been run. If errors occur during execution of the job, then a
copy of the error diagnostics will be sent to the user. If no errors occur, then a short message is sent informing the user that no errors
occurred.
The format of the spool file is as follows: A four line header that includes the owner of the job, the name of the job, the shell used to
run the job, and whether mail will be set after the job is executed. The header is followed by a cd command to the current directory and a
umask command to set the modes on any files created by the job. Then at copies all relevant environment variables to the spool file. When
the script is run, it uses the user and group ID of the creator of the spool file.
The time is 1 to 4 digits, with an optional following `A', `P', `N' or `M' for AM, PM, noon or midnight. One and two digit numbers are
taken to be hours, three and four digits to be hours and minutes. If no letters follow the digits, a 24 hour clock time is understood.
The optional day is either (1) a month name followed by a day number, or (2) a day of the week; if the word `week' follows, invocation is
moved seven days further off. Names of months and days may be recognizably truncated. Examples of legitimate commands are
at 8am jan 24
at -c -m 1530 fr week
at -s -m 1200n week
At programs are executed by periodic execution of the command /usr/libexec/atrun from cron(8). The granularity of at depends upon the how
often atrun is executed.
Error output is lost unless redirected or the -m flag is requested, in which case a copy of the errors is sent to the user via mail(1).
FILES
/usr/spool/at spooling area
/usr/spool/at/yy.ddd.hhhh.* job file
/usr/spool/at/past directory where jobs are executed from
/usr/spool/at/lasttimedone last time atrun was run
/usr/libexec/atrun executor (run by cron(8))
SEE ALSO
atq(1), atrm(1), calendar(1), sleep(1), cron(8)
DIAGNOSTICS
Complains about various syntax errors and times out of range.
BUGS
Due to the granularity of the execution of /usr/libexec/atrun, there may be bugs in scheduling things almost exactly 24 hours into the
future.
If the system crashes, mail is not sent to the user informing them that the job was not completed.
Sometimes old spool files are not removed from the directory /usr/spool/at/past. This is usually due to a system crash, and requires that
they be removed by hand.
4th Berkeley Distribution October 21, 1996 AT(1)