服务监控的方案有不少,譬如naigos,zabbix这种,不但能够监控服务,还能够监控cpu、内存、磁盘、网络流量、服务端口等,关于naigos和zabbix的搭建配置,须要另外篇幅介绍,这里使用服务器自身的定时任务+脚本+邮件功能完成一个简单的监控。node
安装mailxmysql
yum -y install mailx
redis
############################
##qq我的邮箱配置
############################sql
vim /etc/mail.rc
添加以下配置:mongodb
set from=xxxxxx@qq.com set smtp=smtps://smtp.qq.com:465 set smtp-auth-user=xxxxxx@qq.com set smtp-auth-password=你的 QQ 邮箱受权码 (登陆qq邮箱到帐户设置中,打开smtp服务时,提示的验证码,此验证码非密码。) set smtp-auth=login #set smtp-use-starttls 这里是不须要配置的,不少地方没说明,配置了反而会验证失败,因此我注释掉; set ssl-verify=ignore set nss-config-dir=/root/.certs
##建立证书vim
mkdir -p /root/.certs/ cd /root/.certs/ echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt certutil -L -d /root/.certs ##认证 certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
#返回以下提示便可:
Notice: Trust flag u is set automatically if the private key is present.centos
#发送主题为“邮箱测试”,内容为当前目录下 message_fiel.txt 文件内容到 xxxx@qq.com 邮箱。
mailx -s "邮箱测试" xxxx@qq.com < message_file.txtbash
############################
##qq企业邮箱配置
############################服务器
vim /etc/mail.rc
添加以下配置:网络
set from=noreply@xxx.com set smtp=smtps://smtp.exmail.qq.com:465 set smtp-auth-user=noreply@xxx.com set smtp-auth-password=*****(登陆密码,不一样于我的邮箱的受权码) set smtp-auth=login set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb/
cd /etc/pki/nssdb/
#生成证书
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /etc/pki/nssdb/qq.crt certutil -A -n "GeoTrust SSL CA" -t "C,," -d /etc/pki/nssdb/ -i /etc/pki/nssdb/qq.crt certutil -A -n "GeoTrust Global CA" -t "C,," -d /etc/pki/nssdb/ -i /etc/pki/nssdb/qq.crt certutil -L -d /etc/pki/nssdb/ certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt #认证
一样,认证完会返回以下提示:
Notice: Trust flag u is set automatically if the private key is present.
##测试echo "this email come from centos 172.36.27.71"|mail -v -s "mysql check test" aaai@xxx.com
大体想法:mysql监控脚本分别运行在两个实例上,若是当前实例宕机,则重启本机mysql服务,若是其余服务器上的mysql链接不上则邮件通知。
#!/bin/bash notify_addr='aaa@xxx.com,393369540@qq.com' error_log="/opt/script/logs/check_mysql.err" ###定义一个简单判断mysql是否可用的函数 function excute_query { echo -e "`date "+%F %H:%M:%S"` -----checking mysql instance $1 by querying -----" >> ${error_log} /usr/local/mysql/bin/mysql -uroot -pPASWD -h $1 --port 30468 -e "select 1;" 2>> ${error_log} } ###定义没法执行查询,且mysql服务异常时的处理函数 function service_error { echo -e "`date "+%F %H:%M:%S"` -----mysql service error,notify manager now-----" >> ${error_log} systemctl restart mysql.service echo "$1 没法链接并被重启"|mail -s "MYSQL $1 实例正在被重启, 请及时登陆查看状态!" ${notify_addr} 2>> ${error_log} echo -e "\n---------------------------------------------------------\n" >> ${error_log} } ###定义没法执行查询,但mysql服务正常的处理函数 function query_error { echo -e "`date "+%F %H:%M:%S"` -----mysql instance $1 query error, retry after 30s-----" >> ${error_log} sleep 30 excute_query $1 if [ $? -ne 0 ];then echo -e "`date "+%F %H:%M:%S"` -----mysql instance $1 still can't execute query-----" >> ${error_log} echo "mysql isntance $1 is down"|mail -s "MYSQL $1 没法链接查询, 请及时处理!from(172.36.27.70)" ${notify_addr} 2>> ${error_log} else echo -e "`date "+%F %H:%M:%S"` -----mysql instance $1 query ok after 10s-----" >> ${error_log} echo -e "\n---------------------------------------------------------\n" >> ${error_log} fi } ###监控本机mysql状态 excute_query 172.36.27.70 if [ $? -ne 0 ];then systemctl status mysql.service &>/dev/null if [ $? -ne 0 ];then service_error 172.36.27.70 else query_error 172.36.27.70 fi else echo -e "\n-----------mysql instance 172.36.27.70 is ok for query-------------\n" >> ${error_log} fi ###监控备机mysql状态 excute_query 172.36.27.71 if [ $? -ne 0 ];then query_error 172.36.27.71 else echo -e "\n-----------mysql instance 172.36.27.71 is ok for query-------------\n" >> ${error_log} fi
大体思想:经过mongo命令登陆或者mongostat判断节点是否正常运行。
notify_addr='aaa@xxx.com,393369540@qq.com' error_log="/opt/script/logs/check_mongo.err" ###定义一个简单判断mysql是否可用的函数 function connect_db { echo -e "`date "+%F %H:%M:%S"` -----checking mongo instance $1 by login -----" >> ${error_log} echo "db.serverStatus().mem" | /usr/local/mongodb/bin/mongo admin -uroot -pPWD --host $1 --port 20467 2>> ${error_log} } function replication_stat_query { echo -e "`date "+%F %H:%M:%S"` -----checking mongo instance $1 by mongostat -----" >> ${error_log} /usr/local/mongodb/bin/mongostat --uri=mongodb://suroot:PWD@$1:20467/admin 2>> ${error_log} } ###定义没法执行查询,且mysql服务异常时的处理函数 function service_error { echo -e "`date "+%F %H:%M:%S"` -----mongo service $1 error,notify manager now-----" >> ${error_log} ##/usr/local/mongo/bin/mongod -f /etc/mongo.conf --shutdown echo "$1 mongo链接失败,请及时处理"|mail -s "Mongo $1 实例没法链接, 请及时登陆处理!from(172.26.27.70)" ${notify_addr} 2>> ${error_log} echo -e "\n---------------------------------------------------------\n" >> ${error_log} } ###监控本机mongo node 状态 function monitor_node { connect_db $1 if [ $? -ne 0 ];then service_error $1 #else #replication_stat_query $1 #if [ $? -ne 0 ];then #service_error $1 #else #echo -e "\n-----------mongostat of node $1 is ok! -------------\n" >> ${error_log} echo -e "\n-----------mongo connection to node $1 is ok! -------------\n" >> ${error_log} #fi fi } ###监控本机mongo node 状态 monitor_node 172.36.27.70 monitor_node 172.36.27.71 monitor_node 172.36.27.72
大体思想: 经过redis-cli登陆并检索clusterinfo是否enable来判断该节点及集群是否正常工做。
#!/bin/bash notify_addr='aaa@xxx.com,393369540@qq.com' error_log="/opt/script/logs/check_redis.err" ###定义没法执行查询,且mysql服务异常时的处理函数 function service_error { echo -e "`date "+%F %H:%M:%S"` -----redis service $1:$2 error,notify manager now-----" >> ${error_log} ##/usr/local/mongo/bin/mongod -f /etc/mongo.conf --shutdown echo "$1 redis链接异常,请及时处理"|mail -s "Redis $1:$2 节点链接失败, 请及时登陆处理!(from 172.36.27.70)" ${notify_addr} 2>> ${error_log} echo -e "\n---------------------------------------------------------\n" >> ${error_log} } ###监控redis 状态 function monitor_node { echo -e "`date "+%F %H:%M:%S"` -----checking mongo redis $1:$2 by cli -----" >> ${error_log} /usr/local/bin/redis-cli -h $1 -p $2 -a PWD info |grep cluster_enabled if [ $? -ne 0 ];then service_error $1 $2 echo -e "\n-----------redis connection to node $1:$2 is ok! -------------\n" >> ${error_log} fi } ###监控本机mongo node 状态 monitor_node 172.36.27.70 6239 monitor_node 172.36.27.70 6339 monitor_node 172.36.27.71 6239 monitor_node 172.36.27.71 6339 monitor_node 172.36.27.72 6239 monitor_node 172.36.27.72 6339
经过rabbitmqctl查看集群状态或者节点状态
#!/bin/bash notify_addr='aaa@xxx.com,393369540@qq.com' error_log="/opt/script/logs/check_redis.err" ###定义没法执行查询,且mysql服务异常时的处理函数 function service_error { echo -e "`date "+%F %H:%M:%S"` -----rabbitmq service error,notify manager now-----" >> ${error_log} #ps -ef | grep ^rabbitmq | awk '{print $2}' | xargs kill -9 #service rabbitmq-server start echo "$1 rabbitmq服务异常, 请及时处理"|mail -s " $1 RabbitMQ服务异常, 请及时登陆处理!(from $2)" ${notify_addr} 2>> ${error_log} echo -e "\n---------------------------------------------------------\n" >> ${error_log} } ###监控rabbitmq 状态 function monitor_node { echo -e "`date "+%F %H:%M:%S"` -----checking mongo redis $1:$2 by cli -----" >> ${error_log} #/usr/lib/rabbitmq/bin/rabbitmqctl cluster_status |grep cluster_name /usr/sbin/rabbitmqctl cluster_status | grep cluster_name if [ $? -ne 0 ];then service_error $1 $2 echo -e "\n-----------redis connection to node $1:$2 is ok! -------------\n" >> ${error_log} fi } monitor_node 172.36.27.72 172.36.27.72
crontab -e */1 * * * * /opt/script/check_mysql.sh > /opt/script/logs/cron_result.log 2>&1 */3 * * * * /opt/script/check_mongo.sh > /opt/script/logs/cron_result.log 2>&1 */5 * * * * /opt/script/check_redis.sh > /opt/script/logs/cron_result.log 2>&1 */5 * * * * /opt/script/check_rabbitmq.sh > /opt/script/logs/cron_result.log 2>&1