update ov debian/overrides
[grml-etc.git] / etc / skel / .wmi / szs2.1 / diskusage.py
1 #!/usr/bin/python
2
3 # Module to show usage of your partitions
4
5 import szstoolbox
6 from os import statvfs
7 import statvfs as vfs
8
9 def init():
10         """ yeah... init wie schwein """
11
12         global hdds,lst,ready,fstab
13         cfg = szstoolbox.CFG('diskusage')
14         fstabF = file('/etc/fstab','r')
15         fstab = fstabF.readlines()
16         fstabF.close()
17         config = cfg.read('disks')
18         if type(config) <> list:
19                 hdds = config.split(',')
20         else:
21                 hdds = [] 
22                 for it in config:
23                         hdds.extend(it.split(','))
24         hdds = [x.split(':') for x in hdds]
25
26         # rename mountpoints into the corresponding devs                                
27         global fstabD
28         fstabD = {}
29         for x in fstab:
30                 x = x.strip()
31                 if len(x) > 1:
32                         if x[0] <> '#':
33                                 x = x.strip().split()
34                                 fstabD[x[1]] = x[0]
35         for x in range(len(hdds)):
36                 if hdds[x][0] in fstabD.keys():
37                         hdds[x][0] = fstabD[hdds[x][0]]
38
39         for x in range(len(hdds)):
40                 if hdds[x][0][:4] != '/dev':
41                         hdds[x][0] = '/dev/' + hdds[x][0]
42         
43         ready = True
44
45
46
47 def main():
48         """ Module to show usage and capacity of disks """
49
50         if not 'ready' in globals(): init()
51         global mntD
52         
53         mntD = {}
54         mntF = file('/etc/mtab','r')
55         mnt = mntF.readlines()
56         mntF.close()
57         for x in mnt:
58                 x = x.split()
59                 mntD[x[0]] = x[1]
60         
61         lst = []
62         for hdd in hdds:
63                 if hdd[0] in mntD:
64                         stat = statvfs(mntD[hdd[0]])
65                         blocks = stat[vfs.F_BLOCKS]
66                         free = stat[vfs.F_BFREE]
67                         lst.append(str((blocks-free)*100/blocks)+'%'+hdd[1])
68         
69         return ['',','.join(lst)]
70
71
72
73 if __name__ == '__main__':
74         """ helps to debug the script """
75
76         from sys import argv
77         from time import sleep
78
79         print main()
80         if 'loop' in argv:
81                 while '' == '':
82                         x = main()
83                         print '----------------------'
84                         print 'Length:\t'+str(len(x))
85                         print 'Text:\t"'+x[0]+'"'
86                         print 'Bars:\t"'+x[1]+'"'
87                         print '----------------------'
88                         print '/proc/mounts:'
89                         for x in mntD:
90                                 print str(x)+':\t'+str(mntD[x])
91         
92                         print ''
93                         print '/etc/fstab:'
94                         for x in fstabD:
95                                 print str(x)+':\t'+str(fstabD[x])
96                         sleep(1)
97         else:
98                 x = main()
99                 print '----------------------'
100                 print 'Length:\t'+str(len(x))
101                 print 'Text:\t"'+x[0]+'"'
102                 print 'Bars:\t"'+x[1]+'"'
103                 print '----------------------'
104         
105                 print '/proc/mtab:'
106                 for x in mntD:
107                         print str(x)+':\t'+str(mntD[x])
108         
109                 print ''
110                 print '/etc/fstab:'
111                 for x in fstabD:
112                         print str(x)+':\t'+str(fstabD[x])