http://club.topsage.com/forum-119-6.html大家论坛技术中心
- #!/bin/bash
- #根据研发需求,写的服务器游戏进程性能监控脚本。
- . ~/.keychain/`hostname`-sh
- trap 'echo;exit 0' 2 15
- DATE0=`date +%Y%m%d-%H%M`
- DATE1=`date +%F`
- DATE2=`date -d "1day ago" +%F`
- IP_ALL="iplist"
- LDIR="~/monitor_log"
- IP=`perl -nae'print "$F[0]/n" if $_ =~ /groupname1|groupname2/ && $_ !~ /#/' $IP_ALL`
- echo -e "*************$DATE0************/n/n" >>$LDIR/$DATE1.log
- for i in $IP;do
- export i;
- name=`perl -nae'print "$F[1]/n" if $_ =~ //b$ENV{i}/b/' $IP_ALL`
- echo -e "###$name($i)--$DATE0###/n"
- ssh $i "renshu=/`netstat -napl | egrep '3731|3732|3734|3735|3736'| grep -i estab|wc -l/`
- cpu=/$(echo /$(echo 100-/$(mpstat |egrep -v 'CPU|^$|Linux'|awk '{print /$(NF-1)}')|bc)%)
- mem=/$(echo /$(free |grep Mem|awk '{{a=/$3//$2*100}printf /"%2.2f/n/",a}')%)
- export cpu_count=/`grep processor /proc/cpuinfo|wc -l/`
- svr_cpu=/$(top -b -n 2 -d 1|perl -ne'if (/$_ =~ /Cpu|Server|World|ShareMemory|Login/){/$svr_top .= /$_;/$cpu_top = /$svr_top;}END{ /$svr_top =~ s/^Cpu.*%st/n//s;print /$svr_top;if(/$cpu_top =~ /^Cpu.*Cpu.*/s+(/d+/./d+)%id.*$/gs){/$cpu_total = 100 - /$1;print /"/$cpu_total/n/"}}'|perl -ane'/$sum += /$cpu_svr;/$cpu_svr= /$F[8] / /$ENV{/"cpu_count/"};printf /"%-20s/t%-10s/n/",/"/$F[-1]:/",/$cpu_svr;END{printf /"%-20s/t%-10s/n/",/"Total:/",/$sum}'|perl -ane'unless (/$_ =~ /^(/d+/./d+).*/){push @perf,/$_;}else{/$cpu_top = /$1;}END{print @perf;printf /"%-20s/t%-10s/n/",/"CPU_Used:/",/"/$cpu_top/"}')
- process_memory=/`ps aux|egrep -v 'grep|sed' |egrep -i 'server|world|ShareMemory|login'|egrep -v 'gmserver|root|Login..*'|
- sed 's%-loadscriptonce%Server%'|awk '{printf /"%-10s %10s/n/",/$NF,/$4/"%/"}'|sed 's%./%%'/`
- ##process_memory=/$(ps -eo args,rss|grep -v grep |egrep -w '/./ShareMemory/>|/./Login/>|/./Server/>|/./World/>'|awk '{printf /"%-10s %10s/n/",/$1,/$NF/1024}')
- echo -e /"Online: /t/$renshu/nCPU-used:/n/$svr_cpu/n/nMEM-used:/t/$mem/n/nProcesses-in-MEM:/n/$process_memory/n/n/""
- done>>$LDIR/$DATE1.log
- exit 0