##shell 主脚本 CheckZabbix.py#!/bin/shdir=`dirname $0`proc_num=`ps -ef|grep zabbix_server |wc -l`net_num=`netstat -lntp|grep zabbix_server |wc -l`#echo $proc_num ' ' $net_numif [ $proc_num -lt 10 -o $net_num -eq 0 ]then echo "Zabbix_Server is down ========================= `date`" if [ `cat $dir/zabbix_server.log` -eq 0 ] then python $dir/python_email.py 'Zabbix_Server is down ,now restart zabbix-server' 'Zabbix_Server down' /etc/init.d/zabbix-server restart echo '1' > $dir/zabbix_server.log fielse echo "Zabbix_Server is UP ===========================`date`" if [ `cat $dir/zabbix_server.log` -eq 1 ] then python $dir/python_email.py 'Zabbix_Server restart Success' 'Zabbix_Server OK' echo '0' > $dir/zabbix_server.log fifi####Python 报警邮件 python_email.py#!/usr/bin/python # -*- coding: utf-8 -*-import sysdef smtp(content,subject): from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import smtplib #加邮件头 msg=MIMEText(content,_charset='utf-8') to_list=['XXXXX@qq.com','XXXXX1@qq.com','XXXXX2@qq.com'] msg['from'] = 'wdzj_script@XXXXX.com' msg['subject'] = subject #发送邮件 try: server = smtplib.SMTP() server.connect('smtp.exmail.qq.com') server.login('XXX@XXX.com','XXX')#XXX@XXX.com为用户名,XXXXX为密码 server.sendmail(msg['from'],to_list,msg.as_string()) server.quit() print '发送成功' except Exception, e: print str(e)if __name__ == '__main__': smtp(sys.argv[1],sys.argv[2]) ##定时任务监控 */10 * * * * /bin/sh /script/shell/CheckZabbix.sh >>/tmp/CheckZabbix.log