CentOS下自动发邮件检测某进程是否存在,centos发邮件1.检查sendma


目的:利用shell脚本每小时检测数据库是否在运行,当检测到库宕掉时发邮件告警。

1.检查sendmail是否在运行

service sendmail status

没有在运行则启动或安装。

如果是linux 6,则检查postfix是否在运行 servicepostfix status

2.pmon是oracle五大关键进程之一,如果pmon进程不存在则库一定是关闭了,下面就用脚本检测pmon是否存在。

脚本/root/check.sh如下:

#!/bin/bash


source .bash_profile


i=`ps -ef | grep pmon | grep -v grep | wc -l`


if [ $i -lt 1 ]


then


text='数据库故障,pmon进程不存在'


echo "$text" | mail -s "192.168.1.100 alarm" 第一个邮箱地址,第二个邮箱地址


fi

可以同时给多人发邮件,邮箱之间用英文逗号隔开。推荐使用139邮箱,这样告警就自动发到手机上了。以上脚本中本来要写两个邮箱地址的,但本文档保存后,邮箱地址就自动给删除了。另外,脚本中信息尽量用英文,因为有些邮箱显示中文时有乱码。

3.利用crontab每小时执行一次脚本

crontab -e

0 * * * * /root/check.sh

附其他检测脚本:

用ping检测主机是否宕机

#!/bin/bash


source .bash_profile


ping=`ping -c 3 192.168.100.5|awk 'NR==7 {print $4}'`


if [ $ping -eq 0 ]


then


echo "network is timeout"


else


echo "network is ok"


fi

#检测cpu利用率

top -b -n 1 | grep Cpu | awk '{print $2}'| cut -f 1 -d "%"

#检测cpu空闲率

top -b -n 1 | grep Cpu | awk -F, '{print $4}'| cut -f 1 -d "%"

检测负载

uptime | awk '{print $10}' | cut -f 1 -d ","

#检测硬盘空间使用率

df -Th | sed '1,2d' | sed '2,4d'| awk '{print $5}' | cut -f 1 -d "%"

相关内容

    暂无相关文章