#! /opt/imh-python/bin/python3
''' Gather information about system environment '''
from argparse import ArgumentParser
__maintainer__ = "Daniel K"
__email__ = "danielk@inmotionhosting.com"
DATA = {"status": 'uninitiated', "version": __version__}
class EnvInfoError(Exception):
'''Custom error class for module.'''
def __init__(self, message):
'''Init with call to base constructor.'''
super().__init__(message)
def get_data(min_version=None, max_version=None):
Main function for module, used to acquire environment information.
min_version - If set, require envinfo.py
to have a version greater than min_version
max_version - If set, require envinfo.py
to have a version less than max_version
Returns dictionary of environment information. Currently, the only
guaranteed entries are 'type', 'platform', and 'users'. The following
are the keys that which may be present:
type - General type of environment. Can be 'cpanel' or 'unknown'
platform - Operating system information
web_server - Type of webserver. Currently only 'apache'
easyapache - Version of EasyApache if present
apache_conf - Location of httpd.conf for Apache
apache_domlogs - Location of Apache domlogs
On error will throw EnvInfoError.
if min_version is not None and min_version > __version__:
'Minimum version {} is greater than current version {}'.format(
if max_version is not None and max_version < __version__:
'Maximum version {} is less than current version {}'.format(
if os.path.isdir('/var/cpanel'):
if os.path.isfile('/etc/apache2/conf/httpd.conf'):
DATA['apache_conf'] = '/etc/apache2/conf/httpd.conf'
DATA['apache_domlogs'] = '/var/log/apache2/domlogs'
DATA['web_server'] = 'apache'
if DATA['type'] == 'cpanel':
elif os.path.isfile('/usr/local/apache/conf/httpd.conf'):
DATA['apache_conf'] = '/usr/local/apache/conf/httpd.conf'
DATA['apache_domlogs'] = '/usr/local/apache/domlogs'
DATA['web_server'] = 'apache'
if DATA['type'] == 'cpanel':
if DATA['type'] == 'cpanel':
user_list = os.listdir('/var/cpanel/users/')
elif os.path.isfile('/etc/passwd'):
# If we don't know how else to find users, look to /etc/passwd
if os.path.exists('/etc/passwd'):
with open('/etc/passwd') as file_handle:
uid = int(line.split(':')[2])
user_list.append(line.split(':')[0])
# Catch, and throw back IOErrors.
raise EnvInfoError('Could not read from /etc/passwd') from e
# No known way to find users
raise EnvInfoError("Unable to make user list for server type.")
DATA['users'] = user_list
DATA['status'] = 'initiated'
Parse command line arguments
parser = ArgumentParser(description=__doc__)
output_group = parser.add_argument_group("Output options")
output_type_group = output_group.add_mutually_exclusive_group()
output_type_group.add_argument(
"Print general system environment information."
output_type_group.add_argument(
help="Print list of users rather than general environment.",
output_type_group.add_argument(
help="Print location of Apache's httpd.conf.",
output_group.add_argument(
"-j", "--json", action='store_true', help="Print results in JSON"
help="Print version information and exit.",
args = parser.parse_args()
print(f"Environment Information tool version {__version__}")
print(f"Last modified on {__date__}.")
return (args.output_type, args.json)
'''Function to be called when run from the command line'''
(output_type, use_json) = parse_args()
except EnvInfoError as error:
if output_type == 'users':
users, sort_keys=True, indent=4, separators=(',', ': ')
elif output_type == 'apache_conf':
if 'apache_conf' not in data:
sys.stderr.write('No Apache configuration found\n')
print(data['apache_conf'])
data, sort_keys=True, indent=4, separators=(',', ': ')
print("Server Environment")
print("Server Type: {}".format(data['type']))
"OS: {}".format(" ".join(data['platform']))
print("Web Server: {}".format(data['web_server']))
print("Apache configuration: {}".format(data['apache_conf']))
print("Apache domain logs: {}".format(data['apache_domlogs']))
print(" ".join(data['users']))
if __name__ == "__main__":