Monitor File Copy from Local Drive to Network drive and Send Email if Copying Failed
Run as Custom Monitoring Script. Follow the wiki link
https://wiki.itarian.com/frontend/web/topic/how-to-use-custom-script-procedure-monitoring
##### Email Field informations
emailto=['XXXXXX@gmail.com'] # Provide System Administrator email address where the alert mail has to be received
emailfrom='YYYYYY@gmail.com' # Provide an Fromemail address from which the alert mail has to be sent
password='xxxyyy@123' # Provide an Fromemail password
smtpserver='smtp.gmail.com'
port=587
### File sharing informations
src_path=r'C:\\Users\\wind1064\\Desktop\\older versions' #source folder path need to be copied
share_path=r'\\Win-4g2ucfeglbg\e'# Destination share path
des_fname='File_share' # please provide the name of the folder that does not exist earlier in the give share path
share_user="administrator" # share path user name
share_pass="comodo@123" #share path password
import os
import shutil
import platform
import ctypes
import email
import sys
import socket
import smtplib
import mimetypes
import time
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
comp_name=os.environ['COMPUTERNAME']
def alert(arg):
sys.stderr.write("%d%d%d" % (arg, arg, arg))
msgbody=r'''
Hi
Copying from {} to network drive {} failed
Please check your share path and other details.
Thank You
'''.format(comp_name, share_path)
ipaddress=socket.gethostbyname(socket.gethostname())
subject='Copying from '+comp_name+' ('+ ipaddress +' ) to network drive Failed !!!!!'
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
os_details = platform.release()
ipaddress=socket.gethostbyname(socket.gethostname())
subject='Copying from '+comp_name+' ('+ ipaddress +' ) to network drive Failed !!!!!'
def emailreport(subject, emailto,emailfrom,password,smtpserver,port,msgbody):
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = ",".join(emailto)
msg["Subject"] = subject
msg.preamble = subject
body = MIMEText(msgbody)
msg.attach(body)
try:
server = smtplib.SMTP(smtpserver,port)
server.ehlo()
server.starttls()
server.login(emailfrom, password)
server.sendmail(emailfrom, emailto, msg.as_string())
time.sleep(30)
print (" \nThe email report has been sent to "+msg["To"])
server.quit()
except Exception as e:
return e
def checkUAC():
with disable_file_system_redirection():
if "7" in os_details:
print"-------STARTING A SERVICE-----"
start=os.popen("net start VSS").read()
print start
chec=os.popen("sc query VSS").read()
cmd='NET USE "'+share_path+'" /USER:'+share_user+' "'+share_pass+'"'
tar_path=share_path+'\\'+des_fname
print 'Login to network share'
print os.popen(cmd).read()
print 'Copying files to local machine....'
else:
cmd='NET USE "'+share_path+'" /USER:'+share_user+' "'+share_pass+'"'
tar_path=share_path+'\\'+des_fname
print 'Login to network share'
print os.popen(cmd).read()
print 'Copying files to local machine....'
if os.path.isdir(src_path):
try:
shutil.copytree(src_path,tar_path)
print 'Script execution completed successfully'
alert(0)
except:
emailreport(subject, emailto,emailfrom,password,smtpserver,port,msgbody)
print "Check %s is already available and check the File sharing informations"%des_fname
alert(1)
else:
print '%s is not found'%src_path
emailreport(subject, emailto,emailfrom,password,smtpserver,port,msgbody)
alert(1)
checkUAC()
Comments