Hello,
I have a task to check disk space on Remote server. I did it for years using Python 2.7 and running script locally on that Remote server ( I was not able to access it remotely )
Now I have another server to monitor. Old server was Win 2008 R2 and new one is 2012 R2. I had to convert my script to Python 3.
I tried to run it … but now I have error on line: 82Processing: diskUsageMail.py…
import ctypes
import getopt
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
import platform
import logging
import logging.handlers
import os
import shutil
def main():
'''
Usage: diskUsage.py [option] <drive letter>
diskUsage.py checks the total, free space and % of space left on the disk
Optionally it might create a log file <logfile>
-l <logfile>: log the result of evaluation
'''
name="//vhabosfpc1gen/lims/limslz/tools/logs/va-starlims.log",
logger = logging.getLogger()
#rh = logging.handlers.RotatingFileHandler('//vhabosfpc1gen/lims/limslz/tools/logs/va-starlims.log' ,backupCount=0)
#logger.addHandler(rh)
#rh.doRollover()
logging.basicConfig(
filename="//vhabosfpc1gen/lims/limslz/tools/logs/va-starlims.log" ,
format='%(created)f %(message)s',
level=logging.INFO)
programInfo = 'diskUsageMailLog.py'
logging.info('Start:' + programInfo)
#Checking validity of the arguments
try:
opts, args = getopt.getopt(sys.argv[1:],'l:')
except getopt.GetoptError as err:
help(main)
sys.exit(2)
if len(sys.argv) < 2:
help(main)
sys.exit(2)
if len(args) == 0:
help(main)
sys.exit(2)
#Check options value
argMap = dict(opts)
if argMap.get('-l') == None:
logger = sys.stdout
else:
logger = open(argMap.get('-l'), 'w')
if len(sys.argv) == 2:
x = sys.argv[1]
elif argMap.get('-l') != None and len(sys.argv) == 4:
x = sys.argv[3]
else:
if argMap.get('-l') != None:
x = sys.argv[3]
y = sys.argv[4]
else:
x = sys.argv[1]
y = sys.argv[2]
host = platform.uname()[1]
_,total_x,free_x = ctypes.c_ulonglong(), ctypes.c_ulonglong(), ctypes.c_ulonglong()
_,total_y,free_y = ctypes.c_ulonglong(), ctypes.c_ulonglong(), ctypes.c_ulonglong()
ctypes.windll.kernel32.GetDiskFreeSpaceExA(x, ctypes.byref(_), ctypes.byref(total_x), ctypes.byref(free_x))
if 'y' in vars():
ctypes.windll.kernel32.GetDiskFreeSpaceExA(y, ctypes.byref(_), ctypes.byref(total_y), ctypes.byref(free_y))
free_x = free_x.value/1024/1024
total_x = total_x.value/1024/1024
free_y = free_y.value/1024/1024
total_y = total_y.value/1024/1024
dir = ["C:\inetpub\logs\LogFiles","D:\Program Files\SDMS\Site\Logfiles"] # directories to be deleted
if 'y' in vars():
if (float(free_x) / float(total_x) * 100) < 3 or (float(free_y) / float(total_y) * 100) < 3:
mesg = "Free space on drive " + x + " is %s Mb and total space is %s Mb. Availability is %s percent.\
\n" % (int(free_x), int(total_x), int( float(free_x) / float(total_x) * 100))+\
"Free space on drive " + y + " is %s Mb and total space is %s Mb. Availability is %s percent.\
\n" % (int(free_y), int(total_y), int( float(free_y) / float(total_y) * 100))
for p in dir:
if get_size(p) > 8589934592: # 8 Gb
shutil.rmtree(p,"ignore_errors=True")
mesg_r = "Directory " + p + " has been deleted!\n"
logger.write(mesg_r)
sendWarning(host + ": " + mesg_r)
logger.write(mesg)
sendWarning(host + ": " + mesg)
elif (float(free_x) / float(total_x) * 100) < 5 or (float(free_y) / float(total_y) * 100) < 5:
mesg = "Free space on drive " + x + " is %s Mb and total space is %s Mb. Availability is %s percent.\
\n" % (int(free_x), int(total_x),int( float(free_x) / float(total_x) * 100))+\
"Free space on drive " + y + " is %s Mb and total space is %s Mb. Availability is %s percent.\
\n" % (int(free_y), int(total_y) , int( float(free_y) / float(total_y) * 100))
logger.write(mesg)
sendWarning(host + ": " + mesg)
else:
mesg = "Free space on drive " + x + " and drive " + y + " is more than 5 percent\n"
logger.write(mesg)
else:
if (float(free_x) / float(total_x) * 100) < 3:
mesg = "Free space on drive " + x + " is %s Mb and total space is %s Mb. Availability is %s percent.\
\n" % (int(free_x), int(total_x),int( float(free_x) / float(total_x) * 100))
for p in dir:
if get_size(p) > 8589934592: #8 Gb
shutil.rmtree(p,"ignore_errors=True")
mesg_r = "Directory " + p + " has been deleted!\n"
logger.write(mesg_r)
sendWarning(host + ": " + mesg_r)
logger.write(mesg)
sendWarning(host + ": " + mesg)
elif (float(free_x) / float(total_x) * 100) < 5 :
mesg = "Free space on drive " + x + " is %s Mb and total space is %s Mb. Availability is %s percent.\
\n" % (int(free_x), int(total_x),int( float(free_x) / float(total_x) * 100))
logger.write(mesg)
sendWarning(host + ": " + mesg)
else:
mesg = "Free space is more than 5 percent !!\n"
logger.write(mesg)
logging.info('Complete:' + programInfo)
def sendWarning(subject):
# Import smtplib for the actual sending function
import smtplib
# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = subject
me = "eliya.voldman@va.gov"
recepient = "eliya.voldman@va.gov"
msg['From'] = me
msg['To'] = ''.join(recepient)
# Send the email via our own SMTP server.
s = smtplib.SMTP('smtp.va.gov')
s.sendmail(me,recepient,msg.as_string())
s.quit()
def get_size(path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(path):
for f in filenames:
fp = os.path.join(dirpath, f)
if os.path.exists(fp):
total_size += os.path.getsize(fp)
return total_size
if __name__ == '__main__':
main()
ZeroDivisionError: float division by zero
Could you please review it? Why the same script does not work now?
Thanks