Chinaunix首页 | 论坛 | 博客
  • 博客访问: 286583
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 35
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-19 15:16
文章分类

全部博文(81)

文章存档

2021年(1)

2016年(3)

2015年(6)

2014年(6)

2013年(33)

2012年(32)

分类: Python/Ruby

2012-03-26 14:46:33

 
 
shell中计算除法,基本的方法有几种
t1=3
t2=6
1)expr
num=`expr $(($t1/$t2))`
num的结果为0,因为expr不支持浮点算法
2)bc
echo "scale=2;$t1/$t2"|bc
scale用来控制小数点后面保留的位数
3)awk
awk 'BEGIN{printf "%.2f%\n",('$t1'/'$t2')}'
awk 'BEGIN{printf "%.2f%\n",('$t1'/'$t2')*100}'
 
乘法
usage_mem_percent=`echo $t1 \* 100|bc`
 
加法:
total_cpu_percent=`echo "$t1+$t2"|bc`
 
比较:
flag=`expr $usage_cpu \> "0.95"` 大于 flag=1
 
例子(监控cpu和内存总占用量大于95%报警发邮件):
 


点击(此处)折叠或打开

  1. #!/bin/sh

  2. MAILX=/usr/bin/mailx
  3. NOTIFY="194915592@qq.com"
  4. FROM="***@****.com"
  5. DATE=`date +%Y%m%d%H`
  6. DIR="/root/monitor/"
  7. HOSTNAME=`hostname`
  8. NUM="100"

  9. cd $DIR
  10. rm report_cpu_mem.`date -d -1day +%Y%m%d`*
  11. rm report_cpu_mem.$DATE
  12. ########################### cpu ##########################
  13. #system used percent of cpu and user used percent of cpu e.g 3.2%usr , 4.5%sys , user_cpu_percent=3.2 ,sys_cpu_percent=4.5
  14. user_cpu_percent=`top -n 1 |grep Cpu|awk '{print $2}'|cut -d "%" -f 1`
  15. sys_cpu_percent=`top -n 1 |grep Cpu |awk '{print $3 }' |cut -d "%" -f 1`

  16. total_cpu_percent=`echo "$user_cpu_percent+$sys_cpu_percent"|bc`
  17. #cpu number e.g 60% used , then usage_cpu=0.6
  18. usage_cpu=`echo "scale=2;$total_cpu_percent/$NUM"|bc`

  19. ########################### mem ##########################

  20. #total memory and used memory , e.g total 25620K , used 2222K, then total_mem=25620, used_mem=2222k
  21. total_mem=`top -n 1 |grep Mem |awk '{print $3}'|cut -d "k" -f 1`
  22. used_mem=`top -n 1 |grep Mem |awk '{print $5}'|cut -d "k" -f 1`

  23. #memory number e.g 30% used, then usage_mem=0.3
  24. usage_mem=`echo "scale=2;$used_mem/$total_mem"|bc`

  25. #the percent of memory used , e.g 63.3% , then usage_mem_percent=63.6
  26. usage_mem_percent=`echo "0"$usage_mem \* 100|bc`



  27. flag=`expr "0"$usage_cpu \> "0.95"`
  28. flag1=`expr "0"$usage_mem \> "0.95"`

  29. if [ "$flag" == "1" ];then
  30. echo "The cpu Utilization on $HOSTNAME is over 95%. " >>report_cpu_mem.$DATE
  31. echo "user had used "$total_cpu_percent"% cpu" >>report_cpu_mem.$DATE
  32. echo "system had used "$sys_cpu"% cpu">>report_cpu_mem.$DATE
  33. echo "Please check below processes:" >>report_cpu_mem.$DATE

  34. ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head >>report_cpu_mem.$DATE
  35. $MAILX -s "the cpu of $hostname used too much , please check it" $NOTIFY<report_cpu_mem.$DATE
  36. fi
  37. if [ "$flag1" == "1" ] ;then

  38. echo "The mem Utilization on $HOSTNAME is over 95%. " >>report_cpu_mem.$DATE
  39. echo "this host had used $usage_mem_percent"%" memory" >>report_cpu_mem.$DATE
  40. echo "Please check below processes:" >>report_cpu_mem.$DATE

  41. ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head >>report_cpu_mem.$DATE
  42. $MAILX -s "the host $HOSTNAME used too much memory or cpu , please check it" $NOTIFY<report_cpu_mem.$DATE

  43. fi


 
 
阅读(1248) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~