Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入

#!/usr/bin/env python
#encoding:utf8
#
# Zabbix Server 发送邮件脚本
# from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import sys from_addr = 'shixunwuxian@163.com'
password = 'xxxxxx'
smtp_server = 'smtp.163.com' # 报警邮件收件人
to_addr = sys.argv[1] def _format_addr(s):
name, addr = parseaddr(s)
return formataddr(( \
Header(name, 'utf-8').encode(), \
addr.encode('utf-8') if isinstance(addr, unicode) else addr)) def ZabbixInfo():
res = ""
itemList = sys.argv
for index in range(2, len(itemList)):
res = res + itemList[index]
return res def sendMsg():
bodyInfo = ZabbixInfo()
headerInfo = str(bodyInfo.split('<*>')[1])
fromInfo = u"Zabbix监控" msg = MIMEText(bodyInfo , 'plain', 'utf-8')
msg['From'] = _format_addr(fromInfo + "<%s>" % from_addr)
msg['To'] = _format_addr(fromInfo + "<%s>" % to_addr)
msg['Subject'] = Header(headerInfo, 'utf-8').encode() return msg.as_string() def main():
server = smtplib.SMTP(smtp_server, 25)
#server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], sendMsg())
server.quit() if __name__ == "__main__":
main()

脚本中的163邮箱的密码不是登陆密码,是pop3方式的专用密码

下面是日志图片,使用的zabbix版本比较老,保证运行稳定,少BUG

Zabbix调用外部脚本发送邮件:python编写脚本的更多相关文章

  1. 使用 Python 编写脚本并发布

    使用 Python 编写脚本并发布 P1: 脚本 通常在 Linux 服务器上会遇到在命令行中输入命令的操作,而有些操作包含的命令数目较多或者其中的命令包含的参数较多,如果一个一个的敲命令的话就太麻烦 ...

  2. python编写脚本应用实例

    这里主要记录工作中应用python编写脚本的实例.由于shell脚本操作数据库(增.删.改.查)并不是十分直观方便,故这里采用python监控mysql状态,然后将状态保存到数据库中,供前台页面进行调 ...

  3. 【MonkeyRunner】[技术博客]用python编写脚本查看设备信息

    [MonkeyRunner]用python编写脚本查看设备信息 原以为是个简单的操作,在实践的时候发现了一些问题. python脚本 test.py: from com.android.monkeyr ...

  4. python编写脚本,删除固定用户下的所有表

    脚本如下: [oracle@ycr python]$ more t_del.py #/usr/bin/python#coding:utf8 import sysimport cx_Oracle i=0 ...

  5. python编写脚本,登录Github通过指定仓库指定敏感关键字搜索自动化截图生成文件【完美截图】

    前言:为了避免开发人员将敏感信息写入文件传到github,所以测试人员需要检查每个仓库是否有写入,人工搜索审核比较繁琐,所以写一个脚本通过配置 配置文件,指定需要搜索的仓库和每个仓库需要搜索的关键字, ...

  6. Python编写脚本(输出三星形状的‘*’符号)

    环境:python3.* 心得:个人认为脚本非我强项,以下效果可以有更简单解决方案,纯属练习逻辑. 方案一: s=1 while s<=10: #这是决定多少列,起始为1,大循环一圈即加一,就是 ...

  7. python编写脚本

    #!/usr/bin/env python #-*- coding:utf-8 -*- import sys import os from subprocess import Popen,PIPE c ...

  8. python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

    0.关键实现:程序窗口前置 python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作 1.参考 aut ...

  9. 如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样)

    如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样) python标准库中的subprocess可以解决这个问题. from subprocess ...

随机推荐

  1. Ubuntu的奇技淫巧

    sudo apt-get install cmatrix 输入密码,安装后,按F11把terminal全屏,输入cmatrix -b sudo apt-get install sl 安装后执行sl,屏 ...

  2. share团队冲刺1

    团队冲刺第一天 今天:网上学习Android软件开发的知识,完成简单的操作,实现简单功能. 问题:无

  3. dfs--迷宫

    题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...

  4. ZJNU 1372 - 破解情书

    取模运算在数组内循环解密,否则会MLE /* Written By StelaYuri */ #include<stdio.h> ],cn[]; int main() { int i,j, ...

  5. 洛谷 P3371 【模板】单源最短路径(弱化版)(dijkstra邻接链表)

    题目传送门 解题思路: 传送门 AC代码: #include<iostream> #include<cstdio> #include<cstring> using ...

  6. 封装FTP类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  7. DevComponents.DotNetBar2.dll设置样式的使用

    有点模仿QQ消息盒子的感觉,代码如下: using System; using System.Collections.Generic; using System.ComponentModel; usi ...

  8. 893B. Beautiful Divisors#美丽的因子(打表法)

    题目出处:http://codeforces.com/problemset/problem/893/B 题目大意:找到一个数在二进制下,最大的以k个连续的1和k-1个连续的0组成的数字作为因子 #in ...

  9. IMX6开发板Qtopia2.2.0开发环境搭建以及编译镜像

    搭建 Qtopia2.2.0 开发环境,需要先搭建 Android 的编译环境,然后在 Android 编译环境的基础上,再搭建 Qtopia2.2.0 编译环境.以下内容基于迅为-iMX6开发板.Q ...

  10. Android drawable 加载效果

    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:ones ...