1. Remove all logs of manager& admin
- Remove 90 days old logs
- Backup logs greater that 35MB
#!/bin/bash
rm -rf /app/tomcat-6.0.18/logs/*manager* /app/tomcat-6.0.18/logs/*admin*
find /cda1-logs-backups -name ‘*.gz’ -mtime +90 -exec rm {} \;
echo “Initiating logrotate `date +%F-%A`” >> /app/tomcat-6.0.18/logs/diskutil.log
cd /app/tomcat-6.0.18/logs/
siz_catalina=`/bin/ls -l catalina.out | /bin/awk ‘{ print $5 }’`
if [ $siz_catalina -ge “3600000” ];then
echo “catalina.out greater that 35MB, tarring the catalina.out file as catalina.out.`date +%F-%I-%M-%p` and nullifieng the file” >> diskutil.log
tar -cvzf /cda1-logs-backups/catalina.out.`date +%F-%I-%M-%p`.tar.gz catalina.out
if [ -s /cda1-logs-backups/catalina.out.`date +%F-%I-%M-%p`.tar.gz ]; then
`:>catalina.out`
fi
else
echo “Catalina Log file less than 35 MB at `date +%F-%I-%M-%p`” >> diskutil.log
fi
- Backup one day old logs
if [ -s catalina.`date +%F -d “-1 day”`*.log ]
then
tar -zcvf /cda1-logs-backups/catalina.`date +%F -d “-1 day”`*.log.tar.gz catalina.`date +%F -d “-1 day”`*.log
rm -f catalina.`date +%F -d “-1 day”`.log
echo ” Backup of catalina.`date +%F -d “-1 day”`*.log taken and removed ” >> logrotate
else
echo “catalina.`date +%F -d “-1 day”`*.log doesnt exist ” >> logrotate
fi
————————————————————————————————————
#– Removing blank localhost_access files
30 1 * * * find /usr/local/apache-tomcat-6.0.18/logs -name “localhost_access*” -type f -size 0c | xargs rm
30 1 * * * find /usr/local/apache-tomcat-6.0.18/logs -name “manager*” -type f -size 0c | xargs rm
#– Removing blank localhost_access files
30 1 * * * find /usr/local/apache-tomcat-6.0.18/logs -name “localhost_access*” -type f -size 0c | xargs rm
30 1 * * * find /usr/local/apache-tomcat-6.0.18/logs -name “manager*” -type f -size 0c | xargs rm
————————————————————————————————————
Count Apache & tomcat connections
# crontab -e
*/1 * * * * /home/rack/countdbconnections.sh >> /home/rack/countdbconnection
# vi countdbconnections.sh
#!/bin/bash
apache_con=`ps -ef | grep apache | wc -l`
tomcat_total_con=`netstat -an | grep :7001 | wc -l`
tomcat_est=`netstat -an | grep :7001 | grep ESTABLISHED | wc -l`
tomcat_wait=`netstat -an | grep :7001 | grep TIME_WAIT | wc -l`
echo “$(date): apache_conn = $apache_con, tomcat_established_conn = $tomcat_est, tomcat_time_wait = $tomcat_wait”
# less countdbconnection
Wed Jul 18 12:18:01 BST 2012: apache_conn = 33, tomcat_established_conn = 11, tomcat_time_wait = 809
Wed Jul 18 12:19:02 BST 2012: apache_conn = 38, tomcat_established_conn = 22, tomcat_time_wait = 690
————————————————————————————————————
Monitor Java CPU and System load avg
# crontab -e
*/2 * * * * /root/scripts/monitor_pcpu_cda_per.sh >> /root/scripts/monitor_pcpu_cda_per.out
# vi monitor_pcpu_cda_per.sh
java_cpu=”$(date): Java CPU: $(top -b -n1 | grep java | head -1 | awk {‘print $9’})”
server_load=”Load: $(w | head -1 | awk {‘print $10’})”
echo $java_cpu $server_load
# less monitor_pcpu_cda_per.out
Fri Jul 20 06:58:02 BST 2012: Java CPU: 5.9 Load: 0.11,
Fri Jul 20 07:00:02 BST 2012: Java CPU: 96.0 Load: 0.17,
Fri Jul 20 07:02:01 BST 2012: Java CPU: 0.0 Load: 0.36,
————————————————————————————————————
Restart tomcat if it is down and count open files
# vi /root/scripts/tomcatrestart.sh
ps -ef | grep “/rss/tomcat-6.0.18/” | grep -v grep
if [ $? -eq 0 ]; then
Openfiles=`/usr/sbin/lsof | wc -l`
echo “checked at time `date` and rss is up, Openfiles=$Openfiles” >> /root/scripts/tomcatcheck.out
else
/bin/sh /rss/tomcat-6.0.18/bin/startup.sh
echo “<<<<<< RSS is down, restarted it at `date` >>>>>>>” >> /root/scripts/tomcatcheck.out
mail -s “Restarted RSS @ web2-prod” harora4@sapient.com,vsrivastav4@sapient.com
fi
if [ $Openfiles -ge 10000]; then
echo “OpenFiles @ `hostname` reached its limit of $Openfiles” | mail -s “`hostname`: OpenFiles=$Openfiles” harora4@sapient.com,vsrivastav4@sapient.com
else
cat /dev/null
fi
# less tomcatcheck.out
checked at time Wed Mar 14 15:44:03 GMT 2012 and rss is up, Openfiles=24779
checked at time Wed Mar 14 15:45:03 GMT 2012 and rss is up, Openfiles=21161
————————————————————————————————————
# Script to be used for Removal of Cached Memory
#!/bin/bash
CACHE_MEM=`free -m |awk ‘NR==2’ |awk ‘{ print$7 }’`
LIMIT=2000
if [ “$CACHE_MEM” -gt “$LIMIT” ]
then
`sync; echo 3 > /proc/sys/vm/drop_caches`
mail -s “CACHE_MEM is now cleared and the value was ##### $CACHE_MEM #####” vsrivastav4@sapient.com
else
echo exiting > /dev/null
fi
————————————————————————————————————
Restart Tomcat if connections goes high
curl -v http://262670-web1-prod.visitbritain.com:7001/cs/Satellite/en_EN/ > /root/scripts/connstatus.out
count=$(cat /root/scripts/connstatus.out | wc -l)
if [ $count -gt 200 ]; then
echo “checked at time `date` and rss is up and responding, Openfiles=`/usr/sbin/lsof | wc -l`” >> /root/scripts/check_8Sep
else
netstat -alpn > /root/scripts/connectionstat_`date +%F-%I-%M-%p`
# taking thread dump two times
pgrep -f /rss/tomcat-6.0.18/temp | xargs kill -3
pgrep -f /rss/tomcat-6.0.18/temp | xargs kill -3
# kill tomcat process
pgrep -f /rss/tomcat-6.0.18/temp | xargs kill -9
/bin/sh /rss/tomcat-6.0.18/bin/startup.sh
fi
rm -f /root/scripts/connstatus.out
————————————————————————————————————
Checking Disk Space and send alert
#!/bin/sh
limit=80
df -H | grep ” /” | grep -vE ‘^Filesystem|tmpfs|cdrom|boot’ | awk ‘{ print $4 ” ” $5 }’| while read i;
do
perc=$(echo $i | awk ‘{ print $1}’ | cut -d’%’ -f1)
part=$(echo $i | awk ‘{ print $2 }’ )
if [ $perc -ge $limit ]; then
echo “Running out of space \”$part ($perc%)\” on $(hostname) as on $(date)” | mail -s “Alert: Almost out of disk space $usep” vsrivastav4@sapient.com
fi
done
2nd Method
df -h | grep -e [8-9][0-9]%
if [ `echo $?` -ne 0 ]; then
exit
else
df -h | grep -e [8-9][0-9]% -e Use% | mail -s “Disk space more than 80%” arora4@sapient.com -c vsrivastav4@sapient.com
fi
————————————————————————————————————
Script if 15 minutes load goes >= 6.0.
#!/bin/bash
NOTIFY=”6.0″
FTEXT=’load average:’
# 15 min
F15M=”$(uptime | awk -F “$FTEXT” ‘{ print $2 }’ | cut -d, -f3)”
RESULT=$(echo “$F15M > $NOTIFY” | bc)
# if load >= 6.0 create a file /tmp/file.txt
if [ “$RESULT” == “1” ]; then
echo ‘LOAD ISSUE’>/tmp/file.txt
fi
————————————————————————————————————
Rename files .txt to .new in a directory
#!/bin/bash
echo “Enter Directory”
read dir
for i in `ls $dir/*`
do
mv $i $i.new
done
————————————————————————————————————
Cut the huge log file in half
#!/bin/bash
size=`cat $file|wc -l`
half=$(echo “$size/2″|bc)
tail -$half $file > $loc/file1.txt // for lower half
tail +$half $file > $loc/file2.txt // for upper half
If Condition : http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
AWK
Que:- Why awk?
Ans:- The Awk text-processing programming language and is a useful tool for manipulating text.
Awk recognizes the concepts of “file”, “record”, and “field”.
A file consists of records, which by default are the lines of the file. One line becomes one record.
Awk operates on one record at a time.
A record consists of fields, which by default are separated by any number of spaces or tabs.
Field number 1 is accessed with $1, field 2 with $2, and so forth. $0 refers to the whole record.
- To print 2nd line of a file
# cat file |awk NR==2′{print $0}’
- Print section of file between two regular expressions (inclusive)
# cat file |awk ‘/Iowa/,/Montana/’ # case sensitive
- Print section of file based on line numbers (lines 8-12, inclusive)
# cat file |awk NR==8,NR==12′{print $0}’
- Remove duplicate, consecutive lines (emulates “uniq”)
# cat file |awk ‘a !~ $0; {a=$0}’
- Print only lines of less than 65 characters
# awk ‘length < 64’
- To print no. of blanks lines in a file
# cat file |awk /^$/{++x}END'{print x}’
- Average five grades
john 85 92 78 94 88
andrea 89 90 75 90 86
jasper 84 88 80 92 84
# cat file |awk{total = $2 + $3 + $4 + $5 + $6; avg = total / 5; print $1, avg }
- Print and sort the login names of all users
# awk -F “:” ‘{ print $1 | “sort” }’ /etc/passwd
- Print the first 2 fields, in opposite order, of every line
# awk ‘{print $2, $1}’ file
10.Switch the first 2 fields of every line
# awk ‘{temp = $1; $1 = $2; $2 = temp}’ file
11.Print every line, deleting the second field of that line
# awk ‘{ $2 = “”; print }’
12.Print in reverse order the fields of every line
# awk ‘{for (i=NF; i>0; i–) printf(“%s “,i);printf (“\n”)}’ file 13.Print the sums of the fields of every line # awk ‘{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}’
- Add all fields in all lines and print the sum
awk ‘{for (i=1; i<=NF; i++) s=s+$i}; END{print s}’
- Print every line after replacing each field with its absolute value
# awk ‘{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }’
# awk ‘{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }’
- Print the total number of fields (“words”) in all lines
# awk ‘{ total = total + NF }; END {print total}’ file
17.Print the total number of lines that contain “Beth”
# awk ‘/Beth/{n++}; END {print n+0}’ file
18.Print the largest first field and the line that contains it
# awk ‘$1 > max {max=$1; maxline=$0}; END{ print max, maxline}’
- Print the number of fields in each line, followed by the line
# awk ‘{ print NF “:” $0 } ‘
- Print the last field of each line
# awk ‘{ print $NF }’
- Print the last field of the last line
# awk ‘{ field = $NF }; END{ print field }’
- Print every line with more than 4 fields
# awk ‘NF > 4’
- Count the total number of fields in a file.
awk -F ‘:’ ‘{ total += NF }; END { print total }’ /etc/passwd
- Count number of users who is using /bin/sh shell
awk -F ‘:’ ‘$NF ~ /\/bin\/sh/ { n++ }; END { print n }’ /etc/passwd
- Find the user details who is having the highest USER ID
awk -F ‘:’ ‘$3 > maxuid { maxuid=$3; maxline=$0 }; END { print maxuid, maxline }’ /etc/passwd
- Print the even-numbered lines
awk ‘NR % 2 == 0’ /etc/passwd
- Print every line which has the same USER ID and GROUP ID
awk -F ‘:’ ‘$3==$4’ passwd.txt
- Print user details who has USER ID greater than or equal to 100 and who has to use /bin/sh
awk -F ‘:’ ‘$3>=100 && $NF ~ /\/bin\/sh/’ passwd.txt
- Print user details who doesn’t have the comments in /etc/passwd file
awk -F ‘:’ ‘$5 == “” ‘ passwd.txt
How to print line above/below of a string pattern in a file.
# cat Portin.log
<PORTIN_ACK>
< Receiver>220</Receiver>
< TimeStamp>2011-10-19T10:53:07.225+05:30</TimeStamp>
< PortAccepted>N</PortAccepted>
< SubscriberSequence>
< MSISDN>8985001696</MSISDN>
< ResultCode>RC00006</ResultCode>
< /SubscriberSequence>
< /PORTIN_ACK>
# awk ‘{arr[NR]=$0; if($0 ~ /RC00006/)s=NR} END{print arr[s-1]}’ Porting.log
O/P:-< MSISDN>8985001696</MSISDN>
# awk ‘{arr[NR]=$0; if($0 ~ /RC00006/)s=NR} END{print arr[s-2]}’ Porting.log
O/P:- <SubscriberSequence>
GREP:
How to count no of blank lines in a file?
# grep –c “^$” <filename>
How to remove Empty lines form a given file?
# grep –v “^$” filename > temfilename
What is pattern to search 4 digit word in a file?
# grep “\<[0-9] [0-9] [0-9] [0-9]\>” filename
What is pattern to search the line having only three characters?
# grep “^…$” filename
What is pattern to display lines ending with “$” character in a given file?
# grep “\$$” filename
CUT
Getting a List of User Accounts on the System
# cut -d: -f1 /etc/passwd | sort
How to display 2 and 4 th fileds from a given file if the delimetr is “:”?
# Cut –d”:” –f 2,4 filename
O2Scripts : https://drive.google.com/?tab=wo&authuser=0#folders/0BzYdExDgx78PVHYzSTRBQUpfSGM
IF Conditions
File test operators, Returns true if…-e: file exists
-a: file exists -f: file is a regular file (not a directory or device file) -s: file is not zero size -d: file is a directory -b: file is a block device -c: file is a character device device0=”/dev/sda2″ # / (root directory) if [ -b “$device0” ] then echo “$device0 is a block device.” fi Output # /dev/sda2 is a block device. device1=”/dev/ttyS1″ # PCMCIA modem card. if [ -c “$device1” ] then echo “$device1 is a character device.” fi -p: file is a pipe function show_input_type() { [ -p /dev/fd/0 ] && echo PIPE || echo STDIN } show_input_type “Input” # STDIN echo “Input” | show_input_type # PIPE -h: file is a symbolic link -L: file is a symbolic link -S: file is a socket -t: file (descriptor) is associated with a terminal device This test option used to check whether stdin [ -t 0 ] or stdout [ -t 1 ] in a given terminal. -r: file has read permission (for the user running the test) SLAPD_CONF = ”/usr/local/etc/openldap/slapd.conf” for i in “$SLAPD_CONF” do if [ ! -r $i ] then message “alert” “[ALERT] can’t read $i” exit 1 fi done -w: file has write permission (for the user running the test) -x: file has execute permission (for the user running the test) SLAPD_BIN=”$SLAPD_PATH/libexec/slapd” SLAPCAT_BIN=”$SLAPD_PATH/sbin/slapcat” for i in “$SLAPD_BIN” “$SLAPCAT_BIN” “$SLAPINDEX_BIN” “$SLAPTEST_BIN” “$SLURPD_BIN” “$DB_ARCHIVE_BIN” “$DB_RECOVER_BIN” do if [ ! -x $i ] then message “alert” “[ALERT] can’t execute $i” exit 1 fi done -g: set-group-id (sgid) flag set on file or directory -u: set-user-id (suid) flag set on file -k: sticky bit set -O: you are owner of file -G: group-id of file same as yours -N: file modified since it was last read f1 -nt f2: file f1 is newer than f2 f1 -ot f2: file f1 is older than f2 f1 -ef f2: files f1 and f2 are hard links to the same file !: “not” — reverses the sense of the tests above (returns true if condition absent). Link: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html Are you root (for port < 1024)? if [ $PORT -lt 1024 -a `id -u` -ne 0 ] then message “alert” “[ALERT] only root can launch OpenLDAP on port $PORT” exit 1 fi |
Recent Comments