如果 20 分钟后分片路径中没有任何移动,如何使共享路径监控脚本生成 outlook 电子邮件警报?

How to make share path monitoring script to generate outlook email alert if no movement appears in shard path after 20 minutes?

您好!我正在尝试使用下面的看门狗模块来监视工作正常的共享路径,但我不明白 生成 outlook 电子邮件警报,如果没有,则在 20 分钟的时间跨度后 指定路径发生的修改或更新。下面是 代码:

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)

而不是网站监控,我应该如何要求查找代码输出?

您必须将方法附加到您的事件处理程序,如下所示:

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!")

如本博客所述post:http://thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/

让您的处理程序在发生变化时更新 last_changed 时间戳变量。 (因为无论更改是什么,您的所有处理程序都做同样的事情,您可以只定义一个处理程序方法,也许将其称为 on_any_change 并将其附加到所有 4 个处理程序方法。哦,看, watchdog docs 表明有一个 on_any_event 方法,所以你可以挂钩它。)

然后在您的 while True 循环中,如果当前时间 - last_changed 大于您的阈值,则发送电子邮件。