capisuite/scripts/capisuite-checkconfig.in

226 lines
6.9 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/python
"""capisuite-checkconfig - capisuite tool for checking config files
Usage:
capisuite-checkconfig
Check some parameters and print content of config.
capisuite-checkconfig file1 [file2...]
Print contents of the given config files.
capisuite-checkconfig -t
Test if capisuite is configured (returns non zero if not).
capisuite-checkconfig [ -h | --help ]
Print this usage information.
"""
__author__ = "Hartmut Goebel <h.goebel@crazy-compilers.com>"
__copyright__ = "Copyright (c) 2004-2005 by Hartmut Goebel"
__version__ = "$Revision: 0.0 $"
__credits__ = "This is part of www.capisuite.de; thanks to Gernot Hillier"
__license__ = """
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
import sys, os, getopt
def error(*msgs):
for m in msgs:
print >>sys.stderr, m,
print >>sys.stderr
sys.exit(10)
try:
import capisuite.config
import capisuite.fileutils
from capisuite.consts import *
except Exception, e:
error("failed to import capisuite's python modules", str(e))
# Some helper functions, if you don't know what they do, just skip to
# the easy part
def test_nextfile(user, path, queue, prefix):
path = os.path.join(path, user, queue)
if not os.path.exists(path):
print "Directory", path, "does not exist"
print " ==> this is ok, if this queue has never been",
print "used before (e.g. never send a fax)"
print
return
file = os.path.join(path, prefix+"-nextnr")
if not os.path.exists(file):
print "File", file, "does not exist"
print " ==> this is ok, if this queue has never been",
print "used before (e.g. never send a fax)"
print
return
print "Trying to read file:", file
num = None
try:
num = capisuite.fileutils.readCounter(0, file)
except IOError, err:
print >>sys.stderr, "****Failed to read/parse the *nextnr file:", err
except ValueError:
print >>sys.stderr, "****Failed to convert file content to int number"
else:
if num is None:
print ' is unset'
else:
print ' contains number', num
def checkQdirs(user, basedir, queuedir):
path = os.path.join(basedir, user, queuedir)
if not os.path.exists(path):
if os.getuid() == 0:
print 'creating user queue dir', path
capisuite.fileutils._mkuserdir(user, basedir, user, queuedir)
return 1
else:
print >>sys.stderr, 'missing user queue dir', path
return 0
return 1
# END helper functions ================
def checkGlobalConfig(file=None):
print "Reading global configuration ...",
try:
config = capisuite.config.readGlobalConfig(file)
except IOError, err:
print
error("Failed to read/parse config file:", err)
print 'okay'
print
print "========= current configuration ==================="
for section in config.sections():
print "----------------", '['+section+']', "-------------"
options = config.options(section)
options.sort()
for option in options:
print option, '=', config.get(section, option)
print
print
print "=========== Testing User Queues and Sequence Files ====="
print
basedir = config.get('GLOBAL', 'fax_user_dir')
userdirs_missing = 0
for user in config.listUsers():
print "----------------", user, "-------------"
for queue in (SEND_Q, RECEIVED_Q):
if not checkQdirs(user, basedir, queue):
userdirs_missing += 1
else:
test_nextfile(user, basedir, queue, "fax")
if queue == RECEIVED_Q:
test_nextfile(user, basedir, queue, "voice")
if userdirs_missing:
print
print 'Some user queue dirs are missing.'
print "Rerun as 'root' to create them."
print
def do_test_if_configured():
"""
Test whether capisuite is configured. This includes:
- config files must exist
- Some sections must exist
- at least one user must be configured in fax and voice config
- ... further tests to come
"""
def test_configfile(filename, required_sections):
cfg = capisuite.config.CSConfigParser()
try:
config = cfg.read(filename)
except IOError, err:
print >> sys.stderr, "ERROR: Can't read", filename
return 0
all_sections = cfg.sections()
for sect in required_sections:
if not sect in all_sections:
print >> sys.stderr, "Missing section", sect, "in", filename
return 0
for sect in all_sections:
if not sect in required_sections:
return 1
print >> sys.stderr, "Warning: Missing a user section in", filename
return 0
# Read global configuration
#try:
# config = capisuite.config.readConfig(configfile)
#except IOError, err:
# return 10
configured_fax = \
test_configfile(os.path.join("@pkgsysconfdir@", "fax.conf"),
('GLOBAL',
'MailFaxReceived', 'MailFaxSent', 'MailFaxFailed'))
configured_voice = \
test_configfile(os.path.join("@pkgsysconfdir@", "answering_machine.conf"),
('GLOBAL', 'MailVoiceReceived'))
return configured_fax or configured_voice
#----------- usage -----------#
def usage(error=''):
print __doc__
if error:
print
print "ERROR:", error
sys.exit(1)
#----------- main -----------#
test_if_configured = 0
try:
opts, args = getopt.getopt(sys.argv[1:], "c:t", ['config=','test'])
except getopt.GetoptError, e:
usage(e.msg)
sys.exit(1)
for option, param in opts:
# commands
if option in ('-h','--help'): usage()
elif option in ('-t','--test'): test_if_configured = 1
if test_if_configured:
if not do_test_if_configured():
print 'capisuite seams to be not configured!'
sys.exit(10)
else:
print 'capisuite seams to be configured!'
sys.exit(0)
elif len(args) == 0:
checkGlobalConfig()
else:
config = capisuite.config.readGlobalConfig(sys.argv[1])
sections = config.sections()
sections.sort()
for section in sections:
print "----------------", '['+section+']', "-------------"
options = config.options(section)
options.sort()
for option in options:
print option, '=', config.get(section, option)
print
print 'known users:',
print ', '.join(map(repr, config.listUsers()))
# todo: these checks:
# - are all users valid system users?
# - do all voice-users have 'voice_delay' set?