Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- import sys
- from optparse import OptionParser
- import re
- import time
- import subprocess
- import syslog
- import os
- import stat
- INTERVAL = 40
- NUM = 30
- SYSLOG = False
- def log (message):
- global SYSLOG
- if SYSLOG:
- os.system ("logger " + message)
- else:
- print message
- def daemonize ():
- log ("Daemonizing ...")
- try:
- pid = os.fork()
- if pid > 0:
- sys.exit(0)
- except OSError, e:
- sys.exit(1)
- os.chdir("/")
- os.setsid()
- os.umask(0)
- try:
- pid = os.fork()
- if pid > 0:
- sys.exit(0)
- except OSError, e:
- sys.exit(1)
- def check_devices (devices_map):
- for device in sorted (devices_map.iterkeys ()):
- info = os.stat ("/dev/" + device)
- if not stat.S_ISCHR (info.st_mode):
- log ("/dev/%s is not a device" % device)
- sys.exit (1)
- def get_gstat_output (device_map):
- command = "gstat -b -f "
- for device in sorted (devices_map.iterkeys ()):
- command += "^" + device + "$|"
- command = command.rstrip ('|')
- proc = subprocess.Popen(command.split (), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- proc.wait ()
- if proc.returncode != 0:
- error = proc.stderr.read ()
- log ("Cannot execute '%s', error: %s" % (command, error))
- sys.exit (1)
- output = proc.stdout.read ()
- return output
- def get_device_ops (device, status):
- for line in status.splitlines ():
- found = re.search (device, line)
- if (found):
- line = ' '.join (line.split ())
- ops = line.split (' ')[1]
- return int (ops)
- def print_status (devices_map):
- str = ""
- for device in sorted (devices_map.iterkeys ()):
- (time_left, status) = devices_map[device]
- str += "!%s,%d,%s" % (device, time_left, "Spinning" if status else "Stopped")
- str += "!"
- log (str)
- def stop_device (device, sync, test):
- log ("Stopping device: %s" % device)
- if test:
- command = "sleep 2"
- else:
- command = "camcontrol stop %s" % device
- proc = subprocess.Popen(command.split (), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- if sync:
- proc.wait ()
- if proc.returncode != 0:
- error = proc.stderr.read ()
- log ("Cannot execute '%s', error: %s" % (command, error))
- sys.exit (1)
- def run (options, devices_map):
- check_devices (devices_map)
- while (1):
- print_status (devices_map)
- time.sleep (options.interval)
- output = get_gstat_output (devices_map)
- for device in sorted (devices_map.iterkeys()):
- ops = get_device_ops (device, output)
- (time_left, status) = devices_map[device]
- if ops == 0:
- if time_left > 0:
- time_left = time_left - options.interval
- if time_left == 0:
- status = False
- stop_device (device, options.sync, options.test)
- else:
- time_left = options.interval * options.num
- status = True
- devices_map[device] = (time_left, status)
- return 0
- if __name__ == "__main__":
- parser = OptionParser ()
- parser.add_option ("-i", "--interval", type = "int", dest = "interval", default = INTERVAL, help = "interval between checks in seconds (default 40)", metavar = "SECONDS")
- parser.add_option ("-n", "--num", type = "int", dest = "num", default = NUM, help = "number of intervals (default 30)", metavar = "NUMBER")
- parser.add_option ("-d", "--devices", type = "string", dest = "devices", help = "comma separated list of devices (without /dev/)", metavar = "LIST")
- parser.add_option ("-s", "--syslog", action = "store_true", dest = "syslog", default = False, help = "log to syslog")
- parser.add_option ("-y", "--sync", action = "store_true", dest = "sync", default = False, help = "stop devices synchronously")
- parser.add_option ("-t", "--test", action = "store_true", dest = "test", default = False, help = "test only (devices are not stopped)")
- parser.add_option ("-x", "--daemonize", action = "store_true", dest = "daemonize", default = False, help = "run as daemon")
- (options, args) = parser.parse_args ()
- if not options.devices or options.devices == "":
- print "error: -d argument is required\n"
- parser.print_help ()
- sys.exit(1)
- SYSLOG = options.syslog
- #
- # devices_map['device'] = (time left, status)
- # status : [False = Stopped, True = Spinning]
- #
- devices_map = dict ((d.strip (), (options.interval * options.num, True)) for d in options.devices.split (','))
- log ("=" * 31)
- log ("= interval: %s" % options.interval)
- log ("= num: %d" % options.num)
- log ("= total time: %d seconds" % (options.interval * options.num))
- log ("= devices: %s" % options.devices)
- log ("= syslog: %s" % ('on' if options.syslog else 'off'))
- log ("= sync: %s" % ('on' if options.sync else 'off'))
- log ("= test: %s" % ('on' if options.test else 'off'))
- log ("= daemonize: %s" % ('on' if options.daemonize else 'off'))
- log ("=" * 31)
- if options.daemonize:
- daemonize ()
- run (options, devices_map)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement