#!/usr/bin/env python# -*- coding: utf-8 -*-import sysreload (SYS) from email . Mimetext import mimetextimport smtplibsys.setdefaultencoding (' Utf-8 ') import socket, Fcntl, structdef send_mail (to_list,sub,content): mail_host= "smtp.163.com" mail_user= "[ Email protected] " mail_pass=" XXXXXXX " mail_postfix=" 163.com " me=mail_user+" < "+mail_user + "@" +mail_postfix+ ">" msg = mimetext (content) msg[' Subject '] = sub msg[' From '] = me msg[' to '] = to_list try: s = smtplib. SMTP () s.connect (mail_host) s.login (mail_user,mail_pass) s.sendmail (me, To_list, msg.as_string ()) s.close () return true except exception, e: print str (e) return falsedef get_local_ip (ifname = ' Eth0 '): s = soCket.socket (Socket.af_inet, socket. SOCK_DGRAM) inet = fcntl.ioctl (S.fileno (), 0x8915, struct.pack (' 256s ', ifname[ : []]) ret = socket.inet_ntoa (inet[20:24]) return retif sys.argv[1]!= "Master" and sys.argv[1]!= "Backup" and sys.argv[1]!= "Fault": sys.exit () Else: notify_type = sys.argv[1]if __name__ == ' __main__ ': strcontent = get_local_ip () + " " +notify_type+ " state is activated, please make sure the haproxy service running state! " mailto_list = [' [email protected], ]for mailto in mailto_list: Send_mail (mailto, "Haproxy state switch alarm", strcontent.encode (' Utf-8 '))
See South African ant "High-performance Linux server construction"
This article is from the "Li Weizhong Technology Blog" blog, make sure to keep this source http://liweizhong.blog.51cto.com/1383716/1639917
Python Mail Notification Program