python

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

小云
101
2023-08-17 12:43:11
栏目: 编程语言

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”)

0
看了该问题的人还看了