-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspindown.py
117 lines (93 loc) · 3.26 KB
/
spindown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# Spindown every /sys/block/sd* device after specified idle time
# Needs package sdparm and root privileges
# (C) 2009, Taher Shihadeh <[email protected]>
# Licensed: GPL v2
import getopt
import time
import sys
import os
DISKPATH = '/sys/block/'
SLEEPVAL = 60 # 1 minute
INTERVAL = 300 # 5 minute
CMDTMPLT = 'sdparm --flexible --command=%s /dev/%s &>/dev/null'
def help():
help="""Usage: spindown [-h] | [[-i seconds] | [-s] | [-e sda [-e sdb ...]]]
Parameters:
-h, --help this help screen
-e, --exclude do not operate on specified drive
-i, --interval idle seconds above which the drives should spin down
-s, --simulate do not perform actions, just simulate what would happen
"""
print help
def parse_args(params):
try:
opts, args = getopt.getopt(params, "hi:e:s",
["help", "interval=", "exclude=", "simulate"])
except getopt.GetoptError:
help()
sys.exit(1)
params = {
'simulate' : False,
'interval' : INTERVAL,
'sleepval' : SLEEPVAL,
'exclude' : []
}
for o, a in opts:
if o in ("-h", "--help"):
help()
sys.exit(1)
if o in ("-i", "--interval"):
try:
a = int(a)
params['interval'] = a
except ValueError:
help()
sys.exit(1)
if o in ("-s", "--simulate"):
params['simulate'] = True
if o in ("-e", "--exclude"):
params['exclude'].append(a)
if params['interval'] < SLEEPVAL:
params['sleepval'] = params['interval'] / 2
return params
def main(params):
info = {}
# see what drives need monitoring
drives = os.listdir(DISKPATH)
drives = filter (lambda x: len(x) == 3 and x[:2] == 'sd', drives)
drives = filter (lambda x: x not in params['exclude'], drives)
if len(drives) == 0:
print 'Nothing to do.',
return
for drive in drives:
info[drive] = {'stat': '', 'time': 0, 'spin': True}
while True:
for drive in drives:
the_stat = open(DISKPATH + drive + '/stat').read()
the_time = time.time()
# If status has not changed
if info[drive]['stat'] == the_stat:
if the_time - info[drive]['time'] >= params['interval'] and \
info[drive]['spin'] == True:
cmd = CMDTMPLT % ('sync', drive) + ' ; ' + \
CMDTMPLT % ('stop', drive)
if params['simulate']:
print 'Simulation: no %s activity in the last ' \
'%s seconds.\n' % (drive,params['interval'])
else:
ret = os.system (cmd)
if ret != 0:
sys.exit('Could nos stop %s' % drive)
else:
info[drive] = {'stat' : the_stat,
'time' : the_time,
'spin' : True}
time.sleep(params['sleepval'])
if __name__ == "__main__":
try:
params = parse_args(sys.argv[1:])
main(params)
except KeyboardInterrupt:
print "Keyboard interrupt. ",
print "Terminating process."