#!/opt/imh-python/bin/python3
"""Runs check_software on all users and stores the results
in /var/log/software_reports"""
from argparse import ArgumentParser
from cpapis import whmapi1, CpAPIError
LOGDIR = Path("/var/log/software_reports")
MAX_LOAD = multiprocessing.cpu_count()
# users to not run check_software on
RESERVED = rads.OUR_RESELLERS + [rads.SECURE_USER]
# blank argparse so -h doesn't surprise anyone by immediately running
ArgumentParser(description=__doc__).parse_args()
print("This tool must be run as root. Exiting...")
LOGDIR.mkdir(mode=0o755, parents=True, exist_ok=True)
check_software_cmd = ['/opt/sharedrads/check_software', '-r']
api = whmapi1('listaccts', check=True)
except CpAPIError as exc:
sys.exit(f'Unable to retrieve user list! {exc}')
users = [x['user'] for x in api['data']['acct'] if x not in RESERVED]
user_log = LOGDIR / f"{user}.txt"
cmd = check_software_cmd.copy()
with open(user_log, 'wb') as file:
subprocess.run(cmd, stdout=file, stderr=file, check=False)
print("Error running check_software on", user, file=sys.stderr)
if os.getloadavg()[0] > MAX_LOAD:
print("Sleeping due to high server load...")
while os.getloadavg()[0] > MAX_LOAD:
if __name__ == '__main__':