欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

统计 cpu 内存 使用率的shell脚本代码

程序员文章站 2022-06-13 18:50:12
复制代码 代码如下:#!/bin/shclearwhile ((1>0))do net=$(netstat -anp | grep 1000 | grep...
复制代码 代码如下:

#!/bin/sh
clear
while ((1>0))
do
 net=$(netstat -anp | grep 1000 | grep est | wc -l)
 prc=$(ps aux | grep thread1 | grep -v grep | grep -v screen | awk '{ print " cpu: " $3 "%,  mem: " $4 "%, rss: " $6/1024 "m, vsz: " $5/1024 "m, pid: " $2 ""}')
 tput sc; echo -n -e "\b"; tput rc;
 echo "srv:  thread1, net: $net, $prc"
 echo ""
 net=$(netstat -anp | grep 1011 | grep est | wc -l)
 prc=$(ps aux | grep thread2 | grep -v grep | grep -v screen | awk '{ print " cpu: " $3 "%, mem: " $4 "%, rss: " $6/1024 "m, vsz: " $5/1024 "m, pid: " $2 ""}')
 tput sc; echo -n -e "\b"; tput rc;
 echo "srv:  thread2, net: $net, $prc"
 echo ""
 net=$(netstat -anp | grep 1022 | grep est | wc -l)
 prc=$(ps aux | grep thread3 | grep -v grep | grep -v screen | awk '{ print " cpu: " $3 "%, mem: " $4 "%, rss: " $6/1024 "m, vsz: " $5/1024 "m, pid: " $2 ""}')
 tput sc; echo -n -e "\b"; tput rc;
 echo "srv:  thread3, net: $net, $prc"
 echo ""
 net=$(netstat -anp | grep 1033 | grep est | wc -l)
 prc=$(ps aux | grep thread4 | grep -v grep | grep -v screen | awk '{ print " cpu: " $3 "%, mem: " $4 "%, rss: " $6/1024 "m, vsz: " $5/1024 "m, pid: " $2 ""}')
 tput sc; echo -n -e "\b"; tput rc;
 echo "srv:  thread4, net: $net, $prc"
 echo ""
 net=$(netstat -anp | grep beam.smp | grep est | grep 222.222.18.222:800 | wc -l)
 prc=$(ps aux | grep thread | grep -v grep | awk '{cpu += $3} {mem += $4} {rss += $6} {vsz += $5}  end {print "cpu: " cpu "%, mem: " mem "%, rss: " rss/1024 "m, vsz: " vsz/1024 "m,  "  " "}'  | awk 'end {print}')
 tput sc; echo -n -e "\b"; tput rc;
 echo "srv: all, net: $net, $prc"
 echo ""
 sleep 0.5
 tput cup 0 0
done