Chinaunix首页 | 论坛 | 博客
  • 博客访问: 183893
  • 博文数量: 58
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-29 17:51
文章分类
文章存档

2015年(3)

2014年(8)

2013年(47)

我的朋友

分类: LINUX

2014-01-03 13:12:43

响应管理员号召,写博客

#############sys_monitor.sh --by cme@2010-11-03
#!/bin/sh
eth="eth0 eth1 "
function new_df()
{
 s="";
 c=0
 ss=`df | awk 'BEGIN{total=0;avl=0;used=0;}NR > 1{print$0}'`
 for ite in $ss
 do
  s="$s $ite"
  c=`expr $c + 1`
  if [ $c -eq 6 ]
  then
   echo $s
   s=""
   c=`expr 0`
  fi
done
}
function get_eth_info()
{
 name=$1
 cat /proc/net/dev | awk -F ':' '{if(NR>2)print $1" "$2}' | awk '{print$1" "$2" "$3" "$10" "$11}' | while \
 read line
 do
# echo $line
 data=`echo $name" "$line | awk '{if($1==$2)print $3" "$4" "$5" "$6}'`
 if ! [ -z "$data" ]
 then
  echo $data
 fi
 done
}
function sum_eth()
{
 rm -fr tmp_flow.txt
 for name in $eth
 do
  ret=`get_eth_info $name`
  #echo $ret | awk '{bps+=$0;pps+$1;dps+=$2;print bps" "pps" "dps}'
  echo $ret >> tmp_flow.txt
 done
 cat tmp_flow.txt | awk 'BEGIN{rbps=0;rpps=0;tbps=0;tpps=0}{rbps+=$1;rpps+=$2;tbps+=$3;tpps+=$4}END{print rbps" "rpps" "tbps" "tpps}'
}
flow=`sum_eth`
new_df>df.txt
disk=`cat df.txt | awk 'BEGIN{total=0;avl=0;used=0;}NR > 1{total+=$2;used+=$3;avl+=$4;}END{printf"%d", avl/total*100}'`
#top -b -n 1 | grep -w Mem | awk '{print"tot_mem: "$2" used_mem: " $4 " free_mem: "$6" left:"$6/$2"%"}'
mem=`top -b -n 1 | grep -w Mem | awk '{printf"%d",$6/$2*100}'`
 #top -b -n 1 | grep -w Cpu | awk '{print"cpu: "$5}' | awk -F '%' {print$1}
 systype=`lsb_release -a | grep Desc | awk '{print $2}'`
 if [ $systype = "Red" ]
 then
 cpu=`top -b -n 1 | grep -w Cpu | awk '{print$5}' | awk -F '%' '{printf"%d",$1}'`
 else
 cpu=`top -b -n 1 | grep -w Cpu | awk '{print$8}' | awk -F '%' '{print$1}'`
 fi

 tm=`date +%s`
 if ! [ -f "flow.txt" ]
 then
 echo $tm" "$flow >flow.txt
 sleep 1
 tm=`date +%s`
 flow=`sum_eth`
 fi
 old_flow=`cat flow.txt`
 new_flow=`echo $tm $flow`
 echo $new_flow >flow.txt
 final_flow=`echo $old_flow $new_flow | awk '{dif_tm=$6-$1;dif_rbyte=$7-$2;dif_rpkt=$8-$3;dif_tbyte=$9-$4;dif_tpkt=$10-$5;printf"%d %d %d %d", dif_rbyte*8/dif_tm, dif_rpkt/dif_tm, dif_tbyte/dif_tm,dif_tpkt/dif_tm}'`
 #final_flow=`echo $old_flow $new_flow | awk '{print$0}'`
 #echo "cpu "$cpu
 printf "cpu\t%d\n" $cpu
 printf "disk\t%d\n" $disk
 printf "mem\t%s\n" $mem
 printf "nic\t"
 echo $final_flow


阅读(1154) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~