三个linux脚本

一、监测jvm

#!/bin/bash
export PATH=/usr/java/jdk1.6.0_20/bin:$PATH
PIDFILE=`jps|grep HttpServer|awk '{print $1}'`
THatday=`date +%Y%m%d`
nowDate=`date +'%Y-%m-%d %H:%M'`
#echo '-------------------------'$nowDate >> /data/cron_stat_log/jstat_gccapacity_$THatday.log
#jstat -gccapacity $PIDFILE 1000 10 >> /data/cron_stat_log/jstat_gccapacity_$THatday.log
#echo '-------------------------'$nowDate >> /data/cron_stat_log/jstat_gcutil_$THatday.log
#jstat -gcutil $PIDFILE 1000 10 >> /data/cron_stat_log/jstat_gcutil_$THatday.log
echo '-------------------------'$nowDate >> /data/cron_stat_log/netstat_$THatday.log
netstat -anpo | grep :80 | wc -l >> /data/cron_stat_log/netstat_$THatday.log
echo '-------------------------'$nowDate >> /data/cron_stat_log/resinThread_$THatday.log
ps -p $PIDFILE -L|wc -l >> /data/cron_stat_log/resinThread_$THatday.log
echo '------apache thread------'$nowDate >> /data/cron_stat_log/apacheThread_$THatday.log
ps -eLf | grep  apache|wc -l >> /data/cron_stat_log/apacheThread_$THatday.log
echo '-----apache process------'$nowDate >> /data/cron_stat_log/apacheProcess_$THatday.log
ps -ef | grep  apache|wc -l >> /data/cron_stat_log/apacheProcess_$THatday.log



二、较耗cpu的thread

#!/bin/bash
# @Function
# Find out the highest cpu consumed threads of java, and print the stack of these threads.


PROG=`basename $0`

redEcho() {
    if [ -c /dev/stdout ] ; then
        # if stdout is console, turn on color output.
        echo -e "\033[1;31m$@\033[0m"
    else
        echo "$@"
    fi
}

usage() {
    cat <<EOF
Usage: ${PROG} [OPTION]...
Find out the highest cpu consumed threads of java, and print the stack of these threads.
Example: ${PROG} -c 10

Options:
    -p, --pid       find out the highest cpu consumed threads from the specifed java process,
                    default from all java process.
    -c, --count     set the thread count to show, default is 5
    -h, --help      display this help and exit
EOF
    exit $1
}

printStackOfThreads() {
    while read threadLine ; do
        pid=`echo ${threadLine} | awk '{print $1}'`
        threadId=`echo ${threadLine} | awk '{print $2}'`
        threadId0x=`printf %x ${threadId}`
        user=`echo ${threadLine} | awk '{print $3}'`
        pcpu=`echo ${threadLine} | awk '{print $5}'`
        
        jstackFile=/tmp/${uuid}_${pid}
        
        [ ! -f "${jstackFile}" ] && {
            jstack ${pid} > ${jstackFile} || {
                redEcho "Fail to jstack java process ${pid}"
                rm ${jstackFile}
                continue
            }
        }
        
        redEcho "The stack of busy(${pcpu}%) thread(${threadId}/0x${threadId0x}) of java process(${pid}) of user(${user}):"
        sed "/nid=0x${threadId0x}/,/^$/p" -n ${jstackFile}
    done
}

ARGS=`getopt -a -o c:p:h -l count:,pid:,help -- "$@"`
[ $? -ne 0 ] && usage 1
eval set -- "${ARGS}"

while true; do
    case "$1" in
    -c|--count)
        count="$2"
        shift 2
        ;;
    -p|--pid)
        pid="$2"
        shift 2
        ;;
    -h|--help)
        usage
        ;;
    --)
        shift
        break
        ;;
    esac
done
count=${count:-5}

uuid=`date +%s`_${RANDOM}_$$

if ! which jstack &> /dev/null; then
    if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/jstack" ]; then
        export PATH=$PATH:$JAVA_HOME/bin
    else
        redEcho "Error: jstack not found on PATH and JAVA_HOME!"
        exit 1
    fi
fi

if [ -z ${pid} ] ; then
    ps -Leo pid,lwp,user,comm,pcpu --no-headers | awk '$4=="java"{print $0}' |
    sort -k5 -r -n | head --lines "${count}" | printStackOfThreads
else
    ps -Leo pid,lwp,user,comm,pcpu --no-headers | awk '$1=="'${pid}'",$4=="java"{print $0}' |
    sort -k5 -r -n | head --lines "${count}" | printStackOfThreads
fi

rm /tmp/${uuid}_*


三、

#!/bin/sh
iface="$1"

if [ -z "$iface" ]; then
iface="eth0"
fi
#得到访问量最大的域名
echo "domain sort"
/usr/sbin/tcpdump -i "$iface" -tn dst port 80 -c 1000  -A -s0 |grep 'Host: '|awk -F'Host: ' '{print $2}'|sort |uniq -c|sort -n -r |head -10
#访问IP排序
echo "IP sort"
/usr/sbin/tcpdump -i "$iface" -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -10



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值