Sponsored Content
Homework and Emergencies Homework & Coursework Questions monitor log entries and send e-mail Post 302426268 by vin8465 on Tuesday 1st of June 2010 09:08:29 AM
Old 06-01-2010
Bug

Quote:
Originally Posted by Scrutinizer
Hi, since $EMAILMESSAGE is a variable and not a file you can not use input redirection like that. Also it is typically good practice to quote variable references..
You could try:
Code:
echo "$EMAILMESSAGE" | /bin/mailx -s "$SUBJECT" "$RECIPIENT"

or better yet:
Code:
printf "%s\n" "$EMAILMESSAGE" | /bin/mailx -s "$SUBJECT" "$RECIPIENT"

Which would protect against awkward characters in the email message body.

Is $RECIPIENT set to a valid email address?
Hey Scrutinizer,
You seem to live up to your display id.I have made the changes accordingly. And yes the e-mail id is set to a valid id. I recieved the mail with all the the messages that contain error or warning.
I will put up the updated code separately.
SmilieSmilieSmilieSmilieSmilie
Could you also give me a hint on how i could do the tasks of script. The main task is to scan the log file for new entries. I am not able to figure out how to scan the log file for the first time and then when the script is run the 2nd time, it needs to scan only new entries and ignore previous entries.
My approach was to create a tmp_log file of the log that is being scanned and then read from there. But i then realized if a different log file is scanned every time then my approach would fail.
What is your opinion? Could you please give a logic and possible commands to use.
I might consider using wc for counting line numbers and then compare the logfile given asn argument with tmp_log that the script creates. then find the difference of the lines and print that in a second tmp_log file and do operations on that.

Again, Thanks for you valuable feebacks.

---------- Post updated at 11:08 PM ---------- Previous update was at 10:52 PM ----------

New improvements in the script are:
This script now searches for string mentioned searched using egrep.
Stores output of egrep in a variable and is passed to the email message variable.
It successfully sends an email with the entries of the specified log file that matched the string pattern in egrep
The code is below. I have tried to highlight all the recent updates that were made:
Code:
#!/bin/sh -x
#
#
#
PRINTF=/usr/bin/printf
EGREP=/usr/bin/egrep
TAIL=/usr/bin/tail
COPY=/usr/bin/cp
CAT=/usr/bin/cat
LOG_FILE="$1"
LOG_PATH="/var/log/$1"
#To check if log file to check has been given as argument when running the script
if [ $# -ne 0 ] ; then
           $PRINTF "%s: Is the name of the log file you will be scanning. \n" $1
           $PRINTF $LOG_FILE"\n"
           $PRINTF $LOG_PATH"\n"

                if [ -f $LOG_PATH ];       #if logfile exits
                then
                        echo "File $LOG_FILE exists \n"
                        $COPY $LOG_PATH ./  #create copy of original log file in current directory
                        $CAT -n $LOG_FILE > ./tmp_log #Store a copy of original log file in a temporary log file with number index

                        # script to check for string to search
                        SEARCH=`$EGREP -i 'error|warning' $1` #search for strings error or warning in any form
#                       SCAN_LOG=`$TAIL $1|[${SEARCH}]`  #scanning logfile for string pattern

                        # script to send simple email
                        SUBJECT="Error/Warning messages in logfile" # email subject
                        RECIPIENT="validmailid@domain.extension" # Email To ?
#                       EMAILMESSAGE="$SCAN_LOG" # Email text/message
                        EMAILMESSAGE="$SEARCH" # Email text/message
                        echo "mail not sent yet"
                        $PRINTF "%s\n" "$EMAILMESSAGE" | /bin/mailx -s "$SUBJECT" "$RECIPIENT"  #send email with subject message to specified mail id
                                echo "mail sent"
                else
                        echo "File $LOG_FILE does not exists \n"
                fi

        else
           $PRINTF "%s: Need the name of the log that needs to be checked\n" $0
#       EXIT (1)
fi

The output of the script now is as below:
Code:
> sh logmon1 log.04
log.04: Is the name of the log file you will be scanning.
log.04
/../../../../log.04
File log.04 exists

mail not sent yet
mail sent
>


Last edited by vin8465; 06-01-2010 at 10:13 AM.. Reason: Added output of script
 

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

monitor log entries and send e-mail

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! I am stuck and worried. My assignment was due a day ago and I was too busy completing other assignments due during the same time. I worry that not completing this assignment... (1 Reply)
Discussion started by: vin8465
1 Replies

2. UNIX for Advanced & Expert Users

need to configure mail setting to send mail to outlook mail server

i have sun machines having solaris 9 & 10 OS . Now i need to send mail from the machines to my outlook account . I have the ip adress of OUTLOOK mail server. Now what are the setting i need to do in solaris machines so that i can use mailx or sendmail. actually i am trying to automate the high... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

3. Shell Programming and Scripting

Monitor log file for a Error and generate the e-mail.

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (1 Reply)
Discussion started by: hishanms
1 Replies

4. Shell Programming and Scripting

How to monitor log file for a Error and generate the e-mail ( Please help)

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (7 Replies)
Discussion started by: hishanms
7 Replies

5. Shell Programming and Scripting

Looking for shell script to monitor CPU utilization and send mail once exceed 75%

Dear Group, I'm look for shell script to Monitor CPU usage and send mail once it exceed 75% I'm running Suse10.4. (3 Replies)
Discussion started by: clfever
3 Replies

6. Shell Programming and Scripting

Basic script for monitor send mail service

Hi All Need help Can any one share a basic script that is used for monitor sendmail service whether online, offline.etc in solaris Thanks in advance Zimmy (5 Replies)
Discussion started by: zimmyyash
5 Replies

7. Shell Programming and Scripting

Monitor file if match then send mail

Hi I want to monitor a file even if the file rotate. When a text occurs I want to send a mail. Something like this but it's not working correctly: tail -F mylog.log | grep 'MatchMe' | while read line do echo $(date +"%Y-%m-%d %H:%M:%S") MatchMe occurs | mail -s "MatchMe"... (1 Reply)
Discussion started by: chitech
1 Replies

8. Shell Programming and Scripting

Search the string in the active log and send mail

Hello, I wanted to search specific string in the acitve log file and send an email if the search string found in the log. Log file is written by application all the time. So, script has to search if any new log entry has the specific string for example " sample exception" and send an email. (1 Reply)
Discussion started by: balareddy
1 Replies

9. Shell Programming and Scripting

Monitor a file and send mail

I want to monitor the maillog file in real time and send a mail when a certain grep condition is met. Every time the grep condition is met a mail will be sent. I wish to ignore all grep conditions 30 mins after each mail and thereafter continue monitoring. For example: Condition is met, mail... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

10. UNIX for Advanced & Expert Users

Client was not authenticated to send anonymous mail during MAIL FROM (in reply to MAIL FROM comm

I am having trouble getting mail to work on a red hat server. At first I was getting this message. Diagnostic-Code: X-Postfix; delivery temporarily suspended: connect to :25: Connection refused Then added the port to my firewall. Then I temporarily turned off selinux. I then copied this file... (1 Reply)
Discussion started by: cokedude
1 Replies
All times are GMT -4. The time now is 03:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy