此脚本监控主机
1、cpu负载
2、内存使用情况
3、磁盘使用率情况
#!/bin/sh #################################### # 2013.10.15 by zhanghui #################################### #定义变量 v_datetime=`date +"%Y-%m-%d %H:%M:%S"` v_username='mysql' v_hostname=`hostname` #获取物理CPU数量 v_physicalcpu=`cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l` #获取逻辑CPU数量 v_logicalcpu=`cat /proc/cpuinfo | grep "processor" | wc -l` #获取IP地址 v_ip=`/sbin/ifconfig bond1|grep 'inet addr'|awk '{print $2}'|cut -d: -f2` #发送人 v_sendmail=sdtest02@sd.chinamobile.com #接收人 v_receivemail='zhaoying@richinfo.cn' #路径 v_path="/home/${v_username}/scripts" #设置获取top进程的数量 v_process=15 #系统负载监控,最近5分钟负载及Cpu Idle报警阀值 v_loadvaluesfor5min_warning=25 v_cpuusage_warning=20 #剩余物理内存报警阀值,单位M v_freememory_warning=200 #free+buffers+cached报警阀值,单位M v_free_buffers_cached_warning=2000 #使用swap分区报警阀值,单位M v_useswap_warning=1200 #磁盘使用百分比报警阀值 v_diskusepercent_warning=90 #邮件发送程序 MAIL_BIN="/home/crond/bsmtp" #设置LC_ALL export LC_ALL=en_US.UTF-8 #系统负载监控代码开始 #check sys load and cpuidle v_release=`lsb_release -a|head -4|tail -1|awk '{print $2}'|awk -F '.' '{print $1}'` if [ ${v_release} = 4 ]; then #RHEL 4.x v_cpuusage=`mpstat 1 5 | grep Average | awk '{print $9}'|awk -F "." '{print $1}'` #RHEL 5.x elif [ ${v_release} = 5 ]; then v_cpuusage=`mpstat 1 5 | grep Average | awk '{print $10}'|awk -F "." '{print $1}'` #RHEL 6.x elif [ ${v_release} = 6 ]; then v_cpuusage=`mpstat 1 5 | grep Average | awk '{print $11}'|awk -F "." '{print $1}'` fi; v_loadvaluesfor5min=`cat /proc/loadavg |awk '{print $1}'|awk -F "." '{print $1}'` if [ ${v_loadvaluesfor5min} -ge ${v_loadvaluesfor5min_warning} ] || [ ${v_cpuusage} -le ${v_cpuusage_warning} ] ; then echo "############################" > ${v_path}/top${v_process}_text.log echo "USER: ${v_username}" >> ${v_path}/top${v_process}_text.log echo "HOSTNAME: ${v_hostname}" >> ${v_path}/top${v_process}_text.log echo "IP: ${v_ip}" >> ${v_path}/top${v_process}_text.log echo "Physical CPU: ${v_physicalcpu}" >> ${v_path}/top${v_process}_text.log echo "Logical CPU: ${v_logicalcpu}" >> ${v_path}/top${v_process}_text.log echo "Uptime: "`uptime` >> ${v_path}/top${v_process}_text.log echo "############################" >> ${v_path}/top${v_process}_text.log echo "########mpstat 1 5#########" >> ${v_path}/top${v_process}_text.log mpstat 1 5 >> ${v_path}/top${v_process}_text.log echo "######## ps -ef|grep "LOCAL=NO"|wc -l #########" >> ${v_path}/top${v_process}_text.log ps -ef|grep "LOCAL=NO"|wc -l >> ${v_path}/top${v_process}_text.log echo "######## ps aux | ??? #########" >> ${v_path}/top${v_process}_text.log ps aux|head -1 >> ${v_path}/top${v_process}_text.log ps aux|grep -v PID|sort -rn -k +3|head -${v_process} >> ${v_path}/top${v_process}_text.log echo " " | $MAIL_BIN -f ${v_sendmail} -h smtp.sd.chinamobile.com -s "${v_hostname} Five Minutes Loadavg: ${v_loadvaluesfor5min} ; CPU idle: ${v_cpuusage} - ${v_datetime}" ${v_receivemail} < ${v_path}/top${v_process}_text.log fi; #系统负载监控代码结束 #系统内存监控代码开始 #check memory space v_freememory=`free -m|sed -n '\ : p'|gawk '{print $4}'|head -1` v_freebuffers=`free -m|sed -n '\ : p'|gawk '{print $6}'|head -1` v_freecached=`free -m|sed -n '\ : p'|gawk '{print $7}'|head -1` v_free_buffers_cached_total=`echo "($v_freememory+$v_freebuffers+$v_freecached)" |bc` v_useswap=`free -m|sed -n '\ : p'|tail -1|cut -d ":" -f 2|gawk '{print $2}'` if [ ${v_freememory} -le ${v_freememory_warning} ] || [ ${v_useswap} -ge ${v_useswap_warning} ] || [ ${v_free_buffers_cached_total} -le ${v_free_buffers_cached_warning} ] ; then echo "############################" > ${v_path}/memory_text.log echo "USER: ${v_username}" >> ${v_path}/memory_text.log echo "HOSTNAME: ${v_hostname}" >> ${v_path}/memory_text.log echo "IP: ${v_ip}" >> ${v_path}/memory_text.log echo "############################" >> ${v_path}/memory_text.log free -m >> ${v_path}/memory_text.log echo "########vmstat 1 10#########" >> ${v_path}/memory_text.log vmstat 1 10 >> ${v_path}/memory_text.log $MAIL_BIN -f ${v_sendmail} -h smtp.sd.chinamobile.com -s "${v_hostname} MEMORY SPACE WARNING!!! - ${v_datetime}" ${v_receivemail} < ${v_path}/memory_text.log fi; #系统内存监控代码结束 #系统磁盘监控代码开始 #check disk space v_diskusepercent=`df -P| sed -n '\ / p'|gawk '{print $5}'|sed s/%//|sort -nr|head -1` if [ ${v_diskusepercent} -ge ${v_diskusepercent_warning} ] ; then echo "############################" > ${v_path}/disk_text.log echo "USER: ${v_username}" >> ${v_path}/disk_text.log echo "HOSTNAME: ${v_hostname}" >> ${v_path}/disk_text.log echo "IP: ${v_ip}" >> ${v_path}/disk_text.log echo "############################" >> ${v_path}/disk_text.log df -h >> ${v_path}/disk_text.log $MAIL_BIN -f ${v_sendmail} -h smtp.sd.chinamobile.com -s "${v_hostname} DISK SPACE WARNING!!! - ${v_datetime}" ${v_receivemail} < ${v_path}/disk_text.log fi; #系统磁盘监控代码结束