按小时统计unix机器内存和cpu使用率

本文介绍了一个使用Shell脚本实现的自动化监控系统。该系统能够定时收集服务器资源使用情况(如内存利用率、CPU使用率),并记录到文件中进行后续分析。此外,还提供了统计特定日期的小时级资源使用概况及日志文件中的交易量的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给脚本赋权限
chmod 755 *.sh

后台执行该脚本
nohup sum_capa.sh &
x=0totmem=`vmstat | grep 'mem=' | awk -f\= '{ print $3 }' | awk -f\m '{print $1 }'`totmem1=` echo "$totmem * 200 " | bc `while truedo    strdate=`date +"%y%m%d"`    strtime=`date +"%y%m%d%h"`    strrun=`vmstat 1 4 | tail -1 | awk '{ print " "$3" "$14" "$15 }' `    echo $strtime" "$totmem1$strrun >> capa_$strdate.txt    sleep 56    x=`expr $x + 1`    if [ $x -gt 3000 ]    then        break    fidone


三天后得到统计数据,执行该脚本 get_capa.sh
if [ "$#" -ne 1 ]then    echo "usage:get_capa.sh yyyymmdd"    exit 1fiecho "hour         mem(%)        cpu(%) " > capa_hour_$1.txthh="00"x=0while truedo    if [ x -lt 10 ]    then       hh="0"$x    else       hh=$x    fi    strtmp=`grep $1$hh capa_$1.txt | awk 'begin{allmem=0;usemem=0;memrate=0;cpurate=0;num=0;allcpu=0;} { allmem=allmem+$2;usemem=usemem+$3;num=num+1;allcpu=allcpu+$4+$5;} end{ if (allmem>0) { memrate=usemem*100/allmem;cpurate=allcpu/num;} print memrate"     "cpurate}'`    echo $hh"        "$strtmp >> capa_hour_$1.txt    x=`expr $x + 1`    if [ $x -gt 23 ]    then        break    fidonecat capa_hour_$1.txt


统计交易量 sum_tran.sh
if [ "$#" -ne 3 ]then    echo "usage:sum_tran.sh yyyy mm dd"    exit 1fiecho "hour         trans" > tran_hour_$1$2$3.txthh="00"x=0while truedo    if [ x -lt 10 ]    then       hh="0"$x    else       hh=$x    fi    num=`grep txrunaction /usr/ibm/httpserver/logs/access.$1.$2.$3.log|grep $1":"$hh |wc -l`    echo $hh"      "$num >> tran_hour_$1$2$3.txt    x=`expr $x + 1`    if [ $x -gt 23 ]    then        break    fidonecat tran_hour_$1$2$3.txtnum=`cat /usr/ibm/httpserver/logs/access.$1.$2.$3.log | wc -l`echo "tottrans="$numnum=`grep txrunaction /usr/ibm/httpserver/logs/access.$1.$2.$3.log|wc -l`echo "txruntrans="$numnum=`grep linkagetx /usr/ibm/httpserver/logs/access.$1.$2.$3.log|wc -l`echo "linktrans="$num


统计20w数据表 db_20w.sh
if [ "$#" -ne 1 ]then    echo "usage:db_20w.sh ddmmyyyy"    exit 1fidb2 "connect to ecap "db2 "select count(*) from ecapdb.t_tran_log where head_date='"$1"' "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值