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,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  """flumotion-rrdmon entry point, command line parsing and invokation""" 
 19   
 20  import os 
 21  import sys 
 22   
 23  from twisted.internet import reactor 
 24   
 25  from flumotion.admin.rrdmon import rrdmon, config 
 26  from flumotion.common import log 
 27  from flumotion.common.options import OptionGroup, OptionParser 
 28  from flumotion.common.process import startup 
 29  from flumotion.configure import configure 
 30   
 31  __version__ = "$Rev$" 
 32   
 33  # more standard helper functions necessary... 
 34   
 35   
36 -def _createParser():
37 parser = OptionParser(domain="flumotion-rrdmon") 38 39 group = OptionGroup(parser, "rrdmon") 40 group.add_option('-s', '--service-name', 41 action="store", type="string", dest="serviceName", 42 help="name to use for log and pid files " 43 "when run as a daemon") 44 group.add_option('-D', '--daemonize', 45 action="store_true", dest="daemonize", 46 help="run in background as a daemon") 47 group.add_option('', '--daemonize-to', 48 action="store", dest="daemonizeTo", 49 help="what directory to run from when daemonizing") 50 51 parser.add_option('-L', '--logdir', 52 action="store", dest="logdir", 53 help="flumotion log directory (default: %s)" % 54 configure.logdir) 55 parser.add_option('-R', '--rundir', 56 action="store", dest="rundir", 57 help="flumotion run directory (default: %s)" % 58 configure.rundir) 59 parser.add_option_group(group) 60 61 return parser
62 63
64 -def _readConfig(confXml, options):
65 # modifies options dict in-place 66 log.info('rrdmon', 'Reading configuration from %s' % confXml) 67 cfg = config.ConfigParser(confXml).parse() 68 # command-line debug > environment debug > config file debug 69 if not options.debug and cfg['debug'] \ 70 and not 'FLU_DEBUG' in os.environ: 71 options.debug = cfg['debug'] 72 return cfg
73 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