[Linux] linux服务器主机性能、空间监控脚本
2014-04-24 16:12
417 查看
1、下载SendEmail;
2、建立监控脚本:
3、配置crontab,每两分钟运行一次
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
相关文章推荐
- 在nagios中使用python脚本监控linux主机
- Linux下监控磁盘的空闲空间的shell脚本
- 【找一找青春】Linux 性能监控之CPU&内存&IO监控Shell脚本2[转]
- linux服务器历险之linux性能监控
- shell 脚本监控Linux 性能
- linux 性能测试监控shell脚本
- zabbix 监控linux主机获取脚本值
- linux实战(二)----shell脚本监控实例-----日志截取三个命令在性能监控等运用中的实例解析
- 使用linux系统性能监控工具KSysguard监控远端主机介绍
- shell脚本——linux主机监控
- linux实战(二)----shell脚本监控实例-----日志截取三个命令在性能监控等运用中的实例解析
- Linux的shell脚本打印图形和主机监控脚本练习
- Cacti监控Linux主机 脚本
- linux下监控磁盘空间脚本
- Linux系统性能统计工具Sar和实时系统性能监控脚本
- centos nagios监控linux主机监控内存脚本
- nagios监控linux主机监控内存脚本
- shell监控主机性能脚本
- Linux监控系统CPU、内存、磁盘空间和网口收发脚本
- 几个常用的Linux系统性能监控shell脚本