软件功能很简单 当你的主机不在线
[color=]会自动发送邮件告警信息到你的邮箱上
因为我想了解我的网络情况 在论坛上又找到该类似的软件
部署监控平台又麻烦 干脆自己弄一个来检测网络情况
蓝奏云下载链接:https://azmin.lanzouw.com/i9wqV23kkoje密码:1g5h
源代码:
[color=]import
smtplib
[color=]from
email.mime.text
[color=]import
MIMEText
[color=]from
email.mime.multipart
[color=]import
MIMEMultipart
[color=]from
ping3
[color=]import
ping
[color=]import
time
[color=]from
datetime
[color=]import
datetime
[color=]import
threading
[color=]import
tkinter
[color=]as
tk
[color=]from
tkinter
[color=]import
messagebox, filedialog
[color=]import
json
[color=]def
[color=]send_email
(subject, body, to_emails, from_email, smtp_server, smtp_port, smtp_user, smtp_password):
msg = MIMEMultipart()
msg[
[color=]'From'
] = from_email
msg[
[color=]'To'
] =
[color=]", "
.join(to_emails)
msg[
[color=]'Subject'
] = subject
msg.attach(MIMEText(body,
[color=]'plain'
,
[color=]'utf-8'
))
[color=]try
:
[color=]print
(
[color=]"
[color=]连接到
[color=]SMTP
[color=]服务器
[color=]..."
)
server = smtplib.SMTP_SSL(smtp_server, smtp_port)
server.login(smtp_user, smtp_password)
[color=]print
(
[color=]"
[color=]发送邮件
[color=]..."
)
text = msg.as_string()
server.sendmail(from_email, to_emails, text)
server.quit()
[color=]print
(
[color=]"
[color=]邮件发送成功。
[color=]"
)
[color=]except
[color=]Exception
[color=]as
e:
[color=]print
(
[color=]f"
[color=]发送邮件失败
[color=]:
[color=]{
e
[color=]}
[color=]"
)
messagebox.showerror(
[color=]"
[color=]邮件发送失败
[color=]"
,
[color=]f"
[color=]发送邮件失败
[color=]:
[color=]{
e
[color=]}
[color=]"
)
[color=]def
[color=]monitor_ping
(hosts, timeout, check_interval, email_details):
host_status = {host[
[color=]'address'
]:
[color=]True for
host
[color=]in
hosts}
[color=]while True
:
[color=]for
host
[color=]in
hosts:
address = host[
[color=]'address'
]
remark = host[
[color=]'remark'
]
[color=]try
:
[color=]print
(
[color=]f"
[color=]正在检测主机
[color=](
[color=]{
remark
[color=]}
[color=])..."
)
response = ping(address,
[color=]timeout
=timeout)
[color=]if
response
[color=]is None
:
[color=]if
host_status[address]:
[color=]print
(
[color=]f"
[color=]主机
[color=](
[color=]{
remark
[color=]}
[color=])
[color=]断开,发送告警邮件
[color=]..."
)
subject =
[color=]f"
[color=]告警
[color=]: (
[color=]{
remark
[color=]}
[color=])
[color=]已断开
"
[color=]
body =
[color=]f"
[color=]主机
[color=]{
address
[color=]}
[color=] (
[color=]{
remark
[color=]}
[color=])
[color=]已断开连接
[color=]\n
[color=]断网时间为
[color=]:
[color=]{
datetime.now().strftime(
[color=]'%Y-%m-%d %H:%M:%S'
)
[color=]}
"
[color=]
send_email(subject, body, **email_details)
host_status[address] = False
[color=] else
:
[color=]if not
host_status[address]:
[color=]print
(
[color=]f"
[color=]主机
[color=](
[color=]{
remark
[color=]}
[color=])
[color=]已恢复,发送恢复邮件
[color=]..."
)
subject =
[color=]f"
[color=]恢复
[color=]: (
[color=]{
remark
[color=]}
[color=])
[color=]已恢复
"
[color=]
body =
[color=]f"
[color=]主机
[color=]{
address
[color=]}
[color=] (
[color=]{
remark
[color=]}
[color=])
[color=]已恢复响应连接
[color=]\n
[color=]恢复时间为
[color=]:
[color=]{
datetime.now().strftime(
[color=]'%Y-%m-%d %H:%M:%S'
)
[color=]}
"
[color=]
send_email(subject, body, **email_details)
host_status[address] = True
[color=]
[color=]print
(
[color=]f"
[color=]主机
[color=](
[color=]{
remark
[color=]}
[color=])
[color=]可达,响应时间
[color=]:
[color=]{
response
[color=]}
[color=] ms"
)
[color=]except
[color=]Exception
[color=]as
e:
[color=]print
(
[color=]f"ping
[color=]过程中出错
[color=]:
[color=]{
e
[color=]}
[color=]"
)
messagebox.showerror(
[color=]"ping
[color=]出错
[color=]"
,
[color=]f"ping
[color=]过程中出错
[color=]:
[color=]{
e
[color=]}
[color=]"
)
time.sleep(check_interval)
[color=]def
[color=]start_monitoring
():
[color=]try
:
to_emails = [email.strip()
[color=]for
[color=]in
to_email_entry.get().split(
[color=]","
)
[color=]if
email.strip()]
email_details = {
[color=]'to_emails'
: to_emails,
[color=]'from_email'
: from_email_entry.get(),
[color=]'smtp_server'
: smtp_server_entry.get(),
[color=]'smtp_port'
:
[color=]int
(smtp_port_entry.get()),
[color=]'smtp_user'
: smtp_user_entry.get(),
[color=]'smtp_password'
: smtp_password_entry.get()
}
hosts = []
[color=]for
i
[color=]in
[color=]range
(
[color=]len
(host_entries)):
[color=]if
host_entries:
address = host_entries.get()
remark = remark_entries.get()
[color=]if
address
[color=]and
remark:
hosts.append({
[color=]'address'
: address,
[color=]'remark'
: remark})
timeout =
[color=]int
(timeout_entry.get())
check_interval =
[color=]int
(check_interval_entry.get())
[color=]if not
email_details[
[color=]'to_emails'
]
[color=]or not
email_details[
[color=]'from_email'
]
[color=]or not
email_details[
[color=]'smtp_server'
]
[color=]or not
email_details[
[color=]'smtp_user'
]
[color=]or not
email_details[
[color=]'smtp_password'
]:
[color=]raise
[color=]ValueError
(
[color=]"
[color=]所有邮件配置项必须填写完整
[color=]"
)
[color=]if not
hosts:
[color=]raise
[color=]ValueError
(
[color=]"
[color=]至少添加一个主机
[color=]"
)
[color=]print
(
[color=]f"
[color=]开始监控,主机列表
[color=]:
[color=]{
hosts
[color=]}
[color=],
[color=]超时时间
[color=]:
[color=]{
timeout
[color=]}
[color=],
[color=]间隔时间
[color=]:
[color=]{
check_interval
[color=]}
[color=]"
)
[color=]print
(
[color=]f"
[color=]邮件配置
[color=]:
[color=]{
email_details
[color=]}
[color=]"
)
monitoring_thread = threading.Thread(
[color=]target
=monitor_ping,
[color=]args
=(hosts, timeout, check_interval, email_details))
monitoring_thread.daemon = True
[color=]
monitoring_thread.start()
[color=]except
[color=]ValueError
[color=]as
ve:
messagebox.showerror(
[color=]"
[color=]输入错误
[color=]"
,
[color=]str
(ve))
[color=]except
[color=]Exception
[color=]as
e:
messagebox.showerror(
[color=]"
[color=]未知错误
[color=]"
,
[color=]str
(e))
[color=]def
[color=]add_host_entry
():
row =
[color=]len
(host_entries) + 8
[color=]
host_label = tk.Label(inner_frame,
[color=]text
=
[color=]f"
[color=]主机
[color=]{
[color=]len
(host_entries) +
[color=]1
[color=]}
[color=]:"
)
host_label.grid(
[color=]row
=row,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
host_entry = tk.Entry(inner_frame)
host_entry.grid(
[color=]row
=row,
[color=]column
=
[color=]1
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
host_entries.append(host_entry)
remark_entry = tk.Entry(inner_frame)
remark_entry.grid(
[color=]row
=row,
[color=]column
=
[color=]2
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
remark_entries.append(remark_entry)
delete_button = tk.Button(inner_frame,
[color=]text
=
[color=]"
[color=]删除
[color=]"
,
[color=]command
=
[color=]lambda
row=row: delete_host_entry(row))
delete_button.grid(
[color=]row
=row,
[color=]column
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
delete_buttons.append(delete_button)
update_layout()
[color=]def
[color=]delete_host_entry
(row):
index = row - 8
[color=]
[color=]if
[color=]0
销毁主机输入框
[color=]
remark_entries[index].grid_forget()
remark_entries[index].destroy()
[color=]#
销毁备注输入框
[color=]
delete_buttons[index].grid_forget()
delete_buttons[index].destroy()
[color=]#
销毁删除按钮
[color=]
[color=]#
重建列表,剔除已经销毁的项
[color=]
host_entries[:] = [entry
[color=]for
entry
[color=]in
host_entries
[color=]if
entry.winfo_exists()]
remark_entries[:] = [entry
[color=]for
entry
[color=]in
remark_entries
[color=]if
entry.winfo_exists()]
delete_buttons[:] = [button
[color=]for
button
[color=]in
delete_buttons
[color=]if
button.winfo_exists()]
update_layout()
[color=]def
[color=]update_layout
():
[color=]for
i
[color=]in
[color=]range
(
[color=]len
(host_entries)):
host_entries.grid(
[color=]row
=i +
[color=]8
,
[color=]column
=
[color=]1
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
remark_entries.grid(
[color=]row
=i +
[color=]8
,
[color=]column
=
[color=]2
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
delete_buttons.grid(
[color=]row
=i +
[color=]8
,
[color=]column
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
add_host_entry_button.grid(
[color=]row
=
[color=]len
(host_entries) +
[color=]8
,
[color=]column
=
[color=]0
,
[color=]columnspan
=
[color=]4
,
[color=]pady
=
[color=]5
)
start_button.grid(
[color=]row
=
[color=]len
(host_entries) +
[color=]9
,
[color=]column
=
[color=]0
,
[color=]columnspan
=
[color=]4
,
[color=]pady
=
[color=]5
)
save_button.grid(
[color=]row
=
[color=]len
(host_entries) +
[color=]10
,
[color=]column
=
[color=]0
,
[color=]columnspan
=
[color=]2
,
[color=]pady
=
[color=]5
)
load_button.grid(
[color=]row
=
[color=]len
(host_entries) +
[color=]10
,
[color=]column
=
[color=]2
,
[color=]columnspan
=
[color=]2
,
[color=]pady
=
[color=]5
)
canvas.config(
[color=]scrollregion
=canvas.bbox(
[color=]"all"
))
[color=]def
[color=]save_config
():
config = {
[color=]'to_email'
: to_email_entry.get(),
[color=]'from_email'
: from_email_entry.get(),
[color=]'smtp_server'
: smtp_server_entry.get(),
[color=]'smtp_port'
: smtp_port_entry.get(),
[color=]'smtp_user'
: smtp_user_entry.get(),
[color=]'smtp_password'
: smtp_password_entry.get(),
[color=]'timeout'
: timeout_entry.get(),
[color=]'check_interval'
: check_interval_entry.get(),
[color=]'hosts'
: [{
[color=]'address'
: host_entries.get(),
[color=]'remark'
: remark_entries.get()}
[color=]for
i
[color=]in
[color=]range
(
[color=]len
(host_entries))
[color=]if
host_entries.winfo_exists()]
}
[color=]with
[color=]open
(
[color=]'config.json'
,
[color=]'w'
,
[color=]encoding
=
[color=]'utf-8'
)
[color=]as
config_file:
json.dump(config, config_file,
[color=]ensure_ascii
=
[color=]False
,
[color=]indent
=
[color=]4
)
messagebox.showinfo(
[color=]"
[color=]保存配置
[color=]"
,
[color=]"
[color=]配置已保存成功
[color=]"
)
[color=]def
[color=]load_config
():
[color=]try
:
[color=]with
[color=]open
(
[color=]'config.json'
,
[color=]'r'
,
[color=]encoding
=
[color=]'utf-8'
)
[color=]as
config_file:
config = json.load(config_file)
to_email_entry.delete(
[color=]0
, tk.END)
to_email_entry.insert(
[color=]0
, config[
[color=]'to_email'
])
from_email_entry.delete(
[color=]0
, tk.END)
from_email_entry.insert(
[color=]0
, config[
[color=]'from_email'
])
smtp_server_entry.delete(
[color=]0
, tk.END)
smtp_server_entry.insert(
[color=]0
, config[
[color=]'smtp_server'
])
smtp_port_entry.delete(
[color=]0
, tk.END)
smtp_port_entry.insert(
[color=]0
, config[
[color=]'smtp_port'
])
smtp_user_entry.delete(
[color=]0
, tk.END)
smtp_user_entry.insert(
[color=]0
, config[
[color=]'smtp_user'
])
smtp_password_entry.delete(
[color=]0
, tk.END)
smtp_password_entry.insert(
[color=]0
, config[
[color=]'smtp_password'
])
timeout_entry.delete(
[color=]0
, tk.END)
timeout_entry.insert(
[color=]0
, config[
[color=]'timeout'
])
check_interval_entry.delete(
[color=]0
, tk.END)
check_interval_entry.insert(
[color=]0
, config[
[color=]'check_interval'
])
[color=]for
entry
[color=]in
host_entries:
entry.grid_forget()
entry.destroy()
[color=]for
entry
[color=]in
remark_entries:
entry.grid_forget()
entry.destroy()
[color=]for
button
[color=]in
delete_buttons:
button.grid_forget()
button.destroy()
host_entries.clear()
remark_entries.clear()
delete_buttons.clear()
[color=]for
host
[color=]in
config[
[color=]'hosts'
]:
add_host_entry()
host_entries[-
[color=]1
].insert(
[color=]0
, host[
[color=]'address'
])
remark_entries[-
[color=]1
].insert(
[color=]0
, host[
[color=]'remark'
])
update_layout()
[color=]except
[color=]Exception
[color=]as
e:
messagebox.showerror(
[color=]"
[color=]加载配置失败
[color=]"
,
[color=]f"
[color=]加载配置失败
[color=]:
[color=]{
e
[color=]}
[color=]"
)
window = tk.Tk()
window.title(
[color=]"Ping
[color=]监控程序
[color=]"
)
canvas = tk.Canvas(window)
scrollbar = tk.Scrollbar(window,
[color=]orient
=
[color=]"vertical"
,
[color=]command
=canvas.yview)
canvas.configure(
[color=]yscrollcommand
=scrollbar.set)
scrollable_frame = tk.Frame(canvas)
scrollable_frame.bind("",
[color=]lambda
e: canvas.configure(
[color=]scrollregion
=canvas.bbox(
[color=]"all"
)))
canvas.create_window((
[color=]0
,
[color=]0
),
[color=]window
=scrollable_frame,
[color=]anchor
=
[color=]"nw"
)
canvas.pack(
[color=]side
=
[color=]"left"
,
[color=]fill
=
[color=]"both"
,
[color=]expand
=
[color=]True
)
scrollbar.pack(
[color=]side
=
[color=]"right"
,
[color=]fill
=
[color=]"y"
)
inner_frame = tk.Frame(scrollable_frame)
inner_frame.pack(
[color=]fill
=
[color=]"both"
,
[color=]expand
=
[color=]True
)
tk.Label(inner_frame,
[color=]text
=
[color=]"
[color=]接收邮件地址(多个用逗号分隔)
[color=]:"
).grid(
[color=]row
=
[color=]0
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
to_email_entry = tk.Entry(inner_frame)
to_email_entry.grid(
[color=]row
=
[color=]0
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"
[color=]发送邮件地址
[color=]:"
).grid(
[color=]row
=
[color=]1
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
from_email_entry = tk.Entry(inner_frame)
from_email_entry.grid(
[color=]row
=
[color=]1
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"SMTP
[color=]服务器
[color=]:"
).grid(
[color=]row
=
[color=]2
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
smtp_server_entry = tk.Entry(inner_frame)
smtp_server_entry.grid(
[color=]row
=
[color=]2
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"SMTP
[color=]端口
[color=]:"
).grid(
[color=]row
=
[color=]3
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
smtp_port_entry = tk.Entry(inner_frame)
smtp_port_entry.grid(
[color=]row
=
[color=]3
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"SMTP
[color=]用户
[color=]:"
).grid(
[color=]row
=
[color=]4
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
smtp_user_entry = tk.Entry(inner_frame)
smtp_user_entry.grid(
[color=]row
=
[color=]4
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"SMTP
[color=]密码
[color=]:"
).grid(
[color=]row
=
[color=]5
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
smtp_password_entry = tk.Entry(inner_frame,
[color=]show
=
[color=]"*"
)
smtp_password_entry.grid(
[color=]row
=
[color=]5
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"
[color=]超时时间
[color=](
[color=]秒
[color=]):"
).grid(
[color=]row
=
[color=]6
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
timeout_entry = tk.Entry(inner_frame)
timeout_entry.grid(
[color=]row
=
[color=]6
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
tk.Label(inner_frame,
[color=]text
=
[color=]"
[color=]检查间隔
[color=](
[color=]秒
[color=]):"
).grid(
[color=]row
=
[color=]7
,
[color=]column
=
[color=]0
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"w"
)
check_interval_entry = tk.Entry(inner_frame)
check_interval_entry.grid(
[color=]row
=
[color=]7
,
[color=]column
=
[color=]1
,
[color=]columnspan
=
[color=]3
,
[color=]padx
=
[color=]5
,
[color=]pady
=
[color=]5
,
[color=]sticky
=
[color=]"ew"
)
host_entries = []
remark_entries = []
delete_buttons = []
add_host_entry_button = tk.Button(inner_frame,
[color=]text
=
[color=]"
[color=]添加主机
[color=]"
,
[color=]command
=add_host_entry)
add_host_entry_button.grid(
[color=]row
=
[color=]8
,
[color=]column
=
[color=]0
,
[color=]columnspan
=
[color=]4
,
[color=]pady
=
[color=]5
)
start_button = tk.Button(inner_frame,
[color=]text
=
[color=]"
[color=]开始监控
[color=]"
,
[color=]command
=start_monitoring)
start_button.grid(
[color=]row
=
[color=]9
,
[color=]column
=
[color=]0
,
[color=]columnspan
=
[color=]4
,
[color=]pady
=
[color=]5
)
save_button = tk.Button(inner_frame,
[color=]text
=
[color=]"
[color=]保存配置
[color=]"
,
[color=]command
=save_config)
save_button.grid(
[color=]row
=
[color=]10
,
[color=]column
=
[color=]0
,
[color=]columnspan
=
[color=]2
,
[color=]pady
=
[color=]5
)
load_button = tk.Button(inner_frame,
[color=]text
=
[color=]"
[color=]加载配置
[color=]"
,
[color=]command
=load_config)
load_button.grid(
[color=]row
=
[color=]10
,
[color=]column
=
[color=]2
,
[color=]columnspan
=
[color=]2
,
[color=]pady
=
[color=]5
)
window.grid_columnconfigure(
[color=]1
,
[color=]weight
=
[color=]1
)
window.grid_columnconfigure(
[color=]2
,
[color=]weight
=
[color=]1
)
window.mainloop()