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 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  import os 
 23  import sys 
 24   
 25  from twisted.internet import reactor 
 26   
 27  from flumotion.configure import configure 
 28  from flumotion.common import log, keycards, common, errors 
 29  from flumotion.common import connection 
 30  from flumotion.admin.rrdmon import rrdmon, config 
 31  from flumotion.twisted import pb 
 32  from flumotion.common.options import OptionGroup, OptionParser 
 33   
 34  # more standard helper functions necessary... 
35 -def _createParser():
36 parser = OptionParser(domain="flumotion-rrdmon") 37 38 group = OptionGroup(parser, "rrdmon") 39 group.add_option('-s', '--service-name', 40 action="store", type="string", dest="serviceName", 41 help="name to use for log and pid files " 42 "when run as a daemon") 43 group.add_option('-D', '--daemonize', 44 action="store_true", dest="daemonize", 45 help="run in background as a daemon") 46 group.add_option('', '--daemonize-to', 47 action="store", dest="daemonizeTo", 48 help="what directory to run from when daemonizing") 49 50 parser.add_option('-L', '--logdir', 51 action="store", dest="logdir", 52 help="flumotion log directory (default: %s)" % 53 configure.logdir) 54 parser.add_option('-R', '--rundir', 55 action="store", dest="rundir", 56 help="flumotion run directory (default: %s)" % 57 configure.rundir) 58 parser.add_option_group(group) 59 60 return parser
61
62 -def _readConfig(confXml, options):
63 # modifies options dict in-place 64 log.info('rrdmon', 'Reading configuration from %s' % confXml) 65 cfg = config.ConfigParser(confXml).parse() 66 # command-line debug > environment debug > config file debug 67 if not options.debug and cfg['debug'] \ 68 and not os.environ.has_key('FLU_DEBUG'): 69 options.debug = cfg['debug'] 70 return cfg
71
72 -def main(args):
73 parser = _createParser() 74 log.debug('rrdmon', 'Parsing arguments (%r)' % ', '.join(args)) 75 options, args = parser.parse_args(args) 76 77 # Force options down configure's throat 78 for d in ['logdir', 'rundir']: 79 o = getattr(options, d, None) 80 if o: 81 log.debug('rrdmon', 'Setting configure.%s to %s' % (d, o)) 82 setattr(configure, d, o) 83 84 # check if a config file was specified; if so, parse config and copy over 85 if len(args) != 2: 86 raise SystemExit('usage: flumotion-rrdtool [OPTIONS] CONFIG-FILE') 87 88 confXml = args[1] 89 cfg = _readConfig(confXml, options) 90 91 # reset FLU_DEBUG which could be different after parsing XML file 92 if options.debug: 93 log.setFluDebug(options.debug) 94 95 if options.daemonizeTo and not options.daemonize: 96 sys.stderr.write( 97 'ERROR: --daemonize-to can only be used with -D/--daemonize.\n') 98 return 1 99 100 if options.serviceName and not options.daemonize: 101 sys.stderr.write( 102 'ERROR: --service-name can only be used with -D/--daemonize.\n') 103 return 1 104 105 monitor = rrdmon.RRDMonitor(cfg['sources']) 106 107 name = 'rrdmon' 108 if options.daemonize: 109 if options.serviceName: 110 name = options.serviceName 111 if not options.daemonizeTo: 112 options.daemonizeTo = "/" 113 114 common.startup("rrdmon", name, options.daemonize, options.daemonizeTo) 115 116 log.debug('rrdmon', 'Running Flumotion version %s' % 117 configure.version) 118 import twisted.copyright 119 log.debug('rrdmon', 'Running against Twisted version %s' % 120 twisted.copyright.version) 121 122 # go into the reactor main loop 123 reactor.run() 124 125 return 0
126