获取linux系统的各服务进程与CPU、内存、硬盘空间使用状态

 

#!/bin/sh
list=(mcms mcwts mccaches mcsagt phonegateway  hw_IMS_Conference_CBU hw_IMS_Conference_CMU hw_IMS_Conference_focus hw_IMS_Conference_CSipServer)
for i in ${list[@]}
do
  ps aux | grep ${i} | grep -v grep | awk '{print $11}' | while read line
    do
       if [[ "$line"~="/$i/" ]]
 then
    echo $i 1
    break
 else
    echo $i 0
       fi
    done
done

 

file_dir="."

free -m > ${file_dir}/mem_file.txt
mem_file="${file_dir}/mem_file.txt"

 

v_memtotal=`cat ${mem_file} | sed -n '2'p | awk '{print $2}'`
v_memused=`cat ${mem_file} | sed -n '2'p | awk '{print $3}'`
v_memratio=`echo "scale=2; ${v_memused}*100/${v_memtotal}" | bc`

echo memory $v_memratio%

##echo user nice system idle iowait irq softirq
cat /proc/stat | grep 'cpu' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}' > /tmp/cpuinfo.in
user_a=$(awk '{sum+=$1}END{print sum}' /tmp/cpuinfo.in )
nice_a=$(awk '{sum+=$2}END{print sum}' /tmp/cpuinfo.in )
system_a=$(awk '{sum+=$3}END{print sum}' /tmp/cpuinfo.in )
idle_a=$(awk '{sum+=$4}END{print sum}' /tmp/cpuinfo.in )
iowait_a=$(awk '{sum+=$5}END{print sum}' /tmp/cpuinfo.in )
irq_a=$(awk '{sum+=$6}END{print sum}' /tmp/cpuinfo.in )
softirq_a=$(awk '{sum+=$7}END{print sum}' /tmp/cpuinfo.in )

Total_a=`expr $user_a + $nice_a + $system_a + $idle_a + $iowait_a + $irq_a + $softirq_a`

sleep 3

cat /proc/stat | grep 'cpu' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}' > /tmp/cpuinfo.in
user_b=$(awk '{sum+=$1}END{print sum}' /tmp/cpuinfo.in )
nice_b=$(awk '{sum+=$2}END{print sum}' /tmp/cpuinfo.in )
system_b=$(awk '{sum+=$3}END{print sum}' /tmp/cpuinfo.in )
idle_b=$(awk '{sum+=$4}END{print sum}' /tmp/cpuinfo.in )
iowait_b=$(awk '{sum+=$5}END{print sum}' /tmp/cpuinfo.in )
irq_b=$(awk '{sum+=$6}END{print sum}' /tmp/cpuinfo.in )
softirq_b=$(awk '{sum+=$7}END{print sum}' /tmp/cpuinfo.in )

Total_b=`expr $user_b + $nice_b + $system_b + $idle_b + $iowait_b + $irq_b + $softirq_b`

rm /tmp/cpuinfo.in

SYS_IDLE=`expr $idle_b - $idle_a`

Total=`expr $Total_b - $Total_a`
SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l`

SYS_Rate=`expr 100-$SYS_USAGE |bc -l`

Disp_SYS_Rate=`expr "scale=3; $SYS_Rate/1" |bc`
echo cpu $Disp_SYS_Rate%

disk=0;
#df -hl | awk '{if($5~/\w/) print substr($5,0,length($5)-1)}' | while read line
df -hl | awk '{if($5~/\w/) print substr($4,0,length($4)-1)}' > disk.txt
while read line
do
 #echo disk_$line
 #let disk=$line+disk | bc
 disk="`echo \"$line+$disk\" | bc`"

 
done < disk.txt
echo disk $disk"G"

阅读更多
想对作者说点什么? 我来说一句

获取内存使用状态的资源

2010年07月01日 64KB 下载

获取系统参数的源码和程序

2009年12月17日 18KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭