Thursday, March 3, 2011

Monitor log file and send email alert when error occurs

I ran into a situation where I had an error occuring periodically, and I needed an easy way to be notified whenever the error occured, so that I could make some configuration changes to my server.  This script is fairly simple and should be able to be modified to fit many needs.  It basically looks at the tail of any given log file, and searches for a specific error code, or string.  Any time this error code occurs an email alert is sent out, and then the script sleeps for a given period (as to not flood my email box).


######################################################################
#!/bin/bash


errorString='SC-001'
recipient="xxsyntax@gmail.com"
sleepTime=10
logFile='/var/log/program/log'


x=1
while [ $x -gt 0 ] 
do
   checkLog=`tail $logFile | grep $errorString`
   if [[ $checkLog == *$errorString* ]]
      then
      echo "Error found, sending an email."
      echo "$checkLog" | mail -s "Alert:  $errorString" $recipient
      sleep $(( $sleepTime * 60 ))
   fi
done

######################################################################

Wednesday, March 2, 2011

Use special characters as variables in bash

I have often run into problems where I needed to use a special character, such as a tab with grep in bash, and was unable to pass the tab as "\t".  I had to define a variable as the tab character using the octal value of the tabcharacter.

For instance, to grep for "foo(tab)bar" I had to do the following:
tabChar=`echo -e '\011'`
grep "foo${tabChar}bar" < input
Here is a list of the octal values of a few special characters that may be handy.



OCTAL ESC'D CTRL DESCRIPTION
007 \a Ctrl-G Alert/Beep
010 \b Ctrl-H Backspace
011 \t Ctrl-I Horizontal Tab
012 \n Ctrl-J Newline (line feed)
013 \v Ctrl-K Vertical Tab
014 \f Ctrl-L Form Feed (clear terminal)
015 \r Ctrl-M Carriage Return
033 \e Ctrl-[ Escape
047 \' Single Quote
134 \\ Backsplash


I hope this list helps.

Friday, February 25, 2011

Compile list of IPs & PTR Records from list of IP Addresses

Simple bash script to compile a list of IP Addresses and corresponding PTR Records from a list of IP Addresses.  Make sure to give this file executable permissions (chmod +x filename).  This script will read the input file, use dig to lookup ptr records, and will output a list of the ptr records and ip address.

#!/bin/bash
inFile="input_list"
outFile="output_list"
ht=`echo -e '\011'`   # tab character
for ip in `cat $inFile`
   do
   in_addr=`echo $ip | awk -F'.' -vOSF='.' '{ print $4"."$3"."$2"."$1".in-addr.arpa" }'`   ptr=`dig PTR $in_addr | grep "IN${ht}PTR" | grep -v ';' | awk -F"$ht" -vOSF="$ht" '{ print $3 }' | sed 's/\(.*\)./\1/'`
   echo "$ptr  $ip" >> $outFile
done

sort list of ip addresses

Simple bash command to sort a list of IP Addresses, sorted ascending, by each octet.
# sorts file "sloppy_ip_list" into new file "sorted_ip_list"
sort -u -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 < sloppy_ip_list > sorted_ip_list


List all ip addresses on linux box, sorting in ascending order.
ifconfig | grep inet | cut -d ':' -f 2 | cut -d ' ' -f 1 | sort -u -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4