熹熹爸爸的ChinaUnix博客
熹熹爸爸
全部博文(58)
2015年(3)
2014年(8)
2013年(47)
13814089
lwgboy
Anzyfly
ljj34201
ruci1214
时有无
chengxuy
huansuo
xialeiji
分类: LINUX
2014-01-03 13:12:43
原文地址:脚本获取系统CPU,内存,磁盘和网卡信息 作者:duanjigang
响应管理员号召,写博客
#############sys_monitor.sh --by cme@2010-11-03#!/bin/sheth="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` fidone}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.txtdisk=`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
上一篇:处理大量数据的一种异步方法--空间换时间
下一篇:netlink socket理解
登录 注册