Archive

Archive for the ‘RHEL’ Category

Linux bash: get the Apache PID with high CPU consumtion and extract the URL

April 6, 2011 Leave a comment

make sure you have the extended status on and the http://localhost/server-status is accessible

#!/bin/sh
# by dragkh
# Wed Apr 06 12:33:56

# Config
CPU_LIMIT=10
TOP_PROCESSES=10
LOG_FILE=/var/log/high.CPU.apache.URLs.log

top -b -U apache  -n 1 | \
grep -v grep | \
grep apache  | \
sort -rn -k9 | \
head  -${TOP_PROCESSES} | \
awk -v CPU_LIMIT=${CPU_LIMIT} '($9 > CPU_LIMIT){print $0}' | \
while read PID USER      PR  NI  VIRT  RES  SHR S CPU MEM    TIME  COMMAND
do 
  echo -n "$(date) :[$HOSTNAME]: PID[${PID}]:CPU[${CPU}] : Apache => " >> ${LOG_FILE}
  links -dump http://localhost/server-status | grep -v grep | grep "${PID}" >> ${LOG_FILE}
done

Linux bash: check exicting IP in the active vhost configs

April 6, 2011 Leave a comment
ifconfig  | grep "inet addr:" | grep -v grep | awk '{print $2}' | sed 's/^addr://' | sort | uniq | while read IP; do echo -n "checking $IP "; echo $(fgrep $IP /etc/httpd/conf.d/sites/*conf | tr '\n' ' ') ;  done

Linux bash: check the MySQL database disk usage

March 2, 2011 Leave a comment
#!/bin/sh
# get.database.sizes.sh
# by dragkh
# Wed, 02 Mar 2011 17:59:44 +0100
# it will work on linux boxes with working mysql 
ps axu \
| grep datadir \
| grep mysql \
| grep -v grep \
| grep var \
| sed 's/^.*--datadir=//; s/ .*$//'   \
| sort \
| uniq  \
| while read crap
do 
echo ""
sized=$(du -s $crap | awk '{print $1}')
avail=$(df | grep $(dirname $crap) | awk '{print $2}')
echo -e  "$crap\t$sized\t$avail" | awk '{printf ("%s:\tUsed: %6.2fG \tDISK: %6.2fG\t Usage: %6.2f%% \n",$1,$2/1024/1024,$3/1024/1024,($2/$3)*100)}'
find $crap/ -maxdepth  1 -type d ! -type l  ! -path "$crap/"   -printf '"%p"\n'  | xargs  --no-run-if-empty  du -s | sort -nk 1,9 | awk -vavail=$avail '{crapy="";for(i=2; i<=NF; i++) {crapy=crapy" "$i;};printf (" -- %s\tUsed: %6.2fG \tDISK:%6.2fG Usage:%6.2f%%\n",crapy,$1/1024/1024,avail/1024/1024,($1/avail)*100)}'
done

result looks like

root@xxxx:[Wed Mar 02 16:54:59]:
/var/lib/mysql/aaaa/datafiles:               Used:  26.43G   DISK:196.86G       Usage:  13.43% 
 --  /var/lib/mysql/aaaa/datafiles/A        Used:   0.00G   DISK:196.86G        Usage:  0.00%
 --  /var/lib/mysql/aaaa/datafiles/B        Used:   0.00G   DISK:196.86G        Usage:  0.00%
 --  /var/lib/mysql/aaaa/datafiles/mysql  Used:   0.00G   DISK:196.86G        Usage:  0.00%
 --  /var/lib/mysql/aaaa/datafiles/C        Used:   0.01G   DISK:196.86G        Usage:  0.01%
 --  /var/lib/mysql/aaaa/datafiles/D        Used:   8.95G   DISK:196.86G        Usage:  4.55%

Linux bash: copy particular files in between two dates back in time to a network filer

February 18, 2011 Leave a comment

friday.logs.to.filer.sh

#!/bin/sh
# friday.logs.to.filer.sh
# seikath@gmail.com, Barcelona
# Fri Feb 18 09:21:49

PERIOD_IN_DAYS_BACK="-7"
DEST_DIR_ROOT="/mnt/nfs/root"
CURRENT_DEST_DIR="${DEST_DIR_ROOT}/logs/${HOSTNAME}/"$(date +"%Y-%m-%d")
NETWROK_FILER_MOUNT="//192.168.5.4/web /mnt/nfs"
SCRIPT_NAME=${0%.*}
LOG_FILE="/var/log/$(basename ${SCRIPT_NAME}).log"
CONF_FILE=${SCRIPT_NAME}.conf
#NEWER_THAN=$(date -d "today ${PERIOD_IN_DAYS_BACK} day " +"%Y%m%d2359")
#OLDER_THAN=$(date -d "today -1 day " +"%Y%m%d2359")

function logit () {
    echo "$(date)::[${HOSTNAME}] : ${1}"
    echo "$(date)::[${HOSTNAME}] : ${1}" >> "${LOG_FILE}"
}

# check the filer mount
mount_status=$(fgrep "${NETWROK_FILER_MOUNT}" /proc/mounts)
if [ "${mount_status}" == "" ] 
then
    logit "ERROR : //192.168.5.4/web is not mounted as /mnt/nfs, skipping the log backup"
    exit 0
fi

# check destination directory
if [ ! -d "${CURRENT_DEST_DIR}" ]
then
    logit "NOTICE : Missing dir ${CURRENT_DEST_DIR}, creating it"
    mkdir -p "${CURRENT_DEST_DIR}"
fi

# check permissions
if ! touch "${CURRENT_DEST_DIR}/test.permissons"
then
    logit "ERROR : Not able to write to ${CURRENT_DEST_DIR}"
    exit 0
else 
    unlink "${CURRENT_DEST_DIR}/test.permissons"
fi



while read settings
do
  if [ "${settings:0:1}" == "#" ];then continue;fi
  dirs_to_backup_o=${settings/,*/}
  dirs_to_backup=$(echo ${dirs_to_backup_o} | sed 's/\/*$//')
  regexp_to_backup=${settings/,*/}
  regexp_to_backup=${settings##${regexp_to_backup},}
  regexp_to_backup=${regexp_to_backup/,*/}
  regexp_to_ignore=${settings##${dirs_to_backup_o},${regexp_to_backup},}   
  if [ "${settings}" == "${regexp_to_ignore}" ];then regexp_to_ignore="";fi
 if [ ! -d "${dirs_to_backup}" ]
  then
      logit "NOTICE : [${dirs_to_backup}] from ${CONF_FILE} is not existing directory"
      continue
  fi
  #echo "find ${dirs_to_backup}/ -maxdepth 1 -type f -name \"${regexp_to_backup}\" ! -name \"${regexp_to_ignore}\" -daystart -mtime +0 -mtime ${PERIOD_IN_DAYS_BACK} -printf '\"%p\"\n'"
  find ${dirs_to_backup}/ -maxdepth 1 -type f -name "${regexp_to_backup}" ! -name "${regexp_to_ignore}" -daystart -mtime +0 -mtime ${PERIOD_IN_DAYS_BACK} -printf '"%p"\n' | \
  xargs -r -I crap cp -vv crap "${CURRENT_DEST_DIR}"
done < ${CONF_FILE}

root@xxxx:[Fri Feb 18 14:24:56]:[/apps/scripts]$ cat friday.logs.to.filer.conf
# dir,file_patern, ignore_file_patern   
/var/log/httpd/,xxxx.*,*request*
#/backups,xxxx.*hmv*
/backups,xxx.*api.log*

Linux bash: Oracle calculate the sum size of the expdmp files by the expdmp log

February 17, 2011 Leave a comment
[oracle@xxxx:[Thu Feb 17 17:20:30]:[/opt/oracle/rman/xxxx_xxxx/export]$ cat datapump_FULL.20110214.elog  \
| awk '/exported/ {crap=$5;if ($6=="KB") {crap=$5*1024;$6="Bytes"}  else if ($6=="MB") {crap=$5*1024*1024;$6="Bytes"} else if ($6=="GB") {crap=$5*1024*1024*1024;$6="Bytes"} SUM+=crap;} END {printf("%07.2f GB \n",SUM/(1024*1024*1024))}'
0029.14 GB]

more verbose:

cat datapump_FULL.20110214.elog \
| awk '/exported/ {ded=$6;crap=$5;if ($6=="KB") {crap=$5*1024;ded="Bytes"}  else if ($6=="MB") {crap=$5*1024*1024;ded="Bytes"} else if ($6=="GB") {crap=$5*1024*1024*1024;ded="Bytes"} SUM+=crap;printf("%014.2f Bytes [%016.2f] %s\n",crap,SUM,$0)} END {printf("%07.2f GB \n",SUM/(1024*1024*1024))}'

Linux bash: truncate custom logs not able to be moved

February 12, 2011 Leave a comment

rotate.custom.logs.sh

#!/bin/sh
# Barcelona, Sat, 12 Feb 2011 23:06:03 +0100
# seikath@gmail.com
# 
CONF_FILE="${0/.sh/}.conf"
LOGDATE=$(date  +"%Y-%m-%d" -d yesterday)
DESTDIR_DEF="/backups"


if [ -f "${CONF_FILE}" ]
then 

while read settings
do

if [ "${settings:0:1}" == "#" ];then continue;fi

LOGTOROTATE=${settings/,*/}
DESTDIR=${settings/*,/}
if [ "${DESTDIR}" == "${LOGTOROTATE}"];then DESTDIR="";fi
if [ "${DESTDIR}" == ""];then DESTDIR="${DESTDIR_DEF}";fi

if [ ! -f "${LOGTOROTATE}" ]
then
    echo "$(date) : missing file to rotate: ${LOGTOROTATE}"
    continue
fi

if [ ! -d "${DESTDIR}" ]
then
    echo "$(date) : missing ${DESTDIR} - executing mkdir -p ${DESTDIR}"
    mkdir -p "${DESTDIR}"
fi


FILESIZE_ORIGINAL=$(du -sh "${LOGTOROTATE}" | awk '{gsub("[^[:digit:]]","",$1);print $1}')
LINENUMB_ORIGINAL=$(wc -l "${LOGTOROTATE}" | awk '{print $1}')

if [ -f "${LOGTOROTATE}" -a ${FILESIZE_ORIGINAL} -ge 0 -a ${LINENUMB_ORIGINAL} -ge 2 ]
then
    BCKP_NAME="${DESTDIR}/$(hostname).$(basename ${LOGTOROTATE}).${LOGDATE}"
    BCKP_NAME=${BCKP_NAME/$HOSTNAME.$HOSTNAME./$HOSTNAME.}
    echo "$(date) : start copying ${LOGTOROTATE} to ${BCKP_NAME}"
    if cp -vv "${LOGTOROTATE}" "${BCKP_NAME}"
    then
        echo "$(date) : end copy  ${LOGTOROTATE} to ${BCKP_NAME}"
        FILESIZE_BCKP=$(du -sh "${BCKP_NAME}" | awk '{gsub("[^[:digit:]]","",$1);print $1}')
        LINENUMB_BCKP=$(wc -l "${BCKP_NAME}" | awk '{print $1}')
        if [ -f "${BCKP_NAME}" -a ${FILESIZE_BCKP} -ge 0 -a ${LINENUMB_BCKP} -ge 2 ]
        then
            echo "$(date) : status : $(du -sh ${BCKP_NAME})"
            echo "$(date) : truncating ${LOGTOROTATE}"
            echo "$(date) : truncated by ${0}" > ${LOGTOROTATE}                                                                                  
        else
            echo "$(date) : ${BCKP_NAME} is non existing: $(file ${BCKP_NAME}) or size is ${FILESIZE_BCKP} or lines are ${LINENUMB_BCKP}"
        fi
    else
        echo "$(date) : error copying ${LOGTOROTATE} to ${BCKP_NAME}"
    fi
else
    echo "$(date) : ${LOGTOROTATE} is non existing: $(file ${LOGTOROTATE}) or size is ${FILESIZE_ORIGINAL} or lines are ${LINENUMB_ORIGINAL}"
fi

done < "${CONF_FILE}"

else
    echo "$(date) : ${CONF_FILE} is empry or missing"
fi

rotate.custom.logs.conf

root@xxxx:[Fri Feb 18 12:15:36]:[/apps/]$ cat rotate.custom.logs.conf
# file_name_to_rotate,directory_to_store_rotated
/var/log/xxxx.restart.apache.on.high.cpu.usage.log
/var/log/xxxx.monitor.api.log
/srv/www/web/force.log
/var/log/httpd/xxxx.wsgi.ssl_request_log,/var/log/httpd
/var/log/httpd/xxxx.x.xxxx-xxxx.com.wsgi.ssl_request_log,/var/log/httpd

Linux bash: List all directories in a target directory sorted by the size of each found directory

February 10, 2011 Leave a comment
root@xxxx:[Thu Feb 10 14:57:40]:[/apps]$ find /apps/ -maxdepth  1 -type d ! -type l  ! -path "/apps/"   -printf '"%p"\n'  | xargs  --no-run-if-empty  du -s | sort -nk 1,9 | awk '{crap="";for(i=2; i<=NF; i++) {crap=crap" "$i;};printf ("%06.2fG %s\n",$1/1024/1024,crap)}'
000.00G /apps/backups
000.00G /apps/dump_mysql
000.00G /apps/restore
000.01G /apps/dest
000.09G /apps/jre1.5.0_21
000.15G /apps/apache-tomcat-5.5.28
000.19G /apps/software
000.30G /apps/apache-tomcat-5.5.15
000.95G /apps/ops
001.51G /apps/openssl
016.96G /apps/mysql-5.0.86-linux-i686
Categories: bash, RHEL, Slackware Tags: , , ,