Chat Beta

7/27/2015

Disk check in a partition to remind the remaining space(Python script)

1:  __author__ = 'someone'  
2:  #!/usr/bin/env python  
3:    
4:  """  
5:  Return disk usage statistics about the given path as a (total, used, free)  
6:  namedtuple. Values are expressed in bytes.  
7:  """  
8:    
9:  import os  
10:  import collections  
11:    
12:  _ntuple_diskusage = collections.namedtuple('usage', 'total used free')  
13:    
14:  if hasattr(os, 'statvfs'): # POSIX  
15:    def disk_usage(path):  
16:      st = os.statvfs(path)  
17:      free = st.f_bavail * st.f_frsize  
18:      total = st.f_blocks * st.f_frsize  
19:    
20:      used = (st.f_blocks - st.f_bfree) * st.f_frsize  
21:      return _ntuple_diskusage(total, used, free)  
22:    
23:  elif os.name == 'nt':    # Windows  
24:    import ctypes  
25:    import sys  
26:    import smtplib  
27:    
28:    SMTPServer='mail.maildomain.com'  
29:    sender = 'diskcheck@buildDL.test'  
30:    receiver = 'tester@test.test'  
31:    
32:    def sendmail( message):  
33:      try:  
34:        smtpObj = smtplib.SMTP(SMTPServer)  
35:        smtpObj.sendmail(sender, receiver, message)  
36:      except Exception as e:  
37:        print("e-mail sending error occured: " + e)  
38:    
39:    def m_diskCheck():  
40:      msgunreach="From: Auto Builder " + "\n" + \  
41:            "To: Testing Team" + "\n" + \  
42:            "Subject:Disk size in file server is below 20GB, Please delete some older files!!!\n\n" + \  
43:            "This is a system generated e-mail. Do not reply"  
44:      return (msgunreach)  
45:    
46:    def disk_usage(path):  
47:      _, total, free = ctypes.c_ulonglong(), ctypes.c_ulonglong(), \  
48:                ctypes.c_ulonglong()  
49:      if sys.version_info >= (3,) or isinstance(path, unicode):  
50:        fun = ctypes.windll.kernel32.GetDiskFreeSpaceExW  
51:      else:  
52:        fun = ctypes.windll.kernel32.GetDiskFreeSpaceExA  
53:      ret = fun(path, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))  
54:      if ret == 0:  
55:        raise ctypes.WinError()  
56:      used = total.value - free.value  
57:      if int(free.value/1000000000)<150: p="">      sendmail(m_diskCheck())  
58:    
59:      return _ntuple_diskusage(str(int(total.value/1000000000))+' GB', str(int(used/1000000000))+' GB',str(int(free.value/1000000000))+' GB')  
60:  else:  
61:    raise NotImplementedError("platform not supported")  
62:    
63:  disk_usage.__doc__ = __doc__  
64:    
65:  if __name__ == '__main__':  
66:    print (disk_usage('C:\\'))  

No comments:

Post a Comment