1、短信接口文档:
URL
http://xxx.com/interfaces/sendMsg.htm
Method
POST
Description
文字短信调用接口
Request Param List
Name | Identifier | Type | Remark |
---|---|---|---|
短信签名 | sign | string | 格式为 【某某平台】 中文括号不可少 |
信息内容 | msg | string | |
手机号 | phoneNumber | string |
Response Param List
Name | Identifier | Type | Remark |
---|---|---|---|
返回值 | string | 提交失败 {"msg":"","success":"F"} 提交成功 {"msg":"","success":"T"} |
2、自定义端口post方式以及python脚本:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#Author:lzd
import sys
import urllib.request
import urllib.parse
#请求
msg = sys.argv[2]
phoneNumber = sys.argv[1]
data = {
'msg':msg,
'phoneNumber':phoneNumber,
}
data = urllib.parse.urlencode(data).encode('utf-8')
resu = urllib.request.urlopen('http://xxx.com/interfaces/sendVoice.htm', data = data)
#指定编码请求
#print(resu.read().decode('utf-8'))
a=open('/tmp/abc.log','a')
a.write(sys.argv[1]+resu.read().decode('utf-8')+'\n' )
a.close()
3、将sms2.py拷贝到路径/usr/lib/zabbix/alertscripts
4、设置sms2.py的权限为chmod 777 sms2.py
把日志文件abc.log也要chmod 777 abc.log
5、注意sms2.py在centos一定要可以直接执行,而不是python sms2.py这种方式直接。后面加两个参数手机号码+内容
6、设置zabbix里的triggers
7、设置media type,注意这里的三个变量一定要加进去。
8、最后设置动作:actions
当triggers名称为,或者安全级别为什么的时候就触发动作,直接发送短信,选择对应的人和动作!
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_host = 'smtp.189.cn' mail_user = '13913913913' mail_pass = 'xxxxx' mail_postfix = '189.cn' def send_mail(to_list,subject,content): me = "zabbix 监控告警平台"+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = subject 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 as e: print (e) return False #if __name__ == "__main__": send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
SSL邮件发送python代码:
#!/usr/bin/env python #-*- coding:utf-8 -*- #Author:lzd import smtplib from email.mime.text import MIMEText from email.utils import formatdate from email.header import Header import sys # 设置默认字符集为UTF8 不然有些时候转码会出问题 default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) # 发送邮件的相关信息,根据你实际情况填写 smtpHost = 'smtp.exmail.qq.com' smtpPort = '25' sslPort = '465' fromMail = 'xxx@17of.com' # toMail = '997939@qq.com' username = 'xxx@17of.com' password = 'xxxxx' def send_mail(to_list, subject, content): # 邮件标题和内容 # subject = u'[Notice]hello' # body = u'hello,this is a mail from ' + fromMail # 初始化邮件 encoding = 'utf-8' mail = MIMEText(content.encode(encoding), 'plain', encoding) mail['Subject'] = Header(subject, encoding) mail['From'] = fromMail mail['To'] = to_list mail['Date'] = formatdate() try: # 连接smtp服务器,明文/SSL/TLS三种方式,根据你使用的SMTP支持情况选择一种 # 普通方式,通信过程不加密 # smtp = smtplib.SMTP(smtpHost, smtpPort) # smtp.ehlo() # smtp.login(username, password) # tls加密方式,通信过程加密,邮件数据安全,使用正常的smtp端口 # smtp = smtplib.SMTP(smtpHost,smtpPort) # smtp.set_debuglevel(True) # smtp.ehlo() # smtp.starttls() # smtp.ehlo() # smtp.login(username,password) # 纯粹的ssl加密方式,通信过程加密,邮件数据安全 smtp = smtplib.SMTP_SSL(smtpHost,sslPort) smtp.ehlo() smtp.login(username,password) # 发送邮件 smtp.sendmail(fromMail, to_list, mail.as_string()) smtp.close() print("OK") except Exception as e: print("e") send_mail(sys.argv[1], sys.argv[2], sys.argv[3])