Package flumotion :: Package admin :: Package rrdmon :: Module main
[hide private]

Source Code for Module flumotion.admin.rrdmon.main

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """flumotion-rrdmon entry point, command line parsing and invokation""" 
 23   
 24  import os 
 25  import sys 
 26   
 27  from twisted.internet import reactor 
 28   
 29  from flumotion.admin.rrdmon import rrdmon, config 
 30  from flumotion.common import log 
 31  from flumotion.common.options import OptionGroup, OptionParser 
 32  from flumotion.common.process import startup 
 33  from flumotion.configure import configure 
 34   
 35  __version__ = "$Rev: 6961 $" 
 36   
 37  # more standard helper functions necessary... 
38 -def _createParser():
39 parser = OptionParser(domain="flumotion-rrdmon") 40 41 group = OptionGroup(parser, "rrdmon") 42 group.add_option('-s', '--service-name', 43 action="store", type="string", dest="serviceName", 44 help="name to use for log and pid files " 45 "when run as a daemon") 46 group.add_option('-D', '--daemonize', 47 action="store_true", dest="daemonize", 48 help="run in background as a daemon") 49 group.add_option('', '--daemonize-to', 50 action="store", dest="daemonizeTo", 51 help="what directory to run from when daemonizing") 52 53 parser.add_option('-L', '--logdir', 54 action="store", dest="logdir", 55 help="flumotion log directory (default: %s)" % 56 configure.logdir) 57 parser.add_option('-R', '--rundir', 58 action="store", dest="rundir", 59 help="flumotion run directory (default: %s)" % 60 configure.rundir) 61 parser.add_option_group(group) 62 63 return parser
64
65 -def _readConfig(confXml, options):
66 # modifies options dict in-place 67 log.info('rrdmon', 'Reading configuration from %s' % confXml) 68 cfg = config.ConfigParser(confXml).parse() 69 # command-line debug > environment debug > config file debug 70 if not options.debug and cfg['debug'] \ 71 and not os.environ.has_key('FLU_DEBUG'): 72 options.debug = cfg['debug'] 73 return cfg
74
75 -def main(args):
76 parser = _createParser() 77 log.debug('rrdmon', 'Parsing arguments (%r)' % ', '.join(args)) 78 options, args = parser.parse_args(args) 79 80 # Force options down configure's throat 81 for d in ['logdir', 'rundir']: 82 o = getattr(options, d, None) 83 if o: 84 log.debug('rrdmon', 'Setting configure.%s to %s' % (d, o)) 85 setattr(configure, d, o) 86 87 # check if a config file was specified; if so, parse config and copy over 88 if len(args) != 2: 89 raise SystemExit('usage: flumotion-rrdtool [OPTIONS] CONFIG-FILE') 90 91 confXml = args[1] 92 cfg = _readConfig(confXml, options) 93 94 # reset FLU_DEBUG which could be different after parsing XML file 95 if options.debug: 96 log.setFluDebug(options.debug) 97 98 if options.daemonizeTo and not options.daemonize: 99 sys.stderr.write( 100 'ERROR: --daemonize-to can only be used with -D/--daemonize.\n') 101 return 1 102 103 if options.serviceName and not options.daemonize: 104 sys.stderr.write( 105 'ERROR: --service-name can only be used with -D/--daemonize.\n') 106 return 1 107 108 monitor = rrdmon.RRDMonitor(cfg['sources']) 109 110 name = 'rrdmon' 111 if options.daemonize: 112 if options.serviceName: 113 name = options.serviceName 114 if not options.daemonizeTo: 115 options.daemonizeTo = "/" 116 117 startup("rrdmon", name, options.daemonize, options.daemonizeTo) 118 119 log.debug('rrdmon', 'Running Flumotion version %s' % 120 configure.version) 121 import twisted.copyright 122 log.debug('rrdmon', 'Running against Twisted version %s' % 123 twisted.copyright.version) 124 125 # go into the reactor main loop 126 reactor.run() 127 128 return 0
129