如果 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)

我应该如何要求寻找代码输出而不是网站监视器?

解决方法

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

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/

让您的处理程序在发生更改时更新 last_changed 时间戳变量。 (因为无论更改是什么,您的所有处理程序都执行相同的操作,所以您可以只定义一个处理程序方法,也许调用它 on_any_change 并将其附加到所有 4 个处理程序方法。哦,看,{{ 3}} 表明有一个 on_any_event 方法,因此您可以直接使用该方法。)

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