搜索历史
热搜词
shell 实现监控服务器性能脚本

推荐:监控Nginx服务的Shell脚本

[        Nginx 虽然处理并发量比 apache 确实要强点,但它这种 php-cgi 模式不是太稳定,这点网上也有朋友总结了,我在实现项目中也感受到了。 我们一台支付机,偶

shell 实现监控服务器性能脚本

shell中计算除法,

推荐:linux下性能监控shell脚本实现系列一(服务器整体性能监控)

[在实现监控脚本前,我们先了解下一些已经成型的监控程序,比如大名鼎鼎的nmon。nmon官网 http://nmon.sourceforge.net/pmwiki.phpnmon使用简单说明 http://www.ibm.com/de

基本的方法有几种
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%报警发邮件):



点击(此处)折叠或打开
#!/bin/sh

MAILX=/usr/bin/mailx
NOTIFY="194915592@qq.com"
FROM="***@****.com"
DATE=`date %Y%m%d%H`
DIR="/root/monitor/"
HOSTNAME=`hostname`
NUM="100"

cd $DIR
rm report_cpu_mem.`date -d -1day %Y%m%d`*
rm report_cpu_mem.$DATE
########################### cpu ##########################
#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
user_cpu_percent=`top -n 1 |grep Cpu|awk '{print $2}'|cut -d "%" -f 1`
sys_cpu_percent=`top -n 1 |grep Cpu |awk '{print $3 }' |cut -d "%" -f 1`

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

########################### mem ##########################

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

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

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



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

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

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

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

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

fi

推荐:使用shell脚本进行服务器系统监控——进程监控

[#!/bin/ksh typeset -u RUN_PRE_EVENT # Force to UPPERCASEtypeset -u RUN_STARTUP_EVENT # Force to UPPERCASEtypeset -u RUN_POST_EVENT # force to UPPERCASE RUN_P

主题:

分享:

相 关 推 荐

热 门 推 荐