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