[英]How to make share path monitoring script to generate outlook email alert if no movement appears in shard path after 20 minutes?
Greetings,.问候,。 I am trying using below watchdog module to monitor a shared path that works fine but I am not getting the idea to generate outlook email alert: after the time span of 20 minutes if no modification or update happening to the specified path.我正在尝试使用下面的看门狗模块来监视一个工作正常的共享路径,但我不知道生成 outlook email 警报:如果在 20 分钟的时间跨度后没有对指定路径进行修改或更新。 Below is the code:下面是代码:
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()
However, tried this piece of code to receive outlook email alerts, but not sure how to make it work with above script:但是,尝试使用这段代码来接收 outlook email 警报,但不确定如何使其与上述脚本一起使用:
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)
Challenge for me is:对我来说挑战是:
r = requests.get("https://fleet.my.salesforce.com", timeout=5)
Instead of website monitor, how should I ask to look for the code output?而不是网站监视器,我应该如何要求寻找代码 output?
You have to attach methods to your event handler, like this:您必须将方法附加到事件处理程序,如下所示:
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
where the methods look like:方法如下所示:
def on_created(event):
print(f"{event.src_path} has been created!")
as described in this blog post: http://thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/如本博文所述: 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.每当有更改时,让您的处理程序更新last_changed
时间戳变量。 (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.) (因为无论更改是什么,您的所有处理程序都会做同样的事情,您可以只定义一个处理程序方法,也许将其on_any_change
并将其附加到所有 4 个处理程序方法。哦,看, watchdog
文档显示有一个on_any_event
方法,所以你可以挂钩。)
Then in your while True
loop, if the current time - last_changed
is greater than your threshold, send the email.然后在您的while True
循环中,如果当前时间 - last_changed
大于您的阈值,则发送 email。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.