BASH Scripts

log_cal.sh

#!/bin/bash
#ver 1.0 by gavin
###指定文件夹大小计算,可以套用,但不能直接用,这个只适合我这边的生产环境
usage_ver () {
        echo "# ver 0.1 2009-12-09 by gavin"
        exit 0
}
 
usage_help () {
        echo "Usage: $0 optstring parameters"
        echo "       $0 [options] [--] optstring parameters"
        echo "  -V, --version                Show Version info"
        echo "  -r, --directory              Target-directory"
        echo "  -i, --filename               Include-chars"
        echo "  -x, --exclude                Exclude-chars"
        echo "  -p, --maxdepth               Maxdepth directory, 1 digit "
        echo "  example)  $0 -r /tera02/lb05a/ -i 200910 -p 1"
        exit 0
#       echo "Usage: $0 --directory=dir1,dir2,dirN --filename=include-chars [--exclude=chars] [--maxdepth=num]"
}
 
usage_info() {
        echo "$0: missing optstring argument"
        echo "Try $0 --help" for more information.
        exit 0
}
 
[[ "$1" = "--help" || "$1" =~ '^-[hH]$' ]] && usage_help
[[ "$1" = "--version" || "$1" =~ '^-[Vv]$' ]] && usage_ver
[ $# -eq 0 ] && usage_info
 
GETOPT='/usr/bin/getopt'
ARGS=`$GETOPT -o r:i:x:p: -l maxdepth:,exclude:,directory:,filename: -- "$@" 2>/dev/null`
 
if [ $? != 0 ]; then usage_help; fi
 
eval set -- "$ARGS"
 
while true; do
        case "$1" in
        -r|--directory)
                DIRS=$2; shift 2;;
        -i|--filename)
                FILE=$2; shift 2;;
        -x|--exclude)
                EXCLUDE=$2; shift 2;;
        -p|--maxdepth)
                DEPTH=$2; shift 2;;
        --) shift; break;;
        *) echo 'internal error.'; exit 1;;
        esac
done
#echo "DIR -> $DIR, FILE -> $FILE, EXCLUDE -> $EXCLUDE, DEPTH -> $DEPTH"
 
if [[ "A$DEPTH" != "A" && $DEPTH != [[:digit:]] ]]
then
        echo "Error with --maxdepth,1 digit"
        usage_help
fi
 
#echo $DIR $FILE $EXCLUDE $DEPTH
 
TREEDIR=(`
        for DIR in ${DIRS[@]}
        do
          [ ! -d $DIR ] && { echo "*** Error: $DIR is not directory"; continue; }
        [[ "A$EXCLUDE" = "A" && "X$DEPTH" = "X" ]] && find $DIR -type d |grep -v rename
        [[ "A$EXCLUDE" != "A" && "X$DEPTH" = "X" ]] && find $DIR -type d |grep -v $EXCLUDE|grep -v rename
        [[ "A$EXCLUDE" = "A" && "X$DEPTH" != "X" ]] && find $DIR -maxdepth $DEPTH -type d|grep -v rename
        [[ "A$EXCLUDE" != "A" && "X$DEPTH" != "X" ]] && find $DIR -maxdepth $DEPTH -type d|grep -v rename|grep
 -v $EXCLUDE
        done`)
 
for SUBDIR in ${TREEDIR[@]}
do
  echo "<----  Info for $SUBDIR directory  ---->"
  printf "Size of $SUBDIR = "
  find $SUBDIR -type f -regex ".*"$FILE".*" -ls|awk '{total+=$7}; END{print total/(1024^2)"M"}'
  printf "number of files in $SUBDIR = "
  ls $SUBDIR|wc -l
done
 
exit 0

monitor_track.sh

#more monitor_track.sh
#ver 1.50 by gavin
#need inotify-tools.x86_64 && inotify-tools-devel.x86_64 package
#yum -y install inotify-tools.x86_64 inotify-tools-devel.x86_64
 
usage_info () {
    echo "Usage: $0 optstring parameters"
    echo "      --verbose       increase verbosity info"
}
 
install_package () {
    echo "need inotify-tools.x86_64 && inotify-tools-devel.x86_64 package"
    echo ""
    echo "rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt"
    echo "rpm -Uvh http://apt.sw.be/redhat/el5/en/i386/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.i386.rpm"
    echo "rpm -Uvh http://apt.sw.be/redhat/el5/en/x86_64/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm"
    echo "yum -y install inotify-tools.x86_64 inotify-tools-devel.x86_64"
    echo ""
}
 
MONITOR_DIR=/home/atlantis/monitor
LOG_DIR=/home/atlantis/logs
LOG_FILE=$LOG_DIR/monitor.log
#TRANS_DIR=/hdfs2
EVENTS=(-e CREATE,MODIFY)
PID=/tmp/monitor_file.pid
echo $EOF > $LOG_FILE
 
declare -a LBSERVER
 
for i in {a..f}
do
  LBSERVER[${#LBSERVER[@]}]=server01`printf "$i"`
done
 
check () {
 
if [ ! -d $MONITOR_DIR ]
then
  mkdir -p $MONITOR_DIR
fi
 
}
 
monitor_file () {
while inotifywait ${EVENTS[@]} $MONITOR_DIR; do
    HOST=(`ls $MONITOR_DIR`)
#    echo "${#HOST[@]}"
#    echo "${#LBSERVER[@]}"
    if [ "${#HOST[@]}" = "${#LBSERVER[@]}" ]
    then
      for ((i=0;i<${#LBSERVER[@]};i++))
        do
          [ "${HOST[$i]}" != "${LBSERVER[$i]}" ] && echo unfinished && break
        done
      if [ "$i" = "${#LBSERVER[@]}" ]
        then
          mkdir /tmp/hadoop_ready.lock
          rm -rf $MONITOR_DIR/*
          echo "COMPLETED!!"
      fi
    fi
done
}
 
main () {
check
[ ! -x /usr/bin/inotifywait ] && install_package
 
[ -e $PID ] && ps -ef |awk '{print $2}' |grep `cat $PID`|grep -v grep &>/dev/null
if [ `echo $?` -eq 0 ]
then
    echo "$0 can not run : process is already running in `cat $PID` pid"
    exit 1
fi
 
if [ $# -eq 0 ]
then
    monitor_file &> $LOG_FILE &
    echo $! > $PID
elif [ "$1" ==  "--verbose" ]
then
    monitor_file
else
    usage_info
    exit 1
fi
}
 
#monitor_file
main $@
 
exit 0

monitor_node.sh

#!/bin/bash
#ver 1.2 by gavin
#need inotify-tools.x86_64 && inotify-tools-devel.x86_64 package
#yum -y install inotify-tools.x86_64 inotify-tools-devel.x86_64
 
usage_info () {
    echo "Usage: $0 optstring parameters"
    echo "      --verbose       increase verbosity info"
}
 
install_package () {
    echo "need inotify-tools.x86_64 && inotify-tools-devel.x86_64 package"
    echo ""
    echo "rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt"
    echo "rpm -Uvh http://apt.sw.be/redhat/el5/en/i386/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.i386.rpm"
    echo "rpm -Uvh http://apt.sw.be/redhat/el5/en/x86_64/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm"
    echo "yum -y install inotify-tools.x86_64 inotify-tools-devel.x86_64"
    echo ""
}
 
MONITOR_DIR=/home/atlantis/monitor
LOG_DIR=/home/atlantis/logs
LOG_FILE=$LOG_DIR/monitor.log
#TRANS_DIR=/hdfs2
EVENTS=(-e CREATE,MODIFY,MOVED_TO,DELETE)
PID=/tmp/monitor_file.pid
 
check () {
if [ ! -d $MONITOR_DIR ]
then
  mkdir -p $MONITOR_DIR
fi
 
if [ ! -d $LOG_DIR ]
then
  mkdir -p $LOG_DIR
fi
echo $EOF > $LOG_FILE
}
 
monitor_file () {
while inotifywait ${EVENTS[@]} $MONITOR_DIR; do
    HOST=`ls -rt $MONITOR_DIR |tail -n1`
#    DIR=`cat $MONITOR_DIR/$HOST`
    echo "<-- active ftp get $HOST log files `date +%Y%m%d%H%M%S`-->"
#    echo "`dirname %0`/get_log.sh $HOST"
    /home/atlantis/scripts/get_log.sh $HOST &
    echo ""
done
}
 
main () {
check
[ ! -x /usr/bin/inotifywait ] && install_package
 
[ -e $PID ] && ps -ef |awk '{print $2}' |grep `cat $PID`|grep -v grep &>/dev/null
if [ `echo $?` -eq 0 ]
then
    echo "$0 can not run : process is already running in `cat $PID` pid"
    exit 1
fi
 
if [ $# -eq 0 ]
then
    monitor_file &> $LOG_FILE &
    echo $! > $PID
elif [ "$1" ==  "--verbose" ]
then
    monitor_file
else
    usage_info
    exit 1
fi
}
 
#monitor_file
main $@
/home1/yepnnet/public_html/wiki/data/pages/bashscripts.txt · 最后更改: 2010/02/22 07:13 由 admin
到顶部
CC Attribution-Noncommercial-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0