From b205fca88ce929c2ab74a193d9d5913421563e5d Mon Sep 17 00:00:00 2001 From: Erik Nolte Date: Thu, 4 Aug 2011 17:21:10 -0600 Subject: [PATCH] Start logging service in each subsystem. --- butter/kvm/__init__.py | 33 +++++++++++++++++++++++++++++++++ butter/kvm/config.py | 17 +++++++++++++++++ butter/kvmd/__init__.py | 15 +++++++++++++++ butter/statd/__init__.py | 32 ++++++++++++++++++++++++++++++++ butter/statd/config.py | 18 ++++++++++++++++++ conf/kvm | 24 ++++++++++++++++++++++++ conf/kvmd | 24 ++++++++++++++++++++++++ conf/statd | 24 ++++++++++++++++++++++++ 8 files changed, 187 insertions(+) diff --git a/butter/kvm/__init__.py b/butter/kvm/__init__.py index 51659da..abdd9c4 100755 --- a/butter/kvm/__init__.py +++ b/butter/kvm/__init__.py @@ -2,6 +2,7 @@ Initialize interactions with the butter kvm subsytem ''' # Import Python libs +import logging import optparse import os import subprocess @@ -16,6 +17,9 @@ import butter.kvm.create import butter.kvm.migrate import butter.kvm.overlay +import butter.log + +log = logging.getLogger(__name__) def domain(): ''' @@ -26,6 +30,18 @@ def domain(): shell=True, stdout=subprocess.PIPE).communicate()[0].strip() +def verify_env(dirs): + ''' + Verify that the named directories are in place and that the environment + can shake the salt + ''' + for dir_ in dirs: + if not os.path.isdir(dir_): + try: + os.makedirs(dir_) + except OSError, e: + print 'Failed to create directory path "%s" - %s' % (dir_, e) + class KVM(object): ''' The KVM class is used to wrap the functionality of all butter kvm calls @@ -36,6 +52,9 @@ def __init__(self): ''' self.opts = self.__parse() + for name, level in self.opts['log_granular_levels'].iteritems(): + butter.log.set_logger_level(name, level) + def __parse(self): ''' Parse the butter command line options @@ -165,10 +184,21 @@ def __parse(self): help='Pass in an alternative path for the butter kvm'\ + ' configuration file; default: /etc/butter/kvm') + parser.add_option('-l', + '--log-level', + dest='log_level', + default='warning', + choices=butter.log.LOG_LEVELS.keys(), + help='Console log level. One of %s. For the logfile settings ' + 'see the config file. Default: \'%%default\'.' % + ', '.join([repr(l) for l in butter.log.LOG_LEVELS.keys()])) + options, args = parser.parse_args() cli = {} + butter.log.setup_console_logger(options.log_level) + cli['create'] = options.create cli['destroy'] = options.destroy cli['purge'] = options.purge @@ -254,6 +284,9 @@ def run(self): ''' Execute the logic required to act on the passed state data ''' + verify_env([os.path.dirname(self.opts['log_file'])]) + butter.log.setup_logfile_logger(self.opts['log_file'], self.opts['log_level']) + # Each sequence should be a function in the class so that the # capabilities can be manipulated in a more api centric way if self.opts['create']: diff --git a/butter/kvm/config.py b/butter/kvm/config.py index ff46232..039368b 100644 --- a/butter/kvm/config.py +++ b/butter/kvm/config.py @@ -9,6 +9,15 @@ import os import yaml +def prepend_root_dir(opts, path_options): + ''' + Prepends the options that represent filesystem paths with value of the + 'root_dir' option. + ''' + for path_option in path_options: + opts[path_option] = os.path.normpath( + os.sep.join([opts['root_dir'], opts[path_option]])) + def config(path='/etc/butter/kvm'): ''' Load up the configuration for butter kvm @@ -17,6 +26,11 @@ def config(path='/etc/butter/kvm'): 'instances': '/srv/vm/instances', 'local_path': '/mnt/local/vm', 'salt_pki': '/etc/salt/pki', + # Log options + 'root_dir': '/', + 'log_file' : '/var/log/butter/kvm', + 'log_level' : 'warning', + 'log_granular_levels': {}, # Global vm generation options 'storage_type': 'local', # Can be 'local', 'shared', 'choose' 'distro': 'arch', # The default distribution to use @@ -35,4 +49,7 @@ def config(path='/etc/butter/kvm'): except: pass + # Prepend root_dir to other paths + prepend_root_dir(opts, ['log_file']) + return opts diff --git a/butter/kvmd/__init__.py b/butter/kvmd/__init__.py index f8727c6..6aaf442 100644 --- a/butter/kvmd/__init__.py +++ b/butter/kvmd/__init__.py @@ -2,6 +2,7 @@ Initialize interactions with the butter kvm subsytem ''' # Import Python libs +import logging import optparse import os import subprocess @@ -13,6 +14,9 @@ # Import butter libs import butter.kvmd.daemon +import butter.log + +log = logging.getLogger(__name__) class KVMD(object): ''' @@ -41,8 +45,19 @@ def __parse_cli(self): dest='config', help='Pass in an alternative configuration file') + parser.add_option('-l', + '--log-level', + dest='log_level', + default='warning', + choices=butter.log.LOG_LEVELS.keys(), + help='Console log level. One of %s. For the logfile settings ' + 'see the config file. Default: \'%%default\'.' % + ', '.join([repr(l) for l in butter.log.LOG_LEVELS.keys()])) + options, args = parser.parse_args() + butter.log.setup_console_logger(options.log_level) + return {'foreground': options.foreground, 'config': options.config} diff --git a/butter/statd/__init__.py b/butter/statd/__init__.py index 76ddb8a..209e6a8 100644 --- a/butter/statd/__init__.py +++ b/butter/statd/__init__.py @@ -3,6 +3,7 @@ that uses salt for statistics system monitoring. ''' # Import Python modules +import logging import multiprocessing import optparse import os @@ -16,6 +17,20 @@ import butter.statd.monitor import butter.utils +log = logging.getLogger(__name__) + +def verify_env(dirs): + ''' + Verify that the named directories are in place and that the environment + can shake the salt + ''' + for dir_ in dirs: + if not os.path.isdir(dir_): + try: + os.makedirs(dir_) + except OSError, e: + print 'Failed to create directory path "%s" - %s' % (dir_, e) + class StatD(object): ''' The StatD object is used to initialize the stats monitoring subsytem @@ -44,12 +59,26 @@ def __parse(self): help='Choose an alternative config file for the statd ' 'daemon; default /etc/butter/statd') + parser.add_option('-l', + '--log-level', + dest='log_level', + default='warning', + choices=butter.log.LOG_LEVELS.keys(), + help='Console log level. One of %s. For the logfile settings ' + 'see the config file. Default: \'%%default\'.' % + ', '.join([repr(l) for l in butter.log.LOG_LEVELS.keys()])) + options, args = parser.parse_args() opts = butter.statd.config.config(options.config) opts['daemon'] = options.daemon + for name, level in opts['log_granular_levels'].iteritems(): + butter.log.set_logger_level(name, level) + + butter.log.setup_console_logger(options.log_level) + return opts def run(self): @@ -57,6 +86,9 @@ def run(self): Create the multiprocessing/threading interfaces for butter statd and start them. ''' + verify_env([os.path.dirname(self.opts['log_file'])]) + butter.log.setup_logfile_logger(self.opts['log_file'], self.opts['log_level']) + if self.opts['daemon']: butter.utils.daemonize() if self.opts['stats']: diff --git a/butter/statd/config.py b/butter/statd/config.py index 353eb80..002d9ea 100644 --- a/butter/statd/config.py +++ b/butter/statd/config.py @@ -9,6 +9,15 @@ import os import yaml +def prepend_root_dir(opts, path_options): + ''' + Prepends the options that represent filesystem paths with value of the + 'root_dir' option. + ''' + for path_option in path_options: + opts[path_option] = os.path.normpath( + os.sep.join([opts['root_dir'], opts[path_option]])) + def config(path='/etc/butter/statd'): ''' Load up the configuration for butter kvm @@ -28,6 +37,12 @@ def config(path='/etc/butter/statd'): 'target': '.*', 'target_type': 'pcre', 'stats': {}, + + # Log options + 'root_dir': '/', + 'log_file' : '/var/log/butter/statd', + 'log_level' : 'warning', + 'log_granular_levels': {}, } if os.path.isfile(path): @@ -36,4 +51,7 @@ def config(path='/etc/butter/statd'): except: pass + # Prepend root_dir to other paths + prepend_root_dir(opts, ['log_file']) + return opts diff --git a/conf/kvm b/conf/kvm index 26db44f..a77bcf0 100644 --- a/conf/kvm +++ b/conf/kvm @@ -1,3 +1,27 @@ +##### KVM configuration settings ##### +########################################## +# The root directory prepended to these options: log_file +#root_dir: / + +##### Logging settings ##### +########################################## +# The location of the master log file +#log_file: /var/log/butter/kvm +# The level of messages to send to the log file. +# One of 'info', 'quiet', 'critical', 'error', 'debug', 'warning'. +# Default: 'warning' +#log_level: warning +# +# Logger levels can be used to tweak specific loggers logging levels. +# Imagine you want to have the butter library at the 'warning' level, +# but you still wish to have 'butter.modules' at the 'debug' level: +# log_granular_levels: { +# 'butter': 'warning', +# 'butter.modules': 'debug' +# } +# +#log_granular_levels: {} + #images: /srv/vm/images #instances: /srv/vm/instances #local_path: /mnt/local/vm diff --git a/conf/kvmd b/conf/kvmd index 020c873..1e1c361 100644 --- a/conf/kvmd +++ b/conf/kvmd @@ -1,3 +1,27 @@ +##### KVMD configuration settings ##### +########################################## +# The root directory prepended to these options: log_file +#root_dir: / + +##### Logging settings ##### +########################################## +# The location of the master log file +#log_file: /var/log/butter/kvmd +# The level of messages to send to the log file. +# One of 'info', 'quiet', 'critical', 'error', 'debug', 'warning'. +# Default: 'warning' +#log_level: warning +# +# Logger levels can be used to tweak specific loggers logging levels. +# Imagine you want to have the butter library at the 'warning' level, +# but you still wish to have 'butter.modules' at the 'debug' level: +# log_granular_levels: { +# 'butter': 'warning', +# 'butter.modules': 'debug' +# } +# +#log_granular_levels: {} + # Configuration for the kvm daemon #images: /srv/vm/images #pool_size: 5 diff --git a/conf/statd b/conf/statd index 9ad3e01..620df13 100644 --- a/conf/statd +++ b/conf/statd @@ -1,3 +1,27 @@ +##### STATD configuration settings ##### +########################################## +# The root directory prepended to these options: log_file +#root_dir: / + +##### Logging settings ##### +########################################## +# The location of the master log file +#log_file: /var/log/butter/statd +# The level of messages to send to the log file. +# One of 'info', 'quiet', 'critical', 'error', 'debug', 'warning'. +# Default: 'warning' +#log_level: warning +# +# Logger levels can be used to tweak specific loggers logging levels. +# Imagine you want to have the butter library at the 'warning' level, +# but you still wish to have 'butter.modules' at the 'debug' level: +# log_granular_levels: { +# 'butter': 'warning', +# 'butter.modules': 'debug' +# } +# +#log_granular_levels: {} + #### Global Stats Settings ##### ######################################### # Number of seconds between data queries