117.info
人生若只如初见

Python自动化导出zabbix数据并发邮件脚本

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

import os

import subprocess

import time

Zabbix参数

zabbix_server = ‘192.168.1.100’

zabbix_user = ‘admin’

zabbix_password = ‘zabbix’

zabbix_export_script = ‘/usr/lib/zabbix/export_zabbix_data.py’

zabbix_export_output = ‘/tmp/zabbix_data.csv’

邮件参数

smtp_server = ‘smtp.example.com’

smtp_port = 587

smtp_username = ‘sender@example.com’

smtp_password = ‘password’

email_from = ‘sender@example.com’

email_to = [‘recipient@example.com’]

email_subject = ‘Zabbix Data Export’

email_body = ‘Please see the attached Zabbix data file.’

导出Zabbix数据

subprocess.call([‘python’, zabbix_export_script, zabbix_server, zabbix_user, zabbix_password, zabbix_export_output])

发送邮件

msg = MIMEMultipart()

msg[‘From’] = email_from

msg[‘To’] = ‘,’.join(email_to)

msg[‘Subject’] = email_subject

msg.attach(MIMEText(email_body, ‘plain’))

attachment = open(zabbix_export_output, ‘rb’)

part = MIMEBase(‘application’, ‘octet-stream’)

part.set_payload((attachment).read())

encoders.encode_base64(part)

part.add_header(‘Content-Disposition’, “attachment; filename= %s” % os.path.basename(zabbix_export_output))

msg.attach(part)

server = smtplib.SMTP(smtp_server, smtp_port)

server.starttls()

server.login(smtp_username, smtp_password)

server.sendmail(email_from, email_to, msg.as_string())

server.quit()

print(“Email sent successfully”)

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe5f1AzsLBgFWAFU.html

推荐文章

  • linux的python如何进行进程管理

    在Linux中,可以使用Python的内置库os和subprocess进行进程管理 使用os.system()执行外部命令: import os # 执行外部命令,例如:ls
    os.system("ls") 使用...

  • linux的python怎样进行系统调用

    在Linux中,Python可以通过os和sys库进行系统调用 使用os库: os库提供了许多与操作系统交互的函数。例如,要使用os.system()执行系统命令,可以这样做:
    i...

  • python中set怎样进行集合排序

    在Python中,可以使用sorted()函数对集合(set)进行排序。sorted()函数返回一个排序后的列表(list),而不是集合,因为集合本身是无序的。以下是一个示例:

  • python中set如何进行集合转换

    在Python中,可以使用集合(set)的内置方法进行集合转换。以下是一些常用的集合转换方法: 将列表转换为集合: my_list = [1, 2, 3, 4, 5]
    my_set = set(m...

  • python正则表达式对字符串的查找匹配

    Python的re模块提供了强大的正则表达式操作函数,可以用于对字符串进行查找匹配。
    下面是一个示例代码,演示了如何使用re模块进行字符串的匹配:
    impo...

  • 浅谈Map集合中get不存在的key值,会抛出异常吗

    在Java的Map集合中,如果使用get方法获取一个不存在的key值,不会抛出异常。相反,它会返回null值。
    Map集合是基于键值对的数据结构,每个键都是唯一的。当...

  • JS中prototype的用法实例分析

    在JavaScript中,每个对象都有一个prototype属性,它指向了另一个对象。这个对象被称为原型对象(prototype object),原型对象中包含了一些共享给其他对象的属性...

  • filters.revealTrans.Transition使用方法小结

    filters.revealTrans.Transition使用方法小结: 创建Transition对象:使用var transition = new filters.revealTrans.Transition(element),其中element是要应用...