28 |
import sys |
import sys |
29 |
import os |
import os |
30 |
import time |
import time |
31 |
|
import signal |
32 |
|
|
33 |
spindown_cmd = "" |
spindown_cmd = "" |
34 |
spindown_cmd_args = "" |
spindown_cmd_args = "" |
35 |
|
keep_running = True |
36 |
|
signals = {} |
37 |
|
|
38 |
|
def signal_handler(signal, stack): |
39 |
|
global keep_running |
40 |
|
global sigdict |
41 |
|
keep_running = False |
42 |
|
|
43 |
|
|
44 |
|
def getsignals(): |
45 |
|
signals = {} |
46 |
|
for name in dir(signal): |
47 |
|
if name.startswith("SIG"): |
48 |
|
signals[getattr(signal, name)] = name |
49 |
|
return signals |
50 |
|
|
|
# |
|
|
# Disk, how long to wait for, last msio value, spun down |
|
|
# |
|
51 |
|
|
52 |
def search_path(filename, search_path): |
def search_path(filename, search_path): |
53 |
file_found = False |
file_found = False |
66 |
if options.debug: |
if options.debug: |
67 |
print "%s: %s" % (time.asctime(time.localtime()), message) |
print "%s: %s" % (time.asctime(time.localtime()), message) |
68 |
|
|
69 |
|
|
70 |
|
def log(message): |
71 |
|
pass |
72 |
|
|
73 |
|
|
74 |
def load_config(options): |
def load_config(options): |
75 |
global spindown_cmd, spindown_cmd_args |
global spindown_cmd, spindown_cmd_args |
76 |
cf = ConfigParser.SafeConfigParser() |
cf = ConfigParser.SafeConfigParser() |
126 |
|
|
127 |
|
|
128 |
def monitor_disks(config, options): |
def monitor_disks(config, options): |
129 |
|
global keep_running |
130 |
debug(options, "Monitoring disks") |
debug(options, "Monitoring disks") |
131 |
while True: |
while keep_running: |
132 |
for disk in config: |
for disk in config: |
133 |
debug(options, "Considering %s" % (disk)) |
debug(options, "Considering %s" % (disk)) |
134 |
ds = diskstats.DiskStats(disk) |
ds = diskstats.DiskStats(disk) |
161 |
else: |
else: |
162 |
# This disk is ineligible for spinning down at this time |
# This disk is ineligible for spinning down at this time |
163 |
debug(options, "Disk idle for %s seconds, but not for long enough (%s)" % (now - config[disk]["timestamp"], config[disk]["wait"])) |
debug(options, "Disk idle for %s seconds, but not for long enough (%s)" % (now - config[disk]["timestamp"], config[disk]["wait"])) |
|
#config[disk]["timestamp"] = int(time.time()) |
|
164 |
else: |
else: |
165 |
debug(options, "Disk not idle (old msio: %s, current msio: %s)" % (config[disk]["last_msio"], msio)) |
debug(options, "Disk not idle (old msio: %s, current msio: %s)" % (config[disk]["last_msio"], msio)) |
166 |
config[disk]["last_msio"] = msio |
config[disk]["last_msio"] = msio |
168 |
config[disk]["spun_down"] = False |
config[disk]["spun_down"] = False |
169 |
debug(options, "Sleeping") |
debug(options, "Sleeping") |
170 |
time.sleep(60) |
time.sleep(60) |
171 |
|
debug(options, "Shutting down") |
172 |
|
|
173 |
def main(): |
def main(): |
174 |
|
global options |
175 |
|
global signals |
176 |
|
signals = getsignals() |
177 |
|
signal.signal(signal.SIGTERM, signal_handler) |
178 |
|
signal.signal(signal.SIGINT, signal_handler) |
179 |
parser = optparse.OptionParser() |
parser = optparse.OptionParser() |
180 |
parser.add_option("-n", "--dry-run", |
parser.add_option("-n", "--dry-run", |
181 |
action="store_true", |
action="store_true", |