Suppose, there are 4 different types of patterns(errors) in a log each may occur time to time. Eg: "timeout exception", "ldap error "," db error "," error four". Can any one place provide me a script about:- how to grep multiple patterns in a log every hour and if the script finds the any pattern then it should send alert to me only once, no duplicate alerts. Please help me. Thank you
#!/bin/bash
while true; do
export ERRORS=`cat YOUR_LOG_FILE | grep -e "(timeout exception)|(ldap error)|(db error)|(error four)"
if [ $ERRORS ]; then
# sendmail or any other kind of "alert" you prefer.
echo $ERRORS | sendmail "your#email.com"
fi
sleep 1h
done
Make a crontab entry that will run once an hour. That entry can call your script:
logfile=/path/to/logfile/application.out
function send_alert {
# Some sendmail or other tool to send your alert using the args
printf "I want to alert about %s" "$*"
}
# Solution only announcing errors without sending them
grep -qE "timeout exception|ldap error|db error|error four" ${logfile} &&
send_alert "grep found something"
# Solution sending number of errorlines
errorlinecount=$(grep -c "timeout exception|ldap error|db error|error four" )
if [ ${errorcount} -gt 0 ]; then
send_alert "grep found ${errorcount} disturbing lines"
fi
Related
I have this munin plugin:
#!/bin/sh
if [ "$1" = "autoconf" ]; then
# Test for requirements, and return "yes" if met, "no" if not met
echo "yes"
exit 0
fi
if [ "$1" = "config" ]; then
echo "graph_title xrdp process count"
echo "graph_vlabel count"
echo "graph_category processes"
echo "graph_info This graph shows the count of xrdp processes"
echo "my_proc_count_dummy_var.label count"
exit 0
fi
echo "my_proc_count_dummy_var.value $(pgrep -f -a xrdp | grep -c -e 'xrdp$')"
The problem is that it doesn't show output as integer , it's a counter but I have values that are not correct on the graphs and makes not sense.
For example:
I read some faq about integer/float values on munin and looked other solution based on other plugins, for example the psu_ plugin where the output is correct but I was not able to resolve the problem.
Maybe I need to delete (or in some way regenerate) the old data?
Thanks for the help
I want to log data of asterisk command line. But the criteria is I want log data for calls separately, i.e. I want to log data for each call in separate file.
Is there a way to do that?
In case there is no inbuild feature in asterisk to do this, here is a bash solution:
#!/bin/bash
echo "0" >/tmp/numberoflines
IFS=''
pathToLogFile = /path/to/log/file
while [ 1 ]
do
NUMBER=$(cat /tmp/numberoflines)
LINECOUNT=$(wc -l < $pathToLogFile)
DIFFERENCE=$(($LINECOUNT-$NUMBER))
if [ $DIFFERENCE != 0 ]; then
lines=($(tail -n $DIFFERENCE $pathToLogFile))
for line in $lines; do
callID = `expr "$line" : 'CALLID_REGEX (see below)'`
$(echo "$line" >> /path/to/log/directory/$callID)
done
fi
sleep 5;
echo "$LINECOUNT" >/tmp/numberoflines
done
untested
it should be used to get ab idea to solve this problem.
the regular expression: normaly: /\[(C\d{8})\]/. sadly I don't know the syntax in bash. I'm sorry. you have to convert it by yourself into bash-syntax.
The idea is: remember the last line in the logfile that was processed by the bash script. check the line count of the log file. if there are more lines then the remembered line: walk through the new lines and extract the call id at the beginning of each line (format: C******** (* are numbers). in words: a C followed by a number with 8 digits). now append the whole line at the end of a log file. the name of the file is the extracted callid.
EDIT Information about the call id (don't mistake it with the caller id): https://wiki.asterisk.org/wiki/display/AST/Unique+Call-ID+Logging
I try this
#!/bin/sh
until who | grep -E "$*"
do
sleep 60
done
echo "$* logged in"
but it works only with one user written in arguments. I need this shell program to work with multiple users that are written as arguments.
Iterate over $# and check the output of who for every given name:
#!/bin/bash
who="$(who)"; # Save the output of who
for user in "$#"; do # Iterate over $#
if echo "$who" | grep -q "$user"; then # Check if $user is in $who
echo "$user logged in";
fi;
done;
What were you trying to achieve with that loop? Do you want the script to wait until a user logs in?
The grep needs some work, because it does not limit the match to the first word on each line. Alternatively, filter the result from who. Here is a revised script:
#!/bin/sh
DONE=no
while [ $DONE = no ]
do
who="$(who | sed -e 's/[[:space:]].*//' |sort -u)"
for user in "$#"
do
for WHO in $who
do
if [ $WHO = $user ]
then
echo "$user logged in"
DONE=yes
break
fi
done
done
[ $DONE = no ] && sleep 60
done
As you can see, the grep is unnecessary.
Finally, change it to plain /bin/sh, because there is no need for a specific shell in this example.
I have below script but it sends email without any attachment. What is wrong?
sendmail /A "/home/dd/data/list.txt" "dd#gmail.com" -t << EOF
To:dd#gmail.com
Subject:List of ids
This is the message
[new line]
Everything else works as expected. Thanks.
The here document is not completed.
sendmail /A "/home/dd/data/list.txt" "dd#gmail.com" -t << -EOF
To:dd#gmail.com
Subject:List of ids
This is the message
EOF
try -EOF so the trailing EOF does not need to be in the leftmost column.
Try this, I just tested it:
/usr/sbin/sendmail -tv me#myplace.com <<%%
Subject: test of sendmail
This is the note
$(uuencode attachment.file newname.txt)
%%
I did not have time to get back to this. email address goes on line 1
Try the script below:
#!/bin/sh
# send/include list.txt file after "here document" (email headers + start of email body)
cat - "/home/dd/data/list.txt" | /usr/sbin/sendmail -i -- "dd#gmail.com" <<END
To: dd#gmail.com
Subject: List of ids
This is the message
END
I'm very new to shell scripting and i've been struggling with the following shell script. I'm posting the script and the commands i used below for your consideration please help me with the mistake i made.
#
#
#
DBG=0
RLS=0
ALL=0
CLN=0
print_help_uu()
{
echo "Usage: $0 -D -R -A -C ";
echo "Where -C clean the debug project builds";
echo " -D to build in DEBUG config";
echo " -R to build in RELEASE config";
echo " -A to build in both configs";
return
}
#
# Main procedure start here
#
# Check for sufficent args
#
if [ $# -eq 0 ] ; then
print_help_uu
exit 1
fi
#
# Function to clean the project
#
clean()
{
if ["$DBG"="1"]; then
echo "Cleaning debug"
if ["$RLS"="1"]; then
echo "cleaning release + debug"
else
echo "This is bad"
fi
fi
if ["$RLS"="1"]; then
echo "Cleaning release "
fi
return
}
while getopts "DRAC" opt
do
case "$opt" in
D) DBG=1;;
R) RLS=1;;
A) DBG=1;RLS=1;;
C) CLN=1;;
\?) print_help_uu; exit 1;;
esac
clean
done
I'm posting the commands i used to run it and the errors i got when using those commands.
----------
./BuildProject.sh -D
./BuildProject.sh: line 36: [1=1]: command not found
./BuildProject.sh: line 46: [0=1]: command not found
-----------
sh BuildProject.sh -D
BuildProject.sh: 63: [1=1]: not found
BuildProject.sh: 63: [0=1]: not found
-----------
sh ./BuildProject.sh -D
./BuildProject.sh: 63: [1=1]: not found
./BuildProject.sh: 63: [0=1]: not found
I tried to solve it in soo many ways and googled a lot before posting here. But all my trials went in vain. Please tell me where i'm doing the mistake since i'm new to shell scripting.
Thanks in Advance.
[ is a command, but you are trying to invoke the command [1=1]. Add some whitespace:
if [ "$DBG" = "1" ]; then
Try to change ["$DBG"="1"] (and similar if statements) into this: [ "$DBG" = "1" ]
i.e. add some space.
i think it's a "SPACE" problem : try
if [ "$DBG" = "1" ]; then
instead of
if ["$DBG"="1"]; then
It's a space issue indeed.
VAR=VALUE
is only for variable declaration in shell, while
VAR = VALUE
is only for variable testing. It's tricky, you just have to get used to it.
It Worked after adding some extra spaces into it. Thank you all. Is it a Scripting rule to put those spaces in between the variables?? I think i ignored that rule. Thanks for your time.