1、下載SendEmail;
2、建立監控腳本:
[oracle@db1 scripts]$ cat check_system_warning.sh #!/bin/sh #定義變量 v_datetime=`date +"%Y-%m-%d %H:%M:%S"` v_username='oracle' 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 bond0|grep 'inet addr'|awk '{print $2}'|cut -d: -f2` #發送者郵箱 v_sendmail='' #發送者郵箱用戶名 v_mailname='' #發送者郵箱密碼 v_password='' #smtp服務器IP或域名:smtp.163.com v_smtpserver='113.108.212.80' #接收者郵件,如果是多個用空間分開 v_receivemail=' ' #路徑 v_path="/home/${v_username}/scripts" #設置獲取top進程的數量 v_process=15 #系統負載監控,最近5分鐘負載及Cpu Idle報警閥值 v_loadvaluesfor5min_warning=20 v_cpuusage_warning=35 #剩餘物理內存報警閥值,單位M v_freememory_warning=512 #free+buffers+cached報警閥值,單位M v_free_buffers_cached_warning=2000 #使用swap分區報警閥值,單位M v_useswap_warning=1000 #磁盤使用百分比報警閥值 v_diskusepercent_warning=90 #系統負載監控代碼開始 #check sys load and cpuidle #v_release=`lsb_release -a|head -4|tail -1|awk '{print $2}'|awk -F '.' '{print $1}'` v_release=6 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 " " | ${v_path}/bsmtp -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} Five Minutes Loadavg: ${v_loadvaluesfor5min} ; CPU idle: ${v_cpuusage} - ${v_datetime}" ${v_receivemail} < ${v_path}/top${v_process}_text.log ${v_path}/sendEmail -f ${v_sendmail} -t ${v_receivemail} -s ${v_smtpserver} -u "${v_hostname} Five Minutes Loadavg: ${v_loadvaluesfor5min} ; CPU idle: ${v_cpuusage} - ${v_datetime}" -xu ${v_mailname} -xp ${v_password} -o message-content-type=text -o message-charset=UTF-8 message-file=${v_path}/top${v_process}_text.log -l ${v_path}/system_warning_sendmail.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 #####${v_path}/bsmtp -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} MEMORY SPACE WARNING! - ${v_datetime}" ${v_receivemail} < ${v_path}/memory_text.log ${v_path}/sendEmail -f ${v_sendmail} -t ${v_receivemail} -s ${v_smtpserver} -u "${v_hostname} MEMORY SPACE WARNING - ${v_datetime}" -xu ${v_mailname} -xp ${v_password} -o message-content-type=text -o message-charset=UTF-8 message-file=${v_path}/memory_text.log -l ${v_path}/system_warning_sendmail.log fi; #系統內存監控代碼結束 #系統磁盤監控代碼開始 #check disk space v_diskusepercent=`df -P|column -t| 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 #####${v_path}/bsmtp -f ${v_sendmail} -h smtp.api.localdomain -s "${v_hostname} DISK SPACE WARNING!!! - ${v_datetime}" ${v_receivemail} < ${v_path}/disk_text.log ${v_path}/sendEmail -f ${v_sendmail} -t ${v_receivemail} -s ${v_smtpserver} -u "${v_hostname} DISK SPACE WARNING - ${v_datetime}" -xu ${v_mailname} -xp ${v_password} -o message-content-type=text -o message-charset=UTF-8 message-file=${v_path}/disk_text.log -l ${v_path}/system_warning_sendmail.log fi; #系統磁盤監控代碼結束
3、配置crontab,每兩分鐘運行一次
[oracle@db1 scripts]$ crontab -l #os check */2 * * * * /home/oracle/scripts/check_system_warning.sh >/dev/null 2>&1