cpu性能监控

#!/bin/bash
 
 
column_count=2
i=1
m=15
is_want=0
str_msg="";
file_name=./test/`date +%Y-%m-%d`_CPU.txt
 
for p in `ps -eo pcpu,pid |sort -k 1 -rn`
do
 
  if [ `expr $i % $column_count` = 1 ]
  then
    str_msg="";
 
    #if [ `expr $p \> $m` = 1 ] && [ "$p"x != "%CPU"x ]
    if [ `awk -v a=$p -v b=$m 'BEGIN{print(a>b)?"a":"b"}'` = 'a' ] && [ "$p"x != "%CPU"x ]
    then
      str_msg="CPU=$p";
 
      is_want=1
    else
      #is_want=0
      break
    fi
#  elif [ `expr $i % $column_count` = 2 ] && [ $is_want = 1 ]
#  then
#    str_msg=$p;
  elif [ `expr $i % $column_count` = 0 ] && [ $is_want = 1 ]
  then
    str_msg="$str_msg,PID=$p";
    #echo $str_msg
    echo [`date +%Y-%m-%d_%H:%M:%S`]$str_msg >> $file_name;
  fi
 
  i=`expr $i + 1`
 
  #sleep 0.5;
 
done
 
 
echo [`date +%Y-%m-%d_%H:%M:%S`]"" >> $file_name;

 

memory性能监控

#!/bin/bash
 
 
column_count=2
i=1
m=1048576
is_want=0
str_msg="";
file_name=./test/`date +%Y-%m-%d`_memory.txt
 
for p in `ps -eo rss,pid |sort -k 1 -rn`
do
 
  if [ `expr $i % $column_count` = 1 ]
  then
    str_msg="";
 
    #if [ `expr $p \> $m` = 1 ] && [ "$p"x != "%CPU"x ]
    if [ `awk -v a=$p -v b=$m 'BEGIN{print(a>b)?"a":"b"}'` = 'a' ] && [ "$p"x != "%CPU"x ]
    then
      str_msg="memory=$p";
 
      is_want=1
    else
      #is_want=0
      break
    fi
#  elif [ `expr $i % $column_count` = 2 ] && [ $is_want = 1 ]
#  then
#    str_msg=$p;
  elif [ `expr $i % $column_count` = 0 ] && [ $is_want = 1 ]
  then
    str_msg="$str_msg,PID=$p";
    #echo $str_msg
    echo [`date +%Y-%m-%d_%H:%M:%S`]$str_msg >> $file_name;
  fi
 
  i=`expr $i + 1`
 
  #sleep 0.5;
 
done
 
 
echo [`date +%Y-%m-%d_%H:%M:%S`]"" >> $file_name;