python-3.x - 如果分片路径在 20 分钟后没有移动,如何制作共享路径监控脚本以生成 Outlook 电子邮件警报?
问题描述
问候!!我正在尝试使用下面的看门狗模块来监控一个工作正常的共享路径,但如果指定路径没有发生修改或更新,我没有想到在 20 分钟的时间跨度之后生成 Outlook 电子邮件警报。下面是代码:
import os
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
os.system('//fleet.ad/data/Data4/VMSSHARE/its/DOCS')
print("found")
# Defining your own path
path = "//bleet.ad/data/Data4/VMSSHARE/its/DOCS"
print("found")
# Initilaize logging event handler
event_handler = LoggingEventHandler()
# Initialize Observer
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
# Start the observer
observer.start()
try:
while True:
# set the thread sleep time
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
但是,尝试使用这段代码接收 Outlook 电子邮件警报,但不确定如何使其与上述脚本一起使用:
import os
import smtplib
import requests
EMAIL_ADDRESS = os.environ.get('USER_ID')
EMAIL_PASSWORD = os.environ.get('USER_PASSWORD')
r = requests.get("https://fleet.my.salesforce.com", timeout=5)
#if r.status_code!= 200:
with smtplib.SMTP('smtp.office365.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
subject = 'ALARM: MAPILab is stuck from copying Public folders to destination'
body = 'Make sure server is restarted and it is backed up'
msg = f'Subject:{subject}\n\n{body}'
smtp.sendmail(EMAIL_ADDRESS, 'ryadav@elementcorp.com', msg)
对我来说挑战是:
r = requests.get("https://fleet.my.salesforce.com", timeout=5)
而不是 website monitor ,我应该如何要求查找代码输出?
解决方案
您必须将方法附加到事件处理程序,如下所示:
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved
方法如下所示:
def on_created(event):
print(f"{event.src_path} has been created!")
如本博文所述:http: //thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/
Have your handlers update a last_changed
timestamp variable whenever there is a change. (Since all your handlers do the same thing regardless of what the change was, you could get away with just defining one handler method, maybe call it on_any_change
and attach it to all 4 handler methods. Oh look, the watchdog
docs show that there is an on_any_event
method, so you could just hook into that.)
Then in your while True
loop, if the current time - last_changed
is greater than your threshold, send the email.
推荐阅读
- python-3.x - 如何在烧瓶中将数据从一条路线传输到另一条路线
- tally - 多个用户在 Tally ERP9 上远程工作
- javascript - 如何在向下滚动网页时更改图片?
- macos - 移至... > iCloud > 便笺选项已禁用
- azure - VM Scaleset 与 Azure Batch
- azure - Azure AD 访问令牌 appid 声明值
- android - Android 工具栏标题在标题中显示白色背景
- python - 在python中插入排序后的二进制搜索
- scala - 是否可以在扩展的 scala 类中扩展方法?
- javascript - 如何将图像 url 从 firebase 显示到 html 表中?