/[svn.andrew.net.au]/usbspindownd/usbspindownd.py
ViewVC logotype

Diff of /usbspindownd/usbspindownd.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 31 by apollock, Thu Jan 31 02:51:57 2008 UTC revision 34 by apollock, Thu Jan 31 19:03:28 2008 UTC
# Line 28  import ConfigParser Line 28  import ConfigParser
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      keep_running = False
41    
42    
43    def getsignals():
44      signals = {}
45      for name in dir(signal):
46        if name.startswith("SIG"):
47          signals[getattr(signal, name)] = name
48      return signals
49    
 #  
 # Disk, how long to wait for, last msio value, spun down  
 #  
50    
51  def search_path(filename, search_path):  def search_path(filename, search_path):
52    file_found = False    file_found = False
# Line 53  def debug(options, message): Line 65  def debug(options, message):
65    if options.debug:    if options.debug:
66      print "%s: %s" % (time.asctime(time.localtime()), message)      print "%s: %s" % (time.asctime(time.localtime()), message)
67    
68    
69    def log(message):
70      pass
71    
72    
73  def load_config(options):  def load_config(options):
74    global spindown_cmd, spindown_cmd_args    global spindown_cmd, spindown_cmd_args
75    cf = ConfigParser.SafeConfigParser()    cf = ConfigParser.SafeConfigParser()
# Line 62  def load_config(options): Line 79  def load_config(options):
79    except IOError, e:    except IOError, e:
80      print "Got a '%s' trying to read %s" % (e.strerror, options.config)      print "Got a '%s' trying to read %s" % (e.strerror, options.config)
81      sys.exit(1)      sys.exit(1)
82      except ConfigParser.MissingSectionHeaderError, e:
83        print "Parser error: '%s'" % (e.message)
84        sys.exit(1)
85    if cf.has_option("DEFAULT", "wait"):    if cf.has_option("DEFAULT", "wait"):
86      defaultwait = cf.get("DEFAULT", "wait")      defaultwait = cf.get("DEFAULT", "wait")
87    else:    else:
# Line 108  def spin_down(options, disk): Line 128  def spin_down(options, disk):
128    
129    
130  def monitor_disks(config, options):  def monitor_disks(config, options):
131      global keep_running
132    debug(options, "Monitoring disks")    debug(options, "Monitoring disks")
133    while True:    while keep_running:
134      for disk in config:      for disk in config:
135        debug(options, "Considering %s" % (disk))        debug(options, "Considering %s" % (disk))
136        ds = diskstats.DiskStats(disk)        ds = diskstats.DiskStats(disk)
# Line 142  def monitor_disks(config, options): Line 163  def monitor_disks(config, options):
163            else:            else:
164              # This disk is ineligible for spinning down at this time              # This disk is ineligible for spinning down at this time
165              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())  
166          else:          else:
167            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))
168            config[disk]["last_msio"] = msio            config[disk]["last_msio"] = msio
# Line 150  def monitor_disks(config, options): Line 170  def monitor_disks(config, options):
170            config[disk]["spun_down"] = False            config[disk]["spun_down"] = False
171      debug(options, "Sleeping")      debug(options, "Sleeping")
172      time.sleep(60)      time.sleep(60)
173      debug(options, "Shutting down")
174    
175  def main():  def main():
176      global options
177      global signals
178      signals = getsignals()
179      signal.signal(signal.SIGTERM, signal_handler)
180      signal.signal(signal.SIGINT, signal_handler)
181    parser = optparse.OptionParser()    parser = optparse.OptionParser()
182    parser.add_option("-n", "--dry-run",    parser.add_option("-n", "--dry-run",
183      action="store_true",      action="store_true",

Legend:
Removed from v.31  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.22